]> gitweb.ps.run Git - iftint/blobdiff - main2.c
Test with custom printf
[iftint] / main2.c
diff --git a/main2.c b/main2.c
index a3223baa1a3fb4379066c0e9e30e898605ef0786..c8b9023b05f37d161648d6e4a52e629819097204 100644 (file)
--- a/main2.c
+++ b/main2.c
 #define NEWARR(TYPE, NUM) ((TYPE *)calloc(NUM, sizeof(TYPE)))
 
 
 #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
 // getch
 
 #ifdef _WIN32
@@ -78,16 +105,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 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;
 void vt100GetScreenSize(int * v, int * h) {
 #ifdef _WIN32
     CONSOLE_SCREEN_BUFFER_INFO csbi;
@@ -112,6 +139,7 @@ typedef enum {
     JSONNodeKind_Int,
     JSONNodeKind_Str,
     JSONNodeKind_Obj,
     JSONNodeKind_Int,
     JSONNodeKind_Str,
     JSONNodeKind_Obj,
+    JSONNodeKind_Key,
     JSONNodeKind_Arr,
     JSONNodeKind_COUNT
 } JSONNodeKind;
     JSONNodeKind_Arr,
     JSONNodeKind_COUNT
 } JSONNodeKind;
@@ -121,89 +149,67 @@ typedef struct JSONNode {
     JSONNodeKind kind;
     size_t data;
     struct JSONNode * parent;
     JSONNodeKind kind;
     size_t data;
     struct JSONNode * parent;
-    struct JSONNode * children;
+    struct JSONNode * firstChild;
+    int childCount;
+    struct JSONNode * prev;
     struct JSONNode * next;
 } JSONNode;
 
 JSONNode *
     struct JSONNode * next;
 } JSONNode;
 
 JSONNode *
-JSONNodeNew(JSONNodeKind kind, size_t data) {
+JSONNodeNew(JSONNodeKind kind) {
     JSONNode * result = NEW(JSONNode);
     result->kind = kind;
     JSONNode * result = NEW(JSONNode);
     result->kind = kind;
-    result->data = data;
     return result;
 }
 
     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) {
 JSONNode *
 JSONNodePush(JSONNode * this, JSONNode * that) {
-    if (this->children == NULL) {
-        this->children = that;
+    if (this->firstChild == NULL) {
+        this->firstChild = that;
     }
     else {
     }
     else {
-        JSONNode * lastNode = this->children;
+        JSONNode * lastNode = this->firstChild;
         while (lastNode->next != NULL)
             lastNode = lastNode->next;
         lastNode->next = that;
         while (lastNode->next != NULL)
             lastNode = lastNode->next;
         lastNode->next = that;
+        that->prev = lastNode;
     }
     }
+    this->childCount++;
     that->parent = this;
     that->parent = this;
-    that->next = NULL;
     
     return 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;
-        }
+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++)
         printf("  ");
 }
 
 void
 Indent(int indent) {
     for (int i = 0; i < indent; i++)
         printf("  ");
 }
 
