Very old versions for history.
[opsoft_archive.git] / silentbob / silent_bob-1.2 / src / plugins / python.cpp
blob183c074679ff7df3547f510d7229819c6fd22df7
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"
18 namespace PYTHON {
20 char * t_map;
21 char * t_new;
22 char pch;
23 int i;
24 int ii;
25 int t_size;
26 int spaces;
27 int nlines;
28 int t_op_no;
29 pair_t * d_attachment = NULL;
31 #define T t_map[i]
32 #define TN(ch) do { t_new[ii] = ch; ++ii; } while (0)
34 inline void t_attach ()
36 t_op_no++;
37 d_attachment[t_op_no].pair_op = t_op_no;
38 d_attachment[t_op_no].pair_line = nlines;
39 d_attachment[t_op_no].offset = i;
42 inline void tt_skip ()
44 while (T != '\n' && i < t_size)
45 ++i;
46 --i;
49 void doc_skip ()
51 char * ptrn;
53 if (T == '\'')
54 ptrn = "'''";
55 else
56 ptrn = "\"\"\"";
58 i += 3;
59 if (i >= t_size)
60 return;
62 for (; i < t_size; ++i) {
63 if (T == '\n')
64 ++nlines;
66 if (! strncmp (&t_map[i], ptrn, 3))
67 break;
70 i+=2;
73 int py_tt (char * d_input, int size, char * d_output)
75 char ch;
76 char * S;
77 bool b_newline = true;
79 i = 0;
80 ii = 0;
81 t_size = size;
82 t_map = d_input;
83 t_new = d_output;
84 spaces = 0;
85 nlines = 0;
86 t_op_no = 0;
88 for (i = 0; i < t_size; ++i) {
89 if (isalnum (T))
90 goto abc;
92 if (! strncmp (&t_map[i], "\"\"\"", 3) ||
93 ! strncmp (&t_map[i], "'''", 3)) {
94 doc_skip ();
95 continue;
98 if (T == '\"' || T == '\'') {
99 ch = T;
100 S = sstrend (&t_map[i]);
101 assert (S == NULL, "\n\nSibadi 2006\n\n");
102 nlines += Dsyms (&t_map[i], S, '\n');
103 i += S - &t_map[i];
104 TN (ch);
105 TN (ch);
106 continue;
109 if (T == '#') {
110 tt_skip ();
111 continue;
114 if (T == ' ' || T == '\t') {
115 if (b_newline) {
116 ++spaces;
117 TN (T);
119 if (! spaces) {
120 ++spaces;
121 TN (' ');
124 continue;
127 if (T == '\n') {
128 t_attach ();
129 ++nlines;
130 TN (T);
131 continue;
134 if (T == ':')
135 t_attach ();
137 abc:
138 b_newline = false;
139 spaces = 0;
140 TN(T);
143 TN ('\0');
144 return ii+1;
147 char * py_tt_file (struct tt_state_t * tt)
149 if (! tt)
150 return NULL;
152 if (access (tt->d_file_name, R_OK) != F_OK) {
153 fprintf (stderr, "No such file %s\n", tt->d_file_name);
154 return NULL;
157 tt_map (tt);
159 tt->d_output = CNEW (char, 1024*1024);
160 tt->d_output[0] = 0;
161 d_attachment = (pair_t *) CNEW (char, 1024*1024);
163 tt->d_attachment = d_attachment;
164 tt->d_output_size = py_tt (tt->d_file_in, tt->d_filein_size, tt->d_output);
165 return tt->d_output;
168 char t_op (char ** d_in, char ** d_prev)
170 char *d_ptr = *d_in;
171 char ch = 0;
173 if (*d_prev)
174 *d_prev = *d_in;
176 while (*d_ptr) {
177 if (*d_ptr == '\n' || *d_ptr == ':') {
178 ch = *d_ptr;
179 *d_ptr = 0;
180 break;
182 ++d_ptr;
185 ++d_ptr;
186 *d_in = d_ptr;
187 return ch;
190 } // namespace PYTHON