Skip to content

Instantly share code, notes, and snippets.

@davidawad
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidawad/006489d0648474a1a7e5 to your computer and use it in GitHub Desktop.
Save davidawad/006489d0648474a1a7e5 to your computer and use it in GitHub Desktop.
linked list prob for stackoverflow
/*
*
* David Awad (ada80)
* Mariam Tsilosani (mt617)
*
* main.c
*
*/
#include <errno.h>
#include <malloc.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
// gcc -g -O -o bookOrder main.c -lpthread
// ./bookOrder database.txt orders.txt categories.txt
extern int totalProfit;
struct order{
char *bookTitle;
double price;
int copies;
char *category;
int custID;
char processed;
};
typedef struct order order;
struct customer{
char *name;
int custID;
double credit;
char *address;
char *state;
char *zip;
};
typedef struct customer customer;
struct custNode{
customer *customer;
struct custNode *next;
};
typedef struct custNode custNode;
struct orderNode{
order *order;
struct orderNode *next;
};
typedef struct orderNode orderNode;
custNode* attachCustNode(custNode *head, customer *target){
if(!target || !head){
//printf("\nTarget or Head is NULL!!!\n");
}
custNode *temp = malloc(sizeof(custNode));
temp->customer = target ;
// No matter what head is temp is now the head.
temp->next = head;
return temp;
}
void processBookOrders( orderNode **processQueue , custNode *customerList , int rows){ //moneymaker, threads are created here
while(4){
//while something. probably.
while(2){
}
pthread_t tid;
pthread_attr_t attr;
struct gcd *gcdptr;
unsigned int *rptr;
if(3){
printf("breaking loop, no more threads to create.");
break;
}
}
}
void sortOrders(orderNode **processQueue , orderNode *unSortedQueue , int numcats){
//we have an empty array of ordernodes and we need to insert all of our unsorted
//queue into it in order to pass it to process book orders.
if(!processQueue || !unSortedQueue){
printf("Sorting failed in %s on line %d ", __FILE__ , __LINE__ );
_exit(1);//crash and burn motheryucker
}
//both exist. lets traverse the unsorted queue and fill theprocessqueue
orderNode *unSortPtr = unSortedQueue;
while(unSortPtr){
orderNode **processPtr = processQueue;
//temporary pointer to compare against the unsorted order object.
int i;
for( i = 0; i< numcats; i++ ){
if(processPtr[i]){
if( strcmp( unSortPtr->order->category , processPtr[i]->order->category ) == 0 ){
//we have a category match, attach orderNode to front.
unSortPtr->next = processPtr[i];
processPtr[i] = unSortPtr;
}
}
else{
processPtr[i] = unSortPtr;
//no orderNode at i, create row for new category.
}
}
if(unSortPtr->next){
unSortPtr = unSortPtr->next;
}
}
}
custNode *readDatabase(FILE *fp){ //reads the database file for the customers
printf("reading database, creating structures.\n");
int i , size;
size_t fSize; //getline wants size_t ... dumb.
char *buff,*tok;
unsigned int error = 1;
char delim[3] = "|\"";
delim[2] = '\0';
custNode *result = NULL;
fseek(fp, 0L, SEEK_END);
size = ftell(fp);
fSize = size;
fseek(fp , 0 , SEEK_SET) ; //get size of file, reset fseek
printf("database of size %d \n", size);
buff = malloc( (sizeof(char)*size)+1);
/*
printf("Buffer is %s and error is %d \n", buff, error);
if( error <= 0 ){
printf("getline fell and couldn't get up in %s on line %d\n", __FILE__ , __LINE__);
break;
}
*/
while( error > 0 ){
error = getline(&buff, &fSize, fp);
if(error < 0){
//printf("breaking loop\n");
break;
}
customer *temp = (customer *)malloc(sizeof(customer));
tok = strtok(buff , delim);
//temp->name = tok; deprecated
temp->name = strdup(tok);
tok = strtok(NULL, delim);
temp->custID = atoi(tok);
tok = strtok(NULL, delim);
temp->credit = atof(tok);
//printf("added %lf to temp orderNode\n",temp->credit); //%f prints doubles.
tok = strtok(NULL, delim);
//temp->address = tok;
temp->address = strdup(tok);
tok = strtok(NULL, delim);
temp->state = strdup(tok);
tok = strtok(NULL, delim);
temp->zip = strdup(tok);
//printf("attaching temporary node \n");
result = attachCustNode(result,temp);
printf("result contains %s \n",result->customer->name );
printf("result ID is [%d]\n",result->customer->custID);
printf("result address is %s etc. \n", result->customer->address);
/*puts("\nprinting next node\n");
printf("resultNEXT contains %s \n",result->next->customer->name );
printf("resultNEXT ID is [%d]\n",result->next->customer->custID);
printf("resultNEXT address is %s etc. \n \n", result->next->customer->address);
*/
}
printf("\n\n Did we make it? \n");
printf("\n\n Oh, I think we did! \n");
printf("\n\n We, we are!! We're alive! \n");
printf("\n\n Master! we've made it to the promised land! \n");
printf( "\n\n\x1b[31m FUNCTION SUCCESS! \x1b[0m\n");
return result;
}
orderNode *readBookOrders(FILE *fp , int fSize){ //reads the book order file and sets up our queue
printf("Reading the book orders? Probably.\n");
char *orderscopy;
return;
}
int readCategories(FILE *fp){ //reads the categories from the file.
printf("Reading the category files... Probably.\n");
int i , fSize;
unsigned int error;
int result = 0;
char* temp;
char *buff;
fseek(fp, 0L, SEEK_END);
fSize = ftell(fp);
fseek(fp , 0 , SEEK_SET);
buff = malloc( (sizeof(char)*fSize)+1);
if( (error = fscanf(fp , "%s\n", buff ) ) == 0 ){
printf("failed to read file in %s on line %d :%s \n", __FILE__ , __LINE__ , strerror(error) );
_exit(1); //crash and burn, baby
}
result++;
while(error != -1){
error = fscanf(fp , "%s\n", buff );
if(error == EOF){
result--;
}
result ++;
}
return result;
}
void printReport(){
printf("printing report on the customers \n");
return;
}
void printOrders(orderNode *list){
if(!list){
printf("list empty. \n");
return;
}
orderNode *curr;
curr = list;
while(curr){
printf("current order in category %s the book %s \nfor customer number %d, processed value is [%c] \n ", curr->order->category,curr->order->bookTitle,curr->order->custID ,curr->order->processed);
//huge ass print statement. but useful as fuck.
/*
char *bookTitle;
int custID;
char *category;
char processed;
*/
if(curr->next){
if(curr->next->order){
curr = curr->next;
}
else{
printf("next orderNode contains empty order struct");
}
}
}
return;
}
void printCustomers(custNode *list){
if(!list){
printf("Customer list empty.\n");
return;
}
int len = 0;
custNode *curr;
curr = list;
customer *temp;
while(curr){
temp = curr->customer;
len++;
printf("Name:%s ID# %d has credit $%lf. Lives at %s, %s, %s.\n",temp->name,temp->custID,temp->credit,temp->address,temp->state,temp->zip);
//huge ass print statement. but useful as all hell.
if(curr->next){
if(curr->next->customer){
curr = curr->next;
}
else{
printf("next orderNode contains empty order struct");
break;
}
}
else{
break;
}
}
printf("reached end of list, list contained %d nodes", len);
return;
}
int main(int argc, char **argv){
if( argc != 4 ){
printf( "Must specify a Database File, book order file, and a category names. \n Feel Free to refer to the readme for usage.\n" );
_exit( 1 ); // crash and burn
}
else if ( !argv[1] ){ //sscanf returns the number of scanned arguments throws it into gcd -> a
printf( "Must specify Database File on the command line\n" );
_exit( 1 ); // crash and burn
}
else if ( !argv[2] ){
printf( "Must specify Book Order File on the command line\n" );
_exit( 1 ); // crash and burn
}
else if ( !argv[3] ){
printf( "Must specify Category File on the command line\n" );
_exit( 1 ); // crash and burn
}
unsigned int error;
FILE *fp;
if( (fp = fopen(argv[1], "r") ) == NULL){
printf("fopen didn't work in %s line %d: %s\n", __FILE__ , __LINE__ , strerror( error ) );
printf("Please Enter Valid Database File \n");
_exit(1); // crash and burn
}
else if( (fp = fopen(argv[2], "r") ) == NULL){
printf("fopen didn't work in %s line %d: %s\n", __FILE__ , __LINE__ , strerror( error ) );
printf("Please Enter Valid Book Order File \n");
_exit(1); // crash and burn
}
else if( (fp = fopen(argv[3], "r") ) == NULL ){
printf("fopen didn't in %s line %d: %s\n", __FILE__ , __LINE__ , strerror( error ) );
printf("Please Enter Valid Category File \n");
_exit(1); // crash and burn
}
/* Done with error checking. */
fp = fopen(argv[1], "r");
int sz;
custNode *customerList = (custNode *)malloc(sizeof(custNode));
customerList = readDatabase(fp);
printf("SUCCESS \n \n \n");
printCustomers(customerList);
if(error = fclose( fp ) > 0 ){
printf("fclose died in %s line %d: %s \n", __FILE__ , __LINE__ , strerror( error ) );
_exit(1); // crash and burn
}
fp = fopen(argv[2], "r");
fseek(fp, 0L, SEEK_END);
sz = ftell(fp);
fseek(fp , 0 , SEEK_SET) ;
printf("size of bookOrder file is %d \n",sz);
orderNode *unSortedQueue = (orderNode *)malloc(sizeof(orderNode));
unSortedQueue = readBookOrders(fp , sz);
/*
this gives us the full set of orders that we can
then separate into the array of smaller queues based on the category.
*/
if(error = fclose( fp ) > 0 ){
printf("fclose died slowly in %s line %d: %s\n", __FILE__ , __LINE__ , strerror( error ) );
_exit(1); // crash and burn
}
fp = fopen(argv[3], "r");
/*
fseek(fp, 0L, SEEK_END);
sz = ftell(fp);
fseek(fp , 0 , SEEK_SET);
printf("size of category file is %d \n",sz);
*/
int numcats = readCategories(fp);
printf( "There are %d catagories. \n", numcats );
if(error = fclose( fp ) > 0 ){
printf("fclose died slowly in %s line %d: %s\n", __FILE__ , __LINE__ , strerror( error ) );
_exit(1); // crash and burn
}
orderNode **processQueue = malloc(sizeof(orderNode)*numcats) ;
//sort the orders into their respective categories
sortOrders(processQueue, unSortedQueue, numcats);
// process the book orders by creating different threads out of the linked list of book orders.
processBookOrders(processQueue , customerList , numcats);
printReport();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment