#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);
void Generate_format(char *);
struct Generate{
int index = 0;
int size = 0;
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()
{
Generate_format(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)
{
in->index += sprintf(&in->buffer[in->index], "%f" ,number_in);
}
void Generate_out(Generate *in, float number_in)
{
in->index += sprintf(&in->buffer[in->index], "%f" ,number_in);
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)
{
int i = 0;
bool single_line_statment = false;
if(string[i] == '#' || (string[i] == '/' && string[i+1] == '/') )
{
single_line_statment = true;
}
for(i = 0; string[i] != 0; ++i)
{
in->add(string[i]);
}
if(single_line_statment)
{
in->add('\n');
}
assert(in->index < in->size);
}
char get_next(char *buffer, int i)
{
char result = 0;
for(int j = i+1; buffer[j] != 0; ++j)
{
if(buffer[j] == '{')
{
result = '{';
break;
}
else if(buffer[j] == '}')
{
result = '}';
break;
}
else if(buffer[j] == ';')
{
result = ';';
break;
}
}
return result;
}
void new_line(int indent)
{
putchar('\n');
for(int i = 0; i < indent; ++i)
putchar('\t');
}
void Generate_format(char *buffer)
{
int indent = 0;
char next = 0;
bool is_on_new_line = 1;
for(int i = 0; buffer[i] != 0; ++i)
{
if(is_on_new_line == 0 || buffer[i] != ' ')
{
is_on_new_line = 0;
putchar(buffer[i]);
}
#define LINE(control) new_line(control indent); is_on_new_line = 1;
if(buffer[i] == '{')
{
switch(get_next(buffer, i))
{
case '{': LINE(++) break;
case '}': break;
case ';': LINE(++) break;
}
}
else if(buffer[i] == '}')
{
switch(get_next(buffer, i))
{
case '{': LINE() break;
case '}': LINE(--) break;
case ';':{
char struct_or_function_state = 0;
for(int j = i+1; buffer[j] != 0; ++j)
{
if(struct_or_function_state == 0)
{
if(buffer[j] == ';') break;
else if(buffer[j] != ' ') struct_or_function_state = 1;
}
else if(struct_or_function_state == 1)
{
if(buffer[j] == ';') break;
else if(buffer[j] == ' ') struct_or_function_state = 2;
}
else if(struct_or_function_state == 2)
{
if(buffer[j] != ' ')
{
//printf("|%c|", buffer[j]);
if(buffer[j] == ';')
{
break;
}
else
{
LINE();
break;
}
}
}
}
}break; // function or struct name?
}
}
else if(buffer[i] == ';')
{
switch(get_next(buffer, i))
{
case '{': LINE() break;
case '}': LINE(--) break;
case ';': LINE() break;
}
}
#undef LINE
}
}
int main()
{
Generate file(128);
file.out("#include <stdio.h>");
file.out("// Some comment", "// And another");
file.out("struct ta{ int g; char b; void tera(int d) { printf(\"%i \"); struct{ int a; }a ;}{int eple;}};int a = 32;");
file.out("struct tega{{ int ");
file.out("a; int b;}");
file.out("}eple","t;");
file.out("int b = ", 5.f, ";");
file.out("int b[] = {{{12, 23, 3, 343, 234};};};");
file.out("int add(){int eple;} int tera = 32;");
file.out("if(32){int tg;}else{int ver;}int a;");
file.print();
return 0;
}