Mercurial > sos > sos
annotate 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 |
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); |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
31 if(prec) { |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
32 while(*a && *++a); |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
33 int i; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
34 *a++ = '.'; |
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++) { |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
36 d = d*10; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
37 *a++ = ((int) d) + '0'; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
38 d -= (int) d; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
39 } |
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
|
40 a--; //move back to the last digit |
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
|
41 // while(*a == '0') a--; //TODO figure out why this "works" even with the other version of init.c |
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
|
42 while(*a == '0' || *a == '.') a--; //get to the first non-zero decimal digit |
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
|
43 *(a + 1) = '\0'; |
3
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
44 } |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
45 return ret; |
2 | 46 } |
47 | |
48 int atoi(char *a) | |
49 { | |
50 int neg = 0; | |
51 int n = 0; | |
52 if(*a == '-') { | |
53 neg = 1; | |
54 a++; | |
55 } | |
3
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
56 while(*a >= '0' && *a <= '9') |
2 | 57 n = n*10 + (*a++ - '0'); |
58 if(neg) n *= -1; | |
59 return n; | |
60 } | |
61 | |
3
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
62 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
|
63 { |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
64 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
|
65 double x = 0; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
66 double dec = .1; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
67 while(*a++ != '.'); |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
68 while(*a != '\0') { |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
69 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
|
70 dec *= .1; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
71 a++; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
72 } |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
73 return n + x; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
74 } |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
75 |
2 | 76 void sPrint(char *a) |
77 { | |
78 char *b = a; | |
79 while(*b && *++b); | |
80 putline(a, b - a); | |
81 } | |
82 | |
3
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
83 int strcmp(const char *a, const char *b) |
2 | 84 { |
85 while(1) { | |
86 if(*a - *b) return *a - *b; | |
87 if(*a == '\0') return 0; | |
88 a++; | |
89 b++; | |
90 } | |
91 return -1; | |
92 } | |
93 | |
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
|
94 char* sGet(char *a, unsigned int n) //TODO bug Jeff about getline |
2 | 95 { |
96 int length = getline(a, n); | |
97 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
|
98 return a; |
2 | 99 } |
100 | |
101 int arrayLookup(char *text, const char array[][10], int last) | |
102 { | |
103 int i; | |
104 for(i = 0; i < last; i++) | |
105 if(!strcmp(array[i], text)) return i; | |
106 return last; | |
107 } | |
3
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
108 |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
109 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
|
110 { |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
111 char *ret = dest; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
112 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
|
113 while((*dest++ = *src++)); |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
114 return ret; |
0aa0ad9e1cc3
Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
2
diff
changeset
|
115 } |