Refactor
Move json_value and obj_t to same file - Rename json_obj.* -> json_value.* - Move `print_json_value(...)` to json_value.* Move `err_ctx(...)` to util.* parse.h now only exposes `parse_json_value(...)`
This commit is contained in:
@@ -1,22 +0,0 @@
|
||||
|
||||
#ifndef _obj_H
|
||||
#define _obj_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#define OBJ_SIZE 1024
|
||||
|
||||
typedef struct obj_entry {
|
||||
char const* key;
|
||||
struct json_value* val;
|
||||
struct obj_entry* next;
|
||||
} * __p_obj_entry;
|
||||
|
||||
typedef __p_obj_entry obj_t[OBJ_SIZE];
|
||||
|
||||
void* obj_at(obj_t m, char* const key);
|
||||
bool obj_insert(obj_t m, char* const key, struct json_value* value);
|
||||
void obj_delete(obj_t m);
|
||||
|
||||
#endif
|
||||
41
include/json_value.h
Normal file
41
include/json_value.h
Normal file
@@ -0,0 +1,41 @@
|
||||
|
||||
#ifndef _JSON_VALUE_H
|
||||
#define _JSON_VALUE_H
|
||||
|
||||
#include <stdbool.h> // bool
|
||||
|
||||
#define OBJ_SIZE 64
|
||||
|
||||
typedef struct obj_entry {
|
||||
char const* key;
|
||||
struct json_value* val;
|
||||
struct obj_entry* next;
|
||||
} * __p_obj_entry;
|
||||
|
||||
typedef __p_obj_entry obj_t[OBJ_SIZE];
|
||||
|
||||
enum json_type { object,
|
||||
array,
|
||||
string,
|
||||
number,
|
||||
boolean,
|
||||
null };
|
||||
|
||||
struct json_value {
|
||||
enum json_type type;
|
||||
union {
|
||||
obj_t* object;
|
||||
struct json_value** array;
|
||||
char* string;
|
||||
bool boolean;
|
||||
double number;
|
||||
};
|
||||
};
|
||||
|
||||
void* obj_at(obj_t m, char* const key);
|
||||
bool obj_insert(obj_t m, char* const key, struct json_value* value);
|
||||
void obj_delete(obj_t m);
|
||||
|
||||
void print_json(struct json_value val, int indent);
|
||||
|
||||
#endif
|
||||
@@ -2,31 +2,10 @@
|
||||
#ifndef _PARSE_H
|
||||
#define _PARSE_H
|
||||
|
||||
#include "json_obj.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include "json_value.h"
|
||||
|
||||
enum json_type { object,
|
||||
array,
|
||||
string,
|
||||
number,
|
||||
boolean,
|
||||
null };
|
||||
|
||||
struct json_value {
|
||||
enum json_type type;
|
||||
union {
|
||||
obj_t* object;
|
||||
struct json_value** array;
|
||||
char* string;
|
||||
bool boolean;
|
||||
double number;
|
||||
};
|
||||
};
|
||||
#include <stdio.h> // FILE*
|
||||
|
||||
struct json_value parse_json_value(FILE* fp);
|
||||
|
||||
void print_json(struct json_value val, int indent);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -3,6 +3,9 @@
|
||||
#define _UTIL_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
||||
__attribute__((__noreturn__)) void err_ctx(int exit_code, FILE* fp, const char* format, ...);
|
||||
|
||||
void* malloc_or_die(size_t size);
|
||||
void* realloc_or_die(void* ptr, size_t size);
|
||||
|
||||
Reference in New Issue
Block a user