File generation

Run Settings
LanguageC++
Language Version
Run Command
#include <stdio.h> #include <stdlib.h> #include <assert.h> struct nil{}; struct GenerateSubStruct{ int type; union{ char *string; int int_number; float float_number; double double_number; }; GenerateSubStruct(nil){type = 0;} GenerateSubStruct(char *a){string = a; type = 1;} GenerateSubStruct(int a){int_number = a; type = 2;} GenerateSubStruct(float a){float_number = a; type = 3;} GenerateSubStruct(double a){double_number = a; type = 4;} }; struct Generate; void Generate_out(Generate *in, char *string); void Generate_out(Generate *in, int number_in); void Generate_out(Generate *in, float number_in); void Generate_out(Generate *in, double number_in); struct Generate{ int indent_level = 0; int index = 0; int size = 0; bool is_new_line = 1; char *buffer; explicit Generate(int size_in) { size = size_in * 1024; buffer = (char *)calloc(size,1); } void add(char a_in) { buffer[index++] = a_in; } void print() { printf("%s", buffer); } void out( GenerateSubStruct a = nil{}, GenerateSubStruct b = nil{}, GenerateSubStruct c = nil{}, GenerateSubStruct d = nil{}, GenerateSubStruct e = nil{}, GenerateSubStruct f = nil{}, GenerateSubStruct g = nil{}, GenerateSubStruct h = nil{}, GenerateSubStruct i = nil{}, GenerateSubStruct j = nil{}) { GenerateSubStruct array[] = {a,b,c,d,e,f,g,h,i,j}; for(int i = 0; i < sizeof(array) / sizeof(*array); ++i) { switch(array[i].type) { case 1:{ Generate_out(this, array[i].string); }break; case 2:{ Generate_out(this, array[i].int_number); }break; case 3:{ Generate_out(this, array[i].float_number); }break; case 4:{ Generate_out(this, array[i].double_number); }break; } } } }; void Generate_out(Generate *in, double number_in) { int pre_index = in->index; in->index += sprintf(&in->buffer[in->index], "%g" ,number_in); bool found_dot = 0; for(;pre_index < in->index; ++pre_index) { if(in->buffer[pre_index] == '.') { found_dot = 1; break; } } if(found_dot == 0) { in->add('.'); } } void Generate_out(Generate *in, float number_in) { int pre_index = in->index; in->index += sprintf(&in->buffer[in->index], "%g" ,number_in); bool found_dot = 0; for(;pre_index < in->index; ++pre_index) { if(in->buffer[pre_index] == '.') { found_dot = 1; break; } } if(found_dot == 0) { in->add('.'); } in->add('f'); } void Generate_out(Generate *in, int number_in) { in->index += sprintf(&in->buffer[in->index], "%i" ,number_in); } void Generate_out(Generate *in, char *string) { for(int i = 0; string[i] != 0; ++i) { if(in->is_new_line == 1) { while(string[i] == ' ') { ++i; } in->is_new_line = 0; } if(string[i] == '{') { in->add(string[i]); in->add('\n'); in->indent_level++; for(int j = 0; j < in->indent_level; ++j) in->add('\t'); in->is_new_line = 1; } else if(string[i] == '}') { assert(in->buffer[in->index-1] == '\t'); in->index--; in->add(string[i]); bool local_white_space = 1; bool first_word = 1; if(string[i+1] == ';') in->add(string[++i]); in->add('\n'); in->indent_level--; for(int j = 0; j < in->indent_level; ++j) in->add('\t'); in->is_new_line = 1; } else if(string[i] == ';') { in->add(string[i]); in->add('\n'); for(int j = 0; j < in->indent_level; ++j) in->add('\t'); in->is_new_line = 1; } else { in->add(string[i]); } } assert(in->index < in->size); } int main() { Generate file(128); file.out("struct ta{ int g; char b; void tera(int d) { printf(\"%i \"); struct{ int a; } ad;}{int eple;}};int a = 32;"); file.out("struct tega{{ int "); file.out("a; int b;}"); file.out("}eple","t;"); file.out("int b = ", 999.0f, ";"); file.print(); return 0; }
Editor Settings
Theme
Key bindings
Full width
Lines