diff src/paranormal/libcalc/parser.y @ 342:17311560f45f trunk

[svn] - port from fmemopen() to vfs_buffer_new_from_string().
author nenolod
date Fri, 08 Dec 2006 03:27:31 -0800
parents 795d83dcfdb7
children
line wrap: on
line diff
--- a/src/paranormal/libcalc/parser.y	Thu Dec 07 15:20:41 2006 -0800
+++ b/src/paranormal/libcalc/parser.y	Fri Dec 08 03:27:31 2006 -0800
@@ -156,22 +156,30 @@
   parser_control *pc = (parser_control *) yyparam;
   
   /* Ignore whitespace, get first nonwhite character. */
-  while ((c = fgetc (pc->input)) == ' ' || c == '\t' || c == '\n');
+  while ((c = vfs_getc (pc->input)) == ' ' || c == '\t' || c == '\n');
   
   /* End of input ? */
   if (c == EOF)
     return 0;
-  
+
   /* Char starts a number => parse the number. */
   if (isdigit (c)) {
-    ungetc (c, pc->input);/* Put the char back. */
+    vfs_fseek (pc->input, -1, SEEK_CUR); /* Put the char back. */
     {
       char *old_locale, *saved_locale;
 
       old_locale = setlocale (LC_ALL, NULL);
       saved_locale = g_strdup (old_locale);
       setlocale (LC_ALL, "C");
-      fscanf (pc->input, "%lf", &yylval->d_value);
+      sscanf (((VFSBuffer *)(pc->input->handle))->iter, "%lf", &yylval->d_value);
+
+      while (isdigit(c) || c == '.')
+      {
+        c = vfs_getc(pc->input);
+      }
+
+      vfs_fseek(pc->input, -1, SEEK_CUR);
+
       setlocale (LC_ALL, saved_locale);
       g_free (saved_locale);
     }
@@ -188,10 +196,10 @@
       sym_name = g_string_append_c (sym_name, c);
 
       /* Get another character. */
-      c = fgetc (pc->input);
+      c = vfs_getc (pc->input);
     } while (c != EOF && isalnum (c));
     
-    ungetc (c, pc->input);
+    vfs_fseek (pc->input, -1, SEEK_CUR);
 
     yylval->s_value = sym_name->str;
     
@@ -265,11 +273,14 @@
   return TRUE;
 }
      
-expression_t *expr_compile_string (const char* str, symbol_dict_t *dict) {
+expression_t *expr_compile_string (const char* str, symbol_dict_t *dict)
+{
   parser_control pc;
-  FILE *stream;
+  VFSFile *stream;
 
-  stream = (FILE *) fmemopen ( (char *) str, strlen (str), "r");
+  g_return_val_if_fail(str != NULL && dict != NULL, NULL);
+
+  stream = vfs_buffer_new_from_string ( (char *) str );
 
   pc.input = stream;
   pc.expr = expr_new ();
@@ -281,7 +292,7 @@
     pc.expr = NULL;
   }
 
-  fclose (stream);
+  vfs_fclose (stream);
 
   return pc.expr;
 }