parse_text.c
changeset 17 d95d9e7a2b81
parent 15 17a66ceb774a
child 18 64ed4238657f
equal deleted inserted replaced
16:b4e251400e36 17:d95d9e7a2b81
     1 #include <stdio.h>
     1 #include <stdio.h>
       
     2 #include <stdlib.h>
     2 #include "dictre.h"
     3 #include "dictre.h"
       
     4 
       
     5 static int is_http = 0;
       
     6 static int content_length = -1;
       
     7 static struct Dict dakcentiga;
     3 
     8 
     4 static void give_accent_to_word(const char *word)
     9 static void give_accent_to_word(const char *word)
     5 {
    10 {
     6     char def[MAXDEF];
    11     char def[MAXDEF];
     7     char low[MAXWORD];
    12     char low[MAXWORD];
    13 
    18 
    14     /* Get lowercase version */
    19     /* Get lowercase version */
    15     get_lowcase_str(low, word);
    20     get_lowcase_str(low, word);
    16 
    21 
    17     /* Find the lowercase version */
    22     /* Find the lowercase version */
    18     find_def(low, def);
    23     find_def(&dakcentiga, low, def);
    19     if (def[0] != 0) /* found */
    24     if (def[0] != 0) /* found */
    20     {
    25     {
    21         /* Print the word UNTIL a space.
    26         /* Print the word UNTIL a space.
    22          * the definition will have the form:
    27          * the definition will have the form:
    23          *    ACCENTED_WORD NOMINATIVE1 NOMINATIVE2 ... \n */
    28          *    ACCENTED_WORD NOMINATIVE1 NOMINATIVE2 ... \n */
    36 
    41 
    37     /* if first_space == 0 or word not found */
    42     /* if first_space == 0 or word not found */
    38     printf("%s", word);
    43     printf("%s", word);
    39 }
    44 }
    40 
    45 
       
    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 
    41 static void process_text(FILE *in, int pos, int length)
    54 static void process_text(FILE *in, int pos, int length)
    42 {
    55 {
    43     unsigned char tmp[MAXWORD];
    56     unsigned char tmp[MAXWORD];
    44     int wordpos = 0;
    57     int wordpos = 0;
    45     do
    58     do
    46     {
    59     {
    47         int c;
    60         int c;
    48         /* Check pos only if length >= 0 */
    61         /* Check pos only if length >= 0 */
    49         if (length >= 0 && pos >= length)
    62         if (length >= 0 && pos >= length)
    50             break;
    63             break;
    51         c = fgetc(in);
    64         c = my_fgetc(in);
    52         if (c == EOF)
    65         if (c == EOF || c == END_OF_URL)
    53             break;
    66             break;
    54         if (is_ASCII(c))
    67         if (is_ASCII(c))
    55         {
    68         {
    56             if (wordpos != 0)
    69             if (wordpos != 0)
    57             {
    70             {
    66             tmp[wordpos++] = c;
    79             tmp[wordpos++] = c;
    67         }
    80         }
    68 
    81 
    69         pos += 1;
    82         pos += 1;
    70     } while(1);
    83     } while(1);
       
    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;
    71 }
   108 }
    72 
   109 
    73 int main()
   110 int main()
    74 {
   111 {
    75     init_dictionary();
   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     }
    76     process_text(stdin, 0, -1);
   127     process_text(stdin, 0, -1);
    77     end_dictionary();
   128     end_dictionary(&dakcentiga);
       
   129 
       
   130     return 0;
    78 }
   131 }