Guest User

Untitled

a guest
Jul 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. import std.stdio;
  2. import std.string;
  3. import std.algorithm;
  4. import std.random;
  5. import std.file;
  6. import std.traits;
  7. import std.exception;
  8. import std.typecons;
  9. import std.conv;
  10.  
  11. struct Layout {
  12. string a;
  13. string b;
  14. int c;
  15. string d;
  16. float e;
  17. }
  18.  
  19. auto head = "a,b,c,d,e";
  20.  
  21. void main(string[] args) {
  22. enforce(args.length > 2, "Usage: " ~ args[0] ~ " minfilesize file");
  23. auto words = readText("words.english").splitlines;
  24. auto size = to!int(args[1]);
  25.  
  26. std.file.write(args[2], head ~ "\n");
  27.  
  28. Layout data;
  29. while(getSize(args[2]) < size) {
  30. foreach(i, U; FieldTypeTuple!Layout) {
  31. static if(is(U == string))
  32. data.tupleof[i] = buildString(words);
  33. else static if(is(U == int))
  34. data.tupleof[i] = uniform(0,words.length);
  35. else static if(is(U == float))
  36. data.tupleof[i] = uniform(0,10)/15.0 * uniform(0,words.length);
  37. }
  38. writeStruct(args[2], data);
  39. }
  40. }
  41.  
  42. string buildString(string[] words) {
  43. auto wordCount = uniform(1,7);
  44. string result;
  45. while(wordCount > 0) {
  46. scope(exit) wordCount--;
  47. result ~= words[uniform(0,words.length)];
  48. switch(dice([90,10,2,2,1])) {
  49. case 0:
  50. result ~= " ";
  51. break;
  52. case 1:
  53. result ~= ", ";
  54. break;
  55. case 2:
  56. result ~= "\" ";
  57. break;
  58. case 3:
  59. result ~= " \"";
  60. break;
  61. case 4:
  62. result ~= "\n";
  63. break;
  64. default:
  65. break;
  66. }
  67. }
  68.  
  69. return result[0..$-1];
  70. }
  71.  
  72. void writeStruct(string file, Layout data) {
  73. string[] elements;
  74.  
  75. foreach(i, U; FieldTypeTuple!Layout) {
  76. elements ~= to!string(data.tupleof[i]);
  77. }
  78.  
  79. string record;
  80. foreach(e; elements) {
  81. if(find(e, `"`, ",", "\n", "\r")[1] == 0)
  82. record ~= e ~ ",";
  83. else
  84. record ~= `"` ~ replace(e, `"`, `""`) ~ `",`;
  85. }
  86.  
  87. std.file.append(file, record[0..$-1] ~ "\n");
  88.  
  89. }
Add Comment
Please, Sign In to add comment