Very old versions for history.
[opsoft_archive.git] / silentbob / silent_bob / nogui_indent.cpp
bloba3177b076a09f713bb693147d0e8a6c1c3422006
1 /*
2 * (c) Oleg Puchinin 2006
3 * graycardinalster@gmail.com
4 *
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <string.h>
11 #include <dlib.h>
12 #include "functions.h"
13 #include "dbg.h"
15 // \033[01;32m
16 extern FILE * d_stream_dbg;
18 char * d_lpsz_operators [] = { "if", "else", "for", "while", "do", "switch", "#define",
19 "#include", "#else", "#if", "#elif","typedef", "struct", "class",
20 "case", "return", NULL };
22 char * d_lpsz_oneopops [] = {"if", "else", "for", "while", "do", NULL};
23 char * d_lpsz_mustlined_ops [] = {"if", "for", "while", "do", NULL};
24 char * d_lpsz_greenpeace [] = {"struct",
25 "class",
26 "extern",
27 "typedef",
28 "int",
29 "char",
30 "long",
31 "double",
32 "short",
33 NULL
36 // $ silent_bob --indent
37 void nogui_indent () // "UN-TT" ;)
39 struct tt_state_t * d_tt_state;
40 bool b_inmacro = false;
41 char * d_out, *d_ptr;
42 int brace_depth = 0;
43 bool t_lined = true;
44 EArray d_must_lined;
45 EArray d_operators;
46 EArray d_array;
47 char ch;
48 int i;
50 d_tt_state = CNEW (tt_state_t, 1);
51 d_tt_state->d_file_name = "-";
52 d_ptr = THE_TT::do_tt_file (d_tt_state);
53 d_out = d_ptr;
55 d_array.push (d_lpsz_oneopops);
56 d_must_lined.push (d_lpsz_mustlined_ops);
57 d_operators.push (d_lpsz_operators);
59 while (true) {
60 ch = t_op (&d_ptr, &d_out); // t_op on top
62 if (! ch)
63 break;
65 if (*d_out == '#')
66 b_inmacro = true;
68 if (ch == '\n' && d_ptr[-1] != '\\') {
69 printf ("%s\n", d_out);
70 b_inmacro = false;
71 continue;
74 if (ch == '}')
75 brace_depth--;
77 if (d_must_lined.snfind (d_out) != -1 && !t_lined && !b_inmacro) {
78 fputc ('\n', stdout);
79 t_lined = true;
82 if (ch != '\n' && !b_inmacro) {
83 for (i = 0; i < brace_depth; i++)
84 printf (" ");
87 if (ch == '{')
88 brace_depth++;
90 printf ("%s", d_out);
92 fputc (ch, stdout);
93 t_lined = false;
95 if (b_inmacro && ch == '}')
96 fputc (' ', stdout);
98 if (!b_inmacro) {
99 if (ch == '}' && brace_depth == 0) {
100 if (*d_ptr == '#' || *d_ptr == '\n')
101 fputc ('\n', stdout);
102 else if (*d_ptr && *d_ptr != ';') {
103 fputc ('\n', stdout);
104 fputc ('\n', stdout);
106 else if (*d_ptr && *d_ptr == ';') {
107 d_ptr+=2;
108 printf (";\n\n");
110 } else {
111 if (ch != '\n') {
112 fputc ('\n', stdout);
113 } // if
114 if (ch == '{')
115 t_lined = true;
116 } // else
120 THE_TT::free_tt_state (d_tt_state);
121 dbg_out;