Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import std.stdio;
- import std.string;
- import std.algorithm;
- import std.random;
- import std.file;
- import std.traits;
- import std.exception;
- import std.typecons;
- import std.conv;
- struct Layout {
- string a;
- string b;
- int c;
- string d;
- float e;
- }
- auto head = "a,b,c,d,e";
- void main(string[] args) {
- enforce(args.length > 2, "Usage: " ~ args[0] ~ " minfilesize file");
- auto words = readText("words.english").splitlines;
- auto size = to!int(args[1]);
- std.file.write(args[2], head ~ "\n");
- Layout data;
- while(getSize(args[2]) < size) {
- foreach(i, U; FieldTypeTuple!Layout) {
- static if(is(U == string))
- data.tupleof[i] = buildString(words);
- else static if(is(U == int))
- data.tupleof[i] = uniform(0,words.length);
- else static if(is(U == float))
- data.tupleof[i] = uniform(0,10)/15.0 * uniform(0,words.length);
- }
- writeStruct(args[2], data);
- }
- }
- string buildString(string[] words) {
- auto wordCount = uniform(1,7);
- string result;
- while(wordCount > 0) {
- scope(exit) wordCount--;
- result ~= words[uniform(0,words.length)];
- switch(dice([90,10,2,2,1])) {
- case 0:
- result ~= " ";
- break;
- case 1:
- result ~= ", ";
- break;
- case 2:
- result ~= "\" ";
- break;
- case 3:
- result ~= " \"";
- break;
- case 4:
- result ~= "\n";
- break;
- default:
- break;
- }
- }
- return result[0..$-1];
- }
- void writeStruct(string file, Layout data) {
- string[] elements;
- foreach(i, U; FieldTypeTuple!Layout) {
- elements ~= to!string(data.tupleof[i]);
- }
- string record;
- foreach(e; elements) {
- if(find(e, `"`, ",", "\n", "\r")[1] == 0)
- record ~= e ~ ",";
- else
- record ~= `"` ~ replace(e, `"`, `""`) ~ `",`;
- }
- std.file.append(file, record[0..$-1] ~ "\n");
- }
Add Comment
Please, Sign In to add comment