-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileIO.c
190 lines (160 loc) · 6.19 KB
/
fileIO.c
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
/* Jeanine Fallen Bailey, Michael Nguyen, Pasha Pourmand */
/* 2013/06/20 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "userIO.h"
#include "fileIO.h"
#include "team.h"
#include "bst.h"
#include "stack.h"
#include "hash.h"
//===================================================================
// Returns the number of companies in the input file
//===================================================================
int getArrSize(FILE *in) {
int size = 0;
char temp[MAX_CHARS];
while (fgets(temp, MAX_CHARS, in) != NULL)
size++;
fclose(in);
return size * 2; //return double the array size to fit in new user records
}
//===================================================================
// Reads in the data from a file and inserts into BST and hashed
// array.
//===================================================================
void readInFile(DATA_HEAD *data) {
FILE *dataFile;
COMPANY *companyNode;
char filename[MAX_CHARS], tmpName[MAX_CHARS];
char ch;
int tmpRev, tmpProfit, tmpNumEmployee;
int i = 0, isDuplicate, len, size;
do {
printf("Please enter a filename [enter for default]:");
fgets(filename, MAX_CHARS, stdin);
if (filename[0] == '\n')
strcpy(filename, default_file);
else {
//flush new line
len = strlen(filename);
if (filename[len - 1] == '\n' || filename[len - 1] == '\r')
filename[len - 1] = '\0'; // change '\n' to '\0'
else // no '\n' read, so flush to '\n'
while ((ch = getchar()) != '\n' && ch != '\r');
}
dataFile = fopen(filename, "r");
} while(!dataFile);
//Creates structures
data->pTree = createBST(myStringCompare);
data->pStack = createStack();
size = getArrSize(dataFile);
//need to reopen connection
dataFile = fopen(filename, "r");
data->arraySize = size;
data->count = 0;
data->pHash = (HASH *)malloc(sizeof(DATA_HEAD) *data->arraySize);
//initialize hashed array
for (i = 0; i < data->arraySize; i++) {
data->pHash[i].status = 0;
data->pHash[i].numOfCollisions = 0;
data->pHash[i].numOfProbes = 0;
data->pHash[i].hashData = NULL;
}
//Reads in, parses, mallocs, assigns
while (fscanf(dataFile, " %[^,],%d,%d,%d[^\n]", tmpName, &tmpRev, &tmpProfit, &tmpNumEmployee) != EOF)
{
companyNode = (COMPANY *)malloc(sizeof(COMPANY));
//Checks to see if allocated properly
if (!companyNode)
exit(1);
companyNode->companyName = (char *)malloc(strlen(tmpName) + 1);
//Checks to see if allocated properly
if (!(companyNode->companyName))
exit(1);
strcpy(companyNode->companyName, tmpName);
companyNode->numberOfEmployees = tmpNumEmployee;
companyNode->revenuePerBillion = tmpRev;
companyNode->profitPerMillion = tmpProfit;
//Inserts company data (name, revenue, profit, employees) into the BST
//If duplicate, print an error
isDuplicate = searchHash(data, tmpName, companyNode);
if (isDuplicate == 1)
printf("ERROR: DUPLICATE DATA\n");
else
{
insertHash(data, companyNode);
insertBST(data->pTree, companyNode);
data->count++;
}
}
printf("\nNumber Of Data Records read in: %d\n\n", data->count);
fclose(dataFile);
}
//===================================================================
// Compare function for BST
//===================================================================
int myStringCompare(void *pcompany1, void *pcompany2) {
//strcasecmp is case insensitive compare
return strcasecmp( ((COMPANY *)pcompany1)->companyName, ((COMPANY *)pcompany2)->companyName);
}
//===================================================================
// Cleans up and frees all data
//===================================================================
void cleanupData(DATA_HEAD *data) {
destroyBST(data->pTree, freeBST);
destroyStack(data->pStack);
free(data->pHash);
free(data);
}
//===================================================================
// Frees BST nodes
//===================================================================
void freeBST(void *companyData) {
free(((COMPANY *)companyData)->companyName);
free((COMPANY *)companyData);
}
//===================================================================
// Writes to output file and calls function to clear out the stack
//===================================================================
void saveToFile(DATA_HEAD *data) {
COMPANY *temp;
FILE *outputFile;
char filename[MAX_CHARS];
int i, len;
char ch;
do {
printf("Please enter a filename for output file [enter for default]:");
fgets(filename, MAX_CHARS, stdin);
if (filename[0] == '\n')
strcpy(filename, "out.txt");
else {
//flush new line
len = strlen(filename);
if (filename[len - 1] == '\n' || filename[len - 1] == '\r')
filename[len - 1] = '\0'; // change '\n' to '\0'
else
while ((ch = getchar()) != '\n' && ch != '\r');
}
outputFile = fopen(filename, "w");
} while (!outputFile);
//Writes to output file in hashed sequence
printf("Writing to %s...\n", filename);
for (i = 0; i < data->arraySize; i++) {
if (data->pHash[i].status == 1) //status is filled, data exists at index
fprintf(outputFile, "%s,%d,%d,%d\n", data->pHash[i].hashData->companyName, data->pHash[i].hashData->revenuePerBillion, data->pHash[i].hashData->profitPerMillion, data->pHash[i].hashData->numberOfEmployees);
}
//need to free the company name as well as the company
//modifying stack ADT does not work since this is specific to this program
while (!emptyStack(data->pStack)) {
temp = (COMPANY *)popStack(data->pStack); //pop everything from the stack
free(temp->companyName); //free company name and then company
free(temp);
}
data->pStack->count = 0; //reset stack
data->pStack->top = NULL;
fclose(outputFile);
printf("\n");
}