Fixes
[opsoft.git] / silentbob / src / indent.cxx
blob1c21e31885df5247cb209142a3efb8df69f87a40
1 /*
2 * (c) Oleg Puchinin 2006
3 * graycardinalster@gmail.com
4 *
5 */
7 #include <head.h>
8 #include <dbg.h>
9 #include <the_tt.h>
11 // \033[01;32m
12 extern FILE * d_stream_dbg;
14 const char * d_lpsz_operators [] = { "if", "else", "for", "while", "do", "switch", "#define",
15 "#include", "#else", "#if", "#elif","typedef", "struct", "class",
16 "case", "return", NULL };
18 const char * d_lpsz_oneopops [] = {"if", "else", "for", "while", "do", NULL};
19 const char * d_lpsz_mustlined_ops [] = {"if", "for", "while", "do", NULL};
20 const char * d_lpsz_greenpeace [] = {"struct",
21 "class",
22 "extern",
23 "typedef",
24 "int",
25 "char",
26 "long",
27 "double",
28 "short",
29 NULL
32 // $ silent_bob --indent
33 void nogui_indent () // "UN-TT" ;)
35 struct tt_state_t * d_tt_state;
36 bool b_inmacro = false;
37 char * d_out, *d_ptr;
38 int brace_depth = 0;
39 bool t_lined = true;
40 EArray d_must_lined;
41 EArray d_operators;
42 EArray d_array;
43 char ch;
44 int i;
46 d_tt_state = CNEW (tt_state_t, 1);
47 d_tt_state->fileName = strdup ("-");
48 d_ptr = THE_TT::do_tt_file (d_tt_state);
49 d_out = d_ptr;
51 d_array.push ((char **) d_lpsz_oneopops);
52 d_must_lined.push ((char **) d_lpsz_mustlined_ops);
53 d_operators.push ((char **) d_lpsz_operators);
55 while (true) {
56 ch = t_op (&d_ptr, &d_out); // t_op on top
58 if (! ch)
59 break;
61 if (*d_out == '#')
62 b_inmacro = true;
64 if (ch == '\n' && d_ptr[-1] != '\\') {
65 printf ("%s\n", d_out);
66 b_inmacro = false;
67 continue;
70 if (ch == '}')
71 brace_depth--;
73 if (d_must_lined.snfind (d_out) != -1 && !t_lined && !b_inmacro) {
74 fputc ('\n', stdout);
75 t_lined = true;
78 if (ch != '\n' && !b_inmacro) {
79 for (i = 0; i < brace_depth; i++)
80 printf (" ");
83 if (ch == '{')
84 brace_depth++;
86 printf ("%s", d_out);
88 fputc (ch, stdout);
89 t_lined = false;
91 if (b_inmacro && ch == '}')
92 fputc (' ', stdout);
94 if (!b_inmacro) {
95 if (ch == '}' && brace_depth == 0) {
96 if (*d_ptr == '#' || *d_ptr == '\n')
97 fputc ('\n', stdout);
98 else if (*d_ptr && *d_ptr != ';') {
99 fputc ('\n', stdout);
100 fputc ('\n', stdout);
102 else if (*d_ptr && *d_ptr == ';') {
103 d_ptr+=2;
104 printf (";\n\n");
106 } else {
107 if (ch != '\n') {
108 fputc ('\n', stdout);
110 if (ch == '{')
111 t_lined = true;
116 free_tt_state (d_tt_state);
117 dbg_out;