-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcsvConverter.c
185 lines (160 loc) · 5.25 KB
/
csvConverter.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/* additional imports */
#include "csvConverter.h"
#include "linkedList.h"
#include "timeManager.h"
/* Placeholder string length to write to CSV */
#define MAX_LENGTH 100
/*
Helper function to print each row of rowData
*/
void printRowData(char** array, int size) {
int i;
for (i = 0; i < size; i++) {
printf("rowData[%d]: %s\n", i, array[i]);
}
}
/*
Writes each occupied row / timeslot to the CSV file
*/
void writeArrayToCSV(FILE* file, char** array, const time_t startTime, const time_t* endTime, size_t size) {
size_t i;
char* startDate;
char* endDate;
/* Write start date to csv */
startDate = ctime(&startTime);
startDate[strlen(startDate)-1] = '\0';
fprintf(file, "%s,", startDate);
/* Write end date to csv */
endDate = ctime(endTime);
endDate[strlen(endDate)-1] = '\0';
fprintf(file, "%s,", endDate);
/* Write occupied classes to csv */
for (i=0; i<size; i++) {
fprintf(file, "%s", array[i]);
/* Only add comma if element is not the last */
if (i != size-1) {
fprintf(file, ",");
}
}
/* Move file pointer to the next row */
fprintf(file, "\n");
}
/*
Writes 'FREE' to denote all members having free timeslots to the CSV file
*/
void writeFreetoCSV(FILE* file, const time_t* startTime, const time_t endTime, size_t size) {
size_t i;
char* startDate;
char* endDate;
/* write start date to csv */
startDate = ctime(startTime);
startDate[strlen(startDate)-1] = '\0';
fprintf(file, "%s,", startDate);
/* write end date to csv */
endDate = ctime(&endTime);
endDate[strlen(endDate)-1] = '\0';
fprintf(file, "%s,", endDate);
/* write FREE to csv */
for (i=0; i<size; i++) {
fprintf(file, "FREE");
/* only add comma if element is not the last */
if (i != size-1) {
fprintf(file, ",");
}
}
/* move file pointer to the next row */
fprintf(file, "\n");
}
/*
Process a single TimeNode
*/
void processTimenode(timeNode* node, FILE* output, time_t* endTime, char** studentIDS, int numberOfStudents) {
int i;
char** rowData;
time_t currentTime;
dataNode* data;
rowData = malloc(numberOfStudents * sizeof(char*));
for (i = 0; i < numberOfStudents; i++) {
rowData[i] = malloc(MAX_LENGTH * sizeof(char));
}
currentTime = node->startTime;
data = node->data;
/* Populate rowData array with empty strings*/
for (i = 0; i < numberOfStudents; i++) {
strncpy(rowData[i], "", MAX_LENGTH);
}
/* Check if start time corresponds to end time*/
if (difftime(currentTime, *endTime) > 0) {
/* write to CSV as 'FREE' time if there's a time gap*/
writeFreetoCSV(output, endTime, currentTime, numberOfStudents);
/* set new endTime pointer */
*endTime = currentTime;
}
/* Else, timeslot is occupied by someone */
if (difftime(currentTime, *endTime) == 0) {
/* Set new endTime pointer */
*endTime = currentTime + 1800;
/* Traverse data node to populate rowData*/
/* Print all attributes in data */
while (data != NULL) {
for (i = 0; i < numberOfStudents; i++) {
if (data->studentID == atoi(studentIDS[i])) {
strncpy(rowData[i], data->className, strlen(data->className));
break;
}
}
data = data->next;
}
/* printRowData(rowData, numberOfStudents); */
writeArrayToCSV(output, rowData, currentTime, endTime, numberOfStudents);
/* free row data */
for (i = 0; i < numberOfStudents; i++) {
free(rowData[i]);
}
free(rowData);
}
return;
}
void processLinkedList(timeLinkedList *list, char **studentIDS, int numberOfStudents) {
int i;
char* fixedHeader;
char* rowHeader;
FILE *output;
time_t* endTime = malloc(sizeof(time_t));
/* set node head and end time of first timeNode */
timeNode *current = list->head;
*endTime = current->startTime;
/* Open the output CSV file */
output = fopen("output/output.csv", "w");
/* Check for invalid CSV file opening */
if (output == NULL) {
printf("Error opening file!\n");
exit(1);
}
/* Form a string to formulate the rows in the csv: 'Start, End, StudentID1, StudentID2...' */
fixedHeader = "Start,End,";
rowHeader = malloc(strlen(fixedHeader) + 1);
strcpy(rowHeader, fixedHeader);
for (i = 0; i < numberOfStudents; i++) {
rowHeader = realloc(rowHeader, strlen(rowHeader) + strlen(studentIDS[i]) + 1);
strcat(rowHeader, studentIDS[i]);
if (i != numberOfStudents - 1) {
rowHeader = realloc(rowHeader, strlen(rowHeader) + 1);
strcat(rowHeader, ",");
}
}
/* Write column names to CSV */
fprintf(output, "%s\n", rowHeader);
/* Loop through linked list until NULL */
while (current != NULL) {
processTimenode(current, output, endTime, studentIDS, numberOfStudents);
current = current->next;
}
/* Close output CSV file */
fclose(output);
free(endTime);
}