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 vt100GetScreenSize(int * v, int * h) {
#ifdef _WIN32
CONSOLE_SCREEN_BUFFER_INFO csbi;
*v = w.ws_col;
#endif
}
+void vt100EnableAlternateBuffer() { vt100Escape("[?1049h"); }
+void vt100DisableAlternateBuffer() { vt100Escape("[?1049l"); }
// JSON
JSONNodeKind_Str,
JSONNodeKind_Obj,
JSONNodeKind_Arr,
+ JSONNodeKind_COUNT
} JSONNodeKind;
struct JSONNode;
}
void
-JSONNodePrint(JSONNode * node) {
+JSONNodePrint(JSONNode * node, JSONNode * currNode) {
if (node == NULL)
return;
static int indent;
if (node->parent == NULL)
indent = 0;
-
+
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: {
JSONNode * ptr = node->children;
indent++;
while (ptr != NULL) {
- char * key = (char *)ptr->data;
+ JSONNode * key = ptr;
JSONNode * value = ptr->next;
Indent(indent);
- printf("\"%s\": ", key);
- JSONNodePrint(value);
+ JSONNodePrint(key, currNode);
+ printf(": ");
+ JSONNodePrint(value, currNode);
if (ptr->next != NULL)
ptr = ptr->next->next;
else
JSONNode * ptr = node->children;
while (ptr != NULL) {
JSONNode * value = ptr;
- JSONNodePrint(value);
+ JSONNodePrint(value, currNode);
ptr = ptr->next;
printf("%s", (ptr == NULL ? "" : ", "));
}
break;
}
}
+
+ if (currNode == node) {
+ vt100SaveCursor();
+ }
}
// Input
JSONNode * g_DrawNode = NULL;
-const char * g_DrawStr = "";
+JSONNode * g_CurrNode = NULL;
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);
+ vt100RestoreCursor();
+ }
}
int
return c;
}
-int
-GetInt() {
- static char intStr[16];
- intStr[0] = '\0';
- int intStrLen = 0;
- int result = 0;
- 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;
- }
- else if (intStrLen < 16 - 1 && (c >= '0' && c <= '9')) {
- intStr[intStrLen++] = c;
- intStr[intStrLen] = '\0';
- result *= 10;
- result += c - '0';
- }
- }
- g_DrawStr = "";
- return result;
-}
+typedef bool(*CharPredicateFunc)(char, int);
-char *
-GetStr() {
- char * str = NEWARR(char, 16);
+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);
int strLen = 0;
+
+ char * str = (char *)node->data;
+
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) {
- str[strLen++] = c;
+ else if (strLen < 16 - 1 && charPredicate(c, strLen)) {
+ str[strLen] = c;
+ strLen++;
str[strLen] = '\0';
}
}
- g_DrawStr = "";
- return str;
}
-JSONNode *
-GetNode(JSONNode * parent) {
+void
+GetNode(JSONNode * parent, JSONNode * node) {
int c = GetChar();
- JSONNode * result = JSONNodeNewNul();
-
+ g_CurrNode = node;
+
if (parent == NULL)
- g_DrawNode = result;
+ g_DrawNode = node;
- if (parent != NULL && result != NULL)
- JSONNodePush(parent, result);
+ if (parent != NULL && node != NULL)
+ JSONNodePush(parent, node);
switch (c) {
case 'i': {
- result->kind = JSONNodeKind_Int;
- result->data = (size_t)GetInt();
+ node->kind = JSONNodeKind_Int;
+ GetStr(node, predInt);
break;
}
case 's': {
- result->kind = JSONNodeKind_Str;
- result->data = (size_t)GetStr();
+ node->kind = JSONNodeKind_Str;
+ GetStr(node, predStr);
break;
}
case 'o': {
- result->kind = JSONNodeKind_Obj;
+ node->kind = JSONNodeKind_Obj;
while ((c = PeekChar()), (c != '\r') && (c != '\n')) {
- JSONNodePush(result, JSONNodeNewStr(GetStr()));
-
- JSONNodePush(result, GetNode(result));
+ JSONNode * newNode;
+
+ newNode = JSONNodeNewStr("");
+ g_CurrNode = newNode;
+ JSONNodePush(node, newNode);
+ GetStr(newNode, predStr);
+
+ newNode = JSONNodeNewNul();
+ JSONNodePush(node, newNode);
+ GetNode(node, newNode);
}
- GetChar();
+ g_CurrNode = node;
+ getch();
break;
}
case 'a': {
- result->kind = JSONNodeKind_Arr;
+ node->kind = JSONNodeKind_Arr;
while ((c = PeekChar()), (c != '\r') && (c != '\n')) {
- JSONNodePush(result, GetNode(result));
+ JSONNode * newNode;
+
+ newNode = JSONNodeNewNul();
+ //g_CurrNode = newNode;
+ JSONNodePush(node, newNode);
+ GetNode(node, newNode);
}
- GetChar();
+ g_CurrNode = node;
+ getch();
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();
+ node->kind = JSONNodeKind_Int;
+ node->data = (size_t)GetChar();
break;
}
-
- return result;
}
int main() {
+ vt100EnableAlternateBuffer();
+
Draw();
- JSONNode * n = GetNode(NULL);
+ JSONNode * n = JSONNodeNewNul();
+ GetNode(NULL, n);
//JSONNode * n = TestNode();
vt100ClearScreen();
vt100CursorHome();
- JSONNodePrint(n);
+ JSONNodePrint(n, NULL);
printf("\n");
// JSONFree(n);
+
+ vt100DisableAlternateBuffer();
return 0;
}