author | viric <viriketo@gmail.com> |
Fri, 30 Mar 2012 18:54:29 +0200 | |
changeset 30 | d30178b2a9e6 |
parent 28 | 75b6d5659a19 |
permissions | -rw-r--r-- |
14 | 1 |
#include <stdio.h> |
17 | 2 |
#include <stdlib.h> |
14 | 3 |
#include "dictre.h" |
4 |
||
17 | 5 |
static int is_http = 0; |
24 | 6 |
static int give_html = 1; |
17 | 7 |
static int content_length = -1; |
8 |
static struct Dict dakcentiga; |
|
24 | 9 |
static struct Dict dsignifoj; |
10 |
||
11 |
static void print_html_header() |
|
12 |
{ |
|
13 |
printf( |
|
14 |
"<html>\n" |
|
15 |
"<head>\n" |
|
16 |
" <meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\">\n" |
|
17 |
" <title>Akcentigita teksto</title>\n" |
|
18 |
"</head>\n" |
|
19 |
"<style type=\"text/css\" media=\"screen\">\n" |
|
20 |
"div.difino { display: none ;\n" |
|
21 |
" position: absolute;\n" |
|
22 |
" left: 0;\n" |
|
23 |
" background: moccasin;\n" |
|
24 |
" border: thin;\n" |
|
25 |
" border-color: black;\n" |
|
26 |
" padding: 10px 10px 10px 10px;\n" |
|
27 |
" text-indent: 0em;\n" |
|
28 |
" text-align: left;\n" |
|
29 |
" }\n" |
|
30 |
"div.alineo { text-indent: 2em ; text-align: justify }\n" |
|
31 |
"</style>\n" |
|
32 |
"<body>\n" |
|
33 |
"<script language=\"javascript\">\n" |
|
34 |
"var blocked = \"\";\n" |
|
35 |
"\n" |
|
36 |
"function display(element, ev)\n" |
|
37 |
"{\n" |
|
38 |
" var x,y;\n" |
|
28 | 39 |
#if 0 |
40 |
" x = ev.x + window.pageXOffset;\n" |
|
41 |
#endif |
|
42 |
" y = ev.pageY;\n" |
|
24 | 43 |
"\n" |
44 |
" if (blocked != \"\")\n" |
|
45 |
" return;\n" |
|
46 |
#if 0 |
|
47 |
"\n" |
|
48 |
" if (x + 320 > window.innerWidth)\n" |
|
49 |
" x = window.innerWidth - 320;\n" |
|
50 |
"\n" |
|
51 |
" /*\n" |
|
52 |
" if (y < window.innerHeight / 2)\n" |
|
53 |
" {\n" |
|
54 |
" y += 5;\n" |
|
55 |
" document.getElementById(element).style.top = y;\n" |
|
56 |
" } else {\n" |
|
57 |
" y -= 5;\n" |
|
58 |
" document.getElementById(element).style.top = 0;\n" |
|
59 |
" document.getElementById(element).style.bottom = y;\n" |
|
60 |
" }\n" |
|
61 |
" */\n" |
|
62 |
#endif |
|
63 |
" y += 5;\n" |
|
64 |
" document.getElementById(element).style.top = y + 'px';\n" |
|
65 |
" \n" |
|
66 |
/* |
|
67 |
" document.getElementById(element).style.left = 0;\n" |
|
68 |
*/ |
|
69 |
" document.getElementById(element).style.display = 'block';\n" |
|
70 |
"}\n" |
|
71 |
"\n" |
|
72 |
"function undisplay(element)\n" |
|
73 |
"{\n" |
|
74 |
" document.getElementById(element).style.display = 'none';\n" |
|
75 |
"}\n" |
|
76 |
"\n" |
|
28 | 77 |
"function klako(element, ev)\n" |
24 | 78 |
"{\n" |
79 |
" if (blocked == \"\")\n" |
|
80 |
" {\n" |
|
81 |
" display(element, ev);\n" |
|
82 |
" blocked = element;\n" |
|
83 |
" }\n" |
|
84 |
" else\n" |
|
85 |
" {\n" |
|
86 |
" undisplay(blocked);\n" |
|
87 |
" blocked = \"\";\n" |
|
88 |
" }\n" |
|
89 |
"}\n" |
|
90 |
"\n" |
|
91 |
"</script>" |
|
92 |
); |
|
93 |
||
94 |
} |
|
95 |
||
96 |
static void print_html_footer() |
|
97 |
{ |
|
98 |
printf("</body></html>\n"); |
|
99 |
} |
|
100 |
||
25
8d524bb8dcea
Changed the name for zprocess to prepare_akcentiga
viric@llimona
parents:
24
diff
changeset
|
101 |
static void dump_signifo_with_p(const unsigned char *word, |
8d524bb8dcea
Changed the name for zprocess to prepare_akcentiga
viric@llimona
parents:
24
diff
changeset
|
102 |
const unsigned char *signifo) |
24 | 103 |
{ |
104 |
int i; |
|
105 |
char last; |
|
106 |
||
107 |
printf("<p>## Se moto %s:</p>\n<p>", word); |
|
108 |
||
109 |
last = 0; |
|
110 |
i=0; |
|
111 |
do |
|
112 |
{ |
|
113 |
char nun; |
|
114 |
nun = *signifo; |
|
115 |
if (nun == '\0') |
|
116 |
break; |
|
117 |
if (nun == '\n' && last == '\n') |
|
118 |
printf("</p><p>"); |
|
119 |
putchar(nun); |
|
120 |
last = nun; |
|
121 |
++signifo; |
|
122 |
} while(1); |
|
123 |
printf("</p>"); |
|
124 |
} |
|
125 |
||
126 |
static void print_start_signifoj(int id) |
|
127 |
{ |
|
28 | 128 |
printf("<div id=\"vorto%i\" onclick=\"klako('vorto%i', event);\" " |
24 | 129 |
"class=\"difino\"><p>\n", id, id); |
130 |
} |
|
131 |
||
132 |
static void print_end_signifoj() |
|
133 |
{ |
|
134 |
printf("</p></div>"); |
|
135 |
} |
|
136 |
||
137 |
/* This expects the word to start exactly at *wordlist */ |
|
138 |
static void print_signifoj(char *wordlist, int id) |
|
139 |
{ |
|
140 |
char * next_space; |
|
141 |
char def[MAXDEF]; |
|
142 |
int start_shown = 0; |
|
143 |
||
144 |
do { |
|
145 |
next_space = strchr(wordlist, ' '); |
|
146 |
if (next_space == 0) |
|
147 |
next_space = strchr(wordlist, '\n'); |
|
148 |
if (next_space == 0) |
|
149 |
next_space = strchr(wordlist, '\0'); |
|
150 |
if (next_space != 0 && next_space != wordlist) |
|
151 |
{ |
|
152 |
int spacepos; |
|
153 |
spacepos = next_space - wordlist; |
|
154 |
wordlist[spacepos] = 0; |
|
155 |
||
156 |
/* wordlist has the word to search */ |
|
157 |
find_def(&dsignifoj, wordlist, def); |
|
158 |
if (def[0]) |
|
159 |
{ |
|
160 |
if (!start_shown) |
|
161 |
{ |
|
162 |
print_start_signifoj(id); |
|
163 |
start_shown = 1; |
|
164 |
} |
|
165 |
dump_signifo_with_p(wordlist, def); |
|
166 |
} |
|
167 |
} else |
|
168 |
break; |
|
169 |
wordlist = next_space + 1; |
|
170 |
} while(1); |
|
171 |
||
172 |
if (start_shown) |
|
173 |
print_end_signifoj(); |
|
174 |
} |
|
175 |
||
176 |
static print_accented(const char *word, int id) |
|
177 |
{ |
|
178 |
if (give_html) |
|
179 |
{ |
|
28 | 180 |
printf("<span class=\"vorto\" onclick=\"klako('vorto%i', event);\">" |
24 | 181 |
"%s</span>",id,word); |
182 |
} else |
|
183 |
printf("%s", word); |
|
184 |
} |
|
17 | 185 |
|
15 | 186 |
static void give_accent_to_word(const char *word) |
14 | 187 |
{ |
188 |
char def[MAXDEF]; |
|
15 | 189 |
char low[MAXWORD]; |
190 |
char recased[MAXWORD]; |
|
191 |
enum Case vcase[MAXWORD]; |
|
24 | 192 |
static int id = 1; |
14 | 193 |
|
15 | 194 |
/* Get case */ |
195 |
get_case(vcase, word); |
|
196 |
||
197 |
/* Get lowercase version */ |
|
198 |
get_lowcase_str(low, word); |
|
199 |
||
200 |
/* Find the lowercase version */ |
|
17 | 201 |
find_def(&dakcentiga, low, def); |
14 | 202 |
if (def[0] != 0) /* found */ |
203 |
{ |
|
204 |
/* Print the word UNTIL a space. |
|
205 |
* the definition will have the form: |
|
206 |
* ACCENTED_WORD NOMINATIVE1 NOMINATIVE2 ... \n */ |
|
207 |
char *first_space; |
|
15 | 208 |
char spacepos; |
14 | 209 |
first_space = strchr(def, ' '); |
210 |
if (first_space != 0) /* Space found */ |
|
15 | 211 |
{ |
212 |
spacepos = first_space - def; |
|
213 |
def[spacepos] = 0; /* Mark an end of string */ |
|
214 |
reapply_case(recased, def, vcase); |
|
24 | 215 |
print_accented(recased, id); |
216 |
if (give_html) |
|
217 |
print_signifoj(first_space + 1 /*' '*/, id); |
|
218 |
++id; |
|
15 | 219 |
} |
14 | 220 |
return; |
221 |
} |
|
24 | 222 |
else if (give_html) |
223 |
{ |
|
224 |
char def[MAXDEF]; |
|
225 |
/* OPTIMIZE: This, if find, will react in a second search at |
|
226 |
* print_signifoj() */ |
|
227 |
find_def(&dsignifoj, low, def); |
|
228 |
if (def[0]) |
|
229 |
{ |
|
230 |
print_accented(word, id); |
|
231 |
print_signifoj(low, id); |
|
232 |
++id; |
|
233 |
return; |
|
234 |
} |
|
235 |
} |
|
14 | 236 |
|
24 | 237 |
/* else ... */ |
238 |
||
239 |
/* if first_space == 0 or word not found _AND_ word not found in meanings |
|
240 |
* when give_html...*/ |
|
15 | 241 |
printf("%s", word); |
14 | 242 |
} |
243 |
||
17 | 244 |
static int my_fgetc(FILE *f) |
245 |
{ |
|
246 |
if (is_http) |
|
247 |
return http_getc(f); |
|
248 |
else |
|
249 |
return fgetc(f); |
|
250 |
} |
|
251 |
||
14 | 252 |
static void process_text(FILE *in, int pos, int length) |
253 |
{ |
|
254 |
unsigned char tmp[MAXWORD]; |
|
255 |
int wordpos = 0; |
|
18 | 256 |
int skip_non_ascii = 0; |
24 | 257 |
int last = 0; |
18 | 258 |
|
24 | 259 |
if (give_html) |
260 |
printf("<div class=\"alineo\">"); |
|
14 | 261 |
do |
262 |
{ |
|
263 |
int c; |
|
264 |
/* Check pos only if length >= 0 */ |
|
265 |
if (length >= 0 && pos >= length) |
|
266 |
break; |
|
17 | 267 |
c = my_fgetc(in); |
268 |
if (c == EOF || c == END_OF_URL) |
|
14 | 269 |
break; |
24 | 270 |
if (c == '\r') |
271 |
continue; |
|
272 |
||
273 |
/* Process 'last' for eventual line break */ |
|
274 |
if (give_html && c == '\n' && last == '\n') |
|
275 |
printf("</div>\n<div class=\"alineo\">"); |
|
276 |
last = c; |
|
277 |
||
18 | 278 |
if (skip_non_ascii || is_ASCII(c)) |
14 | 279 |
{ |
280 |
if (wordpos != 0) |
|
281 |
{ |
|
282 |
tmp[wordpos] = 0; |
|
283 |
give_accent_to_word(tmp); |
|
284 |
wordpos = 0; |
|
285 |
} |
|
286 |
putchar(c); |
|
18 | 287 |
/* End of skip_non_ascii when we find an |
288 |
* ascii string */ |
|
289 |
if (skip_non_ascii && is_ASCII(c)) |
|
290 |
skip_non_ascii = 0; |
|
14 | 291 |
} |
292 |
else /* non-ASCII - we consider it russian */ |
|
293 |
{ |
|
294 |
tmp[wordpos++] = c; |
|
18 | 295 |
if (wordpos >= MAXWORD) |
296 |
{ |
|
297 |
/* Dump the word and the rest of non-ASCII, because |
|
298 |
* we cannot fit it in 'tmp' */ |
|
299 |
int i; |
|
300 |
for(i=0; i < wordpos; ++i) |
|
301 |
putchar(tmp[i]); |
|
302 |
wordpos=0; |
|
303 |
skip_non_ascii = 1; |
|
304 |
} |
|
14 | 305 |
} |
306 |
||
307 |
pos += 1; |
|
308 |
} while(1); |
|
17 | 309 |
|
310 |
/* End word */ |
|
311 |
if (wordpos != 0) |
|
312 |
{ |
|
313 |
tmp[wordpos] = 0; |
|
314 |
give_accent_to_word(tmp); |
|
315 |
wordpos = 0; |
|
316 |
} |
|
24 | 317 |
if (give_html) |
318 |
printf("</div>"); |
|
17 | 319 |
} |
320 |
||
321 |
static print_http_header() |
|
322 |
{ |
|
323 |
printf("Content-Type:text/html;charset=utf-8\r\n\r\n"); |
|
324 |
} |
|
325 |
||
326 |
int eat_form_ok() |
|
327 |
{ |
|
328 |
const char mask[] = "teksto="; |
|
329 |
char tmp[sizeof(mask)]; |
|
330 |
fread(tmp, 1, sizeof(mask)-1, stdin); |
|
331 |
tmp[sizeof(mask)-1] = 0; |
|
332 |
if (strcmp(mask, tmp) == 0) |
|
333 |
return 1; |
|
334 |
return 0; |
|
14 | 335 |
} |
336 |
||
337 |
int main() |
|
338 |
{ |
|
17 | 339 |
char *c; |
340 |
||
341 |
init_dictionary(&dakcentiga, "akcentiga"); |
|
24 | 342 |
init_dictionary(&dsignifoj, "signifoj"); |
17 | 343 |
|
344 |
if (c = getenv("CONTENT_LENGTH")) |
|
345 |
{ |
|
346 |
content_length = atoi(c); |
|
347 |
is_http = 1; |
|
348 |
} |
|
349 |
if (is_http) |
|
350 |
{ |
|
351 |
print_http_header(); |
|
352 |
if (!eat_form_ok()) |
|
353 |
return -1; |
|
354 |
} |
|
24 | 355 |
|
356 |
if (give_html) |
|
357 |
print_html_header(); |
|
358 |
/* We pass -1 so we don't check content length */ |
|
14 | 359 |
process_text(stdin, 0, -1); |
24 | 360 |
|
361 |
if (give_html) |
|
362 |
print_html_footer(); |
|
17 | 363 |
end_dictionary(&dakcentiga); |
364 |
||
365 |
return 0; |
|
14 | 366 |
} |