Difference between revisions of "Util.c"
(linked list belongs in here) |
(change to more usual pointer/struct based implimentation) |
||
| Line 57: | Line 57: | ||
// ----------------------------------------------------------------------------------------- // | // ----------------------------------------------------------------------------------------- // | ||
// LINKED LIST | // LINKED LIST | ||
| − | + | typedef struct ll link; | |
| − | + | struct ll { | |
| − | void linkBefore( | + | link *prev, *next; |
| − | + | void *data; | |
| − | + | } link; | |
| − | + | ||
| − | + | void linkBefore(link *subject, link *object) { | |
| + | object->prev = subject->prev; | ||
| + | subject->prev = object->prev->next = object; | ||
| + | object->next = subject; | ||
} | } | ||
void linkAfter(item subject, item object) { | void linkAfter(item subject, item object) { | ||
| − | + | object->next = subject->next; | |
| − | + | subject->next = object->next->prev = object; | |
| − | + | object->prev = subject; | |
| − | |||
} | } | ||
void linkRemove(item subject) { | void linkRemove(item subject) { | ||
| − | + | subject->prev->next = subject->next; | |
| − | + | subject->next->prev = subject->prev; | |
| − | |||
} | } | ||
void linkTest() { | void linkTest() { | ||
// create a bunch of items | // create a bunch of items | ||
| − | // give them | + | // give them char* for payloads |
// do some link manipulation on them | // do some link manipulation on them | ||
| − | // render the sequence of | + | // render the sequence of char*'s at each step |
} | } | ||
Revision as of 00:03, 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(err = EXIT_FAILURE); }
// Same as logErr but allows error message to contain an arg int logErr2(char *fmt, char *msg2) { char *msg = malloc(100); sprintf(msg,fmt,msg2); logErr(msg); free(msg); return(err = 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 link; struct ll { link *prev, *next; void *data; } link;
void linkBefore(link *subject, link *object) { object->prev = subject->prev; subject->prev = object->prev->next = object; object->next = subject; }
void linkAfter(item subject, item object) { object->next = subject->next; subject->next = object->next->prev = object; object->prev = subject; }
void linkRemove(item 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 }



