#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
+#include <stdbool.h>
+
+/* TODO
+- whitelist input on GetStr/GetInt
+*/
// Memory
typedef enum {
JSONNodeKind_Nul,
+ JSONNodeKind_Int,
JSONNodeKind_Str,
JSONNodeKind_Obj,
+ JSONNodeKind_Arr,
} JSONNodeKind;
struct JSONNode;
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 *
-JSONNodeNewNul() {
- return JSONNodeNew(JSONNodeKind_Nul, (size_t)NULL);
+JSONNodeNewArr() {
+ return JSONNodeNew(JSONNodeKind_Arr, (size_t)NULL);
}
JSONNode *
-JSONNodeAppend(JSONNode * this, JSONNode * that) {
+JSONNodePush(JSONNode * this, JSONNode * that) {
if (this->children == NULL) {
this->children = that;
}
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++)
printf("null");
break;
}
+ case JSONNodeKind_Int: {
+ int i = (int)node->data;
+ printf("%d", i);
+ break;
+ }
case JSONNodeKind_Str: {
char * str = (char *)node->data;
printf("\"%s\"", str == NULL ? "" : str);
printf("}");
break;
}
+ case JSONNodeKind_Arr: {
+ printf("[ ");
+ JSONNode * ptr = node->children;
+ while (ptr != NULL) {
+ JSONNode * value = ptr;
+ JSONNodePrint(value);
+ ptr = ptr->next;
+ printf("%s", (ptr == NULL ? "" : ", "));
+ }
+ printf(" ]");
+ break;
+ }
}
}
vt100CursorPos(0, strlen(str) + 3);
}
+int
+GetInt() {
+ static char intStr[16];
+ int intStrLen = 0;
+ int result = 0;
+ int c;
+ while ((c = getch()), (c != '\r') && (c != '\n')) {
+ if (c == 8 && intStrLen > 0) {
+ intStrLen--;
+ intStr[intStrLen] = '\0';
+ result /= 10;
+ Draw(g_Node, intStr);
+ }
+ else if (intStrLen < 16 - 1 && (c >= '0' && c <= '9')) {
+ intStr[intStrLen++] = c;
+ intStr[intStrLen] = '\0';
+ result *= 10;
+ result += c - '0';
+ Draw(g_Node, intStr);
+ }
+ }
+ return result;
+}
+
char *
GetStr() {
char * str = NEWARR(char, 16);
int strLen = 0;
int c;
while ((c = getch()), (c != '\r') && (c != '\n')) {
- if (strLen < 16 - 1) {
+ if (c == 8 && strLen > 0) {
+ strLen--;
+ str[strLen] = '\0';
+ Draw(g_Node, str);
+ }
+ else if (strLen < 16 - 1) {
str[strLen++] = c;
str[strLen] = '\0';
Draw(g_Node, str);
g_Node = result;
if (parent != NULL && result != NULL)
- JSONNodeAppend(parent, result);
+ JSONNodePush(parent, result);
switch (c) {
+ case 'i': {
+ result->kind = JSONNodeKind_Int;
+ Draw(g_Node, "");
+ result->data = (size_t)GetInt();
+ Draw(g_Node, "");
+ break;
+ }
case 's': {
result->kind = JSONNodeKind_Str;
Draw(g_Node, "");
while ((c = peekch()), (c != '\r') && (c != '\n')) {
Draw(g_Node, "");
- JSONNodeAppend(result, JSONNodeNewStr(GetStr()));
+ JSONNodePush(result, JSONNodeNewStr(GetStr()));
+ Draw(g_Node, "");
+
+ JSONNodePush(result, GetNode(result));
+ Draw(g_Node, "");
+ }
+ getch();
+ break;
+ }
+ case 'a': {
+ result->kind = JSONNodeKind_Arr;
+ Draw(g_Node, "");
+ while ((c = peekch()), (c != '\r') && (c != '\n')) {
Draw(g_Node, "");
- JSONNodeAppend(result, GetNode(result));
+ JSONNodePush(result, GetNode(result));
Draw(g_Node, "");
}
getch();
+ break;
}
+ case 8:
+ //case 127:
+ JSONNodePop(parent);
+ Draw(g_Node, "");
+ result = GetNode(parent);
+ break;
+ case 't':
+ result->kind = JSONNodeKind_Int;
+ result->data = (size_t)getch();
+ break;
}
return result;
JSONNode * k2 = JSONNodeNewStr("key 2");
JSONNode * v2 = JSONNodeNewStr("val 2");
- JSONNodeAppend(n, k1);
- JSONNodeAppend(n, v1);
- JSONNodeAppend(n, k2);
- JSONNodeAppend(n, v2);
+ JSONNodePush(n, k1);
+ JSONNodePush(n, v1);
+ JSONNodePush(n, k2);
+ JSONNodePush(n, v2);
- JSONNodeAppend(v1, k11);
- JSONNodeAppend(v1, v11);
- JSONNodeAppend(v1, k12);
- JSONNodeAppend(v1, v12);
+ JSONNodePush(v1, k11);
+ JSONNodePush(v1, v11);
+ JSONNodePush(v1, k12);
+ JSONNodePush(v1, v12);
return n;
}
vt100ClearScreen();
vt100CursorHome();
JSONNodePrint(n);
+ printf("\n");
// JSONFree(n);