Difference between revisions of "Util.c"

From Organic Design wiki
(from file.c and add/start split())
 
(split() tested and working fine)
Line 15: Line 15:
 
}
 
}
  
 +
// Return an array of strings resulting from splitting passed text at passed character
 
char **split(char c,char *text) {
 
char **split(char c,char *text) {
node subject = (node)listInsert();
+
int len = strlen(text), items = 0, size = 10;
int len = strlen(lnk);
+
char **list = malloc(size);
char *i = malloc(len+1), *j = i, *link = i, *name = i;
+
char *i = malloc(len+1), *j = i, *k = i, *item = i;
while(*j++ = *lnk++);
+
while(*j++ = *text++);
while(i <= link+len) {
+
while(i <= k+len) {
if (*i == '|') *i = '\0';
+
if (*i == c) *i = '\0';
if (*i++ == '\0') {
+
if ((*i++ == '\0')&&strlen(item)) {
trieSetValue(name,subject);
+
if (items>size-2) realloc(list,size+=10);
// need to store name as nodal attribute of node too
+
list[items++] = item;
name = i;
+
list[items] = NULL;
 +
item = i;
 
}
 
}
 
}
 
}
free(link);
+
return list;
return subject;
 
 
}
 
}

Revision as of 10:27, 15 July 2006

int logAdd(char* msg) { // prepend with a timestamp // append to logfile }

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

int fileRead(char* filename) { }

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

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