Mercurial > sos > sos
changeset 94:191e99dffd6c
Moved some std.h functions to string.h, worked on a simple version of sprintf
sprintf is a very simple version of the C Standard Library version. It should
work for doubles, ints, and strings.
I also added the strlen function.
author | Jonathan Pevarnek <pevarnj@gmail.com> |
---|---|
date | Sat, 14 May 2011 12:54:29 -0400 |
parents | effa07f3c157 |
children | a480d02a10c8 |
files | Makefile include/std.h include/stdio.h include/string.h src/dynamic.c src/operations.c src/sarpn.c src/std.c src/stdio.c src/string.c |
diffstat | 10 files changed, 118 insertions(+), 52 deletions(-) [+] |
line wrap: on
line diff
--- a/Makefile Thu May 12 13:29:23 2011 -0400 +++ b/Makefile Sat May 14 12:54:29 2011 -0400 @@ -13,9 +13,9 @@ BINS=sarpn dynamic testFS -sarpn_OBJS=src/sarpn.o src/std.o src/stack.o src/operations.o src/math.o arch/arch.a -dynamic_OBJS=src/dynamic.o src/std.o src/stack.o arch/arch.a -testFS_OBJS=src/testFS.o src/std.o src/fs.o arch/arch.a +sarpn_OBJS=src/sarpn.o src/std.o src/string.o src/stack.o src/operations.o src/math.o arch/arch.a +dynamic_OBJS=src/dynamic.o src/std.o src/string.o src/stack.o arch/arch.a +testFS_OBJS=src/testFS.o src/std.o src/string.o src/fs.o arch/arch.a ARCH_OBJS=arch/io.o arch/cons.o arch/ebcdic.o arch/fba.o arch/ioint.o \ arch/svc.o arch/svcint.o
--- a/include/std.h Thu May 12 13:29:23 2011 -0400 +++ b/include/std.h Sat May 14 12:54:29 2011 -0400 @@ -26,20 +26,14 @@ void sPrint(char *a); char* sGet(char *a, unsigned int n); -void strcpy(char *dest, const char *src); -int strcmp(const char *a, const char *b); - -char* append(char *dest, char *src); - -void* memcpy(void *destination, const void *source, size_t num); //MALLOC CODE -struct header{ - struct header *next; +struct HEADER{ + struct HEADER *next; size_t size; }; -typedef struct header Header; +typedef struct HEADER Header; typedef Header blockUnit;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/stdio.h Sat May 14 12:54:29 2011 -0400 @@ -0,0 +1,6 @@ +#ifndef __STDIO_H +#define __STDIO_H + +int sprintf(char *str, const char *fmt, ...); + +#endif //__STDIO_H
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/string.h Sat May 14 12:54:29 2011 -0400 @@ -0,0 +1,10 @@ +#ifndef __STD_H +#define __STD_H + +void strcpy(char *dest, const char *src); +int strcmp(const char *a, const char *b); +char* strcat(char *dest, const char *src); +void* memcpy(void *destination, const void *source, size_t num); +size_t strlen(const char *str); + +#endif
--- a/src/dynamic.c Thu May 12 13:29:23 2011 -0400 +++ b/src/dynamic.c Sat May 14 12:54:29 2011 -0400 @@ -3,6 +3,7 @@ */ #include <std.h> +#include <string.h> #include <operations.h> #include <stack.h> @@ -40,7 +41,7 @@ // buffer[n - 1]='\0'; // sPrint(buffer), sPrint("\n"); // dumpBuffer(buffer, n); - sPrint(append(itoa((unsigned long)buffer, input, 16), "\n")); + sPrint(strcat(itoa((unsigned long)buffer, input, 16), "\n")); free(buffer); }
--- a/src/operations.c Thu May 12 13:29:23 2011 -0400 +++ b/src/operations.c Sat May 14 12:54:29 2011 -0400 @@ -1,5 +1,6 @@ #include <operations.h> #include <std.h> +#include <string.h> #include <stack.h> #include <math.h> @@ -41,7 +42,7 @@ char output[30]; eltType val; if(pop(stack, &val)) error_unk(); - else sPrint(append(ftoa(val, output, 10), "\n")); + else sPrint(strcat(ftoa(val, output, 10), "\n")); } else error_small(); }
--- a/src/sarpn.c Thu May 12 13:29:23 2011 -0400 +++ b/src/sarpn.c Sat May 14 12:54:29 2011 -0400 @@ -3,6 +3,7 @@ */ #include <std.h> +#include <string.h> #include <operations.h> #include <stack.h>
--- a/src/std.c Thu May 12 13:29:23 2011 -0400 +++ b/src/std.c Sat May 14 12:54:29 2011 -0400 @@ -1,4 +1,5 @@ #include <std.h> +#include <string.h> void init_all(u64 __memsize) { @@ -122,22 +123,6 @@ } while(a < b); } -void strcpy(char *dest, const char *src) -{ - while((*dest++ = *src++)); -} - -int strcmp(const char *a, const char *b) -{ - while(1) { - if(*a - *b) return *a - *b; - if(*a == '\0') return 0; - a++; - b++; - } - return -1; -} - char* sGet(char *a, unsigned int n) { int length = getline(a, n); @@ -145,26 +130,6 @@ return a; } -char* append(char *dest, char *src) -{ - char *ret = dest; - while(*dest&& *++dest); //get to null in first string - while((*dest++ = *src++)); - return ret; -} - -void* memcpy(void *destination, const void *source, size_t num) -{ - int i; - const u8 *from = source; - u8 *to = destination; - for(i = 0; i < num; i++) - to[i] = from[i]; - return destination; -} - - - //DYNAMIC MEMORY static Header base; @@ -179,8 +144,6 @@ allocp->next->size = memSize/sizeof(blockUnit); if((sizeof(blockUnit)%sizeof(u64))) { sPrint("WARNING: MEMORY NOT 8-BYTE ALIGNED\n"); - char foo[40]; - sPrint(append("MEMORY BLOCK SIZE IS: ", append(itoa(sizeof(Header), foo, 10), "\n"))); } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/stdio.c Sat May 14 12:54:29 2011 -0400 @@ -0,0 +1,50 @@ +#include <stdio.h> +#include <std.h> +#include <string.h> +#include <stdarg.h> + +int sprintf(char *str, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + *str = '\0'; + while(*fmt) { + if(*fmt != '%') { + *str++ = *fmt++; + } else { + switch(*(fmt + 1)) { + case '%': + *str++ = '%'; + break; + case 'i': + { + char buffer[32]; + long long num = va_arg(ap, long long); + itoa(num, buffer, 10); + memcpy(str, buffer, strlen(buffer)); + str += strlen(buffer); + } + break; + case 'f': + { + char buffer[32]; + double num = va_arg(ap, double); + ftoa(num, buffer, 10); + memcpy(str, buffer, strlen(buffer)); + str += strlen(buffer); + } + break; + case 's': + { + char *text = va_arg(ap, char*); + memcpy(str, text, strlen(text)); + str += strlen(text); + } + break; + } + fmt += 2; + } + } + *str = '\0'; + return 0; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/string.c Sat May 14 12:54:29 2011 -0400 @@ -0,0 +1,40 @@ +int strcmp(const char *a, const char *b) +{ + while(1) { + if(*a - *b) return *a - *b; + if(*a == '\0') return 0; + a++; + b++; + } + return -1; +} + +void strcpy(char *dest, const char *src) +{ + while((*dest++ = *src++)); +} + +char* strcat(char *dest, const char *src) +{ + char *ret = dest; + while(*dest&& *++dest); //get to null in first string + while((*dest++ = *src++)); + return ret; +} + +void* memcpy(void *destination, const void *source, size_t num) +{ + int i; + const u8 *from = source; + u8 *to = destination; + for(i = 0; i < num; i++) + to[i] = from[i]; + return destination; +} + +size_t strlen(const char *str) +{ + char *ptr = str; + while(*ptr && *++ptr); + return ptr - str; +}