Difference between revisions of "Util.c"
(using linked list just for list of freed-pointers) |
(add insert/remove pointer using free-pop/push) |
||
| Line 63: | Line 63: | ||
// - internally uses freePush() and freePop() to maintain a linked-list of free array slots | // - internally uses freePush() and freePop() to maintain a linked-list of free array slots | ||
| − | int psize = 100; | + | int psize = 100, pitem = 0; |
| − | void *plist = malloc(psize*sizeof(void*)) | + | void *plist = malloc(psize*sizeof(void*)); |
typedef struct { | typedef struct { | ||
void *data; | void *data; | ||
| Line 88: | Line 88: | ||
int insertPointer(void *ptr) { | int insertPointer(void *ptr) { | ||
| − | + | if (++pitem > psize) realloc(plist,psize += 100); | |
| − | return | + | return freePush(pitem); |
} | } | ||
void *removePointer(int index) { | void *removePointer(int index) { | ||
void *ptr; | void *ptr; | ||
| + | freePop(index); | ||
return ptr; | return ptr; | ||
} | } | ||
Revision as of 05:24, 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; }
// ----------------------------------------------------------------------------------------- // // Global Pointer List // - use insertPointer() and removePointer() to access the dynamically allocating pointer-array // - internally uses freePush() and freePop() to maintain a linked-list of free array slots
int psize = 100, pitem = 0; void *plist = malloc(psize*sizeof(void*)); typedef struct { void *data; struct fitem *next; } fitem; fitem *flist = NULL;
int freePush(int data) { fitem *newitem = (fitem*)malloc(sizeof(fitem)); newitem->next = flist ? flist : NULL; flist = newitem; return newitem->data = data; }
int freePop() { if (flist == NULL) return NULL; void *kill = flist; flist = kill->next; int data = kill->data; free(kill); return data; }
int insertPointer(void *ptr) { if (++pitem > psize) realloc(plist,psize += 100); return freePush(pitem); }
void *removePointer(int index) { void *ptr; freePop(index); return ptr; }



