Difference between revisions of "Util.c"
(split() tested and working fine) |
(no _itoa either! looks like BYO for this one!) |
||
| Line 32: | Line 32: | ||
return list; | return list; | ||
} | } | ||
| + | |||
| + | #if defined(__linux__) | ||
| + | char *itoa(int value, char *str, int radix) { | ||
| + | int rem = 0, pos = 0; | ||
| + | char ch = '!'; | ||
| + | do { | ||
| + | rem = value%radix; | ||
| + | value /= radix; | ||
| + | if (radix == 16) { | ||
| + | if ( rem >= 10 && rem <= 15) { | ||
| + | switch (rem) { | ||
| + | case 10: ch = 'a'; break; | ||
| + | case 11: ch = 'b'; break; | ||
| + | case 12: ch = 'c'; break; | ||
| + | case 13: ch = 'd'; break; | ||
| + | case 14: ch = 'e'; break; | ||
| + | case 15: ch = 'f'; break; | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | if (ch == '!') str[pos++] = (char)(rem+0x30); else str[pos++] = ch; | ||
| + | } while (value); | ||
| + | str[pos] = '\0'; | ||
| + | return strrev(str); | ||
| + | } | ||
| + | #endif | ||
Revision as of 09:11, 21 July 2006
int logAdd(char* msg) { // prepend with a timestamp // append to logfile }
int logErr(char* err) { logAdd(err); // should prepend "ERROR:" exit(1); }
int fileRead(char* filename) { }
int fileWrite(char* filename, char* content) { }
// Return an array of strings resulting from splitting passed text at passed character char **split(char c,char *text) { int len = strlen(text), items = 0, size = 10; char **list = malloc(size); char *i = malloc(len+1), *j = i, *k = i, *item = i; while(*j++ = *text++); while(i <= k+len) { if (*i == c) *i = '\0'; if ((*i++ == '\0')&&strlen(item)) { if (items>size-2) realloc(list,size+=10); list[items++] = item; list[items] = NULL; item = i; } } return list; }
- if defined(__linux__)
char *itoa(int value, char *str, int radix) { int rem = 0, pos = 0; char ch = '!'; do { rem = value%radix; value /= radix; if (radix == 16) { if ( rem >= 10 && rem <= 15) { switch (rem) { case 10: ch = 'a'; break; case 11: ch = 'b'; break; case 12: ch = 'c'; break; case 13: ch = 'd'; break; case 14: ch = 'e'; break; case 15: ch = 'f'; break; } } } if (ch == '!') str[pos++] = (char)(rem+0x30); else str[pos++] = ch; } while (value); str[pos] = '\0'; return strrev(str); }
- endif



