Browse Source

Fix up ctype.h

Eric Andersen 24 years ago
parent
commit
3b81d48f27
5 changed files with 154 additions and 83 deletions
  1. 4 0
      Rules.mak
  2. 42 29
      include/ctype.h
  3. 5 1
      libc/misc/Makefile
  4. 9 5
      libc/misc/ctype/Makefile
  5. 94 48
      libc/misc/ctype/ctype.c

+ 4 - 0
Rules.mak

@@ -56,6 +56,10 @@ HAS_MMU = true
 # Disable this if your CPU has a floating point unit (FPU)
 HAS_FLOATS = true
 
+# Use C functions instead of macros for ctype.h stuff
+# This should generally stay false...
+USE_CTYPE_C_FUNCTIONS = false
+
 # If you are running a cross compiler, you may want to set this
 # to something more interesting...
 CROSS = #powerpc-linux-

+ 42 - 29
include/ctype.h

@@ -1,38 +1,51 @@
-/*
- *	ctype.h		Character classification and conversion
- */
+/* ctype.h
+ * Character classification and conversion */
 
 #ifndef __CTYPE_H
 #define __CTYPE_H
 
-extern	unsigned char	__ctype[];
+#ifdef USE_CTYPE_C_FUNCTIONS
+/* function prototpes */ 
+extern int isalnum(int c);
+extern int isalpha(int c);
+extern int isascii(int c);
+extern int iscntrl(int c);
+extern int isdigit(int c);
+extern int isgraph(int c);
+extern int islower(int c);
+extern int isprint(int c);
+extern int ispunct(int c);
+extern int isspace(int c);
+extern int isupper(int c);
+extern int isxdigit(int c);
+extern int isxlower(int c);
+extern int isxupper(int c);
+extern int toascii(int c);
+extern int tolower(int c);
+extern int toupper(int c);
 
-#define	__CT_d	0x01		/* numeric digit */
-#define	__CT_u	0x02		/* upper case */
-#define	__CT_l	0x04		/* lower case */
-#define	__CT_c	0x08		/* control character */
-#define	__CT_s	0x10		/* whitespace */
-#define	__CT_p	0x20		/* punctuation */
-#define	__CT_x	0x40		/* hexadecimal */
+#else
 
-#define	toupper(c)	(islower(c) ? (c)^0x20 : (c))
-#define	tolower(c)	(isupper(c) ? (c)^0x20 : (c))
-#define	_toupper(c)	((c)^0x20)
-#define	_tolower(c)	((c)^0x20)
-#define	toascii(c)	((c)&0x7F)
+/* macro definitions */
+#define isalnum(c)  (isalpha(c) || isdigit(c))
+#define isalpha(c)  (isupper(c) || islower(c))
+#define isascii(c)  (c > 0 && c <= 0x7f)
+#define iscntrl(c)  ((c > 0) && ((c <= 0x1F) || (c == 0x7f)))
+#define isdigit(c)  (c >= '0' && c <= '9')
+#define isgraph(c)  (c != ' ' && isprint(c))
+#define islower(c)  (c >=  'a' && c <= 'z')
+#define isprint(c)  (c >= ' ' && c <= '~')
+#define ispunct(c)  ((c > ' ' && c <= '~') && !isalnum(c))
+#define isspace(c)  (c == ' ' || c == '\f' || c == '\n' || c == '\r' ||\
+			c == '\t' || c == '\v')
+#define isupper(c)  (c >=  'A' && c <= 'Z')
+#define isxdigit(c) (isxupper(c) || isxlower(c))
+#define isxlower(c) (isdigit(c) || (c >= 'a' && c <= 'f'))
+#define isxupper(c) (isdigit(c) || (c >= 'A' && c <= 'F'))
+#define toascii(c)  (c & 0x7f)
+#define tolower(c)  (isupper(c) ? ( c - 'A' + 'a') : (c))
+#define toupper(c)  (islower(c) ? (c - 'a' + 'A') : (c))
 
