diff --git a/LibCell/CellElement.c b/LibCell/CellElement.c new file mode 100644 index 0000000..07484f9 --- /dev/null +++ b/LibCell/CellElement.c @@ -0,0 +1,16 @@ +/* +* @Author: klmp200 +* @Date: 2016-12-10 01:32:50 +* @Last Modified by: klmp200 +* @Last Modified time: 2016-12-10 01:33:40 +*/ + +#include +#include +#include + +void freeCellElement(cellElement* element) { + if (element != NULL){ + free(element); + } +} \ No newline at end of file diff --git a/CellElement.h b/LibCell/CellElement.h similarity index 92% rename from CellElement.h rename to LibCell/CellElement.h index 83c7111..58c2623 100644 --- a/CellElement.h +++ b/LibCell/CellElement.h @@ -40,5 +40,6 @@ struct cellElement { typedef struct cellElement * cellElement; +void freeCellElement(cellElement* element); #endif \ No newline at end of file diff --git a/LibList/list.c b/LibList/list.c new file mode 100644 index 0000000..e87000a --- /dev/null +++ b/LibList/list.c @@ -0,0 +1,217 @@ +/********************************************************************************* +* File Name : list.c +* Created By : Bartuccio Antoine +* Creation Date : [2016-10-18 13:53] +* Last Modified : [2016-11-08 15:00] +* Description : +**********************************************************************************/ + +#include +#include +#include +#include +#include + +#define SUCCESS 0 +#define FAILURE 1 + +List * CreateList() { + List *list = malloc(sizeof(*list)); + + if(list != NULL){ + list->head = NULL; + list->tail = NULL; + list->size = 0; + } + + return list; +} + +int unshift(List *list, cellElement *data){ + int ok = SUCCESS; + + cellElement* newData = (cellElement*) malloc(sizeof(cellElement)); + + /* Create a new element */ + + ListElement *newElement = malloc(sizeof(*newElement)); + if (list != NULL && newElement != NULL && newData != NULL){ + memcpy(newData, data, size); + newElement->data = newData; + + /* Insert the element at the begining of the list */ + + newElement->previous = NULL; + + if (list->head != NULL){ + list->head->previous = newElement; + } else { + list->tail = newElement; + } + newElement->next = list->head; + + list->head = newElement; + list->size = list->size + 1; + } else { + if (newElement != NULL){ + free(newElement); + } + if (newData != NULL){ + free(newData); + } + ok = FAILURE; + } + + return ok; +} + +int push(List *list, cellElement *data){ + int ok = SUCCESS; + cellElement *newData = (cellElement*) malloc(sizeof(cellElement)); + + ListElement *newElement = malloc(sizeof(*newElement)); + if(list != NULL && newElement != NULL && newData != NULL){ + memcpy(newData, data, size); + newElement->data = newData; + newElement->next = NULL; + if (list->tail == NULL){ + list->tail = newElement; + list->head = newElement; + newElement->previous = NULL; + } else { + newElement->previous = list->tail; + list->tail->next = newElement; + list->tail = newElement; + } + list->size = list->size + 1; + } else { + if (newElement != NULL){ + free(newElement); + } + if (newData != NULL){ + free(newData); + } + ok = FAILURE; + } + return ok; +} + +ListElement * GetElement(List *list, int nb){ + ListElement *current = NULL; + int i; + + if (list != NULL && (nb < list->size || -nb < list->size)){ + if (nb == list->size -1 || nb == -1){ + current = list->tail; + } else if (nb == 0){ + current = list->head; + } else if (nb <= (list->size - 1)/2 && nb > 0){ + i = 0; + current = list->head; + while(inext; + i++; + } + } else { + i = list->size - 1; + if (nb < 0){ + nb = list->size + nb -1; + } + while(i>nb){ + current = current->previous; + i = i - 1; + } + } + } + return current; +} + +int PopPtnList(List *list, ListElement *element){ + int ok = SUCCESS; + + if (list != NULL && element != NULL){ + if (list->head == element && list->tail == element){ + list->head = NULL; + list->tail = NULL; + } else if (list->head == element){ + list->head = element->next; + element->previous = NULL; + } else if (list->tail == element){ + list->tail = element->previous; + element->previous->next = NULL; + } else { + element->next->previous = element->previous; + element->previous->next = element->next; + } + + if (element->data != NULL){ + freeCellElement(element->data); + } + free(element); + list->size = list->size - 1; + } else { + ok = FAILURE; + } + return ok; +} + +int RemoveElement(List *list, int nb){ + int ok = SUCCESS; + ListElement *toDelete = GetElement(list, nb); + + if (toDelete != NULL){ + ok = PopPtnList(list, toDelete); + } else { + ok = FAILURE; + } + return ok; +} + +int shift(List *list){ + return RemoveElement(list, 0); +} + +int pop(List *list){ + return RemoveElement(list, -1); +} + +int DeleteListContent(List *list){ + int ok = SUCCESS; + ListElement * current = NULL; + ListElement * toDelete = NULL; + + if (list != NULL){ + current = list->head; + while (current != NULL){ + toDelete = current; + current = current->next; + + if (toDelete->data != NULL){ + freeCellElement(toDelete->data); + } + + freeCellElement(toDelete); + } + list->head = NULL; + list->tail = NULL; + list->size = 0; + } else { + ok = FAILURE; + } + + return ok; +} + +int FreeList(List *list){ + int ok = SUCCESS; + + if (list != NULL){ + ok = DeleteListContent(list); + if (ok == SUCCESS){ + free(list); + } + } else { + ok = FAILURE; + } + return ok; +} diff --git a/LibList/list.h b/LibList/list.h new file mode 100644 index 0000000..514e02a --- /dev/null +++ b/LibList/list.h @@ -0,0 +1,99 @@ +#ifndef LIST_H +#define LIST_H + +#import + +#define SUCCESS 0 +#define FAILURE 1 + +typedef struct ListElement ListElement; +struct ListElement { + void *data; + ListElement *next; + ListElement *previous; +}; + +typedef struct List { + ListElement *head; + ListElement *tail; + int size; +} List; + +/* + * Create a new list + * @return a pointer of list + */ +List * CreateList(); + +/* + * Insert an element at the begining of a list + * @param list pointer of a list + * @param data any type of data + * @param size size of the data + * @return status of the operation + */ +int unshift(List* list, cellElement* data); + +/* + * Insert an element at the end of a list + * @param list pointer of a list + * @param data any type of data + * @param size size of the data + * @return status of the operation + */ +int push(List* list, cellElement* data, int size); + +/* + * Get an element in a given list + * @param list as a pointer + * @param nb the number of the element (can be negative) + * @return an element + */ +ListElement * GetElement(List *list, int nb); + +/* + * Delete an element with a pointer of element in the list + * @param list as a pointer + * @param element of the list as a pointer + * @return status of the operation + */ +int PopPtnList(List *list, ListElement *element); + +/* + * Delete an element with a position in the list + * @param list as a pointer + * @param position of the element + * @return status of the operation + */ +int RemoveElement(List *list, int nb); + +/* + * Delete the first element of the list + * @param list as a pointer + * @return status of the operation + */ +int shift(List *list); + +/* + * Delete the last element of the list + * @param list as a pointer + * @return status of the operation + */ +int pop(List *list); + +/* + * Delete every elements in a list + * @param list as a pointer + * @return status of the operation + */ +int DeleteListContent(List *list); + + +/* + * Free a list + * @param list as a pointer + * @return status of the operation + */ +int FreeList(List *list); + +#endif /* LIST_H */ diff --git a/Makefile b/Makefile index bf4ff53..b69a9ff 100644 --- a/Makefile +++ b/Makefile @@ -1,35 +1,56 @@ -CXX = gcc -TARGET = exe -SOURCEFILE = main -CFLAGS = -Wall -Werror -I. -ansi -pedantic -fpic -g -LIST_LIBRARY = matrix/libMatrix +# Makefile compiling the whole project +# Don't forget before the first compilation to run that command: +# export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:./Libs -#Generating the executable -$(TARGET): $(SOURCEFILE).o - @echo "Generating the executable" - $(CXX) $(CFLAGS) $(SOURCEFILE).o -o $(TARGET) - -$(SOURCEFILE).o: $(LIST_LIBRARY).so - @echo "Generating $(SOURCEFILE).o" - $(CXX) $(CFLAGS) -Lmatrix -lmatrix -c $(SOURCEFILE).c -o $@ -LlibMatrix -llibMatrix.so - -$(LIST_LIBRARY).so:clean - @echo "Generating libMatrix.so" $@ - #gcc matrix.c -I. -Wall -Werror -fpic -shared -o $(LIST_LIBRARY).so - $(CXX) -Wall -Werror -ansi -pedantic -I. matrix.c -o $(LIST_LIBRARY).so -shared -fpic +CC=gcc +CFLAGS=-Wall -Werror -ansi -pedantic -fpic -g -#Cleaning the executable -clean: - @echo "Cleaning temporary files and libMatrix.so" - rm -rf *.o *- *.so $(TARGET) - rm -rf $(LIST_LIBRARY).so -#Generating library +LIBSDIR=-L/usr/lib -L./Libs +INCLUDEDIR=-I/usr/include -I. -I./LibCell -I./LibList + +#Exe test variables +TARGET=main.exe +SOURCE=main + +SOURCECFILE=$(SOURCE:=.c) +SOURCEOFILE=$(SOURCE:=.o) + +#Library variables +DEPENDENCE=libCellElement.so +DEPENDENCENAME=CellElement + +DEPENDENCELIST=libList.so +DEPENDENCENAMELIST=List + + +all: $(TARGET) + +#Generating the main.exe +$(TARGET): $(SOURCEOFILE) $(DEPENDENCELIST) lib + @echo "\n Generating the " $(TARGET) " binary" + $(CC) $(SOURCEOFILE) $(LIBSDIR) -l$(DEPENDENCENAME) -l$(DEPENDENCENAMELIST) -o $(TARGET) + +#Generating the library binary lib: - @echo "Generating libMatrix.so" - $(CXX) -Wall -Werror -ansi -pedantic -I. matrix.c -o $(LIST_LIBRARY).so -shared -fpic + @echo "\n Generating the automaton library binary" + $(MAKE) -C LibCell -release: - @echo "Generating a release version of the program" - make CFLAGS= '-Wall -Werror -I. -ansi -pedantic -fpic' +$(DEPENDENCELIST): + @echo "\n Generating the list library binary" + $(MAKE) -C LibList + + +#Generating object files +.c.o: + @echo "\n Generating " $@ " from " $< + $(CC) $(CFLAGS) $(INCLUDEDIR) -c -o $@ $< + + +#Cleaning +clean: + @echo "\n Cleaning" + rm -rf *.o ./Libs/*.so *.exe + $(MAKE) -C LibPoly clean + $(MAKE) -C LibList clean diff --git a/main b/main new file mode 100755 index 0000000..c1dee62 Binary files /dev/null and b/main differ diff --git a/main.c b/main.c new file mode 100644 index 0000000..32d1521 --- /dev/null +++ b/main.c @@ -0,0 +1,16 @@ +/********************************************************************************* +* File Name : main.c +* Created By : klmp200 +* Creation Date : [2016-12-10 01:06] +* Last Modified : [2016-12-10 01:07] +* Description : +**********************************************************************************/ + +#include +#include + +int main(int argc, char *argv[]) +{ + printf("Hello World\n"); + return 0; +} diff --git a/main.o b/main.o new file mode 100644 index 0000000..61c790f Binary files /dev/null and b/main.o differ