From 5627bb3992b2f6af61b7a420a5fcc34c6a443f92 Mon Sep 17 00:00:00 2001 From: xethyrion Date: Sun, 4 Dec 2016 22:10:41 +0200 Subject: [PATCH] Update node.h --- include/ipfs/node/node.h | 157 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 149 insertions(+), 8 deletions(-) diff --git a/include/ipfs/node/node.h b/include/ipfs/node/node.h index 106ce1c..d906dc8 100644 --- a/include/ipfs/node/node.h +++ b/include/ipfs/node/node.h @@ -28,6 +28,7 @@ struct Node { unsigned char * data; unsigned char * encoded; + struct Cid * cached; int link_ammount; struct Link * links[]; }; @@ -67,7 +68,64 @@ void Free_Link(struct Link * L) /*==================================================================================== * Node Functions *===================================================================================*/ -//In progress +/*Create_Empty_Node + * Creates an empty node, allocates the required memory + * Returns a fresh new node with no data set in it. + */ +struct Node * Create_Empty_Node() +{ + struct Node * N; + N = (struct Node *)malloc(sizeof(struct Node)); + return N; +} +/*Node_Set_Data + * Sets the data of a node + * @param Node: The node which you want to set data in. + * @param Data, the data you want to assign to the node + * returns 1 on success 0 on failure + */ +int Node_Set_Data(struct Node * N, unsigned char * Data) +{ + if(!N || !Data) + { + return 0; + } + N->encoded = NULL; + N->cached = NULL; + N->data = Data; + return 1; +} +/*Node_Get_Data + * Gets data from a node + * @param Node: = The node you want to get data from. (unsigned char *) + * Returns data of node. + */ +unsigned char * Node_Get_Data(struct Node * N) +{ + unsigned char * DATA; + DATA = N->data; + return DATA; +} + +/*Node_Copy: Returns a copy of the node you input + * @param Node: The node you want to copy (struct CP_Node *) + * Returns a copy of the node you wanted to copy. + */ +struct Node * Node_Copy(struct Node * CP_Node) +{ + struct Node * CN; + CN = (struct Node*) malloc(sizeof(struct Node) + sizeof(struct Link) * 2); + if(CP_Node->link_ammount != 0) + { + for(int i=0; ilink_ammount; i++) + { + CN->links[i] = malloc(sizeof(struct Link)); + } + } + memcpy(CN, CP_Node, sizeof(struct Node)); + memcpy(CN->links[0],CP_Node->links[0], sizeof(struct Link)); + return CN; +} /*Node_Delete * Once you are finished using a node, always delete it using this. * It will take care of the links inside it. @@ -89,12 +147,12 @@ void Node_Delete(struct Node * N) * @param Name: (char * name) searches for link with this name * Returns the link struct if it's found otherwise returns NULL */ -struct Link * Node_Get_Link(char * Name, struct Node * N) +struct Link * Node_Get_Link(struct Node * N, char * Name) { struct Link * L; for(int i=0;ilink_ammount;i++) { - if(N->links[i]->name==Name) + if(strcmp(N->links[i]->name,Name) == 0) { L = (struct Link *)malloc(sizeof(struct Link)); memcpy(L,N->links[i],sizeof(struct Link)); @@ -118,7 +176,6 @@ int Node_Remove_Link(char * Name, struct Node * mynode) { if(mynode->links[i]->name == Name) { - printf("FOUND THIS GUY: %s\n", mynode->links[i]->name); for(int x=i;xlink_ammount && x+1 != mynode->link_ammount;i++) { memcpy(mynode->links[x],mynode->links[x+1],sizeof(struct Link)); @@ -166,18 +223,29 @@ struct Node * N_Add_Link(struct Node ** mynode, struct Link * mylink, size_t lin * @param linksize: sizeof(the link in mylink) (size_T) * Returns a fresh new node with the link you specified. Has to be freed with Node_Free preferably. */ -struct Node * N_Create_From_Link(struct Link * mylink, size_t linksize) +struct Node * N_Create_From_Link(struct Link * mylink) { struct Node * mynode; - mynode = (struct Node *) malloc(sizeof(struct Node) + linksize); + mynode = (struct Node *) malloc(sizeof(struct Node) + sizeof(struct Link)); mynode->link_ammount = 0; mynode->link_ammount++; mynode->links[0] = malloc(sizeof(struct Link)); memcpy(mynode->links[0], mylink, sizeof(struct Link)); return mynode; } -//Finished +/*N_Create_From_Data + * @param data: bytes buffer you want to create the node from + * returns a node with the data you inputted. + */ +struct Node * N_Create_From_Data(unsigned char * data) +{ + struct Node * mynode; + mynode = (struct Node *) malloc(sizeof(struct Node)); + mynode->data = data; + return mynode; +} /*Node_Resolve_Max_Size + * !!!This shouldn't concern you! *Gets the ammount of words that will be returned by Node_Resolve *@Param1: The string that will be processed (eg: char * sentence = "foo/bar/bin") *Returns either -1 if something went wrong or the ammount of words that would be processed. @@ -204,6 +272,7 @@ int Node_Resolve_Max_Size(char * input1) } /*Node_Resolve Basically stores everything in a pointer array eg: char * bla[Max_Words_] + * !!!This shouldn't concern you!!! *@param1: Pointer array(char * foo[x], X=Whatever ammount there is. should be used with the helper function Node_Resolve_Max_Size) *@param2: Sentence to gather words/paths from (Eg: char * meh = "foo/bar/bin") *@Returns 1 or 0, 0 if something went wrong, 1 if everything went smoothly. @@ -233,12 +302,84 @@ int Node_Resolve(char ** result, char * input1) } return 1; } +/************************************************************************************************************************************** + *|||||||||||||||||||||||||||||||||||||||| !!!! IMPORTANT !!! ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||* + ************************************************************************************************************************************** + * Not sure where this is used, I'm making something to easen it up for all of you. + * This in itself will get all the links for you in a link[] array inside Link_Proc + * the memory allocation for storage will be noted in the ammount of links. + * After being done with this, you have to free the array for which you will have a function specially made for you. + * + * What this does: + * It searches for links using a path like /foo/bar/bin/, if links with those names are found in the node you specify, it stores them + * in a custom struct, Link_Proc; which is what Node_Resolve_Link returns. + * Notes: + * Use it, free it, it's all already laid out for you. + * There will also be a tutorial demonstrating it in the same folder here so everyone can understand this. +*/ +struct Link_Proc +{ + char * remaining_links; // Not your concern. + int ammount; //This will store the ammount of links, so you know what to process. + struct Link * links[]; // Link array +}; +/*Node_Resolve_Links + * Processes a path returning all links. + * @param N: The node you want to get links from + * @param path: The "foo/bar/bin" path + */ +struct Link_Proc * Node_Resolve_Links(struct Node * N, char * path) +{ + if(!N || !path) + { + return NULL; + } + int expected_link_ammount = Node_Resolve_Max_Size(path); + struct Link_Proc * LProc = (struct Link_Proc *) malloc(sizeof(struct Link_Proc) + sizeof(struct Link) * expected_link_ammount); + LProc->ammount = 0; + char * linknames[expected_link_ammount]; + Node_Resolve(linknames, path); + for(int i=0;ilinks[i] = (struct Link *)malloc(sizeof(struct Link)); + memcpy(LProc->links[i], proclink, sizeof(struct Link)); + LProc->ammount++; + free(proclink); + } + } + //Freeing pointer array + for(int i=0;iammount != 0) + { + for(int i=0;iammount;i++) + { + Free_Link(LPRC->links[i]); + } + } + free(LPRC); +} + /*Node_Tree() Basically a unix-like ls *@Param1: Result char * foo[strlen(sentence)] *@Param2: char sentence[] = foo/bar/bin/whatever *Return: 0 if failure, 1 if success */ -int Node_Tree(char * result, char * input1) +int Node_Tree(char * result, char * input1) //I don't know where you use this but here it is. { if(!input1) {