-
Language
C
-
Description
Customers record
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
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
#include <stdio.h>
#include <stdlib.h>
#include <search.h>
#include <string.h>
#ifdef __linux__
#include <termios.h>
#include <unistd.h>
#define CLEAR_SCREEN "clear"
int _getch ();
#define _lfind lfind
#define _strdup strdup
#else
#include <conio.h>
#define CLEAR_SCREEN "cls"
#if defined(__MSDOS__) || defined(__BORLANDC__)
#define _getch getch
#define _lfind lfind
#define _strdup strdup
#endif
#endif
typedef struct ADTCustomer {
char* phone;
char* state;
char* tax_id;
char* ZIP_code;
char* municipality;
char* business_name;
char* fiscal_address;
} Customer;
void* array_insert (void *array_var, size_t *length, size_t size, const void *datum);
void* array_remove (void *array_var, size_t *length, size_t size, const void *datum);
void array_foreach (void *array_var, size_t length, size_t size, void *params, void (*func)(void*,void*));
void pause_screen (const char *message);
char* read_string_line (const char *message);
char* read_str (const char *message, char *string_var);
int read_integer (const char *message);
int read_field (FILE *file_stream, char *field);
int compare_customer (const Customer *customer1, const Customer *customer2);
void print_customer (Customer *datum, int *counter);
void print_on_file (Customer *datum, FILE *file_stream);
const char *path = "customers.tsv";
int main (void)
{
Customer *customers = NULL;
size_t length=0;
Customer *datum = NULL, customer;
int counter=0, main_option, suboption;
char field[255];
FILE *file_stream = fopen (path, "r");
if (file_stream!=NULL)
{
while (read_field (file_stream, field))
{
read_field (file_stream, field);
customer.phone = _strdup (field);
read_field (file_stream, field);
customer.state = _strdup (field);
customer.tax_id = _strdup (field);
read_field (file_stream, field);
customer.ZIP_code = _strdup (field);
read_field (file_stream, field);
customer.municipality = _strdup (field);
read_field (file_stream, field);
customer.business_name = _strdup (field);
read_field (file_stream, field);
customer.fiscal_address = _strdup (field);
customers = (Customer*) array_insert (customers, &length, sizeof (Customer), &customer);
}
fclose (file_stream);
}
do {
putchar ('\n');
system (CLEAR_SCREEN);
printf ("MENU\n");
printf ("1.- Insertions\n");
printf ("2.- Queries\n");
printf ("3.- Updates\n");
printf ("4.- Deletions\n");
printf ("5.- Sort records\n");
printf ("6.- List records\n");
printf ("7.- Exit\n");
printf ("Select an option: ");
fflush (stdout);
do
main_option = _getch ();
while (main_option<'1' || main_option>'7');
printf ("%c\n\n", main_option);
if (length==0 && main_option!='1' && main_option!='7')
{
pause_screen ("There are not records.\n");
continue;
}
if (main_option<'5')
{
customer.tax_id = read_str ("Enter the tax id of the customer", field);
datum = (Customer*) _lfind (&customer, customers, &length, sizeof (Customer), (int(*)(const void*,const void*))compare_customer);
if (datum!=NULL)
{
putchar ('\n');
print_customer (datum, &counter);
}
}
if (main_option=='1' && datum!=NULL)
printf ("The record already exists.\n");
else if (main_option>='2' && main_option<='4' && datum==NULL)
printf ("\nRecord not found.\n");
else switch (main_option)
{
case '1':
customer.tax_id = _strdup (field);
customer.phone = read_string_line ("Enter the phone");
customer.state = read_string_line ("Enter the state");
customer.ZIP_code = read_string_line ("Enter the ZIP code");
customer.municipality = read_string_line ("Enter the municipality");
customer.business_name = read_string_line ("Enter the business name");
customer.fiscal_address = read_string_line ("Enter the fiscal address");
customers = (Customer*) array_insert (customers, &length, sizeof (Customer), &customer);
printf ("\nRecord added correctly.\n");
break;
case '3':
printf ("Menu of fields updating\n");
printf ("1.- phone\n");
printf ("2.- state\n");
printf ("3.- ZIP code\n");
printf ("4.- municipality\n");
printf ("5.- business name\n");
printf ("6.- fiscal address\n");
do {
suboption = read_integer ("Select a field number to be updated");
if (suboption<1 || suboption>6)
printf ("Incorrect option.\n");
} while (suboption<1 || suboption>6);
switch (suboption)
{
case 1:
free (datum->phone);
datum->phone = read_string_line ("Enter the new phone");
break;
case 2:
free (datum->state);
datum->state = read_string_line ("Enter the new state");
break;
case 3:
free (datum->ZIP_code);
datum->ZIP_code = read_string_line ("Enter the new ZIP code");
break;
case 4:
free (datum->municipality);
datum->municipality = read_string_line ("Enter the new municipality");
break;
case 5:
free (datum->business_name);
datum->business_name = read_string_line ("Enter the new business name");
break;
case 6:
free (datum->fiscal_address);
datum->fiscal_address = read_string_line ("Enter the new fiscal address");
break;
}
printf ("\nRecord updated correctly.\n");
break;
case '4':
memcpy (&customer, datum, sizeof (Customer));
customers = (Customer*) array_remove ((void**)customers, &length, sizeof (Customer), datum);
free (customer.phone);
free (customer.state);
free (customer.tax_id);
free (customer.ZIP_code);
free (customer.municipality);
free (customer.business_name);
free (customer.fiscal_address);
printf ("Record removed correctly.\n");
break;
case '5':
qsort (customers, length, sizeof (Customer), (int(*)(const void*,const void*))compare_customer);
printf ("Records sorted correctly.\n");
break;
case '6':
counter = 0;
array_foreacjson_decodeh (customers, length, sizeof (Customer), &counter, (void(*)(void*,void*))print_customer);
printf ("Total of records: %d.\n", counter);
break;
}
if (main_option!='7')
pause_screen ("");
} while (main_option!='7');
file_stream = fopen (path, "w");
if (file_stream!=NULL)
{
array_foreach (customers, length, sizeof (Customer), file_stream, (void(*)(void*,void*))print_on_file);
fclose (file_stream);
}
return EXIT_SUCCESS;
}
void* array_insert (void *array_var, size_t *length, size_t size, const void *datum)
{
char *record_var;
array_var = realloc (array_var, size * (*length+1));
record_var = (char*)array_var + *length * size;
memcpy (record_var, datum, size);
(*length)++;
return array_var;
}
void* array_remove (void *array_var, size_t *length, size_t size, const void *datum)
{
size_t i;
char *record_var = (char*)array_var;
for (i=0; i<*length && datum!=record_var; i++, record_var+=size);
if (i<*length)
{
for (i++; i<*length; i++)
{
record_var+=size;
memcpy (record_var-size, record_var, size);
}
(*length)--;
array_var = realloc (array_var, size * (*length));
}
return array_var;
}
void array_foreach (void *array_var, size_t length, size_t size, void *params, void (*func)(void*,void*))
{
char *record_var = (char*)array_var;
if (length>0)
{
func (record_var, params);
array_foreach (record_var+size, length-1, size, params, func);
}
}
int compare_customer (const Customer *customer1, const Customer *customer2)
{
return customer1==customer2 ? 0 : strcmp (customer1->tax_id, customer2->tax_id);
}
void print_customer (Customer *datum, int *counter)
{
printf ("phone : %s\n", datum->phone);
printf ("state : %s\n", datum->state);
printf ("tax id : %s\n", datum->tax_id);
printf ("ZIP code : %s\n", datum->ZIP_code);
printf ("municipality : %s\n", datum->municipality);
printf ("business name : %s\n", datum->business_name);
printf ("fiscal address: %s\n", datum->fiscal_address);
putchar ('\n');
(*counter)++;
}
void print_on_file (Customer *datum, FILE *file_stream)
{
fprintf (file_stream, "%s\t", datum->phone);
fprintf (file_stream, "%s\t", datum->state);
fprintf (file_stream, "%s\t", datum->tax_id);
fprintf (file_stream, "%s\t", datum->ZIP_code);
fprintf (file_stream, "%s\t", datum->municipality);
fprintf (file_stream, "%s\t", datum->business_name);
fprintf (file_stream, "%s\n", datum->fiscal_address);
}
char* read_string_line (const char *message)
{
char string_line[255];
(void) read_str (message, string_line);
return _strdup (string_line);
}
char* read_str (const char *message, char *string_var)
{
printf ("%s: ", message);
scanf ("%[^\r\n]", string_var);
(void) getchar ();
return string_var;
}
int read_integer (const char *message)
{
int integer_var;
printf ("%s: ", message);
scanf ("%d", &integer_var);
(void) getchar();
return integer_var;
}
int read_field (FILE *file_stream, char *field)
{
fscanf (file_stream, "%[^\t\n\r]", field);
fgetc (file_stream);
return feof (file_stream) == 0;
}
void pause_screen (const char *message)
{
printf ("%s\nPress any key to continue . . . ", message);
fflush (stdout);
_getch ();
}
#ifdef __linux__
int _getch ()
{
int ch;
struct termios oldt, newt;
tcgetattr (STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr (STDIN_FILENO, TCSANOW, &newt);
ch = getchar();
tcsetattr (STDIN_FILENO, TCSANOW, &oldt);
return ch;
}
#endif
#include <stdlib.h>
#include <search.h>
#include <string.h>
#ifdef __linux__
#include <termios.h>
#include <unistd.h>
#define CLEAR_SCREEN "clear"
int _getch ();
#define _lfind lfind
#define _strdup strdup
#else
#include <conio.h>
#define CLEAR_SCREEN "cls"
#if defined(__MSDOS__) || defined(__BORLANDC__)
#define _getch getch
#define _lfind lfind
#define _strdup strdup
#endif
#endif
typedef struct ADTCustomer {
char* phone;
char* state;
char* tax_id;
char* ZIP_code;
char* municipality;
char* business_name;
char* fiscal_address;
} Customer;
void* array_insert (void *array_var, size_t *length, size_t size, const void *datum);
void* array_remove (void *array_var, size_t *length, size_t size, const void *datum);
void array_foreach (void *array_var, size_t length, size_t size, void *params, void (*func)(void*,void*));
void pause_screen (const char *message);
char* read_string_line (const char *message);
char* read_str (const char *message, char *string_var);
int read_integer (const char *message);
int read_field (FILE *file_stream, char *field);
int compare_customer (const Customer *customer1, const Customer *customer2);
void print_customer (Customer *datum, int *counter);
void print_on_file (Customer *datum, FILE *file_stream);
const char *path = "customers.tsv";
int main (void)
{
Customer *customers = NULL;
size_t length=0;
Customer *datum = NULL, customer;
int counter=0, main_option, suboption;
char field[255];
FILE *file_stream = fopen (path, "r");
if (file_stream!=NULL)
{
while (read_field (file_stream, field))
{
read_field (file_stream, field);
customer.phone = _strdup (field);
read_field (file_stream, field);
customer.state = _strdup (field);
customer.tax_id = _strdup (field);
read_field (file_stream, field);
customer.ZIP_code = _strdup (field);
read_field (file_stream, field);
customer.municipality = _strdup (field);
read_field (file_stream, field);
customer.business_name = _strdup (field);
read_field (file_stream, field);
customer.fiscal_address = _strdup (field);
customers = (Customer*) array_insert (customers, &length, sizeof (Customer), &customer);
}
fclose (file_stream);
}
do {
putchar ('\n');
system (CLEAR_SCREEN);
printf ("MENU\n");
printf ("1.- Insertions\n");
printf ("2.- Queries\n");
printf ("3.- Updates\n");
printf ("4.- Deletions\n");
printf ("5.- Sort records\n");
printf ("6.- List records\n");
printf ("7.- Exit\n");
printf ("Select an option: ");
fflush (stdout);
do
main_option = _getch ();
while (main_option<'1' || main_option>'7');
printf ("%c\n\n", main_option);
if (length==0 && main_option!='1' && main_option!='7')
{
pause_screen ("There are not records.\n");
continue;
}
if (main_option<'5')
{
customer.tax_id = read_str ("Enter the tax id of the customer", field);
datum = (Customer*) _lfind (&customer, customers, &length, sizeof (Customer), (int(*)(const void*,const void*))compare_customer);
if (datum!=NULL)
{
putchar ('\n');
print_customer (datum, &counter);
}
}
if (main_option=='1' && datum!=NULL)
printf ("The record already exists.\n");
else if (main_option>='2' && main_option<='4' && datum==NULL)
printf ("\nRecord not found.\n");
else switch (main_option)
{
case '1':
customer.tax_id = _strdup (field);
customer.phone = read_string_line ("Enter the phone");
customer.state = read_string_line ("Enter the state");
customer.ZIP_code = read_string_line ("Enter the ZIP code");
customer.municipality = read_string_line ("Enter the municipality");
customer.business_name = read_string_line ("Enter the business name");
customer.fiscal_address = read_string_line ("Enter the fiscal address");
customers = (Customer*) array_insert (customers, &length, sizeof (Customer), &customer);
printf ("\nRecord added correctly.\n");
break;
case '3':
printf ("Menu of fields updating\n");
printf ("1.- phone\n");
printf ("2.- state\n");
printf ("3.- ZIP code\n");
printf ("4.- municipality\n");
printf ("5.- business name\n");
printf ("6.- fiscal address\n");
do {
suboption = read_integer ("Select a field number to be updated");
if (suboption<1 || suboption>6)
printf ("Incorrect option.\n");
} while (suboption<1 || suboption>6);
switch (suboption)
{
case 1:
free (datum->phone);
datum->phone = read_string_line ("Enter the new phone");
break;
case 2:
free (datum->state);
datum->state = read_string_line ("Enter the new state");
break;
case 3:
free (datum->ZIP_code);
datum->ZIP_code = read_string_line ("Enter the new ZIP code");
break;
case 4:
free (datum->municipality);
datum->municipality = read_string_line ("Enter the new municipality");
break;
case 5:
free (datum->business_name);
datum->business_name = read_string_line ("Enter the new business name");
break;
case 6:
free (datum->fiscal_address);
datum->fiscal_address = read_string_line ("Enter the new fiscal address");
break;
}
printf ("\nRecord updated correctly.\n");
break;
case '4':
memcpy (&customer, datum, sizeof (Customer));
customers = (Customer*) array_remove ((void**)customers, &length, sizeof (Customer), datum);
free (customer.phone);
free (customer.state);
free (customer.tax_id);
free (customer.ZIP_code);
free (customer.municipality);
free (customer.business_name);
free (customer.fiscal_address);
printf ("Record removed correctly.\n");
break;
case '5':
qsort (customers, length, sizeof (Customer), (int(*)(const void*,const void*))compare_customer);
printf ("Records sorted correctly.\n");
break;
case '6':
counter = 0;
array_foreacjson_decodeh (customers, length, sizeof (Customer), &counter, (void(*)(void*,void*))print_customer);
printf ("Total of records: %d.\n", counter);
break;
}
if (main_option!='7')
pause_screen ("");
} while (main_option!='7');
file_stream = fopen (path, "w");
if (file_stream!=NULL)
{
array_foreach (customers, length, sizeof (Customer), file_stream, (void(*)(void*,void*))print_on_file);
fclose (file_stream);
}
return EXIT_SUCCESS;
}
void* array_insert (void *array_var, size_t *length, size_t size, const void *datum)
{
char *record_var;
array_var = realloc (array_var, size * (*length+1));
record_var = (char*)array_var + *length * size;
memcpy (record_var, datum, size);
(*length)++;
return array_var;
}
void* array_remove (void *array_var, size_t *length, size_t size, const void *datum)
{
size_t i;
char *record_var = (char*)array_var;
for (i=0; i<*length && datum!=record_var; i++, record_var+=size);
if (i<*length)
{
for (i++; i<*length; i++)
{
record_var+=size;
memcpy (record_var-size, record_var, size);
}
(*length)--;
array_var = realloc (array_var, size * (*length));
}
return array_var;
}
void array_foreach (void *array_var, size_t length, size_t size, void *params, void (*func)(void*,void*))
{
char *record_var = (char*)array_var;
if (length>0)
{
func (record_var, params);
array_foreach (record_var+size, length-1, size, params, func);
}
}
int compare_customer (const Customer *customer1, const Customer *customer2)
{
return customer1==customer2 ? 0 : strcmp (customer1->tax_id, customer2->tax_id);
}
void print_customer (Customer *datum, int *counter)
{
printf ("phone : %s\n", datum->phone);
printf ("state : %s\n", datum->state);
printf ("tax id : %s\n", datum->tax_id);
printf ("ZIP code : %s\n", datum->ZIP_code);
printf ("municipality : %s\n", datum->municipality);
printf ("business name : %s\n", datum->business_name);
printf ("fiscal address: %s\n", datum->fiscal_address);
putchar ('\n');
(*counter)++;
}
void print_on_file (Customer *datum, FILE *file_stream)
{
fprintf (file_stream, "%s\t", datum->phone);
fprintf (file_stream, "%s\t", datum->state);
fprintf (file_stream, "%s\t", datum->tax_id);
fprintf (file_stream, "%s\t", datum->ZIP_code);
fprintf (file_stream, "%s\t", datum->municipality);
fprintf (file_stream, "%s\t", datum->business_name);
fprintf (file_stream, "%s\n", datum->fiscal_address);
}
char* read_string_line (const char *message)
{
char string_line[255];
(void) read_str (message, string_line);
return _strdup (string_line);
}
char* read_str (const char *message, char *string_var)
{
printf ("%s: ", message);
scanf ("%[^\r\n]", string_var);
(void) getchar ();
return string_var;
}
int read_integer (const char *message)
{
int integer_var;
printf ("%s: ", message);
scanf ("%d", &integer_var);
(void) getchar();
return integer_var;
}
int read_field (FILE *file_stream, char *field)
{
fscanf (file_stream, "%[^\t\n\r]", field);
fgetc (file_stream);
return feof (file_stream) == 0;
}
void pause_screen (const char *message)
{
printf ("%s\nPress any key to continue . . . ", message);
fflush (stdout);
_getch ();
}
#ifdef __linux__
int _getch ()
{
int ch;
struct termios oldt, newt;
tcgetattr (STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr (STDIN_FILENO, TCSANOW, &newt);
ch = getchar();
tcsetattr (STDIN_FILENO, TCSANOW, &oldt);
return ch;
}
#endif