* src/SDCCutil.c: fixed a bug in (get_pragma_token)
[fw/sdcc] / src / SDCCutil.c
index 32c7bf74d4f2af67886c93850f24825ed1ab7257..34db23dab44fcadfeddf2bf9d944c1555484fd62 100644 (file)
    what you give them.   Help stamp out software-hoarding!
 -------------------------------------------------------------------------*/
 
-#ifdef _WIN32
+#include <math.h>
 #include <ctype.h>
+
+#ifdef _WIN32
 #include <windows.h>
 #endif
 #include <sys/stat.h>
@@ -136,7 +138,7 @@ getBinPath(const char *prel)
     return path;
   }
   /* not enough info in prel; do it with module name */
-  else if (0 != GetModuleFileName(NULL, path, sizeof path) != 0 &&
+  else if (0 != GetModuleFileName(NULL, path, sizeof path) &&
     NULL != (p = strrchr(path, DIR_SEPARATOR_CHAR))) {
     *p = '\0';
     return path;
@@ -296,8 +298,6 @@ const char *getBuildNumber(void)
   return (SDCC_BUILD_NUMBER);
 }
 
-
-
 #if defined(HAVE_VSNPRINTF) || defined(HAVE_VSPRINTF)
 size_t SDCCsnprintf(char *dst, size_t n, const char *fmt, ...)
 {
@@ -325,5 +325,66 @@ size_t SDCCsnprintf(char *dst, size_t n, const char *fmt, ...)
 
   return len;
 }
-
 #endif
+
+/** Pragma tokenizer
+ */
+void
+init_pragma_token(struct pragma_token_s *token)
+{
+  dbuf_init(&token->dbuf, 16);
+  token->type = TOKEN_UNKNOWN;
+}
+
+char *
+get_pragma_token(const char *s, struct pragma_token_s *token)
+{
+  dbuf_set_size(&token->dbuf, 0);
+
+  /* skip leading spaces */
+  while ('\n' != *s && isspace(*s))
+    ++s;
+
+  if ('\0' == *s || '\n' == *s)
+    {
+      token->type = TOKEN_EOL;
+    }
+  else
+    {
+      char *end;
+
+      long val = strtol(s, &end, 0);
+
+      if (end != s && ('\0' == *end || isspace(*end)))
+        {
+          token->val.int_val = val;
+          token->type = TOKEN_INT;
+          dbuf_append(&token->dbuf, s, end - s);
+          s = end;
+        }
+      else
+        {
+          while ('\0' != *s && !isspace(*s))
+            {
+              dbuf_append(&token->dbuf, s, 1);
+              ++s;
+            }
+
+          token->type = TOKEN_STR;
+        }
+    }
+
+  return (char *)s;
+}
+
+const char *
+get_pragma_string(struct pragma_token_s *token)
+{
+  return dbuf_c_str(&token->dbuf);
+}
+
+void
+free_pragma_token(struct pragma_token_s *token)
+{
+  dbuf_destroy(&token->dbuf);
+}