annotate src/std.c @ 27:1a070e843bf6

Fixed bugs with printing out and reading in numbers If infinity or NAN is passed to ftoa, it will set the character buffer to read either "INF" or "NAN" Negative decimals are now correctly read in by atof. abs() is now a part of the standard library. The math_ prefix has been dropped from all functions but the trivial binary operations. The strcpy function has been added.
author Jonathan Pevarnek <pevarnj@gmail.com>
date Tue, 29 Mar 2011 22:05:03 -0400
parents c1ad124f2aaf
children cced4d365c5e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
27
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
3 double abs(double num)
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
4 {
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
5 if(num < 0) return num*-1;
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
6 else return num;
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
7 }
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
8
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
9 char* itoa(s64 n, char *a, unsigned short base)
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
10 {
3
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
11 char *ret = a;
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
12 if(n < 0) {
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
13 *a++ = '-';
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
14 n *= -1;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
15 }
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
16 char *b = a;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
17 if(!n) *b++ = '0';
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
18 for(; n; b++) {
26
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
19 int temp = n%base;
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
20 if(temp < 10) *b = '0' + temp;
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
21 else *b = 'a' + temp - 10;
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
22 n = n/base;
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
23 }
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
24 *b-- = '\0';
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
25 for(; a < b; a++, b--) { //reverse
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
26 char temp = *b;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
27 *b = *a;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
28 *a = temp;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
29 }
3
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
30 return ret;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
31 }
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
32
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
33 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
34 {
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
35 char *ret = a;
27
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
36 if(x == INF || x == -INF) {
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
37 strcpy(a, "INF");
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
38 } else if(x != x) { //NAN != NAN
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
39 strcpy(a, "NAN");
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
40 } else {
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
41 s64 n = (s64) x; //integer part
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
42 double d = abs(x - (double) n); //fractional part;
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
43 if(d != 0) sPrint("WORKING\n");
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
44 itoa(n, a, 10);
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
45 if(prec) { //only do the decimal part if decimal parts were asked for
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
46 while(*a && *++a); //get to the null character from itoa
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
47 int i; //counter variable for the for loop
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
48 *a++ = '.'; //put the decimal in place
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
49 for(i = 0; i < prec; i++) {
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
50 d *= 10; //the integer part is the decimal digit
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
51 char aoeu[43];
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
52 sPrint(itoa((int) d, aoeu, 10));
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
53 sPrint("\n");
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
54 *a++ = ((int) d) + '0'; //add the integer part of d to the string
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
55 d -= (int) d; //chop off the integer part
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
56 } a--; //move back to the last digit
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
57 while(*a != '.') {
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
58 if(*a == '0') {
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
59 a--;
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
60 continue;
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
61 } else if(*a > '0' && *a <= '9') {
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
62 a++;
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
63 break;
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
64 } else {
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
65 sPrint("ERROR: SOMETHING IS VERY WRONG\n");
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
66 break;
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
67 }
5
348c59c36703 Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 4
diff changeset
68 }
27
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
69 *a = '\0';
3
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
70 }
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
71 }
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
72 return ret;
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
73 }
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
74
27
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
75 s64 atoi(char *a)
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
76 {
8
25b2b501a5fa Cleaned up the stack operations
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 5
diff changeset
77 short neg = 0;
27
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
78 s64 n = 0;
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
79 if(*a == '-') {
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
80 neg = 1;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
81 a++;
8
25b2b501a5fa Cleaned up the stack operations
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 5
diff changeset
82 } 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
83 while(*a >= '0' && *a <= '9')
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
84 n = n*10 + (*a++ - '0');
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
85 if(neg) n *= -1;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
86 return n;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
87 }
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
88
3
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
89 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
90 {
27
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
91 s64 n = atoi(a);
3
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
92 double x = 0;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
93 double dec = .1;
27
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
94 short neg = 0;
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
95 if(*a == '-') {
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
96 neg = 1;
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
97 a++;
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
98 }
5
348c59c36703 Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 4
diff changeset
99 while(*a != '.') {
27
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
100 if(!(*a >= '0' && *a <= '9') && *a != '-') return n;
5
348c59c36703 Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 4
diff changeset
101 a++;
348c59c36703 Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 4
diff changeset
102 } 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
103 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
104 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
105 dec *= .1;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
106 a++;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
107 }
27
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
108 if(neg) x*=-1;
3
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
109 return n + x;
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
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
112 void sPrint(char *a)
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
113 {
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
114 char *b = a;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
115 while(*b && *++b);
21
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
116 do {
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
117 putline(a, (b - a > CON_LEN)?CON_LEN:(b - a));
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
118 a += CON_LEN;
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
119 } while(a < b);
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
120 }
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
121
27
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
122 void strcpy(char *dest, const char *src)
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
123 {
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
124 while((*dest++ = *src++));
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
125 }
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
126
3
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
127 int strcmp(const char *a, const char *b)
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
128 {
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
129 while(1) {
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
130 if(*a - *b) return *a - *b;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
131 if(*a == '\0') return 0;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
132 a++;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
133 b++;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
134 }
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
135 return -1;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
136 }
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
137
15
525eab23e68a I no longer need to bug Jeff
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 8
diff changeset
138 char* sGet(char *a, unsigned int n)
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
139 {
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
140 int length = getline(a, n);
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
141 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
142 return a;
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
143 }
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
144
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
145 int arrayLookup(char *text, const char array[][10], int last)
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
146 {
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
147 int i;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
148 for(i = 0; i < last; i++)
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
149 if(!strcmp(array[i], text)) return i;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
150 return last;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
151 }
3
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
152
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
153 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
154 {
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
155 char *ret = dest;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
156 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
157 while((*dest++ = *src++));
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
158 return ret;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
159 }
21
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
160
24
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
161 static Header base;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
162 static Header *allocp = NULL; //the location of the last known free block
21
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
163
22
3fb0ec050ff3 malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 21
diff changeset
164 void malloc_init(size_t memSize)
3fb0ec050ff3 malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 21
diff changeset
165 {
24
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
166 allocp = &base;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
167 allocp->size = 0;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
168 allocp->next = (void*)HEAP_START;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
169 allocp->next->next = &base;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
170 allocp->next->size = memSize/sizeof(blockUnit);
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
171 if((sizeof(blockUnit)%sizeof(u64))) {
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
172 sPrint("WARNING: MEMORY NOT 8-BYTE ALIGNED\n");
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
173 char foo[40];
26
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
174 sPrint(append("MEMORY BLOCK SIZE IS: ", append(itoa(sizeof(Header), foo, 10), "\n")));
24
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
175 }
22
3fb0ec050ff3 malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 21
diff changeset
176 }
3fb0ec050ff3 malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 21
diff changeset
177
21
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
178 void* malloc(size_t size)
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
179 {
22
3fb0ec050ff3 malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 21
diff changeset
180 if(allocp == NULL) return NULL;
24
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
181 size_t nUnits = ((size + sizeof(Header)) + sizeof(blockUnit) - 1)/sizeof(blockUnit);
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
182 Header *prev = allocp;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
183 Header *cur, *temp;
22
3fb0ec050ff3 malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 21
diff changeset
184 for(cur = prev->next;; prev = cur, cur = cur->next) {
3fb0ec050ff3 malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 21
diff changeset
185 if(cur->size >= nUnits) {
3fb0ec050ff3 malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 21
diff changeset
186 if(cur->size == nUnits) {
3fb0ec050ff3 malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 21
diff changeset
187 prev->next = cur->next;
21
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
188 } else {
24
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
189 temp = cur + nUnits; //This requires sizeof(blockUnit) == sizeof(Header). TODO fix
22
3fb0ec050ff3 malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 21
diff changeset
190 temp->size = cur->size - nUnits;
3fb0ec050ff3 malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 21
diff changeset
191 temp->next = cur->next;
3fb0ec050ff3 malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 21
diff changeset
192 prev->next = temp;
3fb0ec050ff3 malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 21
diff changeset
193 cur->size = nUnits;
21
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
194 }
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
195 allocp = prev;
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
196 return (void*)(cur + 1);
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
197 } else if(cur == allocp) { //We went back to the start...
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
198 return NULL;
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
199 }
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
200 }
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
201 }
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
202
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
203 void free(void *ptr)
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
204 {
24
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
205 Header *toFree = (Header *)ptr - 1;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
206 Header *scan;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
207 for(scan = allocp; !(toFree > scan && toFree < scan->next); scan = scan->next)
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
208 if(scan->next < scan && (toFree > scan || toFree < scan->next)) break;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
209 toFree->next = scan->next;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
210 scan->next = toFree;
26
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
211 if(scan + scan->size == toFree) {
24
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
212 scan->size += toFree->size;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
213 scan->next = toFree->next;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
214 toFree = scan;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
215 }
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
216 if(toFree + toFree->size == toFree->next) {
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
217 toFree->size += toFree->next->size;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
218 toFree->next = toFree->next->next;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
219 }
21
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
220 }
26
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
221
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
222 void printHeader(Header* head)
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
223 {
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
224 char buffer[20];
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
225 sPrint("Memory address is: ");
27
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
226 sPrint(itoa((intptr_t)head, buffer, 16));
26
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
227 sPrint("\n");
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
228 sPrint("The size of the block is: ");
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
229 sPrint(itoa(head->size, buffer, 10));
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
230 sPrint("\n");
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
231 sPrint("Next memory address is: ");
27
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
232 sPrint(itoa((intptr_t)head->next, buffer, 16));
26
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
233 sPrint("\n\n");
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
234 }
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
235
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
236 void printMem()
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
237 {
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
238 Header *cur = allocp;
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
239 do {
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
240 printHeader(cur);
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
241 cur = cur->next;
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
242 } while(cur != allocp);
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
243 sPrint("PRINTOUT DONE\n\n");
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
244 }