dwiju #include "infoTree.h" #include "data.h" #include #include #include /************************************************************** ************************************************************** * Internal function prototypes (for use only within this file) ************************************************************** **************************************************************/ void DeleteKey (FolderKey_t *rootNode_p); LongKey_t *GetLongKey (FolderKey_t *rootNode_p, const char *folderPath_p, const char *keyName_p); StringKey_t *GetStringKey (FolderKey_t *rootNode_p, const char *folderPath_p, const char *keyName_p); FolderKey_t *GetFolderKey (FolderKey_t *rootNode_p, const char *folderPath_p); void AddLongKeyValue (FolderKey_t **rootNode_pp, const char *folderPath_p, const char *keyName_p, long longValue); void AddStringKeyValue (FolderKey_t **rootNode_pp, const char *folderPath_p, const char *keyName_p, const char *string_p); FolderKey_t *CreateFolderKey (FolderKey_t **rootNode_pp, const char *keyName_p); /******************************************************************************************************************** ******************************************************************************************************************** * External functions (available for use outside this file) ******************************************************************************************************************** ********************************************************************************************************************/ void DeleteInformationTree(FolderKey_t **rootNode_pp){ DeleteKey(*rootNode_pp); } int GetLongKeyValue(FolderKey_t **rootNode_pp, const char *keyLocation_p, const char *keyName_p, long *keyValue_p){ LongKey_t *KeyValue; int returnValue = 0; KeyValue = GetLongKey(*rootNode_pp, keyLocation_p ,keyName_p); *keyValue_p = KeyValue->m_Value; if(keyValue_p == NULL) { returnValue = 0; } else { returnValue = 1; } return returnValue; } int SetLongKeyValue(FolderKey_t **rootNode_pp, const char *keyLocation_p, const char *keyName_p, long keyValue){ long KeyValue; int returnValue = 0; AddLongKeyValue(rootNode_pp, keyLocation_p, keyName_p, keyValue); keyValue = KeyValue; if(keyValue == NULL) { returnValue = 0; } else { returnValue = 1; } return returnValue; } int GetStringKeyValue(FolderKey_t **rootNode_pp, const char *keyLocation_p, const char *keyName_p, char *keyValue_p, int *length_p){ int returnValue = 0; StringKey_t *KeyValue; KeyValue = GetStringKey(rootNode_pp, keyLocation_p, keyName_p); keyValue_p = KeyValue->m_Value_p; if(keyValue_p == NULL) { returnValue = 0; } else { returnValue = 1; } return returnValue; } int SetStringKeyValue(FolderKey_t **rootNode_pp, const char *keyLocation_p, const char *keyName_p, const char *keyValue_p){ int returnValue = 0; StringKey_t *KeyValue; AddStringKeyValue(*rootNode_pp, keyLocation_p, keyName_p, keyValue_p); keyValue_p = KeyValue; if(keyValue_p == NULL) { returnValue = 0; } else { returnValue = 1; } return returnValue; } /**************************************************** **************************************************** * Internal functions (for use only within this file) **************************************************** ****************************************************/ void DeleteKey(FolderKey_t *rootNode_p){ //do delete functions of linked lists for each list. if(rootNode_p != NULL) { free(rootNode_p->m_KeyName_p); free(rootNode_p->m_LongList_p); free(rootNode_p->m_StringList_p); DeleteKey(rootNode_p->m_SubFolder_p); DeleteKey(rootNode_p->m_Siblings_p); free(rootNode_p->m_SubFolder_p); free(rootNode_p->m_Siblings_p); } } LongKey_t *GetLongKey(FolderKey_t *rootNode_p, const char *folderPath_p, const char *keyName_p){ FolderKey_t *PathFolder; //if( PathFolder = GetFolderKey(rootNode_p, folderPath_p); return Search(PathFolder->m_LongList_p, keyName_p); } StringKey_t *GetStringKey(FolderKey_t *rootNode_p, const char *folderPath_p, const char *keyName_p){ FolderKey_t *PathFolder; PathFolder = GetFolderKey(rootNode_p, folderPath_p); return Search(PathFolder->m_StringList_p, keyName_p); } FolderKey_t *GetFolderKey(FolderKey_t *rootNode_p, const char *folderPath_p){ /*This function creates a new folderKey if it does not already exist. The folder will have the path * folderPath_p.*/ int returnValue = 1; FolderKey_t *lastNode_p = NULL; char *Token; char pathway[50]; int resultStrcmp = 0; strcpy(pathway, folderPath_p); Token = strtok(pathway, '/'); while((returnValue == 1) && (Token != NULL) && (rootNode_p != NULL)) { lastNode_p = rootNode_p; if((Token != NULL)&&(strcmp(Token, rootNode_p->m_KeyName_p ) == 0)) { lastNode_p = lastNode_p->m_SubFolder_p; Token = strtok(NULL, '/'); } else if((Token != NULL) && (strcmp(Token, rootNode_p->m_KeyName_p) > 0)) { lastNode_p = lastNode_p->m_Siblings_p; } else{ //Folder does not exist returnValue = 0; lastNode_p = NULL; } } return lastNode_p; } void AddLongKeyValue(FolderKey_t **rootNode_pp, const char *folderPath_p, const char *keyName_p, long longValue){ LongKey_t *NewPtr; LongKey_t *CurrentPtr; LongKey_t *PreviousPtr; FolderKey_t *FolderName; FolderName = CreateFolderKey(rootNode_pp, folderPath_p); strcpy(FolderName, keyName_p); NewPtr = malloc(sizeof (LongKey_t)); if(NewPtr == NULL) { printf("Memory not available.\n"); } else { NewPtr->m_KeyName_p = longValue; NewPtr->m_NextLongKey_p = NULL; PreviousPtr = NULL; CurrentPtr = keyName_p; while((CurrentPtr != NULL) && (longValue > CurrentPtr->m_KeyName_p)) { PreviousPtr = CurrentPtr; CurrentPtr = CurrentPtr->m_NextLongKey_p; } if(PreviousPtr == NULL) { NewPtr->m_KeyName_p = keyName_p; keyName_p = NewPtr; } else { PreviousPtr->m_NextLongKey_p = NewPtr; NewPtr->m_NextLongKey_p = CurrentPtr; } } return CurrentPtr; } void AddStringKeyValue(FolderKey_t **rootNode_pp, const char *folderPath_p, const char *keyName_p, const char *string_p){ StringKey_t *NewPtr; StringKey_t *CurrentPtr; StringKey_t *PreviousPtr; char string[50]; strcpy(string, string_p); keyName_p = CreateFolderKey(*rootNode_pp, folderPath_p); NewPtr = malloc(sizeof (StringKey_t)); if(NewPtr == NULL) { printf("Memory not available.\n"); } else { NewPtr->m_KeyName_p = string; NewPtr->m_NextStringKey_p = NULL; PreviousPtr = NULL; CurrentPtr = keyName_p; while((CurrentPtr != NULL) && (string > CurrentPtr->m_KeyName_p)) { PreviousPtr = CurrentPtr; CurrentPtr = CurrentPtr->m_NextStringKey_p; } if(PreviousPtr == NULL) { NewPtr->m_KeyName_p = keyName_p; keyName_p = NewPtr; } else { PreviousPtr->m_NextStringKey_p = NewPtr; NewPtr->m_NextStringKey_p = CurrentPtr; } } return CurrentPtr; } FolderKey_t *CreateFolderKey(FolderKey_t **rootNode_pp, const char *folderPath_p){ //do the same thing as getfolderkey, except everytime you check to see if the folder is the same, check //to see if it exists, and if it doesn't, malloc memory for it and create a folder for it FolderKey_t Path[50]; //int resultStrcmp = 0; char *Token; //FolderKey_t *TempPtr; FolderKey_t *PreviousPtr; FolderKey_t *CurrentPtr; FolderKey_t *NewPtr; FolderKey_t *parentPtr; FolderKey_t *Ptr; FolderKey_t *Folder; CurrentPtr = *rootNode_pp; strcpy(Path, folderPath_p); Token = strtok(Path, '/'); while(Token != NULL) { if((CurrentPtr == NULL) || (strcmp(Token, CurrentPtr->m_KeyName_p)) < 0) { //create folder NewPtr = malloc(sizeof(FolderKey_t)); NewPtr->m_KeyName_p = malloc(sizeof(FolderKey_t)); NewPtr->m_LongList_p = NULL; NewPtr->m_StringList_p = NULL; NewPtr->m_SubFolder_p = NULL; NewPtr->m_Siblings_p = NULL; strcpy(NewPtr->m_KeyName_p, Token); if((CurrentPtr == NULL) && (*rootNode_pp == NULL)) { *rootNode_pp = NewPtr; } else if(parentPtr != NULL)//insert in the beginning of list or empty node) { parentPtr->m_SubFolder_p = NewPtr; NewPtr->m_Siblings_p = CurrentPtr; } else if(PreviousPtr != NULL)//insert in the middle or the end of list) { PreviousPtr->m_Siblings_p = NewPtr; NewPtr->m_Siblings_p = CurrentPtr; } CurrentPtr = NULL; PreviousPtr = NULL; parentPtr = NewPtr; } else if ((strcmp(Token, CurrentPtr->m_KeyName_p)) == 0) { parentPtr = CurrentPtr; PreviousPtr = NULL; CurrentPtr = CurrentPtr->m_SubFolder_p; } else if((strcmp(Token, CurrentPtr->m_KeyName_p)) > 0) { PreviousPtr = CurrentPtr; parentPtr = NULL; CurrentPtr = CurrentPtr->m_Siblings_p; } Token = strtok(NULL, '/'); } //intitialize the members of the folder structure to null so that when you check it to see if it is not null, then it really is null return CurrentPtr; } LongKey_t Search(FolderKey_t *tempPtr, FolderKey_t keyName_p) { if(strcmp(tempPtr->m_KeyName_p, keyName_p) ==0) { return tempPtr; } else { return(Search(tempPtr, keyName_p)); } } /******************************************************************************** * This is a recursive function which deletes the key pointed to by rootNode_p. It also deletes all * of the subfolders, siblings and lists (longLists and stringLists). ******************************************************************************** void DeleteKey(FolderKey_t *rootNode_p) { //base case - if null is passed in we dont do anything if(rootNode_p != NULL) { //recusively delete all the subfolders and siblings DeleteKey(rootNode_p->m_SubFolder_p); DeleteKey(rootNode_p->m_Siblings_p); //delete all the long and string lists referenced by this folderKey DeleteLongList(rootNode_p->m_LongList_p); DeleteStringList(rootNode_p->m_StringList_p); //now that everything this folderKey refrences has been deleted, free the memory for the folderKey free(rootNode_p); } } /******************************************************************************** * This is a recursive function which deletes the key pointed to by LongList_p. It also deletes all * of the siblings ******************************************************************************** void DeleteLongList(LongKey_t *LongList_p) { //base case - if null is passed in we dont do anything if(LongList_p != NULL) { //delete all siblings DeleteLongList(LongList_p->m_NextLongKey_p); //with all siblings gone, free the memory for this list free(LongList_p); } } /******************************************************************************** * This is a recursive function which deletes the key pointed to by StringList_p. It also deletes all * of the siblings ******************************************************************************** void DeleteStringList(StringKey_t *StringList_p) { //base case - if null is passed in we dont do anything if(StringList_p != NULL) { //delete all siblings DeleteStringList(StringList_p->m_NextStringKey_p); //with all siblings gone, free the memory for this list free(StringList_p); } } /******************************************************************************** * This function searches the information tree (rootNode_p) for the longKey with path folderPath_p * and name keyName_p and returns it if it exists, otherwise it returns NULL. ******************************************************************************** LongKey_t *GetLongKey(FolderKey_t *rootNode_p, const char *folderPath_p, const char *keyName_p) { LongKey_t ret; FolderKey_t PathFolder; LongKey_t LongList_p; ret = null; PathFolder = GetFolder(rootNode_p, folderPath_p); LongList_p = PathFolder->m_LongList_p; while(null != LongList_p) { if(0 == strcmp(LongList_p->m_KeyName_p,keyName_p) { //Found It! ret = LongList_p; break; } LongList_p = LongList_p->m_NextLongKey_p; } return ret; } /******************************************************************************** * This function searches the information tree (rootNode_p) for the stringKey with * path folderPath_p and name keyName_p and returns it if it exists, otherwise it * returns NULL. ******************************************************************************** StringKey_t *GetStringKey(FolderKey_t *rootNode_p, const char *folderPath_p, const char *keyName_p) { StringKey_t ret; FolderKey_t PathFolder; StringKey_t StringList_p; ret = null; PathFolder = GetFolder(rootNode_p, folderPath_p); StringList_p = PathFolder->m_StringList_p; while(null != StringList_p) { if(0 == strcmp(StringList_p->m_KeyName_p,keyName_p) { //Found It! ret = StringList_p; break; } StringList_p = StringList_p->m_NextStringKey_p; } return ret; } */