-/* Note the '!!' is a cast to 'bool' and even BCC deletes it in an if()  */
-#define	isalnum(c)	(!!(__ctype[(int) c]&(__CT_u|__CT_l|__CT_d)))
-#define	isalpha(c)	(!!(__ctype[(int) c]&(__CT_u|__CT_l)))
-#define	isascii(c)	(!((c)&~0x7F))
-#define	iscntrl(c)	(!!(__ctype[(int) c]&__CT_c))
-#define	isdigit(c)	(!!(__ctype[(int) c]&__CT_d))
-#define	isgraph(c)	(!(__ctype[(int) c]&(__CT_c|__CT_s)))
-#define	islower(c)	(!!(__ctype[(int) c]&__CT_l))
-#define	isprint(c)	(!(__ctype[(int) c]&__CT_c))
-#define	ispunct(c)	(!!(__ctype[(int) c]&__CT_p))
-#define	isspace(c)	(!!(__ctype[(int) c]&__CT_s))
-#define	isupper(c)	(!!(__ctype[(int) c]&__CT_u))
-#define	isxdigit(c)	(!!(__ctype[(int) c]&__CT_x))
+#endif
 
 #endif /* __CTYPE_H */

+ 5 - 1
libc/misc/Makefile

@@ -21,7 +21,11 @@
 # respective copyright holders.
 
 
-DIRS = assert crypt ctype fnmatch glob internals lsearch regex shm time
+DIRS = assert crypt fnmatch glob internals lsearch regex shm time
+
+ifeq ($(USE_CTYPE_C_FUNCTIONS),true)
+    DIRS+=ctype
+endif
 
 all: libc.a
 

+ 9 - 5
libc/misc/ctype/Makefile

@@ -24,17 +24,21 @@ TOPDIR=../../
 include $(TOPDIR)Rules.mak
 LIBC=$(TOPDIR)libc.a
 
-CSRC=ctype.c
-COBJS=$(patsubst %.c,%.o, $(CSRC))
-OBJS=$(COBJS)
-
-all: $(OBJS) $(LIBC)
+MSRC=ctype.c
+MOBJ= isalnum.o isalpha.o isascii.o iscntrl.o isdigit.o isgraph.o \
+	islower.o isprint.o ispunct.o isspace.o isupper.o isxdigit.o \
+	isxlower.o isxupper.o toascii.o tolower.o toupper.o 
+OBJS=$(MOBJ)
+all: $(MOBJ) $(LIBC)
 
 $(LIBC): ar-target
 
 ar-target: $(OBJS)
 	$(AR) $(ARFLAGS) $(LIBC) $(OBJS)
 
+$(MOBJ): $(MSRC)
+	$(CC) $(CFLAGS) -DL_$* $< -c -o $*.o
+
 $(OBJS): Makefile
 
 clean:

+ 94 - 48
libc/misc/ctype/ctype.c

@@ -1,67 +1,113 @@
-/* Copyright (C) 1995,1996 Robert de Bath <rdebath@cix.compulink.co.uk>
- * This file is part of the Linux-8086 C library and is distributed
+/* ctype.c	
+ * Character classification and conversion
+ * Copyright (C) 2000 Lineo, Inc.
+ * Written by Erik Andersen
+ * This file is part of the uC-Libc C library and is distributed
  * under the GNU Library General Public License.
  */
 
-/*
- *  CTYPE.C	Character classification and conversion
- */
-
 #include <ctype.h>
 
-#undef	toupper
-#undef	tolower
+int
+isalnum( int c )
+{
+    return (isalpha(c) || isdigit(c));
+}
 
