X-Git-Url: https://gitweb.ps.run/iftint/blobdiff_plain/5ae0aff8511d9e55d25b35f2f9c3e2c70f5c6178..7915eed596265bb03d9fc01eec6dd39015b01188:/main2.c diff --git a/main2.c b/main2.c index 5864bc8..96aba10 100644 --- a/main2.c +++ b/main2.c @@ -14,11 +14,13 @@ #define NEWARR(TYPE, NUM) ((TYPE *)calloc(NUM, sizeof(TYPE))) -// getch() +// getch #ifdef _WIN32 +#include #include #else +#include #include #include #include @@ -30,21 +32,7 @@ int getch(void) 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 ); @@ -90,6 +78,19 @@ void vt100CursorHome() { vt100Escape("[H"); } 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) { +#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 +} // JSON @@ -250,73 +251,94 @@ JSONNodePrint(JSONNode * node) { // Input -JSONNode * g_Node; +JSONNode * g_DrawNode = NULL; +const char * g_DrawStr = ""; void -Draw(JSONNode * node, const char * str) { +Draw(void) { vt100ClearScreen(); vt100CursorHome(); - printf("> %s\n\n", str); + if (g_DrawNode != NULL) + JSONNodePrint(g_DrawNode); - if (node != NULL) - JSONNodePrint(node); + int v, h; + vt100GetScreenSize(&v, &h); + vt100CursorPos(v, 0); + printf("> %s", g_DrawStr); - vt100CursorPos(0, strlen(str) + 3); + vt100CursorPos(v, strlen(g_DrawStr) + 3); } int -GetInt() { - static char intStr[16]; +GetChar() { + Draw(); + int c = getch(); + return c; +} + +int +PeekChar() { + Draw(); + int c = peekch(); + return c; +} + +void +GetInt(JSONNode * node) { + char intStr[16] = ""; int intStrLen = 0; - int result = 0; + + size_t * i = &node->data; + int c; - while ((c = getch()), (c != '\r') && (c != '\n')) { - if (c == 8 && intStrLen > 0) { + g_DrawStr = intStr; + while ((c = GetChar()), (c != '\r') && (c != '\n')) { + if ((c == 8 || c == 127) && intStrLen > 0) { intStrLen--; intStr[intStrLen] = '\0'; - result /= 10; - Draw(g_Node, intStr); + *i /= 10; } else if (intStrLen < 16 - 1 && (c >= '0' && c <= '9')) { intStr[intStrLen++] = c; intStr[intStrLen] = '\0'; - result *= 10; - result += c - '0'; - Draw(g_Node, intStr); + *i *= 10; + *i += c - '0'; } } - return result; + g_DrawStr = ""; } -char * -GetStr() { - char * str = NEWARR(char, 16); +void +GetStr(JSONNode * node) { + node->data = (size_t)NEWARR(char, 16); int strLen = 0; + + char * str = (char *)node->data; + int c; - while ((c = getch()), (c != '\r') && (c != '\n')) { - if (c == 8 && strLen > 0) { + g_DrawStr = str; + while ((c = GetChar()), (c != '\r') && (c != '\n')) { + if ((c == 8 || c == 127) && 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); } } - return str; + g_DrawStr = ""; } -JSONNode * -GetNode(JSONNode * parent) { - int c = getch(); +void +GetNode(JSONNode * parent, JSONNode * node) { + int c = GetChar(); + + JSONNode * result = node; - JSONNode * result = JSONNodeNewNul(); - if (parent == NULL) - g_Node = result; + g_DrawNode = result; if (parent != NULL && result != NULL) JSONNodePush(parent, result); @@ -324,89 +346,63 @@ GetNode(JSONNode * parent) { switch (c) { case 'i': { result->kind = JSONNodeKind_Int; - Draw(g_Node, ""); - result->data = (size_t)GetInt(); - Draw(g_Node, ""); + GetInt(result); break; } case 's': { result->kind = JSONNodeKind_Str; - Draw(g_Node, ""); - result->data = (size_t)GetStr(); - Draw(g_Node, ""); + GetStr(result); break; } case 'o': { result->kind = JSONNodeKind_Obj; - Draw(g_Node, ""); - while ((c = peekch()), (c != '\r') && (c != '\n')) { - Draw(g_Node, ""); - - JSONNodePush(result, JSONNodeNewStr(GetStr())); - Draw(g_Node, ""); - - JSONNodePush(result, GetNode(result)); - Draw(g_Node, ""); + while ((c = PeekChar()), (c != '\r') && (c != '\n')) { + JSONNode * newNode; + + newNode = JSONNodeNewStr(""); + JSONNodePush(result, newNode); + GetStr(newNode); + + newNode = JSONNodeNewNul(); + JSONNodePush(result, newNode); + GetNode(result, newNode); } - getch(); + GetChar(); break; } case 'a': { result->kind = JSONNodeKind_Arr; - Draw(g_Node, ""); - while ((c = peekch()), (c != '\r') && (c != '\n')) { - Draw(g_Node, ""); - - JSONNodePush(result, GetNode(result)); - Draw(g_Node, ""); + while ((c = PeekChar()), (c != '\r') && (c != '\n')) { + JSONNode * newNode = JSONNodeNewNul(); + JSONNodePush(result, newNode); + GetNode(result, newNode); } - getch(); + GetChar(); break; } + /* case 8: - //case 127: + case 127: JSONNodePop(parent); - Draw(g_Node, ""); - result = GetNode(parent); + JSONNode * newNode = JSONNodeNewNul(); + GetNode(parent, newNode); break; + */ case 't': result->kind = JSONNodeKind_Int; - result->data = (size_t)getch(); + result->data = (size_t)GetChar(); break; } - - return result; } -JSONNode * -TestNode() { - JSONNode * n = JSONNodeNewObj(); - JSONNode * k1 = JSONNodeNewStr("key 1"); - JSONNode * v1 = JSONNodeNewObj(); - JSONNode * k11 = JSONNodeNewStr("key 11"); - JSONNode * v11 = JSONNodeNewStr("val 11"); - JSONNode * k12 = JSONNodeNewStr("key 12"); - JSONNode * v12 = JSONNodeNewStr("val 12"); - JSONNode * k2 = JSONNodeNewStr("key 2"); - JSONNode * v2 = JSONNodeNewStr("val 2"); - - JSONNodePush(n, k1); - JSONNodePush(n, v1); - JSONNodePush(n, k2); - JSONNodePush(n, v2); - - JSONNodePush(v1, k11); - JSONNodePush(v1, v11); - JSONNodePush(v1, k12); - JSONNodePush(v1, v12); - - return n; -} + + int main() { - Draw(NULL, ""); + Draw(); - JSONNode * n = GetNode(NULL); + JSONNode * n = JSONNodeNewNul(); + GetNode(NULL, n); //JSONNode * n = TestNode(); vt100ClearScreen(); @@ -417,4 +413,4 @@ int main() { // JSONFree(n); return 0; -} \ No newline at end of file +}