-static int currV = 0;
-static int currH = 0;
-
 void
 JSONNodePrint(JSONNode * node, JSONNode * currNode) {
     if (node == NULL)
 void
 JSONNodePrint(JSONNode * node, JSONNode * currNode) {
     if (node == NULL)
@@ -213,14 +219,18 @@ JSONNodePrint(JSONNode * node, JSONNode * currNode) {
     if (node->parent == NULL)
         indent = 0;
 
     if (node->parent == NULL)
         indent = 0;
 
+    if (currNode == node) {
+        vt100SaveCursor();
+    }
+
     switch (node->kind) {
     case JSONNodeKind_Nul: {
         printf("null");
         break;
     }
     case JSONNodeKind_Int: {
     switch (node->kind) {
     case JSONNodeKind_Nul: {
         printf("null");
         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: {
         break;
     }
     case JSONNodeKind_Str: {
@@ -230,49 +240,45 @@ JSONNodePrint(JSONNode * node, JSONNode * currNode) {
     }
     case JSONNodeKind_Obj: {
         printf("{\n");
     }
     case JSONNodeKind_Obj: {
         printf("{\n");
-        JSONNode * ptr = node->children;
+        JSONNode * ptr = node->firstChild;
         indent++;
         while (ptr != NULL) {
         indent++;
         while (ptr != NULL) {
-            char * key = (char *)ptr->data;
-            JSONNode * value = ptr->next;
             Indent(indent);
             Indent(indent);
-            printf("\"%s\": ", key);
-            JSONNodePrint(value, currNode);
+            JSONNodePrint(ptr, currNode);
+            JSONNodePrint(ptr->firstChild, currNode);
             if (ptr->next != NULL)
             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);
         printf("}");
         break;
     }
         }
         indent--;
         Indent(indent);
         printf("}");
         break;
     }
+    case JSONNodeKind_Key: {
+        char * str = (char *)node->data;
+        printf("\"%s\": ", str == NULL ? "" : str);
+        break;
+    }
     case JSONNodeKind_Arr: {
     case JSONNodeKind_Arr: {
-        printf("[ ");
-        JSONNode * ptr = node->children;
+        printf("[\n");
+        JSONNode * ptr = node->firstChild;
+        indent++;
         while (ptr != NULL) {
         while (ptr != NULL) {
-            JSONNode * value = ptr;
-            JSONNodePrint(value, currNode);
+            Indent(indent);
+            JSONNodePrint(ptr, currNode);
+            if (ptr->next != NULL)
+                printf(",");
+            printf("\n");
             ptr = ptr->next;
             ptr = ptr->next;
-            printf("%s", (ptr == NULL ? "" : ", "));
         }
         }
-        printf(" ]");
+        indent--;
+        Indent(indent);
+        printf("]");
         break;
     }
     }
         break;
     }
     }
-
-    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];
-    }
 }
 
 
 }
 
 
@@ -288,7 +294,7 @@ Draw(void) {
     
     if (g_DrawNode != NULL) {
         JSONNodePrint(g_DrawNode, g_CurrNode);
     
     if (g_DrawNode != NULL) {
         JSONNodePrint(g_DrawNode, g_CurrNode);
-       vt100CursorPos(currV, currH);
+        vt100RestoreCursor();
     }
 }
 
     }
 }
 
@@ -306,117 +312,149 @@ PeekChar() {
     return c;
 }
 
     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);
 
 
-void
-GetStr(JSONNode * node) {
-    node->data = (size_t)NEWARR(char, 16);
-    int strLen = 0;
+bool predStr(char c, int i) { return c >= 'a' && c <= 'z'; }
+bool predInt(char c, int i) { return c >= '0' && c <= '9'; }
 
 
-    char * str = (char *)node->data;
+void
+GetInput(JSONNode * node, char c) {
+    if (node->data == (size_t)NULL) {
+        node->data = (size_t)NEWARR(char, 16);
+    }
     
     
-    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) {
-            str[strLen] = c;
-           strLen++;
-            str[strLen] = '\0';
-        }
+    char * str = (char *)node->data;
+    int strLen = strlen(str);
+
+    if (isBackspace(c)) {
+        str[strLen-1] = '\0';
+    }
+    else if (strLen < 16 - 1) {
+        str[strLen] = c;
+        str[strLen+1] = '\0';
     }
 }
 
     }
 }
 
