From 113c5910946f7893bdaf576f0a870c2e58d8be84 Mon Sep 17 00:00:00 2001 From: Jack-Benny Persson Date: Thu, 9 Nov 2017 06:15:57 +0100 Subject: [PATCH] Re-wrote most of the program, last version was a mess --- on-stock.c | 361 ++++++++++++++++++++++------------------------------- 1 file changed, 151 insertions(+), 210 deletions(-) diff --git a/on-stock.c b/on-stock.c index ec4d972..1d4363f 100644 --- a/on-stock.c +++ b/on-stock.c @@ -1,15 +1,15 @@ /* A simple inventory tool written in C, by - * Jack-Benny Persson (jack-benny@cyberinfo.se). - * Released under GNU GPLv2. + Jack-Benny Persson (jack-benny@cyberinfo.se). + Released under GNU GPLv2. */ +#define _XOPEN_SOURCE 500 #include #include #include #include /* Macros */ -#define FILEMAXLENGTH 50 #define NAMEMAXLENGTH 30 /* Globals */ @@ -20,12 +20,12 @@ struct myData float price; }; -char filename[FILEMAXLENGTH] = "storage.bin"; +char filename[] = "storage.bin"; -/* Function prototype */ +/* 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, char *num); +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); @@ -34,39 +34,67 @@ void printHeader(void); int main(int argc, char* argv[]) { + FILE *fp; + int newart = 0; int numRec; int create; - int choice; int opt; + struct myData *data; + + /* Print help and exit if no arguments are given */ if (argc < 2) { printUsage(argv[0]); return 1; } - /* Process command line arugments */ - while ((opt = getopt(argc, argv, "hlsmdnf:")) != -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': - choice = 'l'; + list(data, numRec); break; case 's': - choice = 's'; + search(data, numRec, optarg); break; case 'm': - choice = 'm'; + modify(data, numRec, optarg); break; case 'd': - choice = 'd'; + delete(data, numRec, optarg); break; case 'n': - choice = 'n'; - break; - case 'f': - strncpy(filename, optarg, FILEMAXLENGTH-1); + newart = 1; break; case 'h': printUsage(argv[0]); @@ -77,79 +105,24 @@ int main(int argc, char* argv[]) } } - - /* Check if file exists, if it dosen't, create it */ - 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' ) - { - numRec = 1; - struct myData *data; - data = calloc(numRec, sizeof(struct myData)); - new(data, numRec); - free(data); - } - else - return 1; - } - - /* The structure to contain our database */ - struct myData *data; - - /* Continue processesing the command-line arugments */ - if ( choice == 'l' || choice == 's' || choice == 'm' || choice == 'd' ) - { - /* Open file in read-mode */ - FILE *fp = fopen(filename, "rb"); - - /* Read in the content from the file to the structure */ - 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); - - if ( choice == 'l' ) - list(data, numRec); - else if ( choice == 's' && argc >= 2 ) - search(data, numRec, argv[2]); - else if ( choice == 's' && argc <= 2) - search(data, numRec, NULL); - else if ( choice == 'm' && argc >= 3) - modify(data, numRec, argv[2], argv[3]); - else if ( choice == 'm' && argc <= 4) - modify(data, numRec, NULL, NULL); - else if ( choice == 'd' && argc >= 2 ) - delete(data, numRec, argv[2]); - else if ( choice == 'd' && argc <= 2 ) - delete(data, numRec, NULL); - - } - else if ( choice == 'n' ) + /* Continue here */ + if ( newart == 1 ) { data = calloc(1, sizeof(struct myData)); if ( new(data, numRec) == 1 ) return 1; } - else - { - printUsage(argv[0]); - return 1; - } + /* Finished */ free(data); return 0; } - void list(struct myData *datap, int numRec) { + int i; printHeader(); - for (int i = 0; iname, NAMEMAXLENGTH, stdin); @@ -360,33 +308,26 @@ int new(struct myData *datap, int numRec) void printUsage(char *arg) { - fprintf(stderr, "Usage: %s [-l] [-s [name]] [-m [name (a/s)quantity]]\n" - "[-d [name]] [-n] [-h] [-f filename]\n\n" - "-l = list the articles in the database\n" - "-s = search for an article in the database\n" - " If no name is given as argument, you will be prompted for a name.\n" - "-m = modify a article\n" - " If no name is given as argument, you will be prompted for a\n" - " name. You'll then have the choice to change name, quantity and price.\n" - " If a name is given as argument, the quantity can be changed from the\n" - " command line, such as subtracting the stock by three:\n" - " ./on-stock -m 'Nailgun' s3\n" - "-d = delete a article\n" - " If no name is given as argument, you will be prompted for an article\n" - " to delete.\n" - " If a name is given as argument, no confirmation will be required to\n" - " delete the article from the database.\n" - "-n = create new articles (interactive mode only)\n" - "-h = display this help message\n" - "-f = specifiy a filename for the database\n", 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 (int i = 0; i<=52; i++) + for (i = 0; i<=52; i++) printf("="); printf("\n"); }