Difference between revisions of "Util.c"

From Organic Design wiki
m
m
Line 37: Line 37:
 
}
 
}
  
// Converts passed integer to a binary string if \1 and \2 characters
+
// Replaces missing itoa() and is specialised for binary return a string of \1 and \2 characters
 
void itob(int value, char *buf) {
 
void itob(int value, char *buf) {
 
int i;
 
int i;
 
for (i=1; i<=value>>1; i<<=1) *buf++ = value&i ? '\1' : '\2';
 
for (i=1; i<=value>>1; i<<=1) *buf++ = value&i ? '\1' : '\2';
 
}
 
}

Revision as of 08:47, 22 July 2006

char *logAdd(char *msg) { // prepend with a timestamp // append to logfile printf(msg); printf("\n"); return msg; }

int logErr(char *err) { logAdd(err); // should prepend "ERROR:" return(EXIT_FAILURE); }

int fileRead(char* filename) { }

int fileWrite(char* filename, char* content) { }

// Return an array of strings resulting from splitting passed text at passed character // - the resulting strings are formed from the passed string 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; }

// Replaces missing itoa() and is specialised for binary return a string of \1 and \2 characters void itob(int value, char *buf) { int i; for (i=1; i<=value>>1; i<<=1) *buf++ = value&i ? '\1' : '\2'; }