Include <ctype.h>.
authorMORIOKA Tomohiko <tomo.git@chise.org>
Tue, 16 Apr 2013 01:09:15 +0000 (10:09 +0900)
committerMORIOKA Tomohiko <tomo.git@chise.org>
Tue, 16 Apr 2013 01:09:15 +0000 (10:09 +0900)
(cos_read_int): New function.

read.c

diff --git a/read.c b/read.c
index 2fd1566..f8d0eb2 100644 (file)
--- a/read.c
+++ b/read.c
    02111-1307 USA.  */
 
 #include <stdlib.h>
    02111-1307 USA.  */
 
 #include <stdlib.h>
+#include <ctype.h>
 #include "cos-read.h"
 
 #include "cos-read.h"
 
+COS_object
+cos_read_int (unsigned char *str, size_t len, size_t start, size_t* endp)
+{
+  size_t i = start;
+  int c;
+  int negative_flag;
+  int dest;
+
+  if ( i < len )
+    {
+      switch ( str[i] )
+       {
+       case '+':
+         negative_flag = 0;
+         i++;
+         break;
+       case '-':
+         negative_flag = 1;
+         i++;
+         break;
+       default:
+         negative_flag = 0;
+       }
+
+      if ( (i < len) && (c = str[i++])
+          && ('0' <= c) && (c <= '9') )
+       {
+         dest = c - '0';
+
+         while ( i < len )
+           {
+             c = str[i];
+             if ( ('0' <= c) && (c <= '9') )
+               {
+                 dest = dest * 10 + c - '0';
+                 i++;
+               }
+             else if ( isspace (c) )
+               {
+                 *endp = i;
+                 return cos_make_int ( negative_flag ? - dest : dest );
+               }
+             else
+               return NULL;
+           }
+         *endp = i;
+         return cos_make_int ( negative_flag ? - dest : dest );
+       }
+    }
+  return NULL;
+}
+
+
 int
 cos_read_utf8 (unsigned char *str, size_t len, size_t start, size_t* endp)
 {
 int
 cos_read_utf8 (unsigned char *str, size_t len, size_t start, size_t* endp)
 {
@@ -93,6 +147,7 @@ cos_read_char (unsigned char *str, size_t len, size_t start, size_t* endp)
   return -1;
 }
 
   return -1;
 }
 
+
 COS_String
 cos_read_string (unsigned char *str, size_t len, size_t start, size_t* endp)
 {
 COS_String
 cos_read_string (unsigned char *str, size_t len, size_t start, size_t* endp)
 {