-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.c
294 lines (218 loc) · 8.28 KB
/
functions.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*-----------------------------------------------------------------------------+
File Name: functions.c
+------------------------------------------------------------------------------+
+------------------------------------------------------------------------------+
Programming III COP4338
Author: Daniel Gonzalez P#4926400
assignment 5: Date Validate / Format
Date: 11/08/2016 ELLECTION DAY!!!!
program description
Input: Accept input for the first program via the command-line arguments.
Input will be the number of valid entries to be redirected from
the dates input file (dates.dat). A zero indicates to input all
entries from the dates input file. Your program should validate
(day, month & year - see page 111 for validation ideas) and skip
corrupt dates in the dates.dat file (see page 159 for scanning
ideas). This validated input will then be piped out to the
second program.
The second program will accept these validated dates in the
month/day/year format and convert each of them to the day,
abbreviated month & year format — both exhibited above. The
abbreviated month should consist of the first three letters of
the month, capitalized. These converted results will be redirected
to the output file (output.dat), followed by a copy of the
original (dates.dat) data.
Output: Generates an output file (output.dat) that contains a
converted list of dates in day, abbreviated month & year
format (i.e. 1 JAN 1900), followed by the original list of
dates in month/day/year format (i.e. 1/1/1900). This output file
will be the result of appending the input file (dates.dat), which
is accessed by the first program, with the result output file
(output.dat), generated by the second program.
+-------------------------------------------------------------------------+
| I Daniel Gonzalez #4926400 hereby certify that this collective work is |
| my own and none of it is the work of any other person or entity. |
+-------------------------------------------------------------------------+
how to compile and execute:
1.Open the terminal
Go to the program folder that contains all the files required for
the program to compile including all header files(*.h).
Run the following command "make"
2.Open the terminal
Go to the program folder that contains all the files required for
the program to compile including all header files(*.h).
COMPILE: "gcc -Wall -w -lm readDate.c functions.c -o validateDate"
"gcc -Wall -w -lm format.c functions.c -o format"
Program execution:
From the terminal enter:
“./validateDate < dates.dat [X] | ./format > output.dat"
X: is the amount of validated dates
+-----------------------------------------------------------------------------*/
#include "general.h"
/**----------------------------------------------------------------------------+
* Adds line to a LineList structure
* allocating more memory if necessary.
* @param lineList [The list of lines to append lines to.]
* @param line [The line to be appended to the list]
* @return [TRUE if success otherwise FALSE]
*/
Boolean appendToLineList(LineList * lineList, const char *line){
/* Initialize variables */
int newCapacity = 0;
newCapacity = lineList->capacity * 2;
Line * temp = NULL;
if (lineList->size == lineList->capacity){
temp = (Line *) realloc(lineList->lines,
sizeof(lineList->lines[0]) * newCapacity);
if (temp != NULL){
lineList->capacity = newCapacity;
lineList->lines = temp;
}else{
return FALSE;
}
}else{
}
lineList->lines[lineList->size++].content[0] = '\0';
strncat(lineList->lines[lineList->size++].content, line, MAX_LINE);
return TRUE;
}
/**----------------------------------------------------------------------------+
* Frees the allocated memory from a LineList.
* @param lineList [lineList pointer to be deallocated]
*/
void freeLineList(LineList * lineList){
free(lineList->lines);
lineList->capacity = 0;
lineList->size = 0;
}
/**----------------------------------------------------------------------------+
* Initializes the DateKeys object and returns it
* @param line [line to be scanned for date]
* @return [dateKey object initialized if valid]
*/
DateKey getDateKeys(char * line){
/* Initialize variables */
DateKey tempDate;
double day = 0.0;
double month = 0.0;
double year = 0.0;
/* try to get three variales*/
if (sscanf(line, "%lf/%lf/%lf", &month, &day, &year) != VALID_KEYS){
tempDate.error = ERRORS_UNABLE_TO_READ;
return tempDate;
}else{
}
if (hasValidDateType(&tempDate, month, day, year)){
tempDate.day = (int) day;
tempDate.month = (int) month;
tempDate.year = (int) year;
}else{
return tempDate;
}
if(isValidDate(&tempDate)){
tempDate.error = NO_ERRORS;
}
return tempDate;
}
/**----------------------------------------------------------------------------+
* Checks if the values for the Date are valid
* @param date [date to check]
* @param month [month value]
* @param day [day value]
* @param year [year value]
* @return [true if valid otherwise is false]
*/
Boolean hasValidDateType(
DateKey * date,
double month,
double day,
double year
){
if ((fmod(day, 1.0) != 0.0) ||
(fmod(month, 1.0) != 0.0) ||
(fmod(year, 1.0) != 0.0)){
date->error = ERRORS_DECIMALS_IN_DATE;
return FALSE;
}else{
}
if ((day < INT_MIN || day > INT_MAX) ||
(month < INT_MIN || month > INT_MAX) ||
(year < INT_MIN || year > INT_MAX)){
date->error = ERRORS_HUGE_DATE;
return FALSE;
}else{
}
date->error = NO_ERRORS;
return TRUE;
}
/**----------------------------------------------------------------------------+
* Allocates memory for a line list to hold all
* the values from the input stream
* @param lineList [pointer to be initialized]
*/
void initializeLineList(LineList * lineList){
lineList->capacity = MIN_FILE_LINES;
lineList->lines = (Line *) malloc(sizeof(lineList->lines[0]) *
lineList->capacity);
lineList->size = 0;
}
/**----------------------------------------------------------------------------+
* [Determines if it is a leap year]
* @param year [year value]
* @return [returns true if leap year otherwise false]
*/
Boolean isLeapYear(int year){
return ((year % LEAP_YEAR) == 0)? TRUE : FALSE;
}
/**----------------------------------------------------------------------------+
* Check is the Date has valid values for the date
* @param date [date to be checked]
* @return [returns true if valid date otherwise false]
*/
Boolean isValidDate(DateKey * date){
int monthDays [] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if(isLeapYear(date->year)){
monthDays[1] = 29;
}else{
}
if (date->month < 1 || date->month > 12){
date->error = ERRORS_INVALID_MONTH;
return FALSE;
}else{
}
if (date->day < 1 || date->day > monthDays[date->month - 1]){
date->error = ERRORS_INVALID_DAY;
return FALSE;
}else{
}
return TRUE;
}
/**----------------------------------------------------------------------------+
* pProcedure to print an error if the date could not be read or parsed
* @param stErr [the string to put the error]
* @param line [line with the error]
* @param error [error number]
*/
void printError(char * stErr, char * line, Error error){
strcpy(stErr, ERROR_DESCRIPTIONS[error]);
strncat(stErr,":\t ",MAX_LINE);
strncat(stErr, line, MAX_LINE);
fprintf(stderr,"%s", stErr);
}
/**----------------------------------------------------------------------------+
* [Prints a date in a specific format]
* @param strIn [Buffer to fill with the output.]
* @param format [The format to use when printing.]
* @param date [Date with all the values]
*/
void printfDate(char * strIn, Format format, DateKey * date){
char monthName [MAX_MONTH_NAME];
strcpy(monthName, MONTH_NAMES_SHORT[date->month]);
if (format == DATE_FORMAT_SHORT_MONTH){
snprintf(strIn, MAX_LINE, "%2d %s %5d\n",
date->day, monthName, date->year);
}else {
snprintf(strIn, MAX_LINE, "%d/%d/%d\n",
date->month, date->day, date->year);
}
}