Difference between revisions of "Util.c"
(no need for before/after for link-list) |
(use proper errno) |
||
| Line 18: | Line 18: | ||
int logErr(char *msg) { | int logErr(char *msg) { | ||
logAdd(msg); // should prepend "ERROR:" | logAdd(msg); // should prepend "ERROR:" | ||
| − | return( | + | return(errno = EXIT_FAILURE); |
} | } | ||
// Same as logErr but allows error message to contain an arg | // Same as logErr but allows error message to contain an arg | ||
| − | int | + | int logErrMsg(char *fmt, char *msg2) { |
char *msg = malloc(100); | char *msg = malloc(100); | ||
sprintf(msg,fmt,msg2); | sprintf(msg,fmt,msg2); | ||
logErr(msg); | logErr(msg); | ||
free(msg); | free(msg); | ||
| − | return( | + | return(errno = EXIT_FAILURE); |
| + | } | ||
| + | |||
| + | // Same as logErr but allows error message to contain an arg | ||
| + | int logErrNum(char *fmt, int num) { | ||
| + | char *msg = malloc(100); | ||
| + | sprintf(msg,fmt,num); | ||
| + | logErr(msg); | ||
| + | free(msg); | ||
| + | return(errno = EXIT_FAILURE); | ||
} | } | ||
Revision as of 01:52, 27 July 2006
int fileRead(char* filename) { }
int fileWrite(char* filename, char* content) { }
char *logAdd(char *msg) { // prepend with a timestamp // append to logfile printf(msg); printf("\n"); return msg; }
// Enter a blank line in log as a marker of new peerd session logAdd("");
int logErr(char *msg) { logAdd(msg); // should prepend "ERROR:" return(errno = EXIT_FAILURE); }
// Same as logErr but allows error message to contain an arg int logErrMsg(char *fmt, char *msg2) { char *msg = malloc(100); sprintf(msg,fmt,msg2); logErr(msg); free(msg); return(errno = EXIT_FAILURE); }
// Same as logErr but allows error message to contain an arg int logErrNum(char *fmt, int num) { char *msg = malloc(100); sprintf(msg,fmt,num); logErr(msg); free(msg); return(errno = EXIT_FAILURE); }
// 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'; }
// ----------------------------------------------------------------------------------------- // // LINKED LIST typedef struct ll { void *data; struct ll *prev, *next; } link;
void linkInsert(link *subject, link *object) { object->next = subject->next; subject->next = object->next->prev = object; object->prev = subject; }
void linkRemove(link *subject) { subject->prev->next = subject->next; subject->next->prev = subject->prev; }
void linkTest() { // create a bunch of items // give them char* for payloads // do some link manipulation on them // render the sequence of char*'s at each step }



