]> gitweb.ps.run Git - iftint/blobdiff - main2.c
alternate buffergcc main2.c -o main2 && ./main2!
[iftint] / main2.c
diff --git a/main2.c b/main2.c
index 51c5ad4975c359f22dddb65beb005f4edc178ff1..a3223baa1a3fb4379066c0e9e30e898605ef0786 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)))
 
 
-// getch()
+// getch
 
 #ifdef _WIN32
 
 #ifdef _WIN32
+#include <windows.h>
 #include <conio.h>
 #else
 #include <conio.h>
 #else
+#include <sys/ioctl.h>
 #include <termios.h>
 #include <unistd.h>
 #include <stdio.h>
 #include <termios.h>
 #include <unistd.h>
 #include <stdio.h>
@@ -30,21 +32,7 @@ int getch(void)
     int ch;
     tcgetattr( STDIN_FILENO, &oldattr );
     newattr = oldattr;
     int ch;
     tcgetattr( STDIN_FILENO, &oldattr );
     newattr = oldattr;
-    newattr.c_lflag &= ~( ICANON | ECHO );
-    tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
-    ch = getchar();
-    tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
-    return ch;
-}
-
-/* reads from keypress, echoes */
-int getche(void)
-{
-    struct termios oldattr, newattr;
-    int ch;
-    tcgetattr( STDIN_FILENO, &oldattr );
-    newattr = oldattr;
-    newattr.c_lflag &= ~( ICANON );
+    newattr.c_lflag &= ~( ICANON | ECHO ); // no ECHO for echo(?)
     tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
     ch = getchar();
     tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
     tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
     ch = getchar();
     tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
@@ -90,17 +78,31 @@ 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 vt100GetScreenSize(int * v, int * h) {
+void vt100GetCursor(int * v, int * h) {
     *v = *h = 0;
     *v = *h = 0;
-    vt100CursorPos(1000000, 1000000);
     printf("\033[6n");
     getch(); getch();
     int c;
     while ((c = getch()) != ';')
     printf("\033[6n");
     getch(); getch();
     int c;
     while ((c = getch()) != ';')
-        *v = (10*(*v)+(c-'0'));
+       *v = (10*(*v)+(c-'0'));
     while ((c = getch()) != 'R')
     while ((c = getch()) != 'R')
-        *h = (10*(*h)+(c-'0'));
+       *h = (10*(*h)+(c-'0'));
+}
+void vt100GetScreenSize(int * v, int * h) {
+#ifdef _WIN32
+    CONSOLE_SCREEN_BUFFER_INFO csbi;
+    GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
+    *h = csbi.srWindow.Right - csbi.srWindow.Left + 1;
+    *v = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
+#else
+    struct winsize w;
+    ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
+    *h = w.ws_row;
+    *v = w.ws_col;
+#endif
 }
 }
+void vt100EnableAlternateBuffer() { vt100Escape("[?1049h"); }
+void vt100DisableAlternateBuffer() { vt100Escape("[?1049l"); }
 
 
 // JSON
 
 
 // JSON
@@ -111,6 +113,7 @@ typedef enum {
     JSONNodeKind_Str,
     JSONNodeKind_Obj,
     JSONNodeKind_Arr,
     JSONNodeKind_Str,
     JSONNodeKind_Obj,
     JSONNodeKind_Arr,
+    JSONNodeKind_COUNT
 } JSONNodeKind;
 
 struct JSONNode;
 } JSONNodeKind;
 
 struct JSONNode;
@@ -198,15 +201,18 @@ Indent(int indent) {
         printf("  ");
 }
 
         printf("  ");
 }
 
+static int currV = 0;
+static int currH = 0;
+
 void
 void
