Difference between revisions of "Serialise.c"

From Organic Design wiki
(assign subject outside loop)
(deserialise ready for debugging)
Line 1: Line 1:
 
// Process passed "alias1|alias2|alias3..." string (ext GUID's are just alias's)
 
// Process passed "alias1|alias2|alias3..." string (ext GUID's are just alias's)
// - returns integer index of a newly created internal node to which the passed names map
+
// - 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
 
// - later: if its a number, convert to a binary-coded-string of \x00's and \x01's
node processNodeLink(char *lnk) {
+
item processNodeNames(char *lnk) {
 
node subject = (node)listInsert();
 
node subject = (node)listInsert();
 
int len = strlen(lnk);
 
int len = strlen(lnk);
Line 29: Line 30:
  
 
// Convert textual represntation of one or more nodes and associations to nodal change
 
// Convert textual represntation of one or more nodes and associations to nodal change
node deserialise(char *text) {
+
void deserialise(char *text) {
 
printf("\nPARSING THE FOLLOWING TEXT:\n--------------------------------------------\n%s\n--------------------------------------------\n\n", text);
 
printf("\nPARSING THE FOLLOWING TEXT:\n--------------------------------------------\n%s\n--------------------------------------------\n\n", text);
 
printf("INFORMATION EXTRACTED:\n--------------------------------------------\n");
 
printf("INFORMATION EXTRACTED:\n--------------------------------------------\n");
int node, subject = 0, key, i, j = 0, linkStart, c;
+
item subject = ROOT
 +
int key, val, i, j = 0, linkStart;
 
char *link1 = malloc(50);
 
char *link1 = malloc(50);
 
char *link2 = malloc(50);
 
char *link2 = malloc(50);
 
for (i = 0; i < strlen(text); i++) {
 
for (i = 0; i < strlen(text); i++) {
 
char *s = text+i;
 
char *s = text+i;
if ((c = (int)s[0]) < 32) j = 1;
+
if (*s < 32) j = 1;
 
else switch (j) {
 
else switch (j) {
 
case 0: // waiting for next line
 
case 0: // waiting for next line
 
break;
 
break;
case 1: // starting new line
+
case 1: // starting new line - if not starting with *[[ then wait for next line
// - if not starting with *[[ then wait for next line
 
 
if (strncmp(s, "*[[", 3) == 0) {
 
if (strncmp(s, "*[[", 3) == 0) {
 
j = 2;
 
j = 2;
Line 50: Line 51:
 
case 2: // processing first link
 
case 2: // processing first link
 
if (strncmp(s, "]]", 2) == 0) {
 
if (strncmp(s, "]]", 2) == 0) {
if (strncmp(s, "]]:[[", 5) == 0) {
+
if (strncmp(s, "]]:[[", 5) == 0) j = 3; // link 1 of 2 done
// link1 as an association-key
 
// key = strtoint(...)
 
j = 3; // process link2 next
 
}
 
 
else {
 
else {
// link1 specifies a change of nodal subject
+
// link 1 of 1 done, specifies a change of nodal subject
 
link1 = strncpy(link1, text+linkStart+3, i-linkStart-3);
 
link1 = strncpy(link1, text+linkStart+3, i-linkStart-3);
 
link1[i-linkStart-3] = '\0';
 
link1[i-linkStart-3] = '\0';
Line 67: Line 64:
 
case 3: // processing second link
 
case 3: // processing second link
 
if (strncmp(s, "]]", 2) == 0) {
 
if (strncmp(s, "]]", 2) == 0) {
// process link2
+
// link 2 of 2 done, store nodally
 
link1 = strncpy(link1, text+linkStart+3, i-linkStart-3);
 
link1 = strncpy(link1, text+linkStart+3, i-linkStart-3);
 
link2 = strchr(link1, '[')+2;
 
link2 = strchr(link1, '[')+2;
 
link1[i-linkStart-3] = '\0';
 
link1[i-linkStart-3] = '\0';
 
link1[i-linkStart-strlen(link2)-8] = '\0';
 
link1[i-linkStart-strlen(link2)-8] = '\0';
int key = processNodeLink(link1);
+
key = processNodeLink(link1);
int val = processNodeLink(link2);
+
val = processNodeLink(link2);
// store in nodal-space
 
// - we use list functions because nodal functions cause change
 
 
listSetValue(listTraverse(subject,key),val);
 
listSetValue(listTraverse(subject,key),val);
 
printf("  %d => %d\n", key, val);
 
printf("  %d => %d\n", key, val);
Line 82: Line 77:
 
break;
 
break;
 
}
 
}
//printf("chr:%c,%d\n",*s,j);
 
 
}
 
}
 
printf("--------------------------------------------\n");
 
printf("--------------------------------------------\n");
return node;
 
 
}
 
}

Revision as of 02:44, 10 July 2006

// Process passed "alias1|alias2|alias3..." string (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 *link = malloc(len), *i = link; while(*i++ = *lnk++); *i = '\0'; char *name = i = link; do { if (*i == '|') *i = '\0'; if (*i == '\0') { trieSetValue(name,subject); name = ++i; } } while(*i++); free(link); return subject; }


// Convert a node to text for storage or transport char *serialise(node subject) { char *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(50); char *link2 = malloc(50); for (i = 0; i < strlen(text); i++) { char *s = text+i; if (*s < 32) j = 1; else switch (j) { case 0: // waiting for next line break; 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; 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 = processNodeLink(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 = processNodeLink(link1); val = processNodeLink(link2); listSetValue(listTraverse(subject,key),val); printf("  %d => %d\n", key, val); j = 0; } break; } } printf("--------------------------------------------\n"); }