annotate src/std.c @ 28:cced4d365c5e

Removed some debugging lines
author Jonathan Pevarnek <pevarnj@gmail.com>
date Mon, 04 Apr 2011 14:10:28 -0400
parents 1a070e843bf6
children 39fcefab46ed
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 itoa(n, a, 10);
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
44 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
45 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
46 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
47 *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
48 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
49 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
50 *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
51 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
52 } 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
53 while(*a != '.') {
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
54 if(*a == '0') {
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
55 a--;
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
56 continue;
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
57 } 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
58 a++;
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
59 break;
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
60 } else {
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
61 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
62 break;
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
63 }
5
348c59c36703 Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 4
diff changeset
64 }
27
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
65 *a = '\0';
3
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
66 }
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
67 }
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
68 return ret;
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
69 }
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
70
27
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
71 s64 atoi(char *a)
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
72 {
8
25b2b501a5fa Cleaned up the stack operations
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 5
diff changeset
73 short neg = 0;
27
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
74 s64 n = 0;
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
75 if(*a == '-') {
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
76 neg = 1;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
77 a++;
8
25b2b501a5fa Cleaned up the stack operations
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 5
diff changeset
78 } 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
79 while(*a >= '0' && *a <= '9')
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
80 n = n*10 + (*a++ - '0');
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
81 if(neg) n *= -1;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
82 return n;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
83 }
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
84
3
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
85 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
86 {
27
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
87 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
88 double x = 0;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
89 double dec = .1;
27
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
90 short neg = 0;
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
91 if(*a == '-') {
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
92 neg = 1;
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
93 a++;
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
94 }
5
348c59c36703 Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 4
diff changeset
95 while(*a != '.') {
27
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
96 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
97 a++;
348c59c36703 Fixed a LOT of mistakes I made with atof and ftoa...
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 4
diff changeset
98 } 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
99 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
100 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
101 dec *= .1;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
102 a++;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
103 }
27
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
104 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
105 return n + x;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
106 }
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
107
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
108 void sPrint(char *a)
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
109 {
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
110 char *b = a;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
111 while(*b && *++b);
21
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
112 do {
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
113 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
114 a += CON_LEN;
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
115 } while(a < b);
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
116 }
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
117
27
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
118 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
119 {
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
120 while((*dest++ = *src++));
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
121 }
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
122
3
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
123 int strcmp(const char *a, const char *b)
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
124 {
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
125 while(1) {
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
126 if(*a - *b) return *a - *b;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
127 if(*a == '\0') return 0;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
128 a++;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
129 b++;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
130 }
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
131 return -1;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
132 }
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
133
15
525eab23e68a I no longer need to bug Jeff
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 8
diff changeset
134 char* sGet(char *a, unsigned int n)
2
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
135 {
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
136 int length = getline(a, n);
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
137 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
138 return a;
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
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
141 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
142 {
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
143 int i;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
144 for(i = 0; i < last; i++)
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
145 if(!strcmp(array[i], text)) return i;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
146 return last;
b6182f00de82 The work I have so far
Jonathan Pevarnek <pevarnj@gmail.com>
parents:
diff changeset
147 }
3
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
148
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
149 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
150 {
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
151 char *ret = dest;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
152 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
153 while((*dest++ = *src++));
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
154 return ret;
0aa0ad9e1cc3 Converted to work with doubles instead of ints (and other changes)
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 2
diff changeset
155 }
21
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
156
24
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
157 static Header base;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
158 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
159
22
3fb0ec050ff3 malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 21
diff changeset
160 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
161 {
24
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
162 allocp = &base;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
163 allocp->size = 0;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
164 allocp->next = (void*)HEAP_START;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
165 allocp->next->next = &base;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
166 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
167 if((sizeof(blockUnit)%sizeof(u64))) {
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
168 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
169 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
170 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
171 }
22
3fb0ec050ff3 malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 21
diff changeset
172 }
3fb0ec050ff3 malloc has been restructured to now work in a more logical manner
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 21
diff changeset
173
21
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
174 void* malloc(size_t size)
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
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 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
177 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
178 Header *prev = allocp;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
179 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
180 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
181 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
182 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
183 prev->next = cur->next;
21
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
184 } else {
24
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
185 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
186 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
187 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
188 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
189 cur->size = nUnits;
21
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
190 }
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
191 allocp = prev;
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
192 return (void*)(cur + 1);
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
193 } 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
194 return NULL;
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
195 }
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
196 }
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
197 }
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
198
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
199 void free(void *ptr)
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
200 {
24
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
201 Header *toFree = (Header *)ptr - 1;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
202 Header *scan;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
203 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
204 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
205 toFree->next = scan->next;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
206 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
207 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
208 scan->size += toFree->size;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
209 scan->next = toFree->next;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
210 toFree = scan;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
211 }
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
212 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
213 toFree->size += toFree->next->size;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
214 toFree->next = toFree->next->next;
45a80ea314ae The stack now theroretically is a properly working stack!
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 23
diff changeset
215 }
21
7c2adb65ceac Started working on dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 15
diff changeset
216 }
26
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
217
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
218 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
219 {
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
220 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
221 sPrint("Memory address is: ");
27
1a070e843bf6 Fixed bugs with printing out and reading in numbers
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 26
diff changeset
222 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
223 sPrint("\n");
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
224 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
225 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
226 sPrint("\n");
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
227 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
228 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
229 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
230 }
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
231
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
232 void printMem()
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
233 {
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
234 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
235 do {
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
236 printHeader(cur);
c1ad124f2aaf Re-did errors for pop, added some nice testing for dynamic memory
Jonathan Pevarnek <pevarnj@gmail.com>
parents: 24
diff changeset
237 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
238 } 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
239 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
240 }