You are on page 1of 5

/**********************************************************************

cs1713p1.c
Purpose:
This program reads customer orders to produce output suitable for
warehouse personnel to pull the stock and mail it to the customer.
command Parameters:
order -c customerOrderFileName
Input:
Order
Stream input file which contains orders for products.
There are three different kinds of lines of data for each order:
Customer Identification Information:
o One line per order
o szEmailAddr
szFullName
50s
30s (may contain spaces)
Customer Address Information (separated by commas)
o One line per order
o szStreetAddress
szCity szStateCd szZipCd
30s (may contain spaces) 20s
2s
5s
Purchase Item:
o 0 to many of them per order (terminated by 000000
in the Stock Number and 0 in the Request Quantity)
o szStockNumber iRequestQty
6s
4d
Results:
Prints each order in a readable format which is suitable for warehouse
personnel to use to pull the stock.
Examples:
******************* Order **********************
petem@xyz.net Pete Moss
123 Boggy Lane
New Orleans, LA 70112
Stock
Quantity
SBB001
10
SBG002
50
MCW001
2
SBG002
10
******************* Order **********************
pcorn@abc.net Pop Corn
456 Kernel
San Antonio, TX 78210
Stock
Quantity
BOM001
2
NHC001
50
SBG002
100
MCW001
50
XXX001
20
SBG002
100
Returns:
0 normal
-1 invalid command line syntax
-2 show usage only
-3 error during processing, see stderr for more information
Notes:
order -? will provide the usage information. In some shells,
you will have to type order -\?
**********************************************************************/
// If compiling using visual studio, tell the compiler not to give its warnings
// about the safety of scanf and printf
#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <string.h>
#include "cs1713p2.h"
FILE *pfileOrder;
FILE *pfileInven;

// stream Input for Customer Order data

