Skip to content

Commit 1496377

Browse files
author
jvon1904
committed
refactor main.c, add json.c
1 parent d2ca288 commit 1496377

File tree

13 files changed

+529
-322
lines changed

13 files changed

+529
-322
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
build:
2-
clang main.c -o bin/jdif
2+
clang main.c utils.c json.c -o bin/jdif
33

44
binary: build
55
sudo cp bin/jdif /usr/local/bin

json.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include "utils.h"
2+
#include "json.h"
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <string.h>
6+
7+
8+
void formatLineAsJson(Line *lin) {
9+
String key;
10+
String val;
11+
initString(&key);
12+
initString(&val);
13+
14+
int k = 1;
15+
for (int i = 0; i < lin->len; i++) {
16+
int coln = 0;
17+
int end = 0;
18+
char c = lin->text[i];
19+
char next = lin->text[i + 1];
20+
21+
// Terminate key if sequence ": " is encountered
22+
if (k) {
23+
if (c == COLN) {
24+
k = 0;
25+
} else {
26+
appendString(&key, c);
27+
}
28+
} else {
29+
appendString(&val, c);
30+
}
31+
}
32+
33+
assignKey(lin, cloneString(&key));
34+
appendVal(lin, cloneString(&val));
35+
// freeString(&key);
36+
// freeString(&val);
37+
}
38+

json.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef JSON_H
2+
#define JSON_H
3+
4+
void formatLineAsJson(Line *lin);
5+
6+
#endif
7+

main.bak.c

Lines changed: 0 additions & 187 deletions
This file was deleted.

main.c

Lines changed: 68 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "utils.h"
2+
#include "json.h"
23
#include <stdio.h>
4+
#include <unistd.h>
35
#include <stdlib.h>
46
#include <string.h>
57

@@ -33,20 +35,59 @@ int checkArgs(int argc, char *argv[], int *fopt, FILE **fptr) {
3335
return 0;
3436
}
3537

36-
void getLines(int *fopt, FILE *fptr, Document *doc) {
38+
void getBody(int *fopt, FILE *fptr, Document *doc) {
3739
int c; // Parsed input character
40+
41+
while ((c = (*fopt ? fgetc(fptr) : getchar())) != EOF) {
42+
appendString(doc->body, c);
43+
}
44+
}
45+
46+
void getLines(Document *doc) {
3847
Line line;
3948
initLine(&line);
4049

41-
while ((c = (*fopt ? fgetc(fptr) : getchar())) != EOF) {
42-
if (c == CR) {
43-
appendDocumentLine(doc, cloneLine(&line));
44-
freeLine(&line);
45-
initLine(&line);
50+
int go = 1;
51+
52+
for (int i = 0; i < doc->body->len; i++) {
53+
char c = doc->body->text[i];
54+
char next = doc->body->text[i + 1];
55+
char nextnext;
56+
if (i + 2 < doc->body->len) {
57+
nextnext = doc->body->text[i + 2];
4658
} else {
47-
appendLine(&line, c);
59+
nextnext = '\0';
60+
}
61+
62+
if (c == '\\' && next == CR && nextnext == SP) {
63+
go = 0;
64+
} else if (c == CR && next == SP) {
65+
go = 0;
66+
}
67+
68+
69+
if (go) {
70+
if (c == CR) {
71+
appendDocumentLine(doc, cloneLine(&line));
72+
freeLine(&line);
73+
initLine(&line);
74+
} else {
75+
appendLine(&line, c);
76+
}
77+
} else {
78+
if (c != '\\' && c != CR && c != SP) {
79+
appendLine(&line, c);
80+
go = 1;
81+
}
4882
}
4983
}
84+
85+
if (line.len > 0) {
86+
appendDocumentLine(doc, cloneLine(&line));
87+
freeLine(&line);
88+
} else {
89+
freeLine(&line);
90+
}
5091
}
5192

5293
void filterLines(Document *doc, char filtered) {
@@ -65,18 +106,6 @@ void filterLines(Document *doc, char filtered) {
65106
}
66107
}
67108

68-
void formatLines(Document, *doc) {
69-
Key key;
70-
Value value;
71-
for (int i = 0; i < doc->llen; i++) {
72-
appendKey(&key, '"');
73-
for (int j = 0; j < doc->lines[i]->len; j++) {
74-
75-
}
76-
appendKey(&key, '",');
77-
}
78-
}
79-
80109
void getEntries(Document *doc) {
81110
Entry entry;
82111
initEntry(&entry);
@@ -86,9 +115,15 @@ void getEntries(Document *doc) {
86115
freeEntry(&entry);
87116
initEntry(&entry);
88117
} else {
118+
formatLineAsJson(doc->lines[i]);
89119
appendEntry(&entry, doc->lines[i]);
90120
}
91121
}
122+
123+
if (entry.len > 0) {
124+
appendDocumentEntry(doc, cloneEntry(&entry));
125+
freeEntry(&entry);
126+
}
92127
}
93128

94129
void squashEntries(Document *doc) {
@@ -112,7 +147,13 @@ void printJSON(Document *doc) {
112147
for (int i = 0; i < doc->elen; i++) {
113148
printf(IDNT2 "{\n");
114149
for (int j = 0; j < doc->entries[i]->len; j++) {
115-
printf(IDNT4 "%s\n", doc->entries[i]->lines[j]->text);
150+
char *closing;
151+
if (j == doc->entries[i]->len - 1) {
152+
closing = "\n";
153+
} else {
154+
closing = ",\n";
155+
}
156+
printf(IDNT4 "\"%s\": \"%s\"%s", doc->entries[i]->lines[j]->key->text, doc->entries[i]->lines[j]->vals[0]->text, closing);
116157
}
117158
char *closing;
118159
if (i == doc->elen - 1) {
@@ -136,9 +177,14 @@ int main(int argc, char *argv[]) {
136177
return 0;
137178
}
138179

139-
getLines(&fopt, fptr, &doc); // Gather input data by lines
180+
if (!fopt && isatty(STDIN_FILENO)) {
181+
printf("No input provided.\n");
182+
return 0;
183+
}
184+
185+
getBody(&fopt, fptr, &doc); // Gather input data by lines
186+
getLines(&doc); // Gather input data by lines
140187
filterLines(&doc, '#'); // Remove all comment lines that start with # character
141-
formatLines(&doc); // Format each line into valid JSON
142188
getEntries(&doc); // Gather lines separated by NLs into entries
143189
squashEntries(&doc); // Remove all empty entries
144190
printJSON(&doc); // Output formatted JSON

0 commit comments

Comments
 (0)