Difference between revisions of "Util.c"

From Organic Design wiki
m (struct syntax)
(no need for before/after for link-list)
Line 62: Line 62:
 
} link;
 
} link;
  
void linkBefore(link *subject, link *object) {
+
void linkInsert(link *subject, link *object) {
object->prev = subject->prev;
 
subject->prev = object->prev->next = object;
 
object->next = subject;
 
}
 
 
 
void linkAfter(link *subject, link *object) {
 
 
object->next = subject->next;
 
object->next = subject->next;
 
subject->next = object->next->prev = object;
 
subject->next = object->next->prev = object;

Revision as of 01:34, 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 { 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 }