treesummaryrefslogcommitdiff
path: root/main2.c
blob: 5ea32ef8788d035a48587bec17b1a37c6812bc76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

/* TODO
- whitelist input on GetStr/GetInt
*/

// Memory

#define NEW(TYPE) ((TYPE *)calloc(1, sizeof(TYPE)))
#define NEWARR(TYPE, NUM) ((TYPE *)calloc(NUM, sizeof(TYPE)))


// getch()

#ifdef _WIN32
#include <windows.h>
#include <conio.h>
#else
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
#include <stdio.h>

/* reads from keypress, doesn't echo */
int getch(void)
{
    struct termios oldattr, newattr;
    int ch;
    tcgetattr( STDIN_FILENO, &oldattr );
    newattr = oldattr;
    newattr.c_lflag &= ~( ICANON | ECHO ); // no ECHO for echo(?)
    tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
    ch = getchar();
    tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
    return ch;
}

/* ungets keypress */
void ungetch(int ch)
{
    struct termios oldattr, newattr;
    tcgetattr( STDIN_FILENO, &oldattr );
    newattr = oldattr;
    newattr.c_lflag &= ~( ICANON | ECHO );
    tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
    ungetc(ch, stdin);
    tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
}
#endif

int
peekch() {
    int c = getch();
    //ungetc(c, stdin);
    ungetch(c);
    return c;
}


// VT100

#define ASCII_ESC 27

void vt100Escape(const char * str, ...) {
    va_list args;
    va_start(args, str);

    printf("%c", ASCII_ESC);
    vprintf(str, args);
}

void vt100ClearScreen() { vt100Escape("[2J"); }
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

typedef enum {
    JSONNodeKind_Nul,
    JSONNodeKind_Int,
    JSONNodeKind_Str,
    JSONNodeKind_Obj,
    JSONNodeKind_Arr,
} JSONNodeKind;

struct JSONNode;
typedef struct JSONNode {
    JSONNodeKind kind;
    size_t data;
    struct JSONNode * parent;
    struct JSONNode * children;
    struct JSONNode * next;
} JSONNode;

JSONNode *
JSONNodeNew(JSONNodeKind kind, size_t data) {
    JSONNode * result = NEW(JSONNode);
    result->kind = kind;
    result->data = data;
    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) {
    if (this->children == NULL) {
        this->children = that;
    }
    else {
        JSONNode * lastNode = this->children;
        while (lastNode->next != NULL)
            lastNode = lastNode->next;
        lastNode->next = that;
    }
    that->parent = this;
    that->next = NULL;
    
    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;
        }
    }
}

void
Indent(int indent) {
    for (int i = 0; i < indent; i++)
        printf("  ");
}

void
JSONNodePrint(JSONNode * node) {
    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);
        break;
    }
    case JSONNodeKind_Str: {
        char * str = (char *)node->data;
        printf("\"%s\"", str == NULL ? "" : str);
        break;
    }
    case JSONNodeKind_Obj: {
        printf("{\n");
        JSONNode * ptr = node->children;
        indent++;
        while (ptr != NULL) {
            char * key = (char *)ptr->data;
            JSONNode * value = ptr->next;
            Indent(indent);
            printf("\"%s\": ", key);
            JSONNodePrint(value);
            if (ptr->next != NULL)
                ptr = ptr->next->next;
            else
                ptr = NULL;
            printf("%s\n", (ptr == NULL ? "" : ","));
        }
        indent--;
        Indent(indent);
        printf("}");
        break;
    }
    case JSONNodeKind_Arr: {
        printf("[ ");
        JSONNode * ptr = node->children;
        while (ptr != NULL) {
            JSONNode * value = ptr;
            JSONNodePrint(value);
            ptr = ptr->next;
            printf("%s", (ptr == NULL ? "" : ", "));
        }
        printf(" ]");
        break;
    }
    }
}


// Input

JSONNode * g_DrawNode = NULL;
const char * g_DrawStr = "";

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);
}

int
GetChar() {
    Draw();
    int c = getch();
    return c;
}

int
PeekChar() {
    int c = GetChar();
    ungetch(c);
    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;
}

char *
GetStr() {
    char * str = NEWARR(char, 16);
    int strLen = 0;
    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;
            str[strLen] = '\0';
        }
    }
    g_DrawStr = "";
    return str;
}

JSONNode *
GetNode(JSONNode * parent) {
    int c = GetChar();

    JSONNode * result = JSONNodeNewNul();
    
    if (parent == NULL)
        g_DrawNode = result;

    if (parent != NULL && result != NULL)
        JSONNodePush(parent, result);

    switch (c) {
    case 'i': {
        result->kind = JSONNodeKind_Int;
        result->data = (size_t)GetInt();
        break;
    }
    case 's': {
        result->kind = JSONNodeKind_Str;
        result->data = (size_t)GetStr();
        break;
    }
    case 'o': {
        result->kind = JSONNodeKind_Obj;
        while ((c = peekch()), (c != '\r') && (c != '\n')) {
            JSONNodePush(result, JSONNodeNewStr(GetStr()));

            JSONNodePush(result, GetNode(result));
        }
        getch();
        break;
    }
    case 'a': {
        result->kind = JSONNodeKind_Arr;
        while ((c = peekch()), (c != '\r') && (c != '\n')) {
            JSONNodePush(result, GetNode(result));
        }
        getch();
        break;
    }
    case 8:
    case 127:
        JSONNodePop(parent);
        result = GetNode(parent);
        break;
    case 't':
        result->kind = JSONNodeKind_Int;
        result->data = (size_t)GetChar();
        break;
    }

    return result;
}




int main() {
    Draw();

    JSONNode * n = GetNode(NULL);
    //JSONNode * n = TestNode();

    vt100ClearScreen();
    vt100CursorHome();
    JSONNodePrint(n);
    printf("\n");

    // JSONFree(n);

    return 0;
}