Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
/***************************************
This program shall SORT using a binary tree.
©2005 Mark Crick
****************************************/
typedef struct SBinaryTree
{
char *SBinaryTreeData;
struct SBinaryTree *SBinaryTreeLeft;
struct SBinaryTree *SBinaryTreeRight;
} nBinaryTreeNode;
char *dilimeter;
unsigned int columNumber;
char byColum = 'f';
nBinaryTreeNode *CreateBTNode (char *SBinaryTreeData, int dataSize)// Create tree
{
nBinaryTreeNode *nTempNode = malloc(sizeof (nBinaryTreeNode));
nTempNode->SBinaryTreeData = (char *)calloc (dataSize, sizeof (char));
strncpy (nTempNode->SBinaryTreeData, SBinaryTreeData,dataSize);
nTempNode->SBinaryTreeLeft = NULL;
nTempNode->SBinaryTreeRight = NULL;
return nTempNode;
}
char *splitStringToDilimeter(char *string)//Split string according to dilimeter
{ //And return the substring specified by
char *retVal,*tmpStr; //columNumber
unsigned int count = 0;
size_t sublen ;
tmpStr = strdup(string);
retVal = strtok(tmpStr,dilimeter);
do
{
count++;
if(count == columNumber)
{
break;
}
}
while((retVal = strtok(NULL,dilimeter)));
return retVal;
}
int sortTree(char *string1, char* string2)
{
char *tempString = splitStringToDilimeter(string1);
char *tempString2 = splitStringToDilimeter(string2);
//printf("String1 : %s\nString2 : %s\ntempString : %s\ntempString2 : %s\n",string1,string2,tempString,tempString2);
return strcmp(tempString,tempString2);
}
nBinaryTreeNode *AddBTNode (char *line, nBinaryTreeNode *BTreeRootIn )
{
if (BTreeRootIn == NULL)
return CreateBTNode (line,BUFSIZ);
if(byColum == 't') // If /c flag has been used use sortTree rather then strcmp
{
if (sortTree (line, BTreeRootIn->SBinaryTreeData) < 0)
BTreeRootIn->SBinaryTreeLeft = AddBTNode (line, BTreeRootIn->SBinaryTreeLeft);
else
BTreeRootIn->SBinaryTreeRight = AddBTNode (line, BTreeRootIn->SBinaryTreeRight);
}
else
{
if (strcmp (line, BTreeRootIn->SBinaryTreeData) < 0)
BTreeRootIn->SBinaryTreeLeft = AddBTNode (line, BTreeRootIn->SBinaryTreeLeft);
else
BTreeRootIn->SBinaryTreeRight = AddBTNode (line, BTreeRootIn->SBinaryTreeRight);
}
return BTreeRootIn;
}
void WriteFileFromTree(nBinaryTreeNode *BTRootOut , FILE *filePtr)
{ // Recursive method writes file according to tree struct
if (BTRootOut->SBinaryTreeLeft != NULL)
WriteFileFromTree (BTRootOut->SBinaryTreeLeft,filePtr);
fprintf (filePtr, "%s", BTRootOut->SBinaryTreeData);
if (BTRootOut->SBinaryTreeRight != NULL)
WriteFileFromTree (BTRootOut->SBinaryTreeRight,filePtr);
}
void OpenWriteFileFromTree(char *fileName,nBinaryTreeNode *BTRootOut)//Opens file to write to then calls
{ //method to write tree to file
FILE *filePtr;
if((filePtr = fopen(fileName,"w")) != NULL)
{
WriteFileFromTree(BTRootOut,filePtr);
if(fclose(filePtr)==EOF)
{
perror("Sort");
exit(1);
}
}
else
{
fclose(filePtr); //Didnt catch close errors here because there is allready an open error.
perror("Sort"); // And code would be the same if there was error or no error
exit(1);
}
}
nBinaryTreeNode *ReadFileToTree(char *fileName)// This will read the text file into the buffer array
{
FILE *filePtr;
nBinaryTreeNode *BTRoot = NULL;
if((filePtr = fopen(fileName,"r")) != NULL)
{
char szReadLine[BUFSIZ]= {0};
while (fgets(szReadLine, sizeof(szReadLine), filePtr) != NULL)
{
BTRoot = AddBTNode (szReadLine, BTRoot);
}
if(fclose(filePtr)==EOF)
{
perror("Sort");
exit(1);
}
}
else
{
fclose(filePtr); //Didnt catch close errors here because there is allready an open error.
perror("Sort"); // And code would be the same if there was error or no error
exit(1);
}
return BTRoot;
}
void getInput(char *question, char array[BUFSIZ])
{
char *arrayPtr = array;
int i;
printf(question);
fgets(array,BUFSIZ,stdin);
for(i = 0;i < BUFSIZ;i++, arrayPtr++)
{
if(*arrayPtr == '\n')
{
arrayPtr++;
*arrayPtr = '\0';
break;
}
}
}
int main(int argc, char **argv)
{
if(argc == 2)
{
nBinaryTreeNode *BTRoot = ReadFileToTree(argv[1]);
OpenWriteFileFromTree(argv[1],BTRoot);
printf("Sort.exe wOOt!. \n");
}
else if(argc == 3 && (strcmp(argv[2],"/c") == 0) )
{
nBinaryTreeNode *BTRoot;
char input[BUFSIZ];
byColum = 't';
dilimeter = malloc(sizeof(input));
getInput("Please enter the field Delimiter: \n",dilimeter);
getInput("Please enter the colum number to sort by: \n",input);
columNumber = strtol(input,NULL,10);
BTRoot = ReadFileToTree(argv[1]);
OpenWriteFileFromTree(argv[1],BTRoot);
free(dilimeter);
printf("Sort.exe wOOt!. \n");
}
else if(argc == 4 && (strcmp(argv[3],"/c") == 0) )
{
nBinaryTreeNode *BTRoot;
char input[BUFSIZ];
byColum = 't';
dilimeter = malloc(sizeof(input));
getInput("Please enter the field Delimiter: \n",dilimeter);
getInput("Please enter the colum number to sort by: \n",input);
columNumber = strtol(input,NULL,10);
BTRoot = ReadFileToTree(argv[1]);
OpenWriteFileFromTree(argv[2],BTRoot);
free(dilimeter);
printf("Sort.exe wOOt!. \n");
}
else
{
printf("Sort: Incorrect argument \nSyntax: Sort [file to sort] [file to write sorted data too:Optional] \n/c \t \t \t \t Sort by colum \nPress Any key to Continue\n");
exit(1);
}
return(0);
}