#define NEWARR(TYPE, NUM) ((TYPE *)calloc(NUM, sizeof(TYPE)))
+// Util
+
+bool
+charInString(char c, const char * str) {
+ for (int i = 0; i < strlen(str); i++)
+ if (c == str[i])
+ return true;
+ return false;
+}
+
+bool
+isNewline(char c) {
+ return c == '\n' || c == '\r';
+}
+
+
// getch
#ifdef _WIN32
JSONNodeKind kind;
size_t data;
struct JSONNode * parent;
- struct JSONNode * children;
+ struct JSONNode * firstChild;
+ int childCount;
+ struct JSONNode * prev;
struct JSONNode * next;
} JSONNode;
JSONNode *
-JSONNodeNew(JSONNodeKind kind, size_t data) {
+JSONNodeNew(JSONNodeKind kind) {
JSONNode * result = NEW(JSONNode);
result->kind = kind;
- result->data = data;
return result;
}
-JSONNode *
-JSONNodeNewNul() {
- return JSONNodeNew(JSONNodeKind_Nul, (size_t)NULL);
-}
-
-JSONNode *
-JSONNodeNewInt(int i) {
- return JSONNodeNew(JSONNodeKind_Int, (size_t)i);
-}
-
-JSONNode *
-JSONNodeNewStr(const char * str) {
- return JSONNodeNew(JSONNodeKind_Str, (size_t)str);
-}
-
-JSONNode *
-JSONNodeNewObj() {
- return JSONNodeNew(JSONNodeKind_Obj, (size_t)NULL);
-}
-
-JSONNode *
-JSONNodeNewArr() {
- return JSONNodeNew(JSONNodeKind_Arr, (size_t)NULL);
-}
-
JSONNode *
JSONNodePush(JSONNode * this, JSONNode * that) {
- if (this->children == NULL) {
- this->children = that;
+ if (this->firstChild == NULL) {
+ this->firstChild = that;
}
else {
- JSONNode * lastNode = this->children;
+ JSONNode * lastNode = this->firstChild;
while (lastNode->next != NULL)
lastNode = lastNode->next;
lastNode->next = that;
+ that->prev = lastNode;
}
+ this->childCount++;
that->parent = this;
- that->next = NULL;
return that;
}
-void
-JSONNodePop(JSONNode * this) {
- if (this != NULL) {
- JSONNode * ptr = this->children;
-
- if (ptr == NULL) { // no children
- JSONNodePop(this->parent);
- }
- else if (ptr->next == NULL) { // one child
- this->children = NULL;
-
- }
- else { // more than one child
- while (ptr->next->next != NULL)
- ptr = ptr->next;
- ptr->next = NULL;
- }
- }
-}
-
void
Indent(int indent) {
for (int i = 0; i < indent; i++)
if (node->parent == NULL)
indent = 0;
+ if (currNode == node) {
+ vt100SaveCursor();
+ }
+
switch (node->kind) {
case JSONNodeKind_Nul: {
printf("null");
}
case JSONNodeKind_Obj: {
printf("{\n");
- JSONNode * ptr = node->children;
+ JSONNode * ptr = node->firstChild;
indent++;
while (ptr != NULL) {
- JSONNode * key = ptr;
- JSONNode * value = ptr->next;
Indent(indent);
- JSONNodePrint(key, currNode);
- printf(": ");
- JSONNodePrint(value, currNode);
- if (ptr->next != NULL)
- ptr = ptr->next->next;
- else
- ptr = NULL;
- printf("%s\n", (ptr == NULL ? "" : ","));
+ JSONNodePrint(ptr, currNode);
+ ptr = ptr->next;
+
+ if (ptr != NULL) {
+ printf(": ");
+ JSONNodePrint(ptr, currNode);
+ if (ptr->next != NULL)
+ printf(",");
+ ptr = ptr->next;
+ }
+ printf("\n");
}
indent--;
Indent(indent);
break;
}
case JSONNodeKind_Arr: {
- printf("[ ");
- JSONNode * ptr = node->children;
+ printf("[\n");
+ JSONNode * ptr = node->firstChild;
+ indent++;
while (ptr != NULL) {
- JSONNode * value = ptr;
- JSONNodePrint(value, currNode);
+ Indent(indent);
+ JSONNodePrint(ptr, currNode);
+ if (ptr->next != NULL)
+ printf(",");
+ printf("\n");
ptr = ptr->next;
- printf("%s", (ptr == NULL ? "" : ", "));
}
- printf(" ]");
+ indent--;
+ Indent(indent);
+ printf("]");
break;
}
}
-
- if (currNode == node) {
- vt100SaveCursor();
- }
}
char * str = (char *)node->data;
int c;
- while ((c = GetChar()), (c != '\r') && (c != '\n')) {
+ while ((c = PeekChar()), ! isNewline(c)) {
+ getch();
if ((c == 8 || c == 127) && strLen > 0) {
strLen--;
str[strLen] = '\0';
}
}
-void
-GetNode(JSONNode * parent, JSONNode * node) {
- int c = GetChar();
-
- g_CurrNode = node;
+JSONNode *
+GetNode() {
+ JSONNode * result = NULL;
- if (parent == NULL)
- g_DrawNode = node;
+ JSONNode * node = NULL;
- if (parent != NULL && node != NULL)
- JSONNodePush(parent, node);
+ while (true) {
+ if (isNewline(PeekChar())) {
+ getch();
- switch (c) {
- case 'i': {
- node->kind = JSONNodeKind_Int;
- GetStr(node, predInt);
- break;
- }
- case 's': {
- node->kind = JSONNodeKind_Str;
- GetStr(node, predStr);
- break;
- }
- case 'o': {
- node->kind = JSONNodeKind_Obj;
- while ((c = PeekChar()), (c != '\r') && (c != '\n')) {
- JSONNode * newNode;
+ if (node == NULL || node->parent == NULL)
+ break;
- newNode = JSONNodeNewStr("");
- g_CurrNode = newNode;
+ node = node->parent;
+ g_CurrNode = node;
+
+ continue;
+ }
+
+ // object key
+ if (node != NULL && node->kind == JSONNodeKind_Obj && node->childCount % 2 == 0) {
+ JSONNode * newNode = JSONNodeNew(JSONNodeKind_Str);
JSONNodePush(node, newNode);
+
+ node = newNode;
+ g_CurrNode = node;
+
GetStr(newNode, predStr);
+
+ continue;
+ }
+
+ int c = GetChar();
- newNode = JSONNodeNewNul();
+ if (! charInString(c, "isoa"))
+ continue;
+
+ JSONNode * newNode = JSONNodeNew(JSONNodeKind_Nul);
+
+ if (result == NULL)
+ g_DrawNode = result = newNode;
+
+ if (node != NULL)
JSONNodePush(node, newNode);
- GetNode(node, newNode);
+
+ node = newNode;
+ g_CurrNode = node;
+
+ switch (c) {
+ case 'i': {
+ node->kind = JSONNodeKind_Int;
+ GetStr(node, predInt);
+ break;
}
- g_CurrNode = node;
- getch();
- break;
- }
- case 'a': {
- node->kind = JSONNodeKind_Arr;
- while ((c = PeekChar()), (c != '\r') && (c != '\n')) {
- JSONNode * newNode;
-
- newNode = JSONNodeNewNul();
- //g_CurrNode = newNode;
- JSONNodePush(node, newNode);
- GetNode(node, newNode);
+ case 's': {
+ node->kind = JSONNodeKind_Str;
+ GetStr(node, predStr);
+ break;
+ }
+ case 'o': {
+ node->kind = JSONNodeKind_Obj;
+ break;
+ }
+ case 'a': {
+ node->kind = JSONNodeKind_Arr;
+ break;
+ }
+ case 8:
+ case 127:
+ break;
}
- g_CurrNode = node;
- getch();
- break;
- }
- /*
- case 8:
- case 127:
- JSONNodePop(parent);
- JSONNode * newNode = JSONNodeNewNul();
- GetNode(parent, newNode);
- break;
- */
- case 't':
- node->kind = JSONNodeKind_Int;
- node->data = (size_t)GetChar();
- break;
}
+
+ return result;
}
int main() {
vt100EnableAlternateBuffer();
- Draw();
-
- JSONNode * n = JSONNodeNewNul();
- GetNode(NULL, n);
- //JSONNode * n = TestNode();
+ JSONNode * n = GetNode();
+
+ vt100DisableAlternateBuffer();
- vt100ClearScreen();
- vt100CursorHome();
JSONNodePrint(n, NULL);
- printf("\n");
// JSONFree(n);
-
- vt100DisableAlternateBuffer();
return 0;
}