From: Patrick Date: Thu, 27 Jul 2023 00:45:46 +0000 (+0200) Subject: idk D: X-Git-Url: https://gitweb.ps.run/iftint/commitdiff_plain/5ae0aff8511d9e55d25b35f2f9c3e2c70f5c6178?ds=sidebyside idk D: --- diff --git a/main2.c b/main2.c index 47e11ee..5864bc8 100644 --- a/main2.c +++ b/main2.c @@ -2,6 +2,11 @@ #include #include #include +#include + +/* TODO +- whitelist input on GetStr/GetInt +*/ // Memory @@ -91,8 +96,10 @@ void vt100RestoreCursor() { vt100Escape("8"); } typedef enum { JSONNodeKind_Nul, + JSONNodeKind_Int, JSONNodeKind_Str, JSONNodeKind_Obj, + JSONNodeKind_Arr, } JSONNodeKind; struct JSONNode; @@ -112,6 +119,16 @@ JSONNodeNew(JSONNodeKind kind, size_t 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); @@ -123,12 +140,12 @@ JSONNodeNewObj() { } 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; } @@ -144,6 +161,26 @@ JSONNodeAppend(JSONNode * this, JSONNode * 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++) @@ -164,6 +201,11 @@ JSONNodePrint(JSONNode * node) { 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); @@ -190,6 +232,18 @@ JSONNodePrint(JSONNode * node) { 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; + } } } @@ -211,13 +265,42 @@ Draw(JSONNode * node, const char * str) { 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); @@ -236,9 +319,16 @@ GetNode(JSONNode * parent) { 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, ""); @@ -252,14 +342,37 @@ GetNode(JSONNode * parent) { 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; @@ -277,15 +390,15 @@ TestNode() { 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; } @@ -299,6 +412,7 @@ int main() { vt100ClearScreen(); vt100CursorHome(); JSONNodePrint(n); + printf("\n"); // JSONFree(n);