Very old versions for history.
[opsoft_archive.git] / silentbob / silent_bob / nogui_fdump.cpp
blob98674ef5091658abdea600cf453f8a58011750d6
1 /*
2 * (c) Oleg Puchinin 2006
3 * graycardinalster@gmail.com
4 *
5 */
7 #include "functions.h"
8 #include "dbg.h"
9 /*
10 * February 2006 - comments and strings BUG fixed...
13 bool b_in_comment;
15 bool brace_count (char * d_str, int * d_count, bool b_force_block) // "nice"
17 bool Ret = false;
19 if (! d_str || ! d_count)
20 return false;
22 while (*d_str != 0) {
23 if (!strncmp (d_str, "/*", 2)) {
24 b_in_comment = true;
25 d_str += 2;
26 continue;
29 if (b_in_comment) {
30 if (strncmp (d_str, "*/", 2)) {
31 d_str ++;
32 continue;
33 } else {
34 d_str += 2;
35 b_in_comment = false;
36 continue;
40 if (!strncmp (d_str, "//", 2))
41 break;
43 if (*d_str == '\"' || *d_str == '\'') {
44 d_str = sstrend (d_str);
45 if (d_str == NULL || *d_str == 0) {
46 assert (d_str == NULL, "HimTeh 4");
47 assert (*d_str == 0, "HimTeh 5");
48 break;
52 if (*d_str == '{') {
53 Ret = true;
54 (*d_count)++;
57 if (*d_str == '}') {
58 Ret = true;
59 (*d_count)--;
62 if (*d_str == ';' && *d_count == 0 && !b_force_block) {
63 Ret = true;
64 break;
66 d_str++;
69 return Ret;
72 void nogui_fdump (struct fdump_param_t * d_param)
74 int d_count = 0;
75 DArray d_array;
76 FILE * d_file;
77 int d_size;
78 char * S;
79 int i,a;
81 if (!d_array.from_file (d_param->d_file_name))
82 return;
84 if (d_param->d_file_output == NULL)
85 d_file = stdout;
86 else
87 d_file = fopen (d_param->d_file_output, "w");
89 if (! d_file)
90 return;
92 if (! d_param->linear) {
93 for (a = 0; a < d_param->n_trip; a++)
94 fprintf (d_file, "\t");
95 fprintf (d_file, "//<***>\n");
98 i = d_param->d_line-1;
99 d_size = d_array.get_size ();
100 b_in_comment = false;
102 if (d_array.get (i)[0] != '#') {
103 while (i < d_size) {
104 if (!d_param->linear) {
105 for (a = 0; a < d_param->n_trip; a++)
106 fprintf (d_file, "\t");
108 fprintf (d_file, "%s", d_array.get(i));
110 if (brace_count (d_array.get(i), &d_count, d_param->b_force_block) && !d_count)
111 break;
113 if (!d_count && ((i - d_param->d_line) > 2) && !d_param->b_force_block)
114 break;
116 i++;
118 } else {
119 do {
120 S = d_array.get (i);
121 fprintf (d_file, "%s", S);
122 S = &S[strlen (S)-2];
123 while ((*S == ' ') || (*S == '\t'))
124 S--;
126 if (*S != '\\')
127 break;
128 i++;
129 } while (i < d_size);
132 if (!d_param->linear) {
133 for (a = 0; a < d_param->n_trip; a++)
134 fprintf (d_file, "\t");
135 fprintf (d_file, "//</***>\n");
138 if (d_param->d_file_output != NULL)
139 fclose (d_file);
141 d_array.foreach (free);