-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSorter.c
More file actions
199 lines (179 loc) · 5.07 KB
/
Copy pathFileSorter.c
File metadata and controls
199 lines (179 loc) · 5.07 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
** Name: Jack Lardner
** Student ID: 183*****
**
** Contains main method, user input menu and insertion sort method
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "FileSorter.h"
#include "LinkedList.h"
#include "FileFunctions.h"
int main(int argc, char* argv[])
{
/*
Return values:
0: Success
-1: Invalid command line arguments
-2: FileIO error (file does not exist, unable to write, etc)
*/
int retVal = 0;
/* checking validity of command line arguments */
/* if there aren't 5 arguments then the program has been used incorrectly */
if (argc < 5)
{
retVal = -1;
printf("Not enough arguments\n");
printUsage();
}
else if (argc > 5)
{
retVal = -1;
printf("Too many arguments\n");
printUsage();
}
/* checking the 1st argument is a valid switch */
else if ((strcmp(argv[1], "i") != 0) && (strcmp(argv[1], "o") != 0))
{
retVal = -1;
printf("%s is an invalid switch\n", argv[1]);
printUsage();
}
/* checking the 3rd argument is a valid switch */
else if ((strcmp(argv[3], "i") != 0) && (strcmp(argv[3], "o") != 0))
{
retVal = -1;
printf("%s is an invalid switch\n", argv[3]);
printUsage();
}
/* checking there is exactly one input and output file */
else if (strcmp(argv[1], argv[3]) == 0)
{
retVal = -1;
printf("Too many input/output files\n");
printUsage();
}
/* continue with program */
else
{
/* variable declarations */
LinkedList* list;
char *inFile, *outFile;
int readExitCode, writeExitCode, sortType, sortColumn;
#ifdef DEBUG
int ii;
printf("\n~ Command line arguements passed:\n");
for (ii = 0; ii < argc; ii++)
{
printf("~ argv[%d]: %s\n", ii, argv[ii]);
}
printf("\n\n");
#endif
/* Warn the user if the input and output file are the same */
if (strcmp(argv[2], argv[4]) == 0)
{
printf("WARNING: The input file will be overwritten\n");
}
/* if/else statement to correctly assign inFile and outFile variables */
if (strcmp(argv[1], "i") == 0)
{
inFile = argv[2];
outFile = argv[4];
}
else
{
inFile = argv[4];
outFile = argv[2];
}
#ifdef DEBUG
printf("\n~ inFile: %s\n~ outFile: %s\n\n", inFile, outFile);
#endif
/* read file and store in linked list */
list = readFile(inFile, &readExitCode);
#ifdef DEBUG
printf("\n~ readFile exit code: %d\n\n", readExitCode);
#endif
/* error handling */
if (readExitCode != 0)
{
retVal = -2;
}
else
{
/* asks for user input */
sortColumn = menu(list, &sortType);
/* create a sorted list */
list = insertionSort(list, sortColumn, sortType);
/* write linked list to file */
writeExitCode = writeFile(list, outFile, inFile);
if (writeExitCode != 0)
{
retVal = -2;
}
#ifdef DEBUG
printf("\n~ writeFile exit code: %d\n\n", writeExitCode);
#endif
freeList(list);
}
}
return retVal;
}
/*
Prints usage method
Neater to have one method to print it
than repeat lines in argv[] check in main()
*/
void printUsage()
{
printf("Usage: FileSorter [OPTION] [FILE] [OPTION] [FILE]\n");
printf(" i\tFlag for input file to sort\n");
printf(" o\tFlag for output file to put sorted contents of input file\n");
#ifdef DEBUG
printf("\n~ Return values:\n");
printf("~ 0: Success\n");
printf("~ -1: Incorrect command line arguments passed\n");
printf("~ -2: FileIO error\n");
#endif
}
/*
menu for user input
asks for column to sort and how to sort it
*/
int menu(LinkedList* list, int* sortType)
{
int ii;
int column;
char numbers[] = "1234567890";
printf("Which column would you like sorted?: \n");
do
{
for (ii = 0; ii < list->numAttributes; ii++)
{
printf("(%d): Column %d\n", ii + 1, ii + 1);
}
scanf("%d", &column);
}
while (column > list->numAttributes || column <= 0);
do
{
/*
if column is a string datatype, swap sortType around
this is because of the way strcmp compares strings
*/
if (strpbrk(list->head->data[column - 1], numbers) == NULL)
{
printf("(1): Descending order\n");
printf("(2): Ascending order\n");
scanf("%d", sortType);
}
else
{
printf("(1): Ascending order\n");
printf("(2): Descending order\n");
scanf("%d", sortType);
}
}
while (*sortType > 2 || *sortType < 1);
return column - 1;
}