void initialiseFileWriting() { int ret; pthread_t thread; if(fileWriteThreadInitialized == 0) { fileOpenList = createNewList(); fileWriteList = createNewList(); fileCloseList = createNewList(); ret = pthread_create(&thread,NULL,fileWritingThread,NULL); if(ret == EAGAIN) { printf("ERROR: EAGAIN\n"); } else if (ret == EINVAL) { printf("ERROR: EINVAL\n"); } else if ( ret == EPERM) { printf("ERROR: EPERM\n"); } // Wait until the writing thread is initialized. while(fileWriteThreadInitialized == 0) { usleep(10); } } }
void initialiseFileRead() { int ret; pthread_t thread; readWorkspace = (char*)malloc(sizeof(short)*CHUNK_SIZE); if(fileReadThreadInitialized == 0) { fileOpenList = createNewList(); fileReadList = createNewList(); fileCloseList = createNewList(); ret = pthread_create(&thread,NULL,fileReadingThread,NULL); if(ret == EAGAIN) { printf("ERROR: EAGAIN\n"); } else if (ret == EINVAL) { printf("ERROR: EINVAL\n"); } else if ( ret == EPERM) { printf("ERROR: EPERM\n"); } // Wait until the writing thread is initialized. while(fileReadThreadInitialized == 0) { usleep(10); } } }
static struct FileWriteObject* createNewWriteObject(char *fileName) { struct FileWriteObject *object = NULL; if(fileName != NULL) { object = (struct FileWriteObject*)malloc(sizeof(struct FileWriteObject )); object->writeObjects = NULL; object->stream = NULL; object->fileName = NULL; object->wav = NULL; object->fileName = (char*)malloc(sizeof(char)*128); memset(object->fileName,0,128); unsigned length = strlen(fileName); if(length >=128) strncpy(object->fileName,fileName,127); else strcpy(object->fileName,fileName); object->writeObjects = createNewList(); int channels = 1; int codec = 0; object->wav = createWavSink(channels,codec); } return object; }
static struct FileReadObject* createNewReadObject(char *fileName) { struct FileReadObject *object; if(fileName != NULL) { object = (struct FileReadObject*)malloc(sizeof(struct FileReadObject)); object->wav = NULL; object->readObjects = NULL; object->stream = NULL; object->headerOpen = 0; object->fileName = (char *)malloc(sizeof(char)*128); memset(object->fileName,0,128); unsigned length = strlen(fileName); if(length >=128) strncpy(object->fileName,fileName,127); else strcpy(object->fileName,fileName); object->readObjects = createNewList(); } return object; }
List *prependAndTag(List *l, void *data, Tag tag) { #ifdef DEBUG printf("\033[32m%s\033[00m\n", __func__); #endif if (l == NULL) { l = createNewList(); } if (l->head == NULL) { // First item being added to the list l->head = createNewNode(); l->head->data = data; l->head->tag = tag; l->tail = l->head; } else { // Adding to the front Node *newEnd = createNewNode(); newEnd->data = data; newEnd->next = l->head; newEnd->tag = tag; l->head = newEnd; l->tail->next = l->head; } ++l->size; return l; }
List *appendAndTag(List *l, void *data, Tag tag, void (*freer)(void *)) { if (l == NULL) { l = createNewList(); } if (l->head == NULL) { // First item being added to the list l->head = createNewNodeWithFreer(freer); l->head->data = data; l->head->tag = tag; l->tail = NULL; l->head->next = l->tail; } else if (l->tail == NULL) { l->tail = createNewNodeWithFreer(freer); l->tail->data = data; l->tail->tag = tag; l->head->next = l->tail; } else { l->tail->next = createNewNodeWithFreer(freer); l->tail = l->tail->next; l->tail->tag = tag; l->tail->data = data; } ++l->size; return l; }
void inputData(){ // If our first node in the list has point to the next struct NULL value // we want to initialize a function that will create this new list if(pFirstNode == NULL){ createNewList(); } else { struct product *pNewStruct = (struct product *) malloc(sizeof(struct product)); printf("Enter product name: "); scanf("%s", &(pNewStruct)->productName); printf("Enter product price: "); scanf("%f", &(pNewStruct)->price); // Now we want work with the second struct in the list if(pFirstNode == pLastNode){ // Here we place point to next struct our pNewStruct pFirstNode->next = pNewStruct; // But then our pLastNode is this new struct pLastNode = pNewStruct; // And also we need to put NULL value inside 'point to next struct' for this last struct pNewStruct->next = NULL; } else { // Here we will work with not the first, not the second // but the third and every other node thereafter pLastNode->next = pNewStruct; // Since it is last item in the list - next is equal to null pNewStruct->next = NULL; pLastNode = pNewStruct; } } }
void inputData() { // Special case, trying to insert a node which turns out to be the first new element // in the list if (pFirstNode == NULL) { createNewList(); } else { // This means we are inserting the second or third element in the list // Create a new struct for a new element // Allocating memory for each new struct struct product *pNewStruct = (struct product *) malloc (sizeof(struct product)); // Cast not needed for C, but makes it clearer // what is going on here. (C++ requires it) printf("Enter Product Name: "); scanf("%s", &(pNewStruct)->productName); printf("Enter Product price: "); scanf("%f", &(pNewStruct)->price); // Check if this is going to be the second item in the list if (pFirstNode == pLastNode) { // This means we only have one node in the list pFirstNode->next = pNewStruct; pLastNode = pNewStruct; pNewStruct->next = NULL; } else { // General case for the third and more subsequent new nodes pLastNode->next = pNewStruct; pNewStruct->next = NULL; pLastNode = pNewStruct; } } }