-unsigned char __ctype[128] = {
-	__CT_c, __CT_c, __CT_c, __CT_c,	/* 0x00..0x03 */
-	__CT_c, __CT_c, __CT_c, __CT_c,	/* 0x04..0x07 */
-	__CT_c, __CT_c | __CT_s, __CT_c | __CT_s, __CT_c | __CT_s,	/* 0x08..0x0B */
-	__CT_c | __CT_s, __CT_c | __CT_s, __CT_c, __CT_c,	/* 0x0C..0x0F */
+int
+isalpha( int c )
+{
+    return (isupper(c) || islower(c));
+}
 
-	__CT_c, __CT_c, __CT_c, __CT_c,	/* 0x10..0x13 */
-	__CT_c, __CT_c, __CT_c, __CT_c,	/* 0x14..0x17 */
-	__CT_c, __CT_c, __CT_c, __CT_c,	/* 0x18..0x1B */
-	__CT_c, __CT_c, __CT_c, __CT_c,	/* 0x1C..0x1F */
+int
+isascii( int c )
+{
+    return (c > 0 && c <= 0x7f);
+}
 
-	__CT_s, __CT_p, __CT_p, __CT_p,	/* 0x20..0x23 */
-	__CT_p, __CT_p, __CT_p, __CT_p,	/* 0x24..0x27 */
-	__CT_p, __CT_p, __CT_p, __CT_p,	/* 0x28..0x2B */
-	__CT_p, __CT_p, __CT_p, __CT_p,	/* 0x2C..0x2F */
+int
+iscntrl( int c )
+{
+    return ((c > 0) && ((c <= 0x1f) || (c == 0x7f)));
+}
 
-	__CT_d | __CT_x, __CT_d | __CT_x, __CT_d | __CT_x, __CT_d | __CT_x,	/* 0x30..0x33 */
-	__CT_d | __CT_x, __CT_d | __CT_x, __CT_d | __CT_x, __CT_d | __CT_x,	/* 0x34..0x37 */
-	__CT_d | __CT_x, __CT_d | __CT_x, __CT_p, __CT_p,	/* 0x38..0x3B */
-	__CT_p, __CT_p, __CT_p, __CT_p,	/* 0x3C..0x3F */
+int
+isdigit( int c )
+{
+    return (c >= '0' && c <= '9');
+}
 
-	__CT_p, __CT_u | __CT_x, __CT_u | __CT_x, __CT_u | __CT_x,	/* 0x40..0x43 */
-	__CT_u | __CT_x, __CT_u | __CT_x, __CT_u | __CT_x, __CT_u,	/* 0x44..0x47 */
-	__CT_u, __CT_u, __CT_u, __CT_u,	/* 0x48..0x4B */
-	__CT_u, __CT_u, __CT_u, __CT_u,	/* 0x4C..0x4F */
+int
+isgraph( int c )
+{
+    return (c != ' ' && isprint(c));
+}
 
-	__CT_u, __CT_u, __CT_u, __CT_u,	/* 0x50..0x53 */
-	__CT_u, __CT_u, __CT_u, __CT_u,	/* 0x54..0x57 */
-	__CT_u, __CT_u, __CT_u, __CT_p,	/* 0x58..0x5B */
-	__CT_p, __CT_p, __CT_p, __CT_p,	/* 0x5C..0x5F */
+int
+islower( int c )
+{
+    return (c >=  'a' && c <= 'z');
+}
 
-	__CT_p, __CT_l | __CT_x, __CT_l | __CT_x, __CT_l | __CT_x,	/* 0x60..0x63 */
-	__CT_l | __CT_x, __CT_l | __CT_x, __CT_l | __CT_x, __CT_l,	/* 0x64..0x67 */
-	__CT_l, __CT_l, __CT_l, __CT_l,	/* 0x68..0x6B */
-	__CT_l, __CT_l, __CT_l, __CT_l,	/* 0x6C..0x6F */
+int
+isprint( int c )
+{
+    return (c >= ' ' && c <= '~');
+}
 
-	__CT_l, __CT_l, __CT_l, __CT_l,	/* 0x70..0x73 */
-	__CT_l, __CT_l, __CT_l, __CT_l,	/* 0x74..0x77 */
-	__CT_l, __CT_l, __CT_l, __CT_p,	/* 0x78..0x7B */
-	__CT_p, __CT_p, __CT_p, __CT_c	/* 0x7C..0x7F */
-};
+int
+ispunct( int c )
+{
+    return ((c > ' ' && c <= '~') && !isalnum(c));
+}
 
-int toupper(c)
-int c;
+int
+isspace( int c )
 {
-	return (islower(c) ? (c ^ 0x20) : (c));
+    return (c == ' ' || c == '\f' || c == '\n' || c == '\r' ||
+	    c == '\t' || c == '\v');
 }
 
-int tolower(c)
-int c;
+int
+isupper( int c )
 {
-	return (isupper(c) ? (c ^ 0x20) : (c));
+    return (c >=  'A' && c <= 'Z');
 }
+
+int
+isxdigit( int c )
+{
+    return (isxupper(c) || isxlower(c));
+}
+
+int
+isxlower( int c )
+{
+    return (isdigit(c) || (c >= 'a' && c <= 'f'));
+}
+
+int
+isxupper( int c )
+{
+    return (isdigit(c) || (c >= 'A' && c <= 'F'));
+}
+
+int
+toascii( int c )
+{
+    return (c & 0x7f);
+}
+
+int
+tolower( int c )
+{
+    return (isupper(c) ? ( c - 'A' + 'a') : (c));
+}
+
+int
+toupper( int c )
+{
+    return (islower(c) ? (c - 'a' + 'A') : (c));
+}
+