changeset 124:56447a5e2d2f

Added strtok function, should function just as the C stdlib version does
author Jonathan Pevarnek <pevarnj@gmail.com>
date Thu, 23 Jun 2011 10:51:55 -0400
parents 2a35ea7e123b
children e2396f625db8
files include/string.h src/os/string.c src/prog/echo.c
diffstat 3 files changed, 35 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/include/string.h	Thu Jun 23 10:05:55 2011 -0400
+++ b/include/string.h	Thu Jun 23 10:51:55 2011 -0400
@@ -7,5 +7,6 @@
 void* memcpy(void *dest, const void *src, size_t num);
 void* memset(void *dest, u8 value, size_t num);
 size_t strlen(const char *str);
+char *strtok(char *str, const char *delimiters);
 
 #endif
--- a/src/os/string.c	Thu Jun 23 10:05:55 2011 -0400
+++ b/src/os/string.c	Thu Jun 23 10:51:55 2011 -0400
@@ -1,3 +1,6 @@
+//returns true if c is a character in the c-string characters, false otherwise
+static char isChar(const char c, const char *characters);
+
 int strcmp(const char *a, const char *b)
 {
 	while(1) {
@@ -48,3 +51,26 @@
 	while(*ptr && *++ptr);
 	return ptr - str;
 }
+
+char *strtok(char *str, const char *delimiters)
+{
+	static char* last = NULL;
+	char* ret;
+	if(str) last = str;
+	if(!last) return last; //return null if done
+	while(isChar(*last, delimiters)) last++;
+	ret = last; //first non-deliminator character, start of string
+	while(*last && !isChar(*last, delimiters)) last++;
+	if(!*last) last = NULL;
+	else *last++ = '\0';
+	return ret;
+}
+
+static char isChar(const char c, const char *characters)
+{
+	while(*characters) {
+		if(*characters == c) return *characters;
+		characters++;
+	}
+	return '\0';
+}
--- a/src/prog/echo.c	Thu Jun 23 10:05:55 2011 -0400
+++ b/src/prog/echo.c	Thu Jun 23 10:51:55 2011 -0400
@@ -1,11 +1,17 @@
 #include <prog/svcCalls.h>
 #include <std.h>
+#include <string.h>
 
 void start() //TODO: accept a command line argument of what to print
 {
 	char buffer[128];
+	char *b, *c;
 	sGet(buffer, 128);
-	sPrint(buffer);
-	sPrint("\n");
+	b = buffer;
+	while((c = strtok(b, " ,."))) {
+		if(b) b = NULL;
+		sPrint(c);
+		sPrint("\n");
+	}
 	exit();
 }