Add true makefile + lists

This commit is contained in:
Antoine Bartuccio 2016-12-10 02:28:10 +01:00
parent 2dd85ff6cd
commit 9d57b532f6
8 changed files with 399 additions and 29 deletions

16
LibCell/CellElement.c Normal file
View File

@ -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 <stdio.h>
#include <stdlib.h>
#include <CellElment.h>
void freeCellElement(cellElement* element) {
if (element != NULL){
free(element);
}
}

View File

@ -40,5 +40,6 @@ struct cellElement {
typedef struct cellElement * cellElement;
void freeCellElement(cellElement* element);
#endif

217
LibList/list.c Normal file
View File

@ -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 <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <CellElment.h>
#include <list.h>
#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(i<nb){
current = current->next;
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;
}

99
LibList/list.h Normal file
View File

@ -0,0 +1,99 @@
#ifndef LIST_H
#define LIST_H
#import <CellElement.h>
#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 */

View File

@ -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

BIN
main Executable file

Binary file not shown.

16
main.c Normal file
View File

@ -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 <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
printf("Hello World\n");
return 0;
}

BIN
main.o Normal file

Binary file not shown.