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