fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6.  
  7. #define RAND() ((unsigned int)((rand() & 0xff) | ((rand() & 0xff) << 8) | ((rand() & 0xff) << 16) | ((rand() & 0xff) << 24)))
  8. #define GIGA (uint64_t)((uint64_t)0x400*(uint64_t)0x400*(uint64_t)0x400)
  9. #define MAX_LENGTH_SECTION 0x1f
  10. #define MAX_LENGTH_TEXT 0x7f
  11. #define MAX_LENGTH_BUF 0xff
  12. #define MAX_UINT32 0xffffffff
  13.  
  14. static char const* asz_mon[12] = {
  15. "Jan","Feb","Mar","Apr","May","Jun",
  16. "Jul","Aug","Sep","Oct","Nov","Dec"};
  17.  
  18. static char* gen_string1(char* sz_string, size_t u_length) {
  19.  
  20. unsigned char* psz_string = (unsigned char*)sz_string;
  21. unsigned char* psz_string_end = (unsigned char*)sz_string + u_length;
  22.  
  23. for (; psz_string < psz_string_end; ++psz_string) {
  24. *psz_string = 0x21 + (RAND() % 94);
  25. }
  26.  
  27. *psz_string = '\0';
  28. return sz_string;
  29. }
  30.  
  31. static char* gen_string2(char* sz_string, size_t u_length) {
  32.  
  33. unsigned char c;
  34. char* psz_string = sz_string;
  35. char* psz_string_end = sz_string + u_length;
  36.  
  37. for (; psz_string < psz_string_end; ++psz_string) {
  38. c = RAND() % 52;
  39. if (c < 26) {
  40. *psz_string = 'A' + c;
  41. } else {
  42. *psz_string = 'a' + (c -26);
  43. }
  44. }
  45.  
  46. *psz_string = '\0';
  47. return sz_string;
  48. }
  49.  
  50. int gen_data(FILE* fp, size_t u_GiB) {
  51.  
  52. char sz_buf[MAX_LENGTH_BUF+1];
  53.  
  54. unsigned int u_string_recs;
  55. unsigned int u_hatena;
  56. unsigned int u_xy_recs;
  57. unsigned int u_num_recs;
  58. char x_or_y;
  59. time_t u_time = time(NULL);
  60. tm* pt_tm = ::localtime(&u_time);
  61. uint64_t u_pos;
  62.  
  63. ::srand((unsigned int)time(NULL));
  64.  
  65. for (;;) {
  66. // ①セクション名
  67. // ex. SECTION_NAME ①
  68. ::fprintf(fp, "%s\x0a", gen_string2(sz_buf, 1 + (RAND() % MAX_LENGTH_SECTION)));
  69.  
  70. // ②セクション集計値
  71. // ex. 11200 11200 2 Jun 9 23:23:00 2018 ②
  72. // u_xy_recs = RAND() % 0x8000000 + 1;
  73. u_xy_recs = RAND() % 0x8000 + 1;
  74. u_hatena = u_xy_recs;
  75. u_string_recs = RAND() % 0x20 + 1;
  76. ::sprintf(sz_buf, "%s %.2d %02d:%02d:%02d %d", asz_mon[pt_tm->tm_mon], pt_tm->tm_mday, pt_tm->tm_hour, pt_tm->tm_min, pt_tm->tm_sec, pt_tm->tm_year+1900);
  77. ::fprintf(fp, "%u %u %u %s\x0a", u_xy_recs, u_hatena, u_string_recs, sz_buf);
  78.  
  79. // ③テキスト
  80. // ex. This is pen. ③
  81. // hello world. ③
  82. for (unsigned int j = 0; j < u_string_recs; ++j) {
  83. ::fprintf(fp, "%s\x0a", gen_string1(sz_buf, 1 + (RAND() % MAX_LENGTH_TEXT)));
  84. }
  85.  
  86. // ④数値
  87. // ex. x 1 2 ④
  88. for (unsigned int j = 0; j < u_xy_recs; ++j) {
  89. x_or_y = (RAND() % 2) ? 'x' : 'y';
  90. // u_num_recs = RAND() % 0x400 + 1;
  91. u_num_recs = RAND() % 0x100 + 1;
  92. ::fprintf(fp, "%c %u %u\x0a", x_or_y, j + 1, u_num_recs);
  93. if (RAND() % 2) {
  94. // ⑤QQ 謎の行
  95. // QQ subname -1 0 0 1 -21000000 600000 2
  96. ::fprintf(fp, "QQ %s %d %d %d %d %d %d %d\x0a",
  97. gen_string2(sz_buf, 1 + (RAND() % MAX_LENGTH_SECTION)),
  98. (int32_t)RAND(), (int32_t)RAND(), (int32_t)RAND(), (int32_t)RAND(), (int32_t)RAND(), (int32_t)RAND(), (int32_t)RAND());
  99. }
  100. if (x_or_y == 'x') {
  101. for (unsigned int k = 0; k < u_num_recs; ++k) {
  102. // 100 1 -2000 10
  103. ::fprintf(fp, "%d %d %d %d\x0a",
  104. (int32_t)RAND(), (int32_t)RAND(), (int32_t)RAND(), (int32_t)RAND());
  105. }
  106. } else {
  107. for (unsigned int k = 0; k < u_num_recs; ++k) {
  108. // -100 10000
  109. ::fprintf(fp, "%d %d\x0a",
  110. (int32_t)RAND(), (int32_t)RAND());
  111. }
  112. }
  113. }
  114.  
  115. u_pos = ftello(fp);
  116.  
  117. if (u_pos > GIGA * (uint64_t)u_GiB) {
  118. break;
  119. }
  120. }
  121.  
  122. return 0;
  123. }
  124.  
  125. int main(int argc, char** argv) {
  126.  
  127. if (argc != 3) {
  128. ::fprintf(stderr, "Usage: %s <filepath> <buffer size>\n", argv[0]);
  129. return 1;
  130. }
  131.  
  132. char* filename = argv[1];
  133. size_t u_GiB = ::strtoul(argv[2], NULL, 10);;
  134.  
  135. if (u_GiB == 0) {
  136. ::fprintf(stderr, "Usage: %s <filepath> <GiB size>\n", argv[0]);
  137. ::fprintf(stderr, "<GiB size> must be greater than zero\n");
  138. return 1;
  139. }
  140.  
  141. FILE* fp = ::fopen(filename, "w");
  142.  
  143. if (::gen_data(fp, u_GiB) < 0) {fclose(fp); return -1;}
  144. fclose(fp);
  145. return 0;
  146. }
  147.  
Runtime error #stdin #stdout #stderr 0s 4384KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Usage: ./prog <filepath> <buffer size>