home *** CD-ROM | disk | FTP | other *** search
- /*
- * this example MOAL C program implements an address book
- */
- #include <std_lib.h>
-
- // prototypes
- add();
- list();
-
- // database
- struct ab_tag {
- char var name[],
- var phone[];
- struct {
- char var line[];
- } var addr[].resize[5];
- } var ab[].resize[5];
-
-
- main(struct ar args[]) int status
- {
- char var buf[], ch;
- int wrk1, error;
-
- // arbitrary filename for creation and storage of the database
- #define DB_FILE "c:\\temp\\tbase"
-
- // read the database
- if(!error, buf = fgetall(DB_FILE, BINARY) && buf.occurs)
- ab = buf[0](struct ab_tag []); // store the database in memory
-
- // interact with the user
- for(;;)
- {printf("\nenter a command (q - quit and save, "
- "p - quit and purge,\n "
- "a - add, l - list):\n");
-
- // get a line of input
- for(; fgets(buf, SHRT_MAX, stdin) == EOF;) ;
-
- // extract the first non-whitespace character
- for(ch = ' ', wrk1 = 0; wrk1 < buf.occurs; ++wrk1)
- {if(!isspace(buf[wrk1]))
- {ch = buf[wrk1];
- break;}}
-
- switch(ch)
- {case 'p': case 'q': goto done;
- case 'a': add();
- case 'l': list();
- default: printf("command '%c' is unrecognized\n", ch);}}
-
- // write the database
- done:
- buf.occurs = 0;
- if(ch == 'q')
- buf[0](struct ab_tag []) = ab;
-
- printf("database size:%d file size:%d\n", sizeof(ab), buf.occurs);
-
- if(error = fputall(DB_FILE, BINARY, buf))
- {printf("file '%s' : %s\n", DB_FILE, strerror(error));
- status = EXIT_FAILURE;}
- else
- status = EXIT_SUCCESS;
- }
-
-
- list()
- {
- int wrk1, wrk2;
-
- for(wrk1 = 0; wrk1 < ab.occurs; ++wrk1)
- {printf("\n");
- printf(" %s\n", ab[wrk1].name);
- printf(" %s\n", ab[wrk1].phone);
- for(wrk2 = 0; wrk2 < ab[wrk1].addr.occurs; ++wrk2)
- printf(" %s\n", ab[wrk1].addr[wrk2].line);}
-
- printf("\n");
- }
-
-
- add()
- {
- char buf[1000];
- int wrk1, sub, step;
-
- for(sub = ab.occurs, step = 1;; ++step)
- {if(step == 1)
- printf("enter the name (or q if done):\n");
- else if(step == 2)
- printf("enter the telephone number (or q if done):\n");
- else
- {if(step == 3)
- wrk1 = 0;
- else
- ++wrk1;
- printf("enter address-line %d (or q if done):\n", wrk1 + 1);}
-
- // get a line of input and trim trailing newline
- for(; fgets(buf, SHRT_MAX, stdin) == EOF;) ;
- --buf.occurs;
-
- if(strcmp(buf, "q") == 0)
- break;
-
- // save the data
- if(step == 1)
- strcpy(ab[sub].name, buf);
- else if(step == 2)
- strcpy(ab[sub].phone, buf);
- else
- strcpy(ab[sub].addr[wrk1].line, buf);}
-
- printf("\n");
- }