Run untabify on the code, somehome messed up tabs and spaces
This commit is contained in:
parent
84814d4ba7
commit
fb736b22c7
404
on-stock.c
404
on-stock.c
@ -41,7 +41,7 @@ int main(int argc, char* argv[])
|
|||||||
|
|
||||||
if (argc < 2)
|
if (argc < 2)
|
||||||
{
|
{
|
||||||
printUsage(argv[0]);
|
printUsage(argv[0]);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,51 +49,51 @@ int main(int argc, char* argv[])
|
|||||||
while ((opt = getopt(argc, argv, "hlsmdnf:")) != -1)
|
while ((opt = getopt(argc, argv, "hlsmdnf:")) != -1)
|
||||||
{
|
{
|
||||||
switch (opt)
|
switch (opt)
|
||||||
{
|
{
|
||||||
case 'l':
|
case 'l':
|
||||||
choice = 'l';
|
choice = 'l';
|
||||||
break;
|
break;
|
||||||
case 's':
|
case 's':
|
||||||
choice = 's';
|
choice = 's';
|
||||||
break;
|
break;
|
||||||
case 'm':
|
case 'm':
|
||||||
choice = 'm';
|
choice = 'm';
|
||||||
break;
|
break;
|
||||||
case 'd':
|
case 'd':
|
||||||
choice = 'd';
|
choice = 'd';
|
||||||
break;
|
break;
|
||||||
case 'n':
|
case 'n':
|
||||||
choice = 'n';
|
choice = 'n';
|
||||||
break;
|
break;
|
||||||
case 'f':
|
case 'f':
|
||||||
strncpy(filename, optarg, FILEMAXLENGTH-1);
|
strncpy(filename, optarg, FILEMAXLENGTH-1);
|
||||||
break;
|
break;
|
||||||
case 'h':
|
case 'h':
|
||||||
printUsage(argv[0]);
|
printUsage(argv[0]);
|
||||||
return 0;
|
return 0;
|
||||||
default:
|
default:
|
||||||
printUsage(argv[0]);
|
printUsage(argv[0]);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Check if file exists, if it dosen't, create it */
|
/* Check if file exists, if it dosen't, create it */
|
||||||
if ( access(filename, R_OK|W_OK) != 0 )
|
if ( access(filename, R_OK|W_OK) != 0 )
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Could not open %s\n", filename);
|
fprintf(stderr, "Could not open %s\n", filename);
|
||||||
printf("Create the file and start adding records? (y/n): ");
|
printf("Create the file and start adding records? (y/n): ");
|
||||||
create = getchar();
|
create = getchar();
|
||||||
if ( create == 'y' )
|
if ( create == 'y' )
|
||||||
{
|
{
|
||||||
numRec = 1;
|
numRec = 1;
|
||||||
struct myData *data;
|
struct myData *data;
|
||||||
data = calloc(numRec, sizeof(struct myData));
|
data = calloc(numRec, sizeof(struct myData));
|
||||||
new(data, numRec);
|
new(data, numRec);
|
||||||
free(data);
|
free(data);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* The structure to contain our database */
|
/* The structure to contain our database */
|
||||||
@ -102,43 +102,43 @@ int main(int argc, char* argv[])
|
|||||||
/* Continue processesing the command-line arugments */
|
/* Continue processesing the command-line arugments */
|
||||||
if ( choice == 'l' || choice == 's' || choice == 'm' || choice == 'd' )
|
if ( choice == 'l' || choice == 's' || choice == 'm' || choice == 'd' )
|
||||||
{
|
{
|
||||||
/* Open file in read-mode */
|
/* Open file in read-mode */
|
||||||
FILE *fp = fopen(filename, "rb");
|
FILE *fp = fopen(filename, "rb");
|
||||||
|
|
||||||
/* Read in the content from the file to the structure */
|
/* Read in the content from the file to the structure */
|
||||||
fseek(fp, 0, SEEK_END);
|
fseek(fp, 0, SEEK_END);
|
||||||
numRec = ftell(fp) / sizeof(struct myData);
|
numRec = ftell(fp) / sizeof(struct myData);
|
||||||
data = calloc(numRec, sizeof(struct myData));
|
data = calloc(numRec, sizeof(struct myData));
|
||||||
rewind(fp);
|
rewind(fp);
|
||||||
fread(data, sizeof(struct myData), numRec, fp);
|
fread(data, sizeof(struct myData), numRec, fp);
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
|
||||||
if ( choice == 'l' )
|
if ( choice == 'l' )
|
||||||
list(data, numRec);
|
list(data, numRec);
|
||||||
else if ( choice == 's' && argc >= 2 )
|
else if ( choice == 's' && argc >= 2 )
|
||||||
search(data, numRec, argv[2]);
|
search(data, numRec, argv[2]);
|
||||||
else if ( choice == 's' && argc <= 2)
|
else if ( choice == 's' && argc <= 2)
|
||||||
search(data, numRec, NULL);
|
search(data, numRec, NULL);
|
||||||
else if ( choice == 'm' && argc >= 3)
|
else if ( choice == 'm' && argc >= 3)
|
||||||
modify(data, numRec, argv[2], argv[3]);
|
modify(data, numRec, argv[2], argv[3]);
|
||||||
else if ( choice == 'm' && argc <= 4)
|
else if ( choice == 'm' && argc <= 4)
|
||||||
modify(data, numRec, NULL, NULL);
|
modify(data, numRec, NULL, NULL);
|
||||||
else if ( choice == 'd' && argc >= 2 )
|
else if ( choice == 'd' && argc >= 2 )
|
||||||
delete(data, numRec, argv[2]);
|
delete(data, numRec, argv[2]);
|
||||||
else if ( choice == 'd' && argc <= 2 )
|
else if ( choice == 'd' && argc <= 2 )
|
||||||
delete(data, numRec, NULL);
|
delete(data, numRec, NULL);
|
||||||
|
|
||||||
}
|
}
|
||||||
else if ( choice == 'n' )
|
else if ( choice == 'n' )
|
||||||
{
|
{
|
||||||
data = calloc(1, sizeof(struct myData));
|
data = calloc(1, sizeof(struct myData));
|
||||||
if ( new(data, numRec) == 1 )
|
if ( new(data, numRec) == 1 )
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printUsage(argv[0]);
|
printUsage(argv[0]);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
free(data);
|
free(data);
|
||||||
@ -151,10 +151,10 @@ void list(struct myData *datap, int numRec)
|
|||||||
printHeader();
|
printHeader();
|
||||||
for (int i = 0; i<numRec; i++)
|
for (int i = 0; i<numRec; i++)
|
||||||
{
|
{
|
||||||
printf("%-30s\t", datap[i].name);
|
printf("%-30s\t", datap[i].name);
|
||||||
printf("%-10d\t", datap[i].quantity);
|
printf("%-10d\t", datap[i].quantity);
|
||||||
printf("%.2f\t", datap[i].price);
|
printf("%.2f\t", datap[i].price);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
@ -164,25 +164,25 @@ void search(struct myData *datap, int numRec, char *name)
|
|||||||
char searchword[NAMEMAXLENGTH];
|
char searchword[NAMEMAXLENGTH];
|
||||||
if (name != NULL)
|
if (name != NULL)
|
||||||
{
|
{
|
||||||
strncpy(searchword, name, NAMEMAXLENGTH-1);
|
strncpy(searchword, name, NAMEMAXLENGTH-1);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("Name: ");
|
printf("Name: ");
|
||||||
fgets(searchword, NAMEMAXLENGTH, stdin);
|
fgets(searchword, NAMEMAXLENGTH, stdin);
|
||||||
/* Replace the newline character with a null character */
|
/* Replace the newline character with a null character */
|
||||||
searchword[strcspn(searchword, "\n")] = '\0';
|
searchword[strcspn(searchword, "\n")] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i<numRec; i++)
|
for (int i = 0; i<numRec; i++)
|
||||||
{
|
{
|
||||||
if ( strcmp(searchword, datap[i].name) != 0 )
|
if ( strcmp(searchword, datap[i].name) != 0 )
|
||||||
continue;
|
continue;
|
||||||
printHeader();
|
printHeader();
|
||||||
printf("%-30s\t", datap[i].name);
|
printf("%-30s\t", datap[i].name);
|
||||||
printf("%-10d\t", datap[i].quantity);
|
printf("%-10d\t", datap[i].quantity);
|
||||||
printf("%.2f\t", datap[i].price);
|
printf("%.2f\t", datap[i].price);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -195,73 +195,73 @@ void modify(struct myData *datap, int numRec, char *name, char *num)
|
|||||||
|
|
||||||
if (name != NULL && num != NULL)
|
if (name != NULL && num != NULL)
|
||||||
{
|
{
|
||||||
strncpy(searchword, name, NAMEMAXLENGTH-1);
|
strncpy(searchword, name, NAMEMAXLENGTH-1);
|
||||||
strncpy(what, "quantity", 9);
|
strncpy(what, "quantity", 9);
|
||||||
strncpy(quant, num, 19);
|
strncpy(quant, num, 19);
|
||||||
interactive = 0;
|
interactive = 0;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("Name: ");
|
printf("Name: ");
|
||||||
fgets(searchword, NAMEMAXLENGTH, stdin);
|
fgets(searchword, NAMEMAXLENGTH, stdin);
|
||||||
searchword[strcspn(searchword, "\n")] = '\0';
|
searchword[strcspn(searchword, "\n")] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i<numRec; i++)
|
for (int i = 0; i<numRec; i++)
|
||||||
{
|
{
|
||||||
if ( strcmp(searchword, datap[i].name) == 0 )
|
if ( strcmp(searchword, datap[i].name) == 0 )
|
||||||
{
|
{
|
||||||
if (interactive == 1)
|
if (interactive == 1)
|
||||||
{
|
{
|
||||||
printHeader();
|
printHeader();
|
||||||
printf("%-30s\t", datap[i].name);
|
printf("%-30s\t", datap[i].name);
|
||||||
printf("%-10d\t", datap[i].quantity);
|
printf("%-10d\t", datap[i].quantity);
|
||||||
printf("%.2f\t", datap[i].price);
|
printf("%.2f\t", datap[i].price);
|
||||||
printf("\n\n");
|
printf("\n\n");
|
||||||
|
|
||||||
printf("What do you like to modify? (name, quantity, price): ");
|
printf("What do you like to modify? (name, quantity, price): ");
|
||||||
fgets(what, 10, stdin);
|
fgets(what, 10, stdin);
|
||||||
what[strcspn(what, "\n")] = '\0';
|
what[strcspn(what, "\n")] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( strcmp(what, "name") == 0 )
|
if ( strcmp(what, "name") == 0 )
|
||||||
{
|
{
|
||||||
printf("Name: "); scanf("%29s", datap[i].name);
|
printf("Name: "); scanf("%29s", datap[i].name);
|
||||||
}
|
}
|
||||||
else if ( strcmp(what, "quantity") == 0 )
|
else if ( strcmp(what, "quantity") == 0 )
|
||||||
{
|
{
|
||||||
if (interactive == 1)
|
if (interactive == 1)
|
||||||
{
|
{
|
||||||
printf("Quantity (absolute value or (a)dd/(s)ubtractNUMBER): ");
|
printf("Quantity (absolute value or (a)dd/(s)ubtractNUMBER): ");
|
||||||
fgets(quant, 20, stdin);
|
fgets(quant, 20, stdin);
|
||||||
quant[strcspn(quant, "\n")] = '\0';
|
quant[strcspn(quant, "\n")] = '\0';
|
||||||
}
|
}
|
||||||
/* Process the first character */
|
/* Process the first character */
|
||||||
if (quant[0] == 'a')
|
if (quant[0] == 'a')
|
||||||
{
|
{
|
||||||
quant[0] = ' '; /* We need to remove the character first */
|
quant[0] = ' '; /* We need to remove the character first */
|
||||||
datap[i].quantity = datap[i].quantity + atoi(quant);
|
datap[i].quantity = datap[i].quantity + atoi(quant);
|
||||||
}
|
}
|
||||||
else if (quant[0] == 's')
|
else if (quant[0] == 's')
|
||||||
{
|
{
|
||||||
quant[0] = ' ';
|
quant[0] = ' ';
|
||||||
datap[i].quantity = datap[i].quantity - atoi(quant);
|
datap[i].quantity = datap[i].quantity - atoi(quant);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
datap[i].quantity = atoi(quant);
|
datap[i].quantity = atoi(quant);
|
||||||
|
|
||||||
}
|
}
|
||||||
else if ( strcmp(what, "price") == 0 )
|
else if ( strcmp(what, "price") == 0 )
|
||||||
{
|
{
|
||||||
printf("Price: "); scanf("%f", &datap[i].price);
|
printf("Price: "); scanf("%f", &datap[i].price);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Could not find %s in database\n", searchword);
|
fprintf(stderr, "Could not find %s in database\n", searchword);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
FILE *newfp = fopen(filename, "wb");
|
FILE *newfp = fopen(filename, "wb");
|
||||||
fwrite(datap, sizeof(struct myData), numRec, newfp);
|
fwrite(datap, sizeof(struct myData), numRec, newfp);
|
||||||
@ -276,50 +276,50 @@ void delete(struct myData *datap, int numRec, char *name)
|
|||||||
|
|
||||||
if (name != NULL)
|
if (name != NULL)
|
||||||
{
|
{
|
||||||
strncpy(searchword, name, NAMEMAXLENGTH-1);
|
strncpy(searchword, name, NAMEMAXLENGTH-1);
|
||||||
interactive = 0;
|
interactive = 0;
|
||||||
answer = 'y';
|
answer = 'y';
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("Name: ");
|
printf("Name: ");
|
||||||
fgets(searchword, NAMEMAXLENGTH, stdin);
|
fgets(searchword, NAMEMAXLENGTH, stdin);
|
||||||
searchword[strcspn(searchword, "\n")] = '\0';
|
searchword[strcspn(searchword, "\n")] = '\0';
|
||||||
}
|
}
|
||||||
for (int i = 0; i<numRec; i++)
|
for (int i = 0; i<numRec; i++)
|
||||||
{
|
{
|
||||||
if ( strcmp(searchword, datap[i].name) == 0 )
|
if ( strcmp(searchword, datap[i].name) == 0 )
|
||||||
{
|
{
|
||||||
if (interactive == 1)
|
if (interactive == 1)
|
||||||
{
|
{
|
||||||
printHeader();
|
printHeader();
|
||||||
printf("%-30s\t", datap[i].name);
|
printf("%-30s\t", datap[i].name);
|
||||||
printf("%-10d\t", datap[i].quantity);
|
printf("%-10d\t", datap[i].quantity);
|
||||||
printf("%.2f\t", datap[i].price);
|
printf("%.2f\t", datap[i].price);
|
||||||
printf("\n\n");
|
printf("\n\n");
|
||||||
|
|
||||||
printf("Delete the record listed above? (y/n): ");
|
printf("Delete the record listed above? (y/n): ");
|
||||||
answer = getchar();
|
answer = getchar();
|
||||||
}
|
}
|
||||||
if ( answer == 'y' )
|
if ( answer == 'y' )
|
||||||
{
|
{
|
||||||
FILE *newfp = fopen(filename, "wb");
|
FILE *newfp = fopen(filename, "wb");
|
||||||
/* Write out all records, except the one we cant to delete,
|
/* Write out all records, except the one we cant to delete,
|
||||||
back to the file, one record at a time */
|
back to the file, one record at a time */
|
||||||
for (int j = 0; j<numRec; j++)
|
for (int j = 0; j<numRec; j++)
|
||||||
{
|
{
|
||||||
if ( strcmp(searchword, datap[j].name) == 0 )
|
if ( strcmp(searchword, datap[j].name) == 0 )
|
||||||
continue;
|
continue;
|
||||||
fwrite(&datap[j], sizeof(struct myData), 1, newfp);
|
fwrite(&datap[j], sizeof(struct myData), 1, newfp);
|
||||||
}
|
}
|
||||||
fclose(newfp);
|
fclose(newfp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Could not find %s in database\n", searchword);
|
fprintf(stderr, "Could not find %s in database\n", searchword);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -329,56 +329,56 @@ int new(struct myData *datap, int numRec)
|
|||||||
FILE *fp = fopen(filename, "ab");
|
FILE *fp = fopen(filename, "ab");
|
||||||
if (fp == NULL)
|
if (fp == NULL)
|
||||||
{
|
{
|
||||||
printf("Can't open file for writing\n");
|
printf("Can't open file for writing\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Ask for, and write out, one record at a time */
|
/* Ask for, and write out, one record at a time */
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
/* Clear the input buffer (from the previous iteration) */
|
/* Clear the input buffer (from the previous iteration) */
|
||||||
setbuf(stdin, NULL);
|
setbuf(stdin, NULL);
|
||||||
printf("Name ('done' when finished): ");
|
printf("Name ('done' when finished): ");
|
||||||
fgets(datap->name, NAMEMAXLENGTH, stdin);
|
fgets(datap->name, NAMEMAXLENGTH, stdin);
|
||||||
datap->name[strcspn(datap->name, "\n")] = '\0';
|
datap->name[strcspn(datap->name, "\n")] = '\0';
|
||||||
|
|
||||||
if ( strcmp(datap->name, "done") == 0 )
|
if ( strcmp(datap->name, "done") == 0 )
|
||||||
{
|
{
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
printf("Quantity: "); scanf("%d", &datap->quantity);
|
printf("Quantity: "); scanf("%d", &datap->quantity);
|
||||||
printf("Price: "); scanf("%f", &datap->price);
|
printf("Price: "); scanf("%f", &datap->price);
|
||||||
bytes = fwrite(datap, sizeof(struct myData), 1, fp);
|
bytes = fwrite(datap, sizeof(struct myData), 1, fp);
|
||||||
if (bytes != 1)
|
if (bytes != 1)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Could not write to the file!\n");
|
fprintf(stderr, "Could not write to the file!\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void printUsage(char *arg)
|
void printUsage(char *arg)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Usage: %s [-l] [-s [name]] [-m [name (a/s)quantity]]\n"
|
fprintf(stderr, "Usage: %s [-l] [-s [name]] [-m [name (a/s)quantity]]\n"
|
||||||
"[-d [name]] [-n] [-h] [-f filename]\n\n"
|
"[-d [name]] [-n] [-h] [-f filename]\n\n"
|
||||||
"-l = list the articles in the database\n"
|
"-l = list the articles in the database\n"
|
||||||
"-s = search for an article 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"
|
" If no name is given as argument, you will be prompted for a name.\n"
|
||||||
"-m = modify a article\n"
|
"-m = modify a article\n"
|
||||||
" If no name is given as argument, you will be prompted for a\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"
|
" 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"
|
" 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"
|
" command line, such as subtracting the stock by three:\n"
|
||||||
" ./on-stock -m 'Nailgun' s3\n"
|
" ./on-stock -m 'Nailgun' s3\n"
|
||||||
"-d = delete a article\n"
|
"-d = delete a article\n"
|
||||||
" If no name is given as argument, you will be prompted for an article\n"
|
" If no name is given as argument, you will be prompted for an article\n"
|
||||||
" to delete.\n"
|
" to delete.\n"
|
||||||
" If a name is given as argument, no confirmation will be required to\n"
|
" If a name is given as argument, no confirmation will be required to\n"
|
||||||
" delete the article from the database.\n"
|
" delete the article from the database.\n"
|
||||||
"-n = create new articles (interactive mode only)\n"
|
"-n = create new articles (interactive mode only)\n"
|
||||||
"-h = display this help message\n"
|
"-h = display this help message\n"
|
||||||
"-f = specifiy a filename for the database\n", arg);
|
"-f = specifiy a filename for the database\n", arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
void printHeader(void)
|
void printHeader(void)
|
||||||
@ -387,6 +387,6 @@ void printHeader(void)
|
|||||||
printf("%s\t", "Quantity");
|
printf("%s\t", "Quantity");
|
||||||
printf("%s\t\n", "Price");
|
printf("%s\t\n", "Price");
|
||||||
for (int i = 0; i<=52; i++)
|
for (int i = 0; i<=52; i++)
|
||||||
printf("=");
|
printf("=");
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user