14
|
1 |
#include <stdio.h>
|
17
|
2 |
#include <stdlib.h>
|
14
|
3 |
#include "dictre.h"
|
|
4 |
|
17
|
5 |
static int is_http = 0;
|
|
6 |
static int content_length = -1;
|
|
7 |
static struct Dict dakcentiga;
|
|
8 |
|
15
|
9 |
static void give_accent_to_word(const char *word)
|
14
|
10 |
{
|
|
11 |
char def[MAXDEF];
|
15
|
12 |
char low[MAXWORD];
|
|
13 |
char recased[MAXWORD];
|
|
14 |
enum Case vcase[MAXWORD];
|
14
|
15 |
|
15
|
16 |
/* Get case */
|
|
17 |
get_case(vcase, word);
|
|
18 |
|
|
19 |
/* Get lowercase version */
|
|
20 |
get_lowcase_str(low, word);
|
|
21 |
|
|
22 |
/* Find the lowercase version */
|
17
|
23 |
find_def(&dakcentiga, low, def);
|
14
|
24 |
if (def[0] != 0) /* found */
|
|
25 |
{
|
|
26 |
/* Print the word UNTIL a space.
|
|
27 |
* the definition will have the form:
|
|
28 |
* ACCENTED_WORD NOMINATIVE1 NOMINATIVE2 ... \n */
|
|
29 |
char *first_space;
|
15
|
30 |
char spacepos;
|
14
|
31 |
first_space = strchr(def, ' ');
|
|
32 |
if (first_space != 0) /* Space found */
|
15
|
33 |
{
|
|
34 |
spacepos = first_space - def;
|
|
35 |
def[spacepos] = 0; /* Mark an end of string */
|
|
36 |
reapply_case(recased, def, vcase);
|
|
37 |
printf("%s", recased);
|
|
38 |
}
|
14
|
39 |
return;
|
|
40 |
}
|
|
41 |
|
|
42 |
/* if first_space == 0 or word not found */
|
15
|
43 |
printf("%s", word);
|
14
|
44 |
}
|
|
45 |
|
17
|
46 |
static int my_fgetc(FILE *f)
|
|
47 |
{
|
|
48 |
if (is_http)
|
|
49 |
return http_getc(f);
|
|
50 |
else
|
|
51 |
return fgetc(f);
|
|
52 |
}
|
|
53 |
|
14
|
54 |
static void process_text(FILE *in, int pos, int length)
|
|
55 |
{
|
|
56 |
unsigned char tmp[MAXWORD];
|
|
57 |
int wordpos = 0;
|
|
58 |
do
|
|
59 |
{
|
|
60 |
int c;
|
|
61 |
/* Check pos only if length >= 0 */
|
|
62 |
if (length >= 0 && pos >= length)
|
|
63 |
break;
|
17
|
64 |
c = my_fgetc(in);
|
|
65 |
if (c == EOF || c == END_OF_URL)
|
14
|
66 |
break;
|
|
67 |
if (is_ASCII(c))
|
|
68 |
{
|
|
69 |
if (wordpos != 0)
|
|
70 |
{
|
|
71 |
tmp[wordpos] = 0;
|
|
72 |
give_accent_to_word(tmp);
|
|
73 |
wordpos = 0;
|
|
74 |
}
|
|
75 |
putchar(c);
|
|
76 |
}
|
|
77 |
else /* non-ASCII - we consider it russian */
|
|
78 |
{
|
|
79 |
tmp[wordpos++] = c;
|
|
80 |
}
|
|
81 |
|
|
82 |
pos += 1;
|
|
83 |
} while(1);
|
17
|
84 |
|
|
85 |
/* End word */
|
|
86 |
if (wordpos != 0)
|
|
87 |
{
|
|
88 |
tmp[wordpos] = 0;
|
|
89 |
give_accent_to_word(tmp);
|
|
90 |
wordpos = 0;
|
|
91 |
}
|
|
92 |
}
|
|
93 |
|
|
94 |
static print_http_header()
|
|
95 |
{
|
|
96 |
printf("Content-Type:text/html;charset=utf-8\r\n\r\n");
|
|
97 |
}
|
|
98 |
|
|
99 |
int eat_form_ok()
|
|
100 |
{
|
|
101 |
const char mask[] = "teksto=";
|
|
102 |
char tmp[sizeof(mask)];
|
|
103 |
fread(tmp, 1, sizeof(mask)-1, stdin);
|
|
104 |
tmp[sizeof(mask)-1] = 0;
|
|
105 |
if (strcmp(mask, tmp) == 0)
|
|
106 |
return 1;
|
|
107 |
return 0;
|
14
|
108 |
}
|
|
109 |
|
|
110 |
int main()
|
|
111 |
{
|
17
|
112 |
char *c;
|
|
113 |
|
|
114 |
init_dictionary(&dakcentiga, "akcentiga");
|
|
115 |
|
|
116 |
if (c = getenv("CONTENT_LENGTH"))
|
|
117 |
{
|
|
118 |
content_length = atoi(c);
|
|
119 |
is_http = 1;
|
|
120 |
}
|
|
121 |
if (is_http)
|
|
122 |
{
|
|
123 |
print_http_header();
|
|
124 |
if (!eat_form_ok())
|
|
125 |
return -1;
|
|
126 |
}
|
14
|
127 |
process_text(stdin, 0, -1);
|
17
|
128 |
end_dictionary(&dakcentiga);
|
|
129 |
|
|
130 |
return 0;
|
14
|
131 |
}
|