-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.h
354 lines (341 loc) · 9.77 KB
/
data.h
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
================================================================================
Name : data.h
Author : Randu Karisa
Version :
Copyright : Your copyright notice
Description : Set of functions to get and manipulate data
================================================================================
*/
#ifndef DATA_H
#define DATA_H
#include <string.h>
#include <ctype.h>
#include "array.h"
#define FALSE 0
#define TRUE 1
/**
*isNumber function, Finds out whether the string provided is a number
*@param string, The number represented in a string
*@return integer, Holding either zero for false or 1 for true
*/
int isNumber(const char* string)
{
int hasPoint = FALSE;
for (int i = 0; i < strlen(string); ++i) {
if(isdigit(string[i]) == FALSE){
if((char) string[i] == '.'){
if(hasPoint == TRUE)
return FALSE;
}else
return FALSE;
}
if((char) string[i] == '.') hasPoint = TRUE;
}
return TRUE;
}
/**
* stringSplit function, Splits a string with the given char
* @param string, the string to split
* @param splitter, the character splitter
* @return pointer to StringArray structure in heap
*/
struct StringArray* stringSplit(const char* string,const char splitter)
{
char temp[1000];
int j = 0;
int k = 0;
struct StringArray* str = newStringArray();
for(size_t i = 0; i < strlen(string); ++i){
if(string[i] == splitter || string[i] == '\n')
{
temp[j] = '\0';
addToStringArray(str,temp);
k++;
j=0;
temp[0] = '\0';
}else if(i == strlen(string)-1)
{
temp[j] = string[i];
temp[j+1] = '\0';
addToStringArray(str,temp);
}
else
{
temp[j] = string[i];
j++;
}
}
return str;
}
/**
* stringIsIn function, checks if a string is present in a
* given array
* @param array, an array of strings
* @param string, the string to check
* @return integer 0 for false or 1 for true
*/
int stringIsIn(const struct StringArray* array,const char* string)
{
for(int i = 0; i < array->size; ++i)
if(strcmp(*(array->array+i),string) == 0)
return TRUE;
return FALSE;
}
/**
* integerIsIn function, checks if a integer is present in a
* given array
* @param array, an array of integers
* @param value, the integer to check
* @return integer 0 for false or 1 for true
*/
int integerIsIn(const struct IntegerArray* array,const int value)
{
for(int i = 0; i < array->size; ++i)
if(*(array->array+i) == value)
return TRUE;
return FALSE;
}
/**
* doubleIsIn function, checks if a double is present in a
* given array
* @param array, an array of double
* @param value, the double to check
* @return integer 0 for false or 1 for true
*/
int doubleIsIn(struct DoubleArray* array, double value)
{
for(int i = 0; i < array->size; ++i)
if(*(array->array+i) == value)
return TRUE;
return FALSE;
}
/**
*setOfStringArray function, gets a set of strings in an array
* i.e removes duplicates
*@param array, the array to get a set from
*@return arrayset, a pointer to a stringArray struct in heap
*/
struct StringArray* setOfStringArray(struct StringArray* array)
{
struct StringArray* set = newStringArray();
for (int i = 0; i < array->size; ++i)
if(stringIsIn(set,*(array->array+i)) == 0)
addToStringArray(set, *(array->array+i));
return set;
}
/**
*stringCat function, Concatenates a string with an integer
*@param string, reference to a pointer of characters
*@param value, The integer to concatenate
*/
void stringCat(char **string, int value)
{
int len = (int) log10(value) + 3;
char num[len];
char str[len+strlen(*string)];
strcpy(str,*string);
sprintf(num,"%ld",value);
strcat(str,num);
*string = (char*) str;
}
/**
* importData function, Imports the data from a text file
* @param filename, string representing the file name to open
* @param spliterChar, the character used as a separator in the text file
* @param indexColumn, Column from which index keys will be generated in
* order to have fast data retrieval.
* @return a pointer to a String2DArray data structure in heap
*/
struct String2DArray* importData(const char* filename,const char spliterChar,const int indexColumn)
{
FILE *file = fopen(filename,"r");
if(!file)
{
printf("File '%s' failed to open\n",filename);
exit(1);
}
char* line = "";
size_t size = 0;
int i = 0;
struct String2DArray* data = newString2DArray();
while(-1 != getline(&line,&size,file))
{
struct StringArray* row = stringSplit(line,spliterChar);
addToString2DArray(data,row,indexColumn);
i++;
}
free(line);
fclose(file);
return data;
}
/**
*getIntegerArray function, Converts an string array to an integer array
*@param array, The string array
*@return array, A converted integer array
*/
struct IntegerArray* getIntegerArray(const struct StringArray* array)
{
struct IntegerArray* intArray = newIntegerArray();
for (int i = 0; i < array->size; ++i) {
const char* str = getString(array, i);
if(isNumber(str))
addToIntegerArray(intArray, atoi(str));
else
{
printf("StringArray Contains Non-Integer Values\n");
exit(1);
}
}
return intArray;
}
/**
*getDoubleArray function, Converts the string array to a double array
*@param array, The string array to convert
*@return array, The double array
*/
struct DoubleArray* getDoubleArray(const struct StringArray* array)
{
struct DoubleArray* doubleArray = newDoubleArray();
for (int i = 0; i < array->size; ++i) {
const char* str = getString(array, i);
if(isNumber(str))
addToDoubleArray(doubleArray, atof(str));
else
{
printf("StringArray Contains Non-Double Values\n");
exit(1);
}
}
return doubleArray;
}
/**
*getColumnStringArray function, Gets a column from a 2D array as an array
*@param array, The 2D array to get the column
*@return array, A column from the 2D array
*/
struct StringArray* getColumnStringArray(const struct String2DArray* array,const int columnIndex)
{
if(array->size < 0)
{
printf("Array is Empty\n");exit(1);
}
if(columnIndex < (*array->stringArray)->size && columnIndex > -1 )
{
struct StringArray* column = newStringArray();
for (int i = 0; i < array->size; ++i) {
addToStringArray(column, getString(*(array->stringArray+i), columnIndex));
}
return column;
}
printf("RuntimeError: Index %d is Out of Array Bounds, Size of Array %d\n",columnIndex,getStringArraySize(*array->stringArray)-1);
exit(1);
}
/**
*filterString2DArray function, filters row of a 2D array by the given column value
*@param array, The 2D array to filter
*@param columnValue, The value to use as a filter
*Note: The index column will be used to apply this filter
*/
struct String2DArray* filterString2DArray(const struct String2DArray* array,const char* columnValue)
{
struct String2DArray* arrayPart = newString2DArray();
int start = 0;
int stop = 0;
for (int i = 0; i < array->setsSize; ++i) {
struct String* str = *(array->sets+i);
struct StringArray* s = stringSplit(str->string, ',');
if(strcmp( (char*) getString(s,0), columnValue) == 0)
{
start = atoi(getString(s, 1));
if(s->size > 2)
stop = atoi(getString(s, 2));
else
stop = array->size;
freeStringArray(s);
break;
}
freeStringArray(s);
}
for (int i = start; i < stop; ++i)
addToString2DArray(arrayPart, *(array->stringArray+i),array->indexColumn);
return arrayPart;
}
/**
*firstRowString2DArray function, Does the same as filterString2DArray function, but this
* returns only one single row(the first) rather than every row filters
*@param array, The 2D array to filter
*@param columnValue, The values to be used as the filter
*/
struct String2DArray* firstRowString2DArray(struct String2DArray* array, char* columnValue)
{
struct String2DArray* arrayPart = newString2DArray();
int start = 0;
int stop = 0;
for (int i = 0; i < array->setsSize; ++i) {
struct String* str = *(array->sets+i);
struct StringArray* s = stringSplit(str->string, ',');
if(strcmp( (char*) getString(s,0), columnValue) == 0)
{
start = atoi(getString(s, 1));
if(s->size > 2)
stop = atoi(getString(s, 2));
else
stop = array->size;
freeStringArray(s);
break;
}
freeStringArray(s);
}
for (int i = start; i < stop; ++i)
{
addToString2DArray(arrayPart, *(array->stringArray+i),array->indexColumn);
break;
}
return arrayPart;
}
/**
*getIndexKey function, Gets the keys for a specified index from a 2D array
*@param array, The 2D array
*@param index, The index of the data
*/
char* getIndexKey(const struct String2DArray* array,const int index)
{
struct String* set = *(array->sets+index);
struct StringArray* s = stringSplit(set->string, ',');
char* key = malloc((strlen(*s->array)+1) * sizeof(char));
strcpy(key,*s->array);
freeStringArray(s);
return key;
}
/**
*addAllToIntegerArray function, Adds all the numbers from the array of
* numbers represented as a string
*@param array, The array to add the numbers
*@param stringIntegerArray, The numbers in a string
*/
void addAllToIntegerArray(struct IntegerArray* array,const char* stringIntegerArray)
{
struct StringArray* stringArray = stringSplit(stringIntegerArray, ',');
struct IntegerArray* intArray = getIntegerArray(stringArray);
for (int i = 0; i < intArray->size; ++i)
addToIntegerArray(array, *getIntegerValue(intArray, i));
freeStringArray(stringArray);
freeIntegerArray(intArray);
}
/**
*addAllToIntegerArray function, Adds all the numbers from the array of
* numbers represented as a string
*@param array, The array to add the numbers
*@param stringIntegerArray, The numbers in a string
*/
void addAllToDoubleArray(struct DoubleArray* array,const char* stringDoubleArray)
{
struct StringArray* stringArray = stringSplit(stringDoubleArray, ',');
struct DoubleArray* dubArray = getDoubleArray(stringArray);
for (int i = 0; i < dubArray->size; ++i)
addToDoubleArray(array, *getDoubleValue(dubArray, i));
freeStringArray(stringArray);
freeDoubleArray(dubArray);
}
#endif