-void
-GetNode(JSONNode * parent, JSONNode * node) {
-    int c = GetChar();
+JSONNode *
+GetNode() {
+    JSONNode * result = NULL;
+    JSONNode * node = NULL;
+    int c;
 
 
-    JSONNode * result = node;
-    g_CurrNode = node;
+    bool editingNode = false;
 
 
-    if (parent == NULL)
-        g_DrawNode = result;
+    while (true) {
+        Draw();
+        c = getch();
 
 
-    if (parent != NULL && result != NULL)
-        JSONNodePush(parent, result);
+        if (c == KEY_CTRL_C)
+            break;
 
 
-    switch (c) {
-    case 'i': {
-        result->kind = JSONNodeKind_Int;
-       GetInt(result);
-        break;
-    }
-    case 's': {
-        result->kind = JSONNodeKind_Str;
-       GetStr(result);
-        break;
-    }
-    case 'o': {
-        result->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);
+        if (node == NULL) {
+            /**/ 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); editingNode = true; }
+            else if (c == 'a') { node = JSONNodeNew(JSONNodeKind_Arr); editingNode = true; }
+            g_DrawNode = g_CurrNode = result = node;
         }
         }
-       g_CurrNode = result;
-        GetChar();
-        break;
-    }
-    case 'a': {
-        result->kind = JSONNodeKind_Arr;
-        while ((c = PeekChar()), (c != '\r') && (c != '\n')) {
-           JSONNode * newNode = JSONNodeNewNul();
-           g_CurrNode = newNode;
-            JSONNodePush(result, newNode);
-           GetNode(result, newNode);
+        else if (node->kind ==JSONNodeKind_Int) {
+            if (editingNode) {
+                GetInput(node, c);
+            }
+            else {
+                /**/ if (c == 'i' && node->parent != NULL) { node = JSONNodePush(node->parent, JSONNodeNew(JSONNodeKind_Int)); editingNode = true; }
+                else if (c == 's' && node->parent != NULL) { node = JSONNodePush(node->parent, JSONNodeNew(JSONNodeKind_Str)); editingNode = true; }
+                else if (c == 'o' && node->parent != NULL) { node = JSONNodePush(node->parent, JSONNodeNew(JSONNodeKind_Obj)); editingNode = true; }
+                else if (c == 'a' && node->parent != NULL) { node = JSONNodePush(node->parent, JSONNodeNew(JSONNodeKind_Arr)); editingNode = true; }
+                else if (c == 'h') { if (node->prev       != NULL) g_CurrNode = node = node->prev; }
+                else if (c == 'l') { if (node->next       != NULL) g_CurrNode = node = node->next; }
+                else if (c == 'k') { if (node->parent     != NULL) g_CurrNode = node = node->parent; }
+                else if (c == 'j') { if (node->firstChild != NULL) g_CurrNode = node = node->firstChild; }
+                else if (c == ' ') { editingNode = true; }
+                else if (isBackspace(c)) { g_CurrNode = node = JSONNodeRemove(node); }
+
+                g_CurrNode = node;
+            }
+        }
+        else if (node->kind ==JSONNodeKind_Str) {
+            if (editingNode) {
+                GetInput(node, c);
+            }
+            else {
+                /**/ if (c == 'i' && node->parent != NULL) { node = JSONNodePush(node->parent, JSONNodeNew(JSONNodeKind_Int)); editingNode = true; }
+                else if (c == 's' && node->parent != NULL) { node = JSONNodePush(node->parent, JSONNodeNew(JSONNodeKind_Str)); editingNode = true; }
+                else if (c == 'o' && node->parent != NULL) { node = JSONNodePush(node->parent, JSONNodeNew(JSONNodeKind_Obj)); editingNode = true; }
+                else if (c == 'a' && node->parent != NULL) { node = JSONNodePush(node->parent, JSONNodeNew(JSONNodeKind_Arr)); editingNode = true; }
+                else if (c == 'h') { if (node->prev       != NULL) g_CurrNode = node = node->prev; }
+                else if (c == 'l') { if (node->next       != NULL) g_CurrNode = node = node->next; }
+                else if (c == 'k') { if (node->parent     != NULL) g_CurrNode = node = node->parent; }
+                else if (c == 'j') { if (node->firstChild != NULL) g_CurrNode = node = node->firstChild; }
+                else if (c == ' ') { editingNode = true; }
+                else if (isBackspace(c)) { g_CurrNode = node = JSONNodeRemove(node); }
+
+                g_CurrNode = node;
+            }
+        }
+        else if (node->kind ==JSONNodeKind_Obj) {
+            if (editingNode) {
+                GetInput(node, c);
+            }
+            else {
+                /**/ if (c == 'i' && node->parent != NULL) { node = JSONNodePush(node->parent, JSONNodeNew(JSONNodeKind_Int)); editingNode = true; }
+                else if (c == 's' && node->parent != NULL) { node = JSONNodePush(node->parent, JSONNodeNew(JSONNodeKind_Str)); editingNode = true; }
+                else if (c == 'o' && node->parent != NULL) { node = JSONNodePush(node->parent, JSONNodeNew(JSONNodeKind_Obj)); editingNode = true; }
+                else if (c == 'a' && node->parent != NULL) { node = JSONNodePush(node->parent, JSONNodeNew(JSONNodeKind_Arr)); editingNode = true; }
+                else if (c == 'h') { if (node->prev       != NULL) g_CurrNode = node = node->prev; }
+                else if (c == 'l') { if (node->next       != NULL) g_CurrNode = node = node->next; }
+                else if (c == 'k') { if (node->parent     != NULL) g_CurrNode = node = node->parent; }
+                else if (c == 'j') { if (node->firstChild != NULL) g_CurrNode = node = node->firstChild; }
+                else if (c == ' ') { editingNode = true; }
+                else if (isBackspace(c)) { g_CurrNode = node = JSONNodeRemove(node); }
+
+                g_CurrNode = node;
+            }
+        }
+        else if (node->kind ==JSONNodeKind_Key) {
+            if (editingNode) {
+                GetInput(node, c);
+            }
+            else {
+                /**/ if (c == 'i' && node->parent != NULL) { node = JSONNodePush(node->parent, JSONNodeNew(JSONNodeKind_Int)); editingNode = true; }
+                else if (c == 's' && node->parent != NULL) { node = JSONNodePush(node->parent, JSONNodeNew(JSONNodeKind_Str)); editingNode = true; }
+                else if (c == 'o' && node->parent != NULL) { node = JSONNodePush(node->parent, JSONNodeNew(JSONNodeKind_Obj)); editingNode = true; }
+                else if (c == 'a' && node->parent != NULL) { node = JSONNodePush(node->parent, JSONNodeNew(JSONNodeKind_Arr)); editingNode = true; }
+                else if (c == 'h') { if (node->prev       != NULL) g_CurrNode = node = node->prev; }
+                else if (c == 'l') { if (node->next       != NULL) g_CurrNode = node = node->next; }
+                else if (c == 'k') { if (node->parent     != NULL) g_CurrNode = node = node->parent; }
+                else if (c == 'j') { if (node->firstChild != NULL) g_CurrNode = node = node->firstChild; }
+                else if (c == ' ') { editingNode = true; }
+                else if (isBackspace(c)) { g_CurrNode = node = JSONNodeRemove(node); }
+
+                g_CurrNode = node;
+            }
+        }
+        else if (node->kind ==JSONNodeKind_Arr) {
+            if (editingNode) {
+                GetInput(node, c);
+            }
+            else {
+                /**/ if (c == 'i' && node->parent != NULL) { node = JSONNodePush(node->parent, JSONNodeNew(JSONNodeKind_Int)); editingNode = true; }
+                else if (c == 's' && node->parent != NULL) { node = JSONNodePush(node->parent, JSONNodeNew(JSONNodeKind_Str)); editingNode = true; }
+                else if (c == 'o' && node->parent != NULL) { node = JSONNodePush(node->parent, JSONNodeNew(JSONNodeKind_Obj)); editingNode = true; }
+                else if (c == 'a' && node->parent != NULL) { node = JSONNodePush(node->parent, JSONNodeNew(JSONNodeKind_Arr)); editingNode = true; }
+                else if (c == 'h') { if (node->prev       != NULL) g_CurrNode = node = node->prev; }
+                else if (c == 'l') { if (node->next       != NULL) g_CurrNode = node = node->next; }
+                else if (c == 'k') { if (node->parent     != NULL) g_CurrNode = node = node->parent; }
+                else if (c == 'j') { if (node->firstChild != NULL) g_CurrNode = node = node->firstChild; }
+                else if (c == ' ') { editingNode = true; }
+                else if (isBackspace(c)) { g_CurrNode = node = JSONNodeRemove(node); }
+
+                g_CurrNode = node;
+            }
         }
         }
-       g_CurrNode = result;
-        GetChar();
-        break;
-    }
-    /*
-    case 8:
-    case 127:
-        JSONNodePop(parent);
-       JSONNode * newNode = JSONNodeNewNul();
-        GetNode(parent, newNode);
-        break;
-    */
-    case 't':
-        result->kind = JSONNodeKind_Int;
-        result->data = (size_t)GetChar();
-        break;
     }
     }
+
+    return result;
 }
 
 
 }
 
 
@@ -425,20 +463,13 @@ GetNode(JSONNode * parent, JSONNode * node) {
 int main() {
     vt100EnableAlternateBuffer();
 
 int main() {
     vt100EnableAlternateBuffer();
 
-    Draw();
-
-    JSONNode * n = JSONNodeNewNul();
-    GetNode(NULL, n);
-    //JSONNode * n = TestNode();
+    JSONNode * n = GetNode();
+    
+    vt100DisableAlternateBuffer();
 
 
-    vt100ClearScreen();
-    vt100CursorHome();
     JSONNodePrint(n, NULL);
     JSONNodePrint(n, NULL);
-    printf("\n");
 
     // JSONFree(n);
 
     // JSONFree(n);
-    
-    vt100DisableAlternateBuffer();
 
     return 0;
 }
 
     return 0;
 }