parse_text.c
changeset 17 d95d9e7a2b81
parent 15 17a66ceb774a
child 18 64ed4238657f
--- a/parse_text.c	Sat Sep 01 01:19:18 2007 +0200
+++ b/parse_text.c	Sat Sep 01 12:26:22 2007 +0200
@@ -1,6 +1,11 @@
 #include <stdio.h>
+#include <stdlib.h>
 #include "dictre.h"
 
+static int is_http = 0;
+static int content_length = -1;
+static struct Dict dakcentiga;
+
 static void give_accent_to_word(const char *word)
 {
     char def[MAXDEF];
@@ -15,7 +20,7 @@
     get_lowcase_str(low, word);
 
     /* Find the lowercase version */
-    find_def(low, def);
+    find_def(&dakcentiga, low, def);
     if (def[0] != 0) /* found */
     {
         /* Print the word UNTIL a space.
@@ -38,6 +43,14 @@
     printf("%s", word);
 }
 
+static int my_fgetc(FILE *f)
+{
+    if (is_http)
+        return http_getc(f);
+    else
+        return fgetc(f);
+}
+
 static void process_text(FILE *in, int pos, int length)
 {
     unsigned char tmp[MAXWORD];
@@ -48,8 +61,8 @@
         /* Check pos only if length >= 0 */
         if (length >= 0 && pos >= length)
             break;
-        c = fgetc(in);
-        if (c == EOF)
+        c = my_fgetc(in);
+        if (c == EOF || c == END_OF_URL)
             break;
         if (is_ASCII(c))
         {
@@ -68,11 +81,51 @@
 
         pos += 1;
     } while(1);
+
+    /* End word */
+    if (wordpos != 0)
+    {
+        tmp[wordpos] = 0;
+        give_accent_to_word(tmp);
+        wordpos = 0;
+    }
+}
+
+static print_http_header()
+{
+    printf("Content-Type:text/html;charset=utf-8\r\n\r\n");
+}
+
+int eat_form_ok()
+{
+    const char mask[] = "teksto=";
+    char tmp[sizeof(mask)];
+    fread(tmp, 1, sizeof(mask)-1, stdin);
+    tmp[sizeof(mask)-1] = 0;
+    if (strcmp(mask, tmp) == 0)
+        return 1;
+    return 0;
 }
 
 int main()
 {
-    init_dictionary();
+    char *c;
+
+    init_dictionary(&dakcentiga, "akcentiga");
+
+    if (c = getenv("CONTENT_LENGTH"))
+    {
+        content_length = atoi(c);
+        is_http = 1;
+    }
+    if (is_http)
+    {
+        print_http_header();
+        if (!eat_form_ok())
+            return -1;
+    }
     process_text(stdin, 0, -1);
-    end_dictionary();
+    end_dictionary(&dakcentiga);
+
+    return 0;
 }