+/EE312

software design

git clone https://git.julia.blue/+/EE312

Joseph RyanProject 40c27270

master
2.4 KiB88 linesraw
1#include <stdio.h> // declarations of FILE and printf
2#include <stdlib.h> // includes declaration of exit()
3#include "MyString.h" // String ADT case study from class
4#include "Invent.h"
5
6FILE* input_stream;
7String inventory;
8String purchase;
9String summarize;
10String quit;
11
12void displayTailAndClose() {
13    char* rval;
14    char buff[BUFSIZ];
15
16    rval = fgets(buff, BUFSIZ, input_stream);
17    while (rval != 0) {
18        printf("%s", buff);
19        rval = fgets(buff, BUFSIZ, input_stream);
20    }
21    fclose(input_stream);
22}
23
24void readInput(const char* file_name) {
25    String next_command;
26    bool all_done = false;
27
28    /* open the file */
29    input_stream = fopen(file_name, "r");
30    if (input_stream == 0) {
31        printf("sorry, I could not open %s for reading\n", file_name);
32        exit(-1);
33    }
34
35    /* main loop, read commands from input file */
36    do {
37        readString(&next_command);
38        if (StringIsEqualTo(&next_command, &inventory)) {
39            processInventory();
40        } else if (StringIsEqualTo(&next_command, &purchase)) {
41            processPurchase();
42        } else if (StringIsEqualTo(&next_command, &summarize)) {
43            processSummarize();
44        } else if (StringIsEqualTo(&next_command, &quit)) {
45            all_done = true;
46        } else {
47            printf("Encountered an error in input file\n");
48            printf("The offending line has been ignored\n");
49        }
50        StringDestroy(&next_command);
51    } while (! all_done);
52
53    displayTailAndClose();
54}
55
56
57int main(void) {
58    /* we make four "constant" strings for the four comamnds */
59    inventory = StringCreate("Inventory");
60    purchase = StringCreate("Purchase");
61    summarize = StringCreate("Summarize");
62    quit = StringCreate("Quit");
63
64    readInput("test1.txt");
65    reset();
66    readInput("test2.txt");
67    reset();
68    readInput("test3.txt");
69    reset();
70    readInput("test4.txt");
71    reset();
72
73    /* all the test are done, time to clean up, let's deallocate our "constant strings" */
74    StringDestroy(&inventory);
75    StringDestroy(&purchase);
76    StringDestroy(&summarize);
77    StringDestroy(&quit);
78}
79
80void readNum(int* p) {
81    fscanf(input_stream, "%d", p);
82}
83
84void readString(String* p) {
85    char buff[BUFSIZ];
86    fscanf(input_stream, "%s", buff);
87    *p = StringCreate(buff);
88}