parse_text.c
changeset 14 a961bb8806b9
child 15 17a66ceb774a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/parse_text.c	Wed Aug 29 00:19:14 2007 +0200
@@ -0,0 +1,64 @@
+#include <stdio.h>
+#include "dictre.h"
+
+static void give_accent_to_word(const char *tmp)
+{
+    char def[MAXDEF];
+
+    find_def(tmp, def);
+    if (def[0] != 0) /* found */
+    {
+        /* Print the word UNTIL a space.
+         * the definition will have the form:
+         *    ACCENTED_WORD NOMINATIVE1 NOMINATIVE2 ... \n */
+        char *first_space;
+        char *pos;
+        first_space = strchr(def, ' ');
+        if (first_space != 0) /* Space found */
+            for(pos = def; pos < first_space; ++pos)
+                putchar(*pos);
+        return;
+    }
+
+    /* if first_space == 0 or word not found */
+    printf("%s", tmp);
+}
+
+static void process_text(FILE *in, int pos, int length)
+{
+    unsigned char tmp[MAXWORD];
+    int wordpos = 0;
+    do
+    {
+        int c;
+        /* Check pos only if length >= 0 */
+        if (length >= 0 && pos >= length)
+            break;
+        c = fgetc(in);
+        if (c == EOF)
+            break;
+        if (is_ASCII(c))
+        {
+            if (wordpos != 0)
+            {
+                tmp[wordpos] = 0;
+                give_accent_to_word(tmp);
+                wordpos = 0;
+            }
+            putchar(c);
+        }
+        else /* non-ASCII - we consider it russian */
+        {
+            tmp[wordpos++] = c;
+        }
+
+        pos += 1;
+    } while(1);
+}
+
+int main()
+{
+    init_dictionary();
+    process_text(stdin, 0, -1);
+    end_dictionary();
+}