Mercurial > sos > sos
annotate src/std.c @ 8:25b2b501a5fa
Cleaned up the stack operations
The stack operations now handle error checking correctly (and are no longer a
tangled mass of duplicated code)
atoi now will work correctly if there is a "+" symbol in front of the number
atof will no longer treat something like "aoe235.357" as ".357"
author | Jonathan Pevarnek <pevarnj@gmail.com> |
---|---|
date | Wed, 09 Mar 2011 12:08:48 -0500 |
parents | 348c59c36703 |
children | 525eab23e68a |
rev | line source |
---|---|
3
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
1 #include <std.h> |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
2 |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
3 char* itoa(int n, char *a) |
2 | 4 { |
3
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
5 char *ret = a; |
2 | 6 if(n < 0) { |
7 *a++ = '-'; | |
8 n *= -1; | |
9 } | |
10 char *b = a; | |
11 if(!n) *b++ = '0'; | |
12 for(; n; b++) { | |
13 *b = n%10 + '0'; | |
14 n = n/10; | |
15 } | |
16 *b-- = '\0'; | |
17 for(; a < b; a++, b--) { //reverse | |
18 char temp = *b; | |
19 *b = *a; | |
20 *a = temp; | |
21 } | |
3
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
22 return ret; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
23 } |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
24 |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
25 char* ftoa(double x, char *a, unsigned int prec) |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
26 { |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
27 char *ret = a; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
28 int n = (int) x; //integer part |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
29 double d = x - (double) n; //fractional part; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
30 itoa(n, a); |
5
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
31 if(prec) { //only do the decimal part if decimal parts were asked for |
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
32 while(*a && *++a); //get to the null character from itoa |
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
33 int i; //counter variable for the for loop |
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
34 *a++ = '.'; //put the decimal in place |
3
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
35 for(i = 0; i < prec; i++) { |
5
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
36 d = d*10; //the integer part is the decimal digit |
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
37 *a++ = ((int) d) + '0'; //add the integer part of d to the string |
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
38 d -= (int) d; //chop off the integer part |
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
39 } a--; //move back to the last digit |
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
40 while(*a != '.') { |
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
41 if(*a == '0') { |
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
42 a--; |
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
43 continue; |
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
44 } else if(*a > '0' && *a <= '9') { |
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
45 a++; |
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
46 break; |
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
47 } |
3
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
48 } |
5
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
49 *a = '\0'; |
3
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
50 } |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
51 return ret; |
2 | 52 } |
53 | |
54 int atoi(char *a) | |
55 { | |
8
25b2b501a5fa
Cleaned up the stack operations
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
5
diff
changeset
|
56 short neg = 0; |
2 | 57 int n = 0; |
58 if(*a == '-') { | |
59 neg = 1; | |
60 a++; | |
8
25b2b501a5fa
Cleaned up the stack operations
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
5
diff
changeset
|
61 } else if(*a == '+') a++; |
3
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
62 while(*a >= '0' && *a <= '9') |
2 | 63 n = n*10 + (*a++ - '0'); |
64 if(neg) n *= -1; | |
65 return n; | |
66 } | |
67 | |
3
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
68 double atof(char *a) |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
69 { |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
70 int n = atoi(a); |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
71 double x = 0; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
72 double dec = .1; |
5
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
73 while(*a != '.') { |
8
25b2b501a5fa
Cleaned up the stack operations
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
5
diff
changeset
|
74 if(!(*a >= '0' && *a <= '9')) return n; |
5
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
75 a++; |
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
76 } a++; //a will be immediately after the decimal point |
348c59c36703
Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
4
diff
changeset
|
77 while(*a >= '0' && *a <= '9') { //goes through the decimal part |
3
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
78 x += (*a - '0')*dec; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
79 dec *= .1; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
80 a++; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
81 } |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
82 return n + x; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
83 } |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
84 |
2 | 85 void sPrint(char *a) |
86 { | |
87 char *b = a; | |
88 while(*b && *++b); | |
89 putline(a, b - a); | |
90 } | |
91 | |
3
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
92 int strcmp(const char *a, const char *b) |
2 | 93 { |
94 while(1) { | |
95 if(*a - *b) return *a - *b; | |
96 if(*a == '\0') return 0; | |
97 a++; | |
98 b++; | |
99 } | |
100 return -1; | |
101 } | |
102 | |
4
90cd3d9a6ca3
Moved function array declaration, modified the ftoa function so it does not return trailing 0s
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
3
diff
changeset
|
103 char* sGet(char *a, unsigned int n) //TODO bug Jeff about getline |
2 | 104 { |
105 int length = getline(a, n); | |
106 a[(length < n)?length:n - 1] = '\0'; | |
4
90cd3d9a6ca3
Moved function array declaration, modified the ftoa function so it does not return trailing 0s
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
3
diff
changeset
|
107 return a; |
2 | 108 } |
109 | |
110 int arrayLookup(char *text, const char array[][10], int last) | |
111 { | |
112 int i; | |
113 for(i = 0; i < last; i++) | |
114 if(!strcmp(array[i], text)) return i; | |
115 return last; | |
116 } | |
3
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
117 |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
118 char* append(char *dest, char *src) |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
119 { |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
120 char *ret = dest; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
121 while(*dest&& *++dest); //get to null in first string |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
122 while((*dest++ = *src++)); |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
123 return ret; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
124 } |