-JSONNodePrint(JSONNode * node) {
+JSONNodePrint(JSONNode * node, JSONNode * currNode) {
     if (node == NULL)
         return;
     
     static int indent;
     if (node->parent == NULL)
         indent = 0;
     if (node == NULL)
         return;
     
     static int indent;
     if (node->parent == NULL)
         indent = 0;
-    
+
     switch (node->kind) {
     case JSONNodeKind_Nul: {
         printf("null");
     switch (node->kind) {
     case JSONNodeKind_Nul: {
         printf("null");
@@ -231,7 +237,7 @@ JSONNodePrint(JSONNode * node) {
             JSONNode * value = ptr->next;
             Indent(indent);
             printf("\"%s\": ", key);
             JSONNode * value = ptr->next;
             Indent(indent);
             printf("\"%s\": ", key);
-            JSONNodePrint(value);
+            JSONNodePrint(value, currNode);
             if (ptr->next != NULL)
                 ptr = ptr->next->next;
             else
             if (ptr->next != NULL)
                 ptr = ptr->next->next;
             else
@@ -248,7 +254,7 @@ JSONNodePrint(JSONNode * node) {
         JSONNode * ptr = node->children;
         while (ptr != NULL) {
             JSONNode * value = ptr;
         JSONNode * ptr = node->children;
         while (ptr != NULL) {
             JSONNode * value = ptr;
-            JSONNodePrint(value);
+            JSONNodePrint(value, currNode);
             ptr = ptr->next;
             printf("%s", (ptr == NULL ? "" : ", "));
         }
             ptr = ptr->next;
             printf("%s", (ptr == NULL ? "" : ", "));
         }
@@ -256,28 +262,34 @@ JSONNodePrint(JSONNode * node) {
         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];
+    }
 }
 
 
 // Input
 
 JSONNode * g_DrawNode = NULL;
 }
 
 
 // Input
 
 JSONNode * g_DrawNode = NULL;
-const char * g_DrawStr = "";
+JSONNode * g_CurrNode = NULL;
 
 void
 Draw(void) {
     vt100ClearScreen();
     vt100CursorHome();
     
 
 void
 Draw(void) {
     vt100ClearScreen();
     vt100CursorHome();
     
-    if (g_DrawNode != NULL)
-        JSONNodePrint(g_DrawNode);
-
-    int v, h;
-    vt100GetScreenSize(&v, &h);
-    vt100CursorPos(v, 0);
-    printf("> %s", g_DrawStr);
-
-    vt100CursorPos(v, strlen(g_DrawStr) + 3);
+    if (g_DrawNode != NULL) {
+        JSONNodePrint(g_DrawNode, g_CurrNode);
+       vt100CursorPos(currV, currH);
+    }
 }
 
 int
 }
 
 int
@@ -289,62 +301,62 @@ GetChar() {
 
 int
 PeekChar() {
 
 int
 PeekChar() {
-    int c = GetChar();
-    ungetch(c);
+    Draw();
+    int c = peekch();
     return c;
 }
 
     return c;
 }
 
-int
-GetInt() {
-    static char intStr[16];
-    intStr[0] = '\0';
+void
+GetInt(JSONNode * node) {
+    char intStr[16] = "";
     int intStrLen = 0;
     int intStrLen = 0;
-    int result = 0;
+    
+    size_t * i = &node->data;
+    
     int c;
     int c;
-    g_DrawStr = intStr;
     while ((c = GetChar()), (c != '\r') && (c != '\n')) {
         if ((c == 8 || c == 127) && intStrLen > 0) {
             intStrLen--;
             intStr[intStrLen] = '\0';
     while ((c = GetChar()), (c != '\r') && (c != '\n')) {
         if ((c == 8 || c == 127) && intStrLen > 0) {
             intStrLen--;
             intStr[intStrLen] = '\0';
-            result /= 10;
+            *i /= 10;
         }
         else if (intStrLen < 16 - 1 && (c >= '0' && c <= '9')) {
             intStr[intStrLen++] = c;
             intStr[intStrLen] = '\0';
         }
         else if (intStrLen < 16 - 1 && (c >= '0' && c <= '9')) {
             intStr[intStrLen++] = c;
             intStr[intStrLen] = '\0';
-            result *= 10;
-            result += c - '0';
+            *i *= 10;
+            *i += c - '0';
         }
     }
         }
     }
-    g_DrawStr = "";
-    return result;
 }
 
 }
 
-char *
-GetStr() {
-    char * str = NEWARR(char, 16);
+void
+GetStr(JSONNode * node) {
+    node->data = (size_t)NEWARR(char, 16);
     int strLen = 0;
     int strLen = 0;
+
+    char * str = (char *)node->data;
+    
     int c;
     int c;
-    g_DrawStr = str;
     while ((c = GetChar()), (c != '\r') && (c != '\n')) {
         if ((c == 8 || c == 127) && strLen > 0) {
             strLen--;
             str[strLen] = '\0';
         }
         else if (strLen < 16 - 1) {
     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;
+            str[strLen] = c;
+           strLen++;
             str[strLen] = '\0';
         }
     }
             str[strLen] = '\0';
         }
     }
-    g_DrawStr = "";
-    return str;
 }
 
 }
 
-JSONNode *
-GetNode(JSONNode * parent) {
+void
+GetNode(JSONNode * parent, JSONNode * node) {
     int c = GetChar();
 
     int c = GetChar();
 
-    JSONNode * result = JSONNodeNewNul();
-    
+    JSONNode * result = node;
+    g_CurrNode = node;
+
     if (parent == NULL)
         g_DrawNode = result;
 
     if (parent == NULL)
         g_DrawNode = result;
 
@@ -354,61 +366,79 @@ GetNode(JSONNode * parent) {
     switch (c) {
     case 'i': {
         result->kind = JSONNodeKind_Int;
     switch (c) {
     case 'i': {
         result->kind = JSONNodeKind_Int;
-        result->data = (size_t)GetInt();
+       GetInt(result);
         break;
     }
     case 's': {
         result->kind = JSONNodeKind_Str;
         break;
     }
     case 's': {
         result->kind = JSONNodeKind_Str;
-        result->data = (size_t)GetStr();
+       GetStr(result);
         break;
     }
     case 'o': {
         result->kind = JSONNodeKind_Obj;
         break;
     }
     case 'o': {
         result->kind = JSONNodeKind_Obj;
-        while ((c = peekch()), (c != '\r') && (c != '\n')) {
-            JSONNodePush(result, JSONNodeNewStr(GetStr()));
-
-            JSONNodePush(result, GetNode(result));
+        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);
         }
         }
-        getch();
+       g_CurrNode = result;
+        GetChar();
         break;
     }
     case 'a': {
         result->kind = JSONNodeKind_Arr;
         break;
     }
     case 'a': {
         result->kind = JSONNodeKind_Arr;
-        while ((c = peekch()), (c != '\r') && (c != '\n')) {
-            JSONNodePush(result, GetNode(result));
+        while ((c = PeekChar()), (c != '\r') && (c != '\n')) {
+           JSONNode * newNode = JSONNodeNewNul();
+           g_CurrNode = newNode;
+            JSONNodePush(result, newNode);
+           GetNode(result, newNode);
         }
         }
-        getch();
+       g_CurrNode = result;
+        GetChar();
         break;
     }
         break;
     }
+    /*
     case 8:
     case 127:
         JSONNodePop(parent);
     case 8:
     case 127:
         JSONNodePop(parent);
-        result = GetNode(parent);
+       JSONNode * newNode = JSONNodeNewNul();
+        GetNode(parent, newNode);
         break;
         break;
+    */
     case 't':
         result->kind = JSONNodeKind_Int;
         result->data = (size_t)GetChar();
         break;
     }
     case 't':
         result->kind = JSONNodeKind_Int;
         result->data = (size_t)GetChar();
         break;
     }
-
-    return result;
 }
 
 
 
 
 int main() {
 }
 
 
 
 
 int main() {
+    vt100EnableAlternateBuffer();
+
     Draw();
 
     Draw();
 
-    JSONNode * n = GetNode(NULL);
+    JSONNode * n = JSONNodeNewNul();
+    GetNode(NULL, n);
     //JSONNode * n = TestNode();
 
     vt100ClearScreen();
     vt100CursorHome();
     //JSONNode * n = TestNode();
 
     vt100ClearScreen();
     vt100CursorHome();
-    JSONNodePrint(n);
+    JSONNodePrint(n, NULL);
     printf("\n");
 
     // JSONFree(n);
     printf("\n");
 
     // JSONFree(n);
+    
+    vt100DisableAlternateBuffer();
 
     return 0;
 }
 
     return 0;
 }