From 1936134aadf78731f03ef98d0a65e2908f6b697e Mon Sep 17 00:00:00 2001 From: xethyrion Date: Thu, 24 Nov 2016 20:06:33 +0200 Subject: [PATCH] Add files via upload --- ipld.h | 359 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 359 insertions(+) create mode 100644 ipld.h diff --git a/ipld.h b/ipld.h new file mode 100644 index 0000000..c029445 --- /dev/null +++ b/ipld.h @@ -0,0 +1,359 @@ +#ifndef IPLD_HEADER_H + +#define IPLD_HEADER_H + +#include +#include +#include + +//Predefined values: +#define IDKey "@id" +#define TypeKey "@type" +#define ValueKey "@value" +#define CtxKey "@context" +#define CodecKey "@codec" +#define LinkKey "mlink" + +int NODE_H_DEBUG = 0; //Turn this on if you want to debug the program + +/* Node struct + * obj = json_t A jansson json object. + * node_size = size of the node +*/ +struct NODE +{ + json_t * obj; + size_t node_size; +}; +/* LOAD_NODE(@param1) + * Creates a new node from a string. + * @Param1 a json string(char *) + * returns a json_t object! (jansson.h) +*/ +struct NODE LOAD_NODE(char * str) +{ + struct NODE X; + json_error_t error; + X.obj = json_loads(str, 0, &error); + X.node_size = strlen(str); + return X; +} +/* Unload_Node(@pa for(int i=0; iField1<:{}} + * @param1: LINK.name //Storing in type from LINK struct. + * @param2: LINK // The link structure we are using. +*/ +void lName(char * str, struct LINK O) +{ + json_t * value; + const char * key; + json_object_foreach(O.obj, key, value) + { + if(NODE_H_DEBUG == 1) + { + printf("lName: %s\n", key); + } + strcat(str, key); + } +} + +/* LOAD_LINK(@param1) + * Creates a new LINK from a string. + * @Param1 a json string(char *) + * returns a json_t object! (jansson.h) + * This is made to also give you easy access to any part of the link. + * It will have LINK.valid = 0 if it's not a valid link, you should check for this always. +*/ +struct LINK LOAD_LINK(char * str) +{ + struct LINK X; + json_error_t error; + X.obj = json_loads(str, 0, &error); + X.link_size = strlen(str); + bzero(X.type,100); + bzero(X.name,100); + bzero(X.hash,100); + bzero(X.b58hash,1000); + lName(X.name, X); + lType(X.type, X); + lHash(X.hash, X); + return X; +} +/* Unload_LINK(@param1) - Same as unload_node, makes it easier to avoid leaks and better structuring of your programs + * Well, really easy, after being done with a struct if you don't want memory leaks.. + * you're supposed to free them, so use this to unload the node you create. + * this design makes it easier to read and mentain a good program architecture. + * @param1 = node you previously created with NEW_NODE +*/ +void UNLOAD_LINK(struct LINK X) +{ + json_decref(X.obj); +} +#endif \ No newline at end of file