Mercurial > sos > sos
view src/std.c @ 4:90cd3d9a6ca3
Moved function array declaration, modified the ftoa function so it does not return trailing 0s
Also added a return value to sGet though that seems to break some things...
The errors for this were very odd, see TODO notes in std.c and init.c
author | Jonathan Pevarnek <pevarnj@gmail.com> |
---|---|
date | Wed, 02 Mar 2011 00:37:32 -0500 |
parents | 0aa0ad9e1cc3 |
children | 348c59c36703 |
line wrap: on
line source
#include <std.h> char* itoa(int n, char *a) { char *ret = a; if(n < 0) { *a++ = '-'; n *= -1; } char *b = a; if(!n) *b++ = '0'; for(; n; b++) { *b = n%10 + '0'; n = n/10; } *b-- = '\0'; for(; a < b; a++, b--) { //reverse char temp = *b; *b = *a; *a = temp; } return ret; } char* ftoa(double x, char *a, unsigned int prec) { char *ret = a; int n = (int) x; //integer part double d = x - (double) n; //fractional part; itoa(n, a); if(prec) { while(*a && *++a); int i; *a++ = '.'; for(i = 0; i < prec; i++) { d = d*10; *a++ = ((int) d) + '0'; d -= (int) d; } a--; //move back to the last digit // while(*a == '0') a--; //TODO figure out why this "works" even with the other version of init.c while(*a == '0' || *a == '.') a--; //get to the first non-zero decimal digit *(a + 1) = '\0'; } return ret; } int atoi(char *a) { int neg = 0; int n = 0; if(*a == '-') { neg = 1; a++; } while(*a >= '0' && *a <= '9') n = n*10 + (*a++ - '0'); if(neg) n *= -1; return n; } double atof(char *a) { int n = atoi(a); double x = 0; double dec = .1; while(*a++ != '.'); while(*a != '\0') { x += (*a - '0')*dec; dec *= .1; a++; } return n + x; } void sPrint(char *a) { char *b = a; while(*b && *++b); putline(a, b - a); } 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) //TODO bug Jeff about getline { int length = getline(a, n); a[(length < n)?length:n - 1] = '\0'; return a; } int arrayLookup(char *text, const char array[][10], int last) { int i; for(i = 0; i < last; i++) if(!strcmp(array[i], text)) return i; return last; } char* append(char *dest, char *src) { char *ret = dest; while(*dest&& *++dest); //get to null in first string while((*dest++ = *src++)); return ret; }