From: Patrick Date: Sat, 5 Aug 2023 20:47:21 +0000 (+0200) Subject: fix input and cursors X-Git-Url: https://gitweb.ps.run/iftint/commitdiff_plain/4889cf922e51c34f714941498d25588ceb258e67?hp=40bbfb3371163c52e4c009020a5d1c99e387d2db fix input and cursors --- diff --git a/main2.c b/main2.c index a3223ba..86cc7b7 100644 --- a/main2.c +++ b/main2.c @@ -78,16 +78,16 @@ void vt100CursorHome() { vt100Escape("[H"); } void vt100CursorPos(int v, int h) { vt100Escape("[%d;%dH", v, h); } void vt100SaveCursor() { vt100Escape("7"); } void vt100RestoreCursor() { vt100Escape("8"); } -void vt100GetCursor(int * v, int * h) { - *v = *h = 0; - printf("\033[6n"); - getch(); getch(); - int c; - while ((c = getch()) != ';') - *v = (10*(*v)+(c-'0')); - while ((c = getch()) != 'R') - *h = (10*(*h)+(c-'0')); -} +// void vt100GetCursor(int * v, int * h) { +// *v = *h = 0; +// printf("\033[6n"); +// getch(); getch(); +// int c; +// while ((c = getch()) != ';') +// *v = (10*(*v)+(c-'0')); +// while ((c = getch()) != 'R') +// *h = (10*(*h)+(c-'0')); +// } void vt100GetScreenSize(int * v, int * h) { #ifdef _WIN32 CONSOLE_SCREEN_BUFFER_INFO csbi; @@ -201,9 +201,6 @@ Indent(int indent) { printf(" "); } -static int currV = 0; -static int currH = 0; - void JSONNodePrint(JSONNode * node, JSONNode * currNode) { if (node == NULL) @@ -219,8 +216,8 @@ JSONNodePrint(JSONNode * node, JSONNode * currNode) { break; } case JSONNodeKind_Int: { - int i = (int)node->data; - printf("%d", i); + char * str = (char *)node->data; + printf("%s", (str == NULL || strlen(str) == 0) ? "0" : str); break; } case JSONNodeKind_Str: { @@ -233,10 +230,11 @@ JSONNodePrint(JSONNode * node, JSONNode * currNode) { JSONNode * ptr = node->children; indent++; while (ptr != NULL) { - char * key = (char *)ptr->data; + JSONNode * key = ptr; JSONNode * value = ptr->next; Indent(indent); - printf("\"%s\": ", key); + JSONNodePrint(key, currNode); + printf(": "); JSONNodePrint(value, currNode); if (ptr->next != NULL) ptr = ptr->next->next; @@ -264,14 +262,7 @@ JSONNodePrint(JSONNode * node, JSONNode * currNode) { } if (currNode == node) { - int currOffsets[JSONNodeKind_COUNT]; - currOffsets[JSONNodeKind_Nul] = 0; - currOffsets[JSONNodeKind_Int] = 0; - currOffsets[JSONNodeKind_Str] = 1; - currOffsets[JSONNodeKind_Obj] = 1; - currOffsets[JSONNodeKind_Arr] = 2; - vt100GetCursor(&currV, &currH); - currH -= currOffsets[node->kind]; + vt100SaveCursor(); } } @@ -288,7 +279,7 @@ Draw(void) { if (g_DrawNode != NULL) { JSONNodePrint(g_DrawNode, g_CurrNode); - vt100CursorPos(currV, currH); + vt100RestoreCursor(); } } @@ -306,31 +297,13 @@ PeekChar() { return c; } -void -GetInt(JSONNode * node) { - char intStr[16] = ""; - int intStrLen = 0; - - size_t * i = &node->data; - - int c; - while ((c = GetChar()), (c != '\r') && (c != '\n')) { - if ((c == 8 || c == 127) && intStrLen > 0) { - intStrLen--; - intStr[intStrLen] = '\0'; - *i /= 10; - } - else if (intStrLen < 16 - 1 && (c >= '0' && c <= '9')) { - intStr[intStrLen++] = c; - intStr[intStrLen] = '\0'; - *i *= 10; - *i += c - '0'; - } - } -} +typedef bool(*CharPredicateFunc)(char, int); + +bool predStr(char c, int i) { return c >= 'a' && c <= 'z'; } +bool predInt(char c, int i) { return c >= '0' && c <= '9'; } void -GetStr(JSONNode * node) { +GetStr(JSONNode * node, CharPredicateFunc charPredicate) { node->data = (size_t)NEWARR(char, 16); int strLen = 0; @@ -342,9 +315,9 @@ GetStr(JSONNode * node) { strLen--; str[strLen] = '\0'; } - else if (strLen < 16 - 1) { + else if (strLen < 16 - 1 && charPredicate(c, strLen)) { str[strLen] = c; - strLen++; + strLen++; str[strLen] = '\0'; } } @@ -354,54 +327,55 @@ void GetNode(JSONNode * parent, JSONNode * node) { int c = GetChar(); - JSONNode * result = node; g_CurrNode = node; if (parent == NULL) - g_DrawNode = result; + g_DrawNode = node; - if (parent != NULL && result != NULL) - JSONNodePush(parent, result); + if (parent != NULL && node != NULL) + JSONNodePush(parent, node); switch (c) { case 'i': { - result->kind = JSONNodeKind_Int; - GetInt(result); + node->kind = JSONNodeKind_Int; + GetStr(node, predInt); break; } case 's': { - result->kind = JSONNodeKind_Str; - GetStr(result); + node->kind = JSONNodeKind_Str; + GetStr(node, predStr); break; } case 'o': { - result->kind = JSONNodeKind_Obj; + node->kind = JSONNodeKind_Obj; while ((c = PeekChar()), (c != '\r') && (c != '\n')) { - JSONNode * newNode; - - newNode = JSONNodeNewStr(""); - g_CurrNode = newNode; - JSONNodePush(result, newNode); - GetStr(newNode); - - newNode = JSONNodeNewNul(); - JSONNodePush(result, newNode); - GetNode(result, newNode); + JSONNode * newNode; + + newNode = JSONNodeNewStr(""); + g_CurrNode = newNode; + JSONNodePush(node, newNode); + GetStr(newNode, predStr); + + newNode = JSONNodeNewNul(); + JSONNodePush(node, newNode); + GetNode(node, newNode); } - g_CurrNode = result; - GetChar(); + g_CurrNode = node; + getch(); break; } case 'a': { - result->kind = JSONNodeKind_Arr; + node->kind = JSONNodeKind_Arr; while ((c = PeekChar()), (c != '\r') && (c != '\n')) { - JSONNode * newNode = JSONNodeNewNul(); - g_CurrNode = newNode; - JSONNodePush(result, newNode); - GetNode(result, newNode); + JSONNode * newNode; + + newNode = JSONNodeNewNul(); + //g_CurrNode = newNode; + JSONNodePush(node, newNode); + GetNode(node, newNode); } - g_CurrNode = result; - GetChar(); + g_CurrNode = node; + getch(); break; } /* @@ -413,8 +387,8 @@ GetNode(JSONNode * parent, JSONNode * node) { break; */ case 't': - result->kind = JSONNodeKind_Int; - result->data = (size_t)GetChar(); + node->kind = JSONNodeKind_Int; + node->data = (size_t)GetChar(); break; } }