75 lines
1.2 KiB
C
75 lines
1.2 KiB
C
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <stdbool.h>
|
|
|
|
const int indent = 2;
|
|
int scope = 0;
|
|
|
|
void print_indent() {
|
|
for (int i = 0; i < indent * scope; i++) {
|
|
putchar(' ');
|
|
}
|
|
}
|
|
|
|
void ln() {
|
|
putchar('\n');
|
|
print_indent();
|
|
}
|
|
|
|
|
|
int main() {
|
|
bool in_string = false;
|
|
bool escape_next = false;
|
|
|
|
char c;
|
|
while (read(STDIN_FILENO, &c, 1) > 0) {
|
|
switch (c) {
|
|
case '\"':
|
|
if (!escape_next) {
|
|
in_string = !in_string;
|
|
}
|
|
|
|
putchar(c);
|
|
break;
|
|
case '\\':
|
|
escape_next = true;
|
|
|
|
putchar(c);
|
|
break;
|
|
case '{':
|
|
case '[':
|
|
putchar(c);
|
|
scope++;
|
|
|
|
ln();
|
|
break;
|
|
case '}':
|
|
case ']':
|
|
scope--;
|
|
ln();
|
|
|
|
putchar(c);
|
|
break;
|
|
case ',':
|
|
putchar(c);
|
|
if (!in_string) {
|
|
ln();
|
|
}
|
|
|
|
break;
|
|
case ':':
|
|
putchar(c);
|
|
if (!in_string) {
|
|
putchar(' ');
|
|
}
|
|
|
|
break;
|
|
default:
|
|
putchar(c);
|
|
break;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|