Skip to content

Commit e558668

Browse files
author
jvon1904
committed
Handle array values
1 parent 72982ba commit e558668

File tree

14 files changed

+806
-159
lines changed

14 files changed

+806
-159
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"C_Cpp.errorSquiggles": "disabled"
3+
}

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

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Convert LDIF (LDAP Data Interchange Format) to JSON.
66

77
To compile run `make`.
88

9-
To test and example run `make example`.
9+
To test an example run `make example`.
1010

1111
## Usage
1212

json.c

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
// Return early if already formatted
10+
if (lin->vlen > 0) return;
11+
12+
// Initialize key and val strings
13+
String key;
14+
String val;
15+
initString(&key);
16+
initString(&val);
17+
18+
// Set flags
19+
int k = 1; // parsing key, initialize as true
20+
int colon = 0; // colon encountered
21+
22+
// Iterate over each char of the line text
23+
for (int i = 0; i < lin->len; i++) {
24+
char c = lin->text[i];
25+
if (k) {
26+
// Handle colons
27+
if (c == COLN) {
28+
// First colon
29+
if (!colon) {
30+
colon = 1;
31+
32+
// double colon means base64 (::)
33+
if (i + 1 < lin->len && lin->text[i + 1] == COLN) {
34+
i++;
35+
}
36+
37+
// optional space after colon(s)
38+
if (i + 1 < lin->len && lin->text[i + 1] == SP) {
39+
i++;
40+
}
41+
42+
k = 0; // switch to value
43+
} else {
44+
appendString(&key, c); // stray colon inside key (unlikely)
45+
}
46+
} else {
47+
appendString(&key, c);
48+
}
49+
} else {
50+
appendString(&val, c);
51+
}
52+
}
53+
54+
// Assign key and value to line
55+
assignKey(lin, &key);
56+
appendVal(lin, &val);
57+
58+
// Cleanup temporary strings
59+
freeString(&key);
60+
freeString(&val);
61+
}
62+
63+
void printJSON(Document *doc) {
64+
// Output JSON array start
65+
printf("[\n");
66+
67+
// Iterate over each entry in document
68+
for (int i = 0; i < doc->elen; i++) {
69+
printf(IDNT2);
70+
printf("{\n");
71+
72+
// Iterate over each line in entry
73+
for (int j = 0; j < doc->entries[i]->len; j++) {
74+
char *closing;
75+
if (j == doc->entries[i]->len - 1) {
76+
closing = "\n";
77+
} else {
78+
closing = ",\n";
79+
}
80+
printf(IDNT4);
81+
printf("\"%s\": ", doc->entries[i]->lines[j]->key->text);
82+
83+
// Check if line has multiple values
84+
if (doc->entries[i]->lines[j]->vlen > 1) {
85+
printf("[\n");
86+
// Iterate over each value in line
87+
for (int k = 0; k < doc->entries[i]->lines[j]->vlen; k++) {
88+
char *closing;
89+
if (k == doc->entries[i]->lines[j]->vlen - 1) {
90+
closing = "\n";
91+
} else {
92+
closing = ",\n";
93+
}
94+
printf(IDNT6);
95+
printf("\"%s\"%s", doc->entries[i]->lines[j]->vals[k]->text, closing);
96+
}
97+
printf(IDNT4);
98+
printf("]\n");
99+
} else {
100+
printf("\"%s\"%s", doc->entries[i]->lines[j]->vals[0]->text, closing);
101+
}
102+
}
103+
104+
char *closing;
105+
if (i == doc->elen - 1) {
106+
closing = "}\n";
107+
} else {
108+
closing = "},\n";
109+
}
110+
111+
printf(IDNT2);
112+
printf("%s", closing);
113+
}
114+
printf("]\n");
115+
}

json.h

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

0 commit comments

Comments
 (0)