+ Fixes
[opsoft.git] / silentbob / sblib / py_tt.cxx
blob39f89bb87c6973adba360f88e7e600bc09081e52
1 /*
2 * (c) Oleg Puchinin 2006
3 * graycardinalster@gmail.com
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <ctype.h>
11 #include <gclib/gclib.h>
12 #include <mod.h>
13 #include <head.h>
14 #include <dbg.h>
15 #include <the_tt.h>
17 namespace PYTHON {
19 char * t_map;
20 char * t_new;
21 char pch;
22 int i;
23 int ii;
24 int t_size;
25 int spaces;
26 int nlines;
27 int t_op_no;
28 pair_t * d_attachment = NULL;
30 #define T t_map[i]
31 #define TN(ch) do { t_new[ii] = ch; ++ii; } while (0)
33 inline void t_attach ()
35 t_op_no++;
36 d_attachment[t_op_no].pair_op = t_op_no;
37 d_attachment[t_op_no].pair_line = nlines;
38 d_attachment[t_op_no].offset = i;
41 inline void tt_skip ()
43 while (T != '\n' && i < t_size)
44 ++i;
45 --i;
48 void doc_skip ()
50 char * ptrn;
52 if (T == '\'')
53 ptrn = (char *) "'''";
54 else
55 ptrn = (char *) "\"\"\"";
57 i += 3;
58 if (i >= t_size)
59 return;
61 for (; i < t_size; ++i) {
62 if (T == '\n')
63 ++nlines;
65 if (! strncmp (&t_map[i], ptrn, 3))
66 break;
69 i+=2;
72 int py_tt (char * d_input, int size, char * d_output)
74 char ch;
75 char * S;
76 bool b_newline = true;
78 i = 0;
79 ii = 0;
80 t_size = size;
81 t_map = d_input;
82 t_new = d_output;
83 spaces = 0;
84 nlines = 0;
85 t_op_no = 0;
87 for (i = 0; i < t_size; ++i) {
88 if (isalnum (T))
89 goto abc;
91 if (! strncmp (&t_map[i], "\"\"\"", 3) ||
92 ! strncmp (&t_map[i], "'''", 3)) {
93 doc_skip ();
94 continue;
97 if (T == '\"' || T == '\'') {
98 ch = T;
99 S = sstrend (&t_map[i]);
100 assert (S == NULL, "\n\nSibadi 2006\n\n");
101 nlines += Dsyms (&t_map[i], S, '\n');
102 i += S - &t_map[i];
103 TN (ch);
104 TN (ch);
105 continue;
108 if (T == '#') {
109 tt_skip ();
110 continue;
113 if (T == ' ' || T == '\t') {
114 if (b_newline) {
115 ++spaces;
116 TN (T);
118 if (! spaces) {
119 ++spaces;
120 TN (' ');
123 continue;
126 if (T == '\n') {
127 t_attach ();
128 ++nlines;
129 TN (T);
130 continue;
133 if (T == ':')
134 t_attach ();
136 abc:
137 b_newline = false;
138 spaces = 0;
139 TN(T);
142 TN ('\0');
143 return ii+1;
146 char * py_tt_file (struct tt_state_t * tt)
148 if (! tt)
149 return NULL;
151 if (access (tt->fileName, R_OK) != F_OK) {
152 fprintf (stderr, "No such file %s\n", tt->fileName);
153 return NULL;
156 tt_map (tt);
158 tt->result = CNEW (char, 1024*1024);
159 tt->result[0] = 0;
160 d_attachment = (pair_t *) CNEW (char, 1024*1024);
162 tt->attachment = d_attachment;
163 tt->resultSize = py_tt (tt->fileData, tt->fileDataSize, tt->result);
164 return tt->result;
167 char t_op (char ** d_in, char ** d_prev)
169 char *d_ptr = *d_in;
170 char ch = 0;
172 if (*d_prev)
173 *d_prev = *d_in;
175 while (*d_ptr) {
176 if (*d_ptr == '\n' || *d_ptr == ':') {
177 ch = *d_ptr;
178 *d_ptr = 0;
179 break;
181 ++d_ptr;
184 ++d_ptr;
185 *d_in = d_ptr;
186 return ch;
189 } // namespace PYTHON