c-programming

Simple C programs
git clone git://git.laack.co/c-programming.git
Log | Files | Refs

commit 4af61b5b35326d4b6cd134d4535217c76f887115
parent 25b5c4dd75a9068220538f3e5b4d4d229773cfd2
Author: Andrew Laack <andrew@laack.co>
Date:   Sun, 18 Oct 2026 10:36:26 -0500

Parse the json

Diffstat:
Mperf-aware-programming/haversine/Makefile | 6++++++
Mperf-aware-programming/haversine/gen.py | 2+-
Aperf-aware-programming/haversine/json.c | 140+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mperf-aware-programming/haversine/main.c | 9+++++++++
4 files changed, 156 insertions(+), 1 deletion(-)

diff --git a/perf-aware-programming/haversine/Makefile b/perf-aware-programming/haversine/Makefile @@ -3,3 +3,9 @@ gen: python3 gen.py artifacts/out.json 100 clean: rm -rf out.json + +build: + gcc main.c + +run: build + ./a.out diff --git a/perf-aware-programming/haversine/gen.py b/perf-aware-programming/haversine/gen.py @@ -56,7 +56,7 @@ def genJson(): rndy.append(y0) rndy.append(y1) - st = f"{{\"x0\" : {x0}, \"y0\": {y0}, \"x1\" : {x1}, \"y1\": {y1}}}" + st = f"{{\"x0\" : {x0}, \"y0\" : {y0}, \"x1\" : {x1}, \"y1\" : {y1}}}" hav.append(haversine(x0,x1,y0,y1, 6372.8)) return st diff --git a/perf-aware-programming/haversine/json.c b/perf-aware-programming/haversine/json.c @@ -0,0 +1,140 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +struct Coordinates { + float x0; + float x1; + float y0; + float y1; +}; + +struct Json { + struct Coordinates* coords; +}; + +char* loadFileToString(char* filename) { + printf("Loading %s\n" , filename); + FILE* fp = fopen(filename, "r"); + char *line = NULL; + size_t len = 0; + ssize_t read; + read = getline(&line, &len, fp); + fclose(fp); + return line; +} + +int isFloatNumeric(char ch) { + if( ((ch > '9') || (ch < '0')) && ch != '.' && ch != '-') { + return 0; + } + return 1; +} + +// idxStart must be first character of the number +float loadNextNumber(char* jsonStr, int idxStart) { + char chars[100]; + int idx = 0; + while(isFloatNumeric(jsonStr[idx+idxStart])) { + chars[idx] = jsonStr[idx+idxStart]; + idx += 1; + } + chars[idx+1] = '\0'; + float num = atof(chars); + return num; +} + +int getCoordinateCount(char* jsonData) { + int elements = strlen(jsonData); + int count = 0; + for(int i = 0; i < elements; ++i) { + if(jsonData[i] == 'x' && jsonData[i+1] == '1') { + count += 1; + } + } + return count; +} +struct Coordinates* parseCoordinates(char* jsonData, int* sz) { + + int elements = strlen(jsonData); + + int lfN = 0; + int onX = 1; + char next = 'x'; + + int coordinateCount = getCoordinateCount(jsonData); + sz[0] = coordinateCount; + struct Coordinates* coordinates = (struct Coordinates*)malloc(coordinateCount * sizeof(struct Coordinates)); + + int cNum = 0; + struct Coordinates* current = &coordinates[cNum]; + + for(int i = 0; i < elements; ++i) { + if(jsonData[i] != next) { + continue; + } else { + if(next == 'x' ) { + if(lfN == 0) { + next = '0'; + } else { + next = '1'; + } + continue; + } + + + if(next == 'y') { + if(lfN == 0) { + next = '0'; + lfN = 1; + } else { + next = '1'; + lfN = 0; + } + continue; + } + if(next == '0' || next == '1') { + float num = loadNextNumber(jsonData,i+5); + + if(next == '0' && onX == 1) { + current->x0 = num; + } + if(next == '1' && onX == 1) { + current->x1 = num; + } + if(next == '0' && onX == 0) { + current->y0 = num; + } + if(next == '1' && onX == 0) { + current->y1 = num; + cNum += 1; + if(cNum < coordinateCount) { + current = &coordinates[cNum]; + } + } + + if(onX == 0) { + next = 'x'; + onX = 1; + } else { + next = 'y'; + onX = 0; + } + continue; + } + } + } + return coordinates; +} + +struct Coordinates* loadJson(char* filename, int* size) { + char* line = loadFileToString(filename); + + int sz = 0; + struct Coordinates* res = parseCoordinates(line, &sz); + size[0] = sz; + + free(line); + printf("%d", sz); + return res; +} diff --git a/perf-aware-programming/haversine/main.c b/perf-aware-programming/haversine/main.c @@ -1,3 +1,12 @@ +#include "json.c" + int main(int argc, char** argv) { + int size = 0; + struct Coordinates* coords = loadJson("artifacts/out.json", &size); + + for(int i = 0; i < size; ++i) { + struct Coordinates* current = &coords[i]; + printf("x0: %.10f, x1: %.10f, y0: %.10f, y1: %.10f\n", current->x0, current->x1, current->y0, current->y1); + } }