-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathon-stock.c
333 lines (302 loc) · 8.63 KB
/
on-stock.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
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
/* A simple inventory tool written in C, by
Jack-Benny Persson (jack-benny@cyberinfo.se).
Released under GNU GPLv2.
*/
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/* Macros */
#define NAMEMAXLENGTH 30
/* Globals */
struct myData
{
char name[NAMEMAXLENGTH];
int quantity;
float price;
};
char filename[] = "storage.bin";
/* Function prototypes */
void list(struct myData *datap, int numRec);
void search(struct myData *datap, int numRec, char *name);
void modify(struct myData *datap, int numRec, char *name);
void delete(struct myData *datap, int numRec, char *name);
int new(struct myData *datap, int numRec);
void printUsage(char *arg);
void printHeader(void);
int main(int argc, char* argv[])
{
FILE *fp;
int newart = 0;
int numRec;
int create;
int opt;
struct myData *data;
/* Print help and exit if no arguments are given */
if (argc < 2)
{
printUsage(argv[0]);
return 1;
}
/* Check file access mode */
if ( access(filename, R_OK|W_OK) != 0 )
{
fprintf(stderr, "Could not open %s\n", filename);
printf("Create the file and start adding records? (y/n): ");
create = getchar();
if ( create == 'y' )
{
/* Start createing records */
numRec = 1;
data = calloc(numRec, sizeof(struct myData));
new(data, numRec);
free(data);
}
else
return 1;
}
fp = fopen(filename, "rb");
fseek(fp, 0, SEEK_END);
numRec = ftell(fp) / sizeof(struct myData);
data = calloc(numRec, sizeof(struct myData));
rewind(fp);
fread(data, sizeof(struct myData), numRec, fp);
fclose(fp);
/* Process arguments with getopt() */
while ((opt = getopt(argc, argv, "hls:m:d:n")) != -1)
{
switch (opt)
{
case 'l':
list(data, numRec);
break;
case 's':
search(data, numRec, optarg);
break;
case 'm':
modify(data, numRec, optarg);
break;
case 'd':
delete(data, numRec, optarg);
break;
case 'n':
newart = 1;
break;
case 'h':
printUsage(argv[0]);
return 0;
default:
printUsage(argv[0]);
return 1;
}
}
/* Continue here */
if ( newart == 1 )
{
data = calloc(1, sizeof(struct myData));
if ( new(data, numRec) == 1 )
return 1;
}
/* Finished */
free(data);
return 0;
}
void list(struct myData *datap, int numRec)
{
int i;
printHeader();
for (i = 0; i<numRec; i++)
{
printf("%-30s\t", datap[i].name);
printf("%-10d\t", datap[i].quantity);
printf("%.2f\t", datap[i].price);
printf("\n");
}
printf("\n");
}
void search(struct myData *datap, int numRec, char *name)
{
int i;
/* Iterate over the database and search */
for (i = 0; i<numRec; i++)
{
if ( strcmp(name, datap[i].name) != 0 )
continue;
printHeader();
printf("%-30s\t", datap[i].name);
printf("%-10d\t", datap[i].quantity);
printf("%.2f\t", datap[i].price);
printf("\n");
}
}
void modify(struct myData *datap, int numRec, char *name)
{
FILE *newfp;
int i;
int match = 0;
char what[10];
char quant[20];
/* Iterate over the database and search */
for (i = 0; i<numRec; i++)
{
if ( strcmp(name, datap[i].name) == 0 )
{
match = 1;
printHeader();
printf("%-30s\t", datap[i].name);
printf("%-10d\t", datap[i].quantity);
printf("%.2f\t", datap[i].price);
printf("\n\n");
printf("What do you like to modify? "
"(name, quantity, price): ");
fgets(what, 10, stdin);
what[strcspn(what, "\n")] = '\0';
if ( strcmp(what, "name") == 0 )
{
printf("Name: ");
fgets(datap[i].name, NAMEMAXLENGTH, stdin);
datap[i].name[strcspn(datap[i].name, "\n")] = '\0';
}
else if ( strcmp(what, "quantity") == 0 )
{
printf("Quantity (absolute value or "
"(a)dd/(s)ubtractNUMBER): ");
fgets(quant, 20, stdin);
quant[strcspn(quant, "\n")] = '\0';
if (quant[0] == 'a') /* a for add */
{
quant[0] = ' '; /* Remove the sign */
datap[i].quantity = datap[i].quantity +
atoi(quant);
}
else if (quant[0] == 's') /* s for subtract */
{
quant[0] = ' ';
datap[i].quantity = datap[i].quantity -
atoi(quant);
}
else
datap[i].quantity = atoi(quant);
}
else if ( strcmp(what, "price") == 0 )
{
printf("Price: "); scanf("%f", &datap[i].price);
}
}
}
if (match == 0)
{
fprintf(stderr, "Could not find %s in database\n", name);
exit(1);
}
/* Write out the new content to file */
if ( (newfp = fopen(filename, "wb")) == 0 )
{
fprintf(stderr, "Could not open file for writing\n");
exit(1);
}
fwrite(datap, sizeof(struct myData), numRec, newfp);
fclose(newfp);
}
void delete(struct myData *datap, int numRec, char *name)
{
int i, j;
int match = 0;
FILE *newfp;
int answer;
/* Iterate the database and search */
for (i = 0; i<numRec; i++)
{
if ( strcmp(name, datap[i].name) == 0 )
{
match = 1;
printHeader();
printf("%-30s\t", datap[i].name);
printf("%-10d\t", datap[i].quantity);
printf("%.2f\t", datap[i].price);
printf("\n\n");
printf("Delete the record listed above? (y/n): ");
answer = getchar();
if ( answer == 'y' )
{
if ( (newfp = fopen(filename, "wb")) == 0 )
{
fprintf(stderr,
"Could not open file for writing\n");
exit(1);
}
/* Iterate the database and write out every
record except those that matched! */
for (j = 0; j<numRec; j++)
{
if ( strcmp(name, datap[j].name) == 0 )
continue;
fwrite(&datap[j], sizeof(struct myData), 1,
newfp);
}
fclose(newfp);
}
}
}
if (match == 0)
{
fprintf(stderr, "Could not find %s in database\n", name);
exit(1);
}
}
int new(struct myData *datap, int numRec)
{
FILE *fp;
int bytes;
if ( (fp = fopen(filename, "ab")) == 0 )
{
fprintf(stderr, "Could not open file for writing\n");
return 1;
}
/* End loop when user types 'done' */
for (;;)
{
setbuf(stdin, NULL);
printf("Name ('done' when finished): ");
fgets(datap->name, NAMEMAXLENGTH, stdin);
datap->name[strcspn(datap->name, "\n")] = '\0';
if ( strcmp(datap->name, "done") == 0 )
{
fclose(fp);
return 0;
}
printf("Quantity: "); scanf("%d", &datap->quantity);
printf("Price: "); scanf("%f", &datap->price);
bytes = fwrite(datap, sizeof(struct myData), 1, fp);
if (bytes != 1)
{
fprintf(stderr, "Could not write to the file!\n");
return 1;
}
}
}
void printUsage(char *arg)
{
fprintf(stderr, "Usage: %s [-l] [-s name]"
"[-m name \n"
"[-d name] [-n] [-h] [-f filename]\n\n"
"-l = list the articles in the database\n"
"-s name = search for an 'name' in the database\n"
"-m name = modify the article named 'name' in the database\n"
" You'll then have the choice to change name, quantity "
"and price.\n"
"-d name = delete the article named 'name'\n"
"-n = create new articles (interactive mode only)\n"
"-h = display this help message\n", arg);
}
void printHeader(void)
{
int i;
printf("\n%-30s\t", "Name");
printf("%s\t", "Quantity");
printf("%s\t\n", "Price");
for (i = 0; i<=52; i++)
printf("=");
printf("\n");
}