diff --git a/src/spaceListFindId.c b/src/spaceListFindId.c new file mode 100644 index 0000000..8b1132e --- /dev/null +++ b/src/spaceListFindId.c @@ -0,0 +1,125 @@ +#include "libnazgul.h" + +#define DEBUG 1 + +int msgSpaceListFindId(msgSpaceId spaceId){ + msgSpaceList * list; + msgSpaceListElemId listHeadElemId; + msgSpaceListElemId listTailElemId; + msgSpaceListId listId; + list=msgSpaceListOpen(); + if (list==NULL){ + NZG_ERROR("msgSpaceListOpen",""); + goto ERROR; + } + if (DEBUG) { printf("Before ListStrCpy\n"); } + strcpy(listHeadElemId,list->headId); + strcpy(listTailElemId,list->tailId); + strcpy(listId,list->id); + if (DEBUG) { printf("After ListStrCpy\n"); } + if ((strcmp(listHeadElemId,listId)==0) + && strcmp(listTailElemId,listId)==0){ + if (DEBUG) { printf("SpaceList : vide\n"); } + return 1; + } else { + bool found=false; + msgSpaceListElemId prevElemId; + msgSpaceListElemId currElemId; + msgSpaceListElemId nextElemId; + msgSpaceListElem prevElem; + msgSpaceListElem currElem; + msgSpaceListElem nextElem; + msgSpaceId currSpaceId; + + strcpy(prevElemId,listHeadElemId,list->headId); + strcpy(currElemId,listHeadElemId,list->headId); + while(!found){ + printf("Recherche dans l'element %s\n",listHeadElemId); + listElem=msgSpaceListElemOpen(elemId); + if (listElem==NULL){ + NZG_ERROR("msgSpaceListElemOpen",elemId); + goto ERROR; + } + strcpy(nextElemId,listElem->next); + strcpy(currSpaceId,listElem->spaceId); + if (msgSpaceListElemClose(listElem) <0){ + NZG_ERROR("msgSpaceListElemClose",elemId); + goto ERROR; + } + if (strcmp(prevElemId,nextElemId)==0){ + // list à 1 seul élement + if (strcmp(listElem->spaceId,spaceId)==0){ + // on a trouvé l'elem + strcpy(list->headId,list->id); + strcpy(list->tailId,list->id); + // on efface + shm_unlink(currElemId); + found=true; + } else { + break; + } + } else { + // liste à plusieurs élements... + if (strcmp(listElem->spaceId,spaceId)==0){ + // ca correspond + // si on est en début de liste (prev=current) + // - la tete de liste pointe sur le suivant + // - on détruit l'actuel + // si on est en find de liste (current=next) + // - on fait pointer le précédent sur luimeme + // - on fait pointer la queue de liste sur le précédent + // - on détruit l'actuel + // sinon : + // - on fait pointer le précédent sur le suivant + // - on détruit l'actuel + found=true; + break; + } else { + //cela ne correspond pas + //on recopie + strcpy(prevElemId,currElemId); + strcpu(currElemId,nextElemId); + } + } + + } + if (msgSpaceListClose(list) < 0){ + NZG_ERROR("msgSpaceListClose",""); + goto ERROR; + } + return 0; + } +ERROR: + return -1; +} + +int msgSpaceListElemFindId(msgSpaceListElemId elemId,msgSpaceId spaceId){ + msgSpaceListElem * listElem; + msgSpaceListElemId listElemIdNext; + msgSpaceId currentElemSpaceId; + + strcpy(listElemIdNext,listElem->next); + strcpy(currentElemSpaceId,listElem->spaceId); + printf("Current ListElem: Next:%s, Sid:%s\n", + listElemIdNext,currentElemSpaceId); + printf("comparisons...\n"); + if (strcmp(currentElemSpaceId,spaceId)==0) { + printf("Found %s in spaceList !\n",spaceId); + return 0; + } else { + if (strcmp(listElemIdNext,elemId)==0){ + printf("End of spaceList reached.\n"); + /* fin de liste ?? */ + return 1; + } else { + /* continuer sur l'element suivant */ + return msgSpaceListElemFindId(listElemIdNext,spaceId); + } + } + + return 0; +ERROR: + return -1; +} + +