Skip to content

Instantly share code, notes, and snippets.

@heatblazer
Last active March 11, 2023 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heatblazer/6a00d0025dc6fc08065f2076f3e6671c to your computer and use it in GitHub Desktop.
Save heatblazer/6a00d0025dc6fc08065f2076f3e6671c to your computer and use it in GitHub Desktop.
// fmake.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#ifndef _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_DEPRECATE 1
#endif // !_CRT_SECURE_NO_DEPRECATE - fopen and friends
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cstdint>
#define KBYES (1024)
#define MBYTES (1024 * 1024)
#define MAYBE_UNUSED (void)
#define MAX_RND_BYTES 4096
#define MAX_RND_GEN 4
extern uint8_t** get_randbf(size_t size);
int main(int argc, char** argv)
{
if (argc < 5) {
fprintf(stderr, "Use -n <num files> -m <size per file>\r\n");
}
size_t i=0, fsize=0, fcount=0, fidx=0, rndidx=0;
char filename[64];
static const char FILE_AUX_NAME[][64] = { "small", "medium", "big" };
srand(time(0x00));
for (i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-n")) {
fcount = atoi(argv[i + 1]);
}
if (!strcmp(argv[i], "-m")) {
fsize = atoi(argv[i + 1]);
fsize *= (MBYTES); }
}
if (fsize < 20 * MBYTES) {
fidx = 0;
}
else if (fsize > 1000 * MBYTES) {
fidx = 2;
}
else {
fidx = 1;
}
uint8_t** buffrnd = get_randbf(MAX_RND_GEN);
clock_t begin = clock();
for (i = 0; i < fcount; i++) {
snprintf(filename, sizeof(filename), "%d_%s.file", (int)i, FILE_AUX_NAME[fidx]);
FILE* fp = fopen(filename, "wb");
size_t j = 0;
// unsigned char buffrnd[4][MAX_RND_BYTES] = { 0 };
for (j = 0; j < 4; j++) {
for (rndidx=0; rndidx < MAX_RND_BYTES / 8 ; ) {
buffrnd[j][rndidx++] = (unsigned char)(rand() % 0xff);
buffrnd[j][rndidx++] = (unsigned char)(rand() % 0xff);
buffrnd[j][rndidx++] = (unsigned char)(rand() % 0xff);
buffrnd[j][rndidx++] = (unsigned char)(rand() % 0xff);
buffrnd[j][rndidx++] = (unsigned char)(rand() % 0xff);
buffrnd[j][rndidx++] = (unsigned char)(rand() % 0xff);
buffrnd[j][rndidx++] = (unsigned char)(rand() % 0xff);
buffrnd[j][rndidx++] = (unsigned char)(rand() % 0xff);
}
}
for (j = 0; j < fsize; ) {
j += fwrite(buffrnd[rndidx++ & MAX_RND_GEN], sizeof(unsigned char), MAX_RND_BYTES, fp);
j += fwrite(buffrnd[rndidx++ & MAX_RND_GEN], sizeof(unsigned char), MAX_RND_BYTES, fp);
j += fwrite(buffrnd[rndidx++ & MAX_RND_GEN], sizeof(unsigned char), MAX_RND_BYTES, fp);
j += fwrite(buffrnd[rndidx++ & MAX_RND_GEN], sizeof(unsigned char), MAX_RND_BYTES, fp);
}
fclose(fp);
fp = 0x00;
}
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
fprintf(stdout, "Time elasped %f\r\n", time_spent);
for (i = 0; i < MAX_RND_GEN; i++) {
unsigned char* it = buffrnd[i];
if (it)
free(it);
}
free(buffrnd);
return 0;
}
uint8_t** get_randbf(size_t size)
{
uint8_t** buffrnd = 0;
size_t i;
buffrnd = (uint8_t**) malloc(sizeof(uint8_t*) * size);
if (!buffrnd)
return 0;
for (i = 0; i < size; i++) {
buffrnd[i] = (uint8_t*) malloc(sizeof(uint8_t) * MAX_RND_BYTES);
if (!buffrnd[i]) {
size_t j = 0;
while (buffrnd[j]) {
free(buffrnd[j]);
buffrnd[j++] = 0;
}
free(buffrnd);
buffrnd = 0;
break;
}
}
return buffrnd;
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
@heatblazer
Copy link
Author

Generates n files of m size for Win32
fmake -n <file count> -m <file size>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment