tape-kernel 1.0
a modular modern independent kernel
Loading...
Searching...
No Matches
utils.c
Go to the documentation of this file.
1//utils.c - various utilitys
2#include "utils.h"
3#include "types.h"
4
5int abs(int x) { //absolute check
6 if (x < 0) return -x; //if under 0
7 return x; //if not
8}
9int strlen(const char* s) {
10 int len = 0; //int lenth
11 while (s[len]) len++; //calc length
12 return len;
13}
14
15int strcmp(const char* a, const char* b) { //string comparison
16 while (*a && *b && *a == *b) { a++; b++; } //complex
17 //while *a and *b and *a == *b aka while a and b are the same and exist increment them and return a - b
18 return *a - *b; //return
19}
20
21void strcpy(char* dest, const char* src) { //dest and char
22 while (*src) *dest++ = *src++; //while src exists dest++ is the same as src++
23 *dest = 0;
24}
25
26void strcat(char* dest, const char* src) { //string concatonate
27 while (*dest) dest++; //while *dest exists increment dest, not the ptr itself
28 while (*src) *dest++ = *src++; //while src exist dest++ is src++
29 *dest = 0; //set dest 0
30}
31
32/**
33 * @brief itoa converts an integer to a decimal string
34 *
35 * itoa is a function that converts a signed integer into a
36 * null-terminated decimal string representation
37 * @param num, the integer to convert
38 * @param str, the output buffer for the resulting string
39 *
40 * using itoa is done with
41 * @code{.c}
42 * #include "../lib/utils.h" //or just utils.h
43 * char buff[12];
44 * itoa(-42, buff);
45 * @endcode
46 *
47 * @see atoi(), prtd()
48 */
49void itoa(int num, char* str) {
50 char digits[12];
51 int i = 0;
52 int is_neg = 0;
53
54 //handle 0
55 if (num == 0) {
56 str[0] = '0';
57 str[1] = '\0';
58 return;
59 }
60
61 //handle negative
62 if (num < 0) {
63 is_neg = 1;
64 //special case for -2,147,483,648
65 if (num == -2147483648) {
66 str[0] = '-';
67 str[1] = '2';
68 str[2] = '1';
69 str[3] = '4';
70 str[4] = '7';
71 str[5] = '4';
72 str[6] = '8';
73 str[7] = '3';
74 str[8] = '6';
75 str[9] = '4';
76 str[10] = '8';
77 str[11] = '\0';
78 return;
79 }
80 num = -num;
81 }
82
83 //build digits in reverse
84 while (num > 0) {
85 digits[i++] = (char)('0' + (num % 10));
86 num /= 10;
87 }
88
89 //add negative sign
90 int idx = 0;
91 if (is_neg) {
92 str[idx++] = '-';
93 }
94
95 //reverse digits
96 while (i > 0) {
97 str[idx++] = digits[--i];
98 }
99
100 str[idx] = '\0';
101}
102
103/**
104 * @brief atoi converts a decimal string to an integer
105 *
106 * atoi is a function that parses a string of digit characters
107 * and returns the corresponding integer value
108 * @param str, the null-terminated decimal string to parse
109 *
110 * using atoi is done with
111 * @code{.c}
112 * #include "../lib/utils.h" //or just utils.h
113 * int value = atoi("42");
114 * @endcode
115 *
116 * @see itoa()
117 */
118int atoi(const char *str) {
119 int num = 0;
120 while (*str >= '0' && *str <= '9') { //while string over zero and under 9 (1-8)
121 num = num * 10 + (*str - '0'); //then do num is num * 10 + string - 0 and increment string
122 str++;
123 }
124 return num; //return num
125}
126
127static uint32_t rng_state = 123456789;
128
129/**
130 * @brief srand seeds the random number generator
131 *
132 * srand is a function that initializes the xor-shift rng state
133 * with a given seed value
134 * @param seed, the initial seed value for the rng
135 *
136 * using srand is done with
137 * @code{.c}
138 * #include "../lib/utils.h" //or just utils.h
139 * srand(12345);
140 * @endcode
141 *
142 * @see rand()
143 */
144void srand(uint32_t seed) {
145 rng_state = seed;
146}
147
148/**
149 * @brief rand returns a pseudo-random 32 bit integer
150 *
151 * rand is a function that generates a random number using a
152 * xor-shift algorithm on the internal rng state
153 *
154 * using rand is done with
155 * @code{.c}
156 * #include "../lib/utils.h" //or just utils.h
157 * uint32_t rnd = rand();
158 * @endcode
159 *
160 * @see srand()
161 */
163 rng_state ^= rng_state << 13; //shift state 3 times for randomness
164 rng_state ^= rng_state >> 17;
165 rng_state ^= rng_state << 5;
166 return rng_state;
167}
168
169/**
170 * @brief pargs parses a command line into an argument array
171 *
172 * pargs is a function that splits a string into an argv-style
173 * array of null-terminated arguments, supporting quoted strings
174 * and skipping leading '/' characters
175 * @param line, the input line to parse (modified in-place)
176 * @param args, the output array of string pointers
177 *
178 * using pargs is done with
179 * @code{.c}
180 * #include "../lib/utils.h" //or just utils.h
181 * char input[100];
182 * char *args[MAX_ARGS];
183 * rdln(input, 100);
184 * int argc = pargs(input, args);
185 * @endcode
186 *
187 * @see shell(), MAX_ARGS
188 */
189int pargs(char *line, char *args[]) {
190 int argc = 0;
191 int i = 0;
192 int in_quote = 0;
193
194 //skip leading '/'
195 if (line[0] == '/') i = 1;
196
197 while (line[i] != '\0' && argc < MAX_ARGS) {
198 //skip spaces (but not inside quotes)
199 while (line[i] == ' ' && !in_quote) i++;
200 if (line[i] == '\0') break;
201
202 //start of argument
203 args[argc] = &line[i];
204 argc++;
205
206 //check if arg starts with quote
207 if (line[i] == '"') {
208 in_quote = 1;
209 args[argc-1] = &line[i+1]; //skip the quote
210 i++; //move past the quote
211 }
212
213 //find end of argument
214 while (line[i] != '\0') {
215 if (in_quote) {
216 if (line[i] == '"') {
217 line[i] = '\0'; //terminate at closing quote
218 i++; //move past the quote
219 in_quote = 0;
220 break;
221 }
222 } else {
223 if (line[i] == ' ') { //if its a space
224 line[i] = '\0'; //term
225 i++; //inc and break
226 break;
227 }
228 }
229 i++; //inc outside so it continues
230 }
231 }
232
233 return argc; //return
234}
unsigned int uint32_t
Definition types.h:30
int strcmp(const char *a, const char *b)
Definition utils.c:15
int abs(int x)
Definition utils.c:5
void strcpy(char *dest, const char *src)
Definition utils.c:21
void itoa(int num, char *str)
itoa converts an integer to a decimal string
Definition utils.c:49
int strlen(const char *s)
Definition utils.c:9
int pargs(char *line, char *args[])
pargs parses a command line into an argument array
Definition utils.c:189
static uint32_t rng_state
Definition utils.c:127
void strcat(char *dest, const char *src)
Definition utils.c:26
uint32_t rand(void)
rand returns a pseudo-random 32 bit integer
Definition utils.c:162
int atoi(const char *str)
atoi converts a decimal string to an integer
Definition utils.c:118
void srand(uint32_t seed)
srand seeds the random number generator
Definition utils.c:144
#define MAX_ARGS
Definition utils.h:5