void processCommandSwitches(int argc, char *argv[], char **ppszOrderFileName, ch


ar **ppszInvenOrderFile);
int processOrders(Stock stockM[], int iStockCnt);
int main(int argc, char *argv[])
{
char
*pszOrderFileName = NULL;
char
*pszInvenFileName = NULL;
int
rc;
Stock stockM[MAX_STOCK];
int iStockCnt;
// Process the command switches
processCommandSwitches(argc, argv, &pszOrderFileName, &pszInvenFileName);
// open the Customer Order stream data file
if (pszOrderFileName == NULL)
exitError(ERR_MISSING_SWITCH, "-c");
pfileOrder = fopen(pszOrderFileName, "r");
if (pfileOrder == NULL)
exitError(ERR_CUSTOMER_ORDER_FILENAME, pszOrderFileName);
// open the Inventory Order stream data file
if (pszInvenFileName == NULL)
exitError(ERR_MISSING_SWITCH, "-i");
pfileInven = fopen(pszInvenFileName, "r");
if (pfileInven == NULL)
exitError(ERR_CUSTOMER_ORDER_FILENAME, pszInvenFileName);
// reads inventory
iStockCnt = readInventory(stockM);
// process the orders
rc = processOrders(stockM, iStockCnt);
printf("\n");
return rc;

// included so that you can put a breakpoint on this line

}
int processOrders(Stock stockM[],int iStockCnt)
{
char szInputBuffer[100];
Customer customer;
PurchaseItem item;
int iscanCnt;
//establishes the buffer to the file
while(fgets(szInputBuffer, 100, pfileOrder) != NULL)
{
printf("**********************ORDER***************************");
//reads first line and puts data into customer
iscanCnt = sscanf(szInputBuffer, "%[^ ] %30[^\n]\n",
customer.szEmailAddr,

customer.szFullName);
//checks if the appropriate data is read
if(iscanCnt < 2)
exitError("Bad Input Data", "");
//prints name and email of customer
printf("\n%-20s %-s\n",
customer.szEmailAddr,
customer.szFullName);
//moves to next line of file
if(fgets(szInputBuffer, 100, pfileOrder) != NULL)
{
//reads and puts data into customer
iscanCnt = sscanf(szInputBuffer, "%[^,],%[^,],%2s,%s",
customer.szStreetAddress,
customer.szCity,
customer.szStateCd,
customer.szZipCd);
//checks if data is read correctly
if(iscanCnt < 2)
exitError("Bad Input Data", "");
//prints customer data: address, city, state, zip
printf("%-30s\n%-s, %-2s %-5s\n",
customer.szStreetAddress,
customer.szCity,
customer.szStateCd,
customer.szZipCd);
printf("%8s %12s", "Stock", "Quantity");
//loops through stock orders
while(fgets(szInputBuffer, 100, pfileOrder) != NULL)
{
//reads stock to be ordered and quantity
iscanCnt = sscanf(szInputBuffer, "%[^ ] %d",
item.szStockNumber,
&item.iRequestQty);
//checks if data is correclty read
if(iscanCnt < 2)
exitError("Bad Order Data", "");
//checks if it is the end of order
if(strcmp(item.szStockNumber, "000000") == 0)
break;
//prints orders
printf("\n%9s %11d",
item.szStockNumber,
item.iRequestQty);
}
printf("\n");
}
}
return 0;
}
/******************** processCommandSwitches *****************************
void processCommandSwitches(int argc, char *argv[], char **ppszOrderFileName)
Purpose:
Checks the syntax of command line arguments and returns the filenames.
If any switches are unknown, it exits with an error.
Parameters:
I int argc
count of command line arguments
I char *argv[]
array of command line arguments

O char **ppszOrderFileName
order file name
Notes:
If a -? switch is passed, the usage is printed and the program exits
with USAGE_ONLY.
If a syntax error is encountered (e.g., unknown switch), the program
prints a message to stderr and exits with ERR_COMMAND_LINE_SYNTAX.
**************************************************************************/
void processCommandSwitches(int argc, char *argv[], char **ppszOrderFileName, ch
ar **ppszInvenFileName)
{
int i;
int rc = 0;
int bShowCommandHelp = FALSE;
for (i = 1; i < argc; i++)
{
// check for a switch
if (argv[i][0] != '-')
exitUsage(i, ERR_EXPECTED_SWITCH, argv[i]);
// determine which switch it is
switch (argv[i][1])
{
case 'c':
// Customer Order File Name
if (++i >= argc)
exitUsage(i, ERR_MISSING_ARGUMENT, argv[i - 1]);
else
*ppszOrderFileName = argv[i];
break;
case 'i':
// Inventory Order File Name
if (++i >= argc)
exitUsage(i, ERR_MISSING_ARGUMENT, argv[
i - 1]);
else
*ppszInvenFileName = argv[i];
break;
case '?':
exitUsage(USAGE_ONLY, "", "");
break;
default:
exitUsage(i, ERR_EXPECTED_SWITCH, argv[i]);
}
}
}
/******************** exitError *****************************
void exitError(char *pszMessage, char *pszDiagnosticInfo)
Purpose:
Prints an error message and diagnostic to stderr. Exits with
ERROR_PROCESSING.
Parameters:
I char *pszMessage
error message to print
I char *pszDiagnosticInfo
supplemental diagnostic information
Notes:
This routine causes the program to exit.
**************************************************************************/
void exitError(char *pszMessage, char *pszDiagnosticInfo)
{
fprintf(stderr, "Error: %s %s\n"
, pszMessage
, pszDiagnosticInfo);

exit(ERROR_PROCESSING);
}
/******************** exitUsage *****************************
void exitUsage(int iArg, char *pszMessage, char *pszDiagnosticInfo)
Purpose:
If this is an argument error (iArg >= 0), it prints a formatted message
showing which argument was in error, the specified message, and
supplemental diagnostic information. It also shows the usage. It exits
with ERR_COMMAND_LINE_SYNTAX.
If this is just asking for usage (iArg will be -1), the usage is shown.
It exits with USAGE_ONLY.
Parameters:
I int iArg
command argument subscript
I char *pszMessage
error message to print
I char *pszDiagnosticInfo
supplemental diagnostic information
Notes:
This routine causes the program to exit.
**************************************************************************/
void exitUsage(int iArg, char *pszMessage, char *pszDiagnosticInfo)
{
if (iArg >= 0)
fprintf(stderr, "Error: bad argument #%d. %s %s\n"
, iArg
, pszMessage
, pszDiagnosticInfo);
fprintf(stderr, "order -c customerOrderFile -i inventoryOrderFile\n");
if (iArg >= 0)
exit(-1);
else
exit(-2);
}

You might also like