X-Git-Url: https://gitweb.ps.run/iftint/blobdiff_plain/4889cf922e51c34f714941498d25588ceb258e67..HEAD:/main2.c diff --git a/main2.c b/main2.c index 86cc7b7..3dcbf84 100644 --- a/main2.c +++ b/main2.c @@ -14,6 +14,33 @@ #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'; +} +bool +isBackspace(char c) { + return c == 8 || c == 127; +} + + +// Key defines + +#define KEY_CTRL_C 3 +#define KEY_BACKSPACE1 8 +#define KEY_BACKSPACE2 127 + + // getch #ifdef _WIN32 @@ -121,80 +148,61 @@ typedef struct JSONNode { 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; - } +JSONNode * +JSONNodeRemove(JSONNode * node) { + if (node->prev == NULL) { // first child + node->parent->firstChild = node->next; + if (node->next != NULL) + node->next->prev = NULL; + return node->parent; + } + else { // second child + node->prev->next = node->next; + if (node->next != NULL) + node->next->prev = node->prev; + return node->prev; } } +bool +JSONNodeEditable(JSONNode * node) { + if (node == NULL) return false; + else if (node->kind == JSONNodeKind_Int) return true; + else if (node->kind == JSONNodeKind_Str) return true; + return false; +} + void Indent(int indent) { for (int i = 0; i < indent; i++) @@ -210,6 +218,10 @@ JSONNodePrint(JSONNode * node, JSONNode * currNode) { if (node->parent == NULL) indent = 0; + if (currNode == node) { + vt100SaveCursor(); + } + switch (node->kind) { case JSONNodeKind_Nul: { printf("null"); @@ -227,20 +239,17 @@ JSONNodePrint(JSONNode * node, JSONNode * currNode) { } 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); + JSONNodePrint(ptr, currNode); printf(": "); - JSONNodePrint(value, currNode); + JSONNodePrint(ptr->firstChild, currNode); if (ptr->next != NULL) - ptr = ptr->next->next; - else - ptr = NULL; - printf("%s\n", (ptr == NULL ? "" : ",")); + printf(","); + ptr = ptr->next; + printf("\n"); } indent--; Indent(indent); @@ -248,22 +257,23 @@ JSONNodePrint(JSONNode * node, JSONNode * currNode) { 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(); - } } @@ -302,95 +312,97 @@ 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, CharPredicateFunc charPredicate) { - node->data = (size_t)NEWARR(char, 16); +JSONNode * +GetNode() { + JSONNode * result = NULL; + JSONNode * node = NULL; + int c; int strLen = 0; - char * str = (char *)node->data; - - int c; - while ((c = GetChar()), (c != '\r') && (c != '\n')) { - if ((c == 8 || c == 127) && strLen > 0) { - strLen--; - str[strLen] = '\0'; - } - else if (strLen < 16 - 1 && charPredicate(c, strLen)) { - str[strLen] = c; - strLen++; - str[strLen] = '\0'; - } - } -} + typedef enum { + InputState_Initial, + InputState_Primitive, + InputState_Object, + InputState_KeyValue, + InputState_Array, + } InputState; + InputState inputState; -void -GetNode(JSONNode * parent, JSONNode * node) { - int c = GetChar(); + bool editingNode = false; - g_CurrNode = node; + while (true) { + c = GetChar(); - if (parent == NULL) - g_DrawNode = node; + if (c == KEY_CTRL_C) + break; - if (parent != NULL && node != NULL) - JSONNodePush(parent, node); + if (isNewline(c)) { + if (node == NULL || node->parent == NULL) + break; - 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->parent->kind == JSONNodeKind_Obj) // editing obj key + g_CurrNode = node = node->firstChild; + else if (node->parent->kind == JSONNodeKind_Str) // editing obj value + g_CurrNode = node = node->parent->parent; + else + g_CurrNode = node = node->parent; - newNode = JSONNodeNewStr(""); - g_CurrNode = newNode; - JSONNodePush(node, newNode); - GetStr(newNode, predStr); - - newNode = JSONNodeNewNul(); - JSONNodePush(node, newNode); - GetNode(node, newNode); - } - g_CurrNode = node; - getch(); - break; - } - case 'a': { - node->kind = JSONNodeKind_Arr; - while ((c = PeekChar()), (c != '\r') && (c != '\n')) { - JSONNode * newNode; + editingNode = JSONNodeEditable(node); - newNode = JSONNodeNewNul(); - //g_CurrNode = newNode; - JSONNodePush(node, newNode); - GetNode(node, newNode); + continue; + } + + if (JSONNodeEditable(node) && editingNode) { + if (node->data == (size_t)NULL) { + node->data = (size_t)NEWARR(char, 16); + strLen = 0; + } + char * str = (char *)node->data; + + if (isBackspace(c)) { + str[strLen-1] = '\0'; + strLen--; + } + else if (strLen < 16 - 1) { + str[strLen] = c; + str[strLen+1] = '\0'; + strLen++; + } + } + else { + JSONNode * old = node; + + /**/ if (c == 'i') { node = JSONNodeNew(JSONNodeKind_Int); editingNode = true; } + else if (c == 's') { node = JSONNodeNew(JSONNodeKind_Str); editingNode = true; } + else if (c == 'o') { node = JSONNodeNew(JSONNodeKind_Obj); } + else if (c == 'a') { node = JSONNodeNew(JSONNodeKind_Arr); } + else if (c == 'h') { if (node->prev != NULL) g_CurrNode = node = node->prev; continue; } + else if (c == 'l') { if (node->next != NULL) g_CurrNode = node = node->next; continue; } + else if (c == 'k') { if (node->parent != NULL) g_CurrNode = node = node->parent; continue; } + else if (c == 'j') { if (node->firstChild != NULL) g_CurrNode = node = node->firstChild; continue; } + else if (c == ' ') { if (JSONNodeEditable(node)) editingNode = true; continue; } + else if (isBackspace(c) && node != NULL) { g_CurrNode = node = JSONNodeRemove(node); continue; } + else { continue; } + + if (old != NULL) { + // new node was added to an object + if (old->kind == JSONNodeKind_Obj) { + JSONNode * keyNode = JSONNodeNew(JSONNodeKind_Str); + JSONNodePush(keyNode, node); + node = keyNode; + editingNode = true; + } + JSONNodePush(old, node); + } + else { + g_DrawNode = result = node; + } + + g_CurrNode = node; } - 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; } @@ -399,20 +411,13 @@ GetNode(JSONNode * parent, JSONNode * node) { 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; }