1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "List-Driver.h"
#define LINE_LENGTH 128
#define INITIAL_VALUE 1
//Open the file that will be use to test the function CreateList
FILE *OpenFile(const char *fileName)
{
FILE *FilePointer;
if ((FilePointer = fopen(fileName, "r" )) == NULL)
{
fprintf(stderr, "Can't open \"filename\"\n");
exit(EXIT_FAILURE);
}
return(FilePointer);
}
//This function will create a new LIST
LIST *CreateNewNode()
{
LIST *NewNode;
if ((NewNode =(LIST*) malloc( sizeof(LIST) )) == NULL)
{
fprintf(stderr, "Can't create Node \"pntr\"\n");
exit(EXIT_FAILURE);
}
return(NewNode);
}
LIST *CreateList(FILE *fp)
{
//variable used on the linklist
int createHeaderDone = 0, duplicateFound = 0, doneProcessingTheline = 0;
LIST *headerList = NULL, *currentList = NULL, *traverserList = NULL;
//Variable used on parsing the file
char oneLine[LINE_LENGTH], *cutString, *delimiter = " ";
int leadingSpace;
while (fgets(oneLine, LINE_LENGTH, fp))
{
//skip the leading white space
leadingSpace = 0;
while (isspace(oneLine[leadingSpace++]))
;
//this portion gets the section of a string up to but not including the delimiter.
//cutString = strtok(oneLine, delimiter);
//Create a header with one list element, this only happens once, only during the beginning when the file
//is read for the first time.
if (!createHeaderDone)
{
headerList = NULL;
cutString = strtok(oneLine, delimiter);
currentList = CreateNewNode();
currentList->next = headerList;
headerList = currentList;
headerList->str = cutString;
headerList->count = INITIAL_VALUE;
createHeaderDone = 1;
}
//this only happens once when reading the line
if (doneProcessingTheline)
{
cutString = strtok(oneLine, delimiter);
currentList = CreateNewNode();
currentList->next = headerList;
headerList = currentList;
currentList->count = INITIAL_VALUE;
currentList->str = cutString;
doneProcessingTheline = 0;
}
while (cutString != NULL)
{
cutString = strtok(NULL, delimiter);
currentList = CreateNewNode();
currentList->next = headerList;
headerList = currentList;
headerList->count = INITIAL_VALUE;
headerList->str = cutString;
}
//Set the Flag indicating that the end of the line has been reached
doneProcessingTheline = 1;
}
getchar();
return(headerList);
}
|