Difference between revisions of "Serialise.c"

From Organic Design wiki
(move buildAssocTree() out to listSpace.c as listKeys())
m
Line 31: Line 31:
 
char *serialise(node subject) {
 
char *serialise(node subject) {
 
char *text;
 
char *text;
buildAssocTree((item)subject,0,1);
+
void* keys = listGetKeys((item)subject);
 +
while (*keys++) {
 +
// add returned key info as text
 +
}
 
return text;
 
return text;
 
}
 
}

Revision as of 02:10, 13 July 2006

// This article and all its includes are licenced under LGPL // http://www.gnu.org/copyleft/lesser.html // - used for storing and communicating nodal structure as plain text // - rather than XML we've used a simple wiki-link format for easy human editing


// Process passed string of pipe-separated names (ext GUID's are just alias's) // - returns index of a newly created internal node to which the passed names map // - returns index as item not node because nodal storage functions are active // - later: if its a number, convert to a binary-coded-string of \x00's and \x01's item processNodeNames(char *lnk) { node subject = (node)listInsert(); int len = strlen(lnk); char *i = malloc(len+1), *j = i, *link = i, *name = i; while(*j++ = *lnk++); while(i <= link+len) { if (*i == '|') *i = '\0'; if (*i++ == '\0') { trieSetValue(name,subject); // need to store name as nodal attribute of node too name = i; } } free(link); return subject; }

// Convert a node to text for storage or transport // - needs to do a binary traversal into the passed node to build association list char *serialise(node subject) { char *text; void* keys = listGetKeys((item)subject); while (*keys++) { // add returned key info as text } return text; }

// Convert textual represntation of one or more nodes and associations to nodal change void deserialise(char *text) { printf("\nPARSING THE FOLLOWING TEXT:\n--------------------------------------------\n%s\n--------------------------------------------\n\n", text); printf("INFORMATION EXTRACTED:\n--------------------------------------------\n"); item subject = ROOT int key, val, i, j = 0, linkStart; char *link1 = malloc(100), *link2 = link1+50; for (i = 0; i < strlen(text); i++) { char *s = text+i; if (*s < 32) j = 1; else switch (j) { case 1: // starting new line - if not starting with *[[ then wait for next line if (strncmp(s, "*[[", 3) == 0) { j = 2; linkStart = i; } else j = 0; // 0 is wait for next line break; case 2: // processing first link if (strncmp(s, "]]", 2) == 0) { if (strncmp(s, "]]:[[", 5) == 0) j = 3; // link 1 of 2 done else { // link 1 of 1 done, specifies a change of nodal subject link1 = strncpy(link1, text+linkStart+3, i-linkStart-3); link1[i-linkStart-3] = '\0'; subject = processNodeNames(link1); printf("%d:\n", subject); j = 0; // wait for new line } } break; case 3: // processing second link if (strncmp(s, "]]", 2) == 0) { // link 2 of 2 done, store nodally link1 = strncpy(link1, text+linkStart+3, i-linkStart-3); link2 = strchr(link1, '[')+2; link1[i-linkStart-3] = '\0'; link1[i-linkStart-strlen(link2)-8] = '\0'; key = processNodeNames(link1); val = processNodeNames(link2); listSetValue(listTraverse(subject,key),val); printf("  %d => %d\n", key, val); j = 0; } break; } } printf("--------------------------------------------\n"); }