#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <json.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>

int main(int argc, char **argv){
	//FILE *infile;
	char *infile_name = "./entities.json";
	int input;
	struct stat buffer;
	int status;
	size_t file_bytes = 0;
	//struct json_object *obj1, *tmp;
	char linestring[1024];
	char *mem_file;
	int type, stringlen;
	struct json_object *obj, *obj2;
	char *str_ptr;
	struct json_object *tmp;

	input = open(infile_name, O_RDONLY);
	if(input == -1){
		fprintf( stderr, "Error opening %s\n", infile_name);
		exit(1);
	}
	status = fstat(input, &buffer);
	file_bytes=buffer.st_size;

	mem_file = (char *)mmap(0, file_bytes, PROT_READ, MAP_SHARED, input, 0);
	if(mem_file == MAP_FAILED) {
		close(input);
		printf("Error: mmap MAP_FAILED\n");
		exit(1);
	}
	obj = json_tokener_parse(mem_file);

	type = json_object_get_type(obj);
	printf("type: %d\n", type);

	json_object_object_foreach(obj, key, val){
		str_ptr = (char *)json_object_get_string(val);
		printf("key = %s value = %s\n",key, str_ptr);
		//stringlen = strlen((const char *)str_ptr);

		//json_object_object_get_ex(jobj, key, &tmp);

		obj2 = json_tokener_parse((const char *)str_ptr);
		json_object_object_get_ex(obj2, "codepoints", &tmp);


		json_object_object_foreach(tmp, key2, val2){
			printf("\tkey = %s value = %s\n", key2, json_object_get_string(val2));
		}
		//objtmp = json_tokener_parse(val);
		//json_object_object_foreach(objtmp, "codepoints", val){
		//	printf("key = %s value = %s\n",key, json_object_get_string(val));
		//}
	}



	//json_object_put((struct json_object *)mem_file);
	//json_object_is_type();
//	type = json_object_array_length(obj);
//	printf("array_length: %d\n", type);
	//	obj1 = json_object_new_object();
	//	json_object_object_add(obj1, KEY1, json_object_new_int(1234));
		//print_json_object(obj1, "obj1 in plaintext");
	//	json_object_object_get_ex(jobj, "codepages", &tmp);


	if(munmap(mem_file, file_bytes) == -1){
		printf("Error: munmap\n");
	}
	close(input);
	return 0;
}
