X-Git-Url: https://gitweb.ps.run/iftint/blobdiff_plain/4c67fbe5877ad07a0cfb613c161aa8526970ea7f..7915eed596265bb03d9fc01eec6dd39015b01188:/main2.c diff --git a/main2.c b/main2.c index 51c5ad4..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 ); @@ -91,15 +79,17 @@ 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) { - *v = *h = 0; - vt100CursorPos(1000000, 1000000); - printf("\033[6n"); - getch(); getch(); - int c; - while ((c = getch()) != ';') - *v = (10*(*v)+(c-'0')); - while ((c = getch()) != 'R') - *h = (10*(*h)+(c-'0')); +#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 } @@ -289,40 +279,43 @@ GetChar() { int PeekChar() { - int c = GetChar(); - ungetch(c); + Draw(); + int c = peekch(); return c; } -int -GetInt() { - static char intStr[16]; - intStr[0] = '\0'; +void +GetInt(JSONNode * node) { + char intStr[16] = ""; int intStrLen = 0; - int result = 0; + + size_t * i = &node->data; + int c; g_DrawStr = intStr; 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'; - 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; + + char * str = (char *)node->data; + int c; g_DrawStr = str; while ((c = GetChar()), (c != '\r') && (c != '\n')) { @@ -336,15 +329,14 @@ GetStr() { } } g_DrawStr = ""; - return str; } -JSONNode * -GetNode(JSONNode * parent) { +void +GetNode(JSONNode * parent, JSONNode * node) { int c = GetChar(); - JSONNode * result = JSONNodeNewNul(); - + JSONNode * result = node; + if (parent == NULL) g_DrawNode = result; @@ -354,44 +346,53 @@ GetNode(JSONNode * parent) { switch (c) { case 'i': { result->kind = JSONNodeKind_Int; - result->data = (size_t)GetInt(); + GetInt(result); break; } case 's': { result->kind = JSONNodeKind_Str; - result->data = (size_t)GetStr(); + GetStr(result); 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(""); + JSONNodePush(result, newNode); + GetStr(newNode); + + newNode = JSONNodeNewNul(); + JSONNodePush(result, newNode); + GetNode(result, newNode); } - getch(); + GetChar(); 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(); + JSONNodePush(result, newNode); + GetNode(result, newNode); } - getch(); + GetChar(); break; } + /* case 8: case 127: JSONNodePop(parent); - result = GetNode(parent); + JSONNode * newNode = JSONNodeNewNul(); + GetNode(parent, newNode); break; + */ case 't': result->kind = JSONNodeKind_Int; result->data = (size_t)GetChar(); break; } - - return result; } @@ -400,7 +401,8 @@ GetNode(JSONNode * parent) { int main() { Draw(); - JSONNode * n = GetNode(NULL); + JSONNode * n = JSONNodeNewNul(); + GetNode(NULL, n); //JSONNode * n = TestNode(); vt100ClearScreen();