home *** CD-ROM | disk | FTP | other *** search
/ The Best of Windows 95.com 1996 September / WIN95_09964.iso / program / mc.exe / SAMPLE.MC < prev    next >
Encoding:
Text File  |  1996-05-10  |  2.7 KB  |  117 lines

  1. /*
  2. * this example MOAL C program implements an address book
  3. */
  4. #include <std_lib.h>
  5.  
  6. // prototypes
  7. add();
  8. list();
  9.  
  10. // database
  11. struct ab_tag {
  12.     char var name[],
  13.          var phone[];
  14.     struct {
  15.         char var line[];
  16.     } var addr[].resize[5];
  17. } var ab[].resize[5];
  18.  
  19.  
  20. main(struct ar args[]) int status
  21. {
  22. char var buf[], ch;
  23. int wrk1, error;
  24.  
  25. // arbitrary filename for creation and storage of the database
  26. #define DB_FILE "c:\\temp\\tbase"
  27.  
  28. // read the database
  29. if(!error, buf = fgetall(DB_FILE, BINARY) && buf.occurs)
  30.     ab = buf[0](struct ab_tag []); // store the database in memory
  31.  
  32. // interact with the user
  33. for(;;)
  34.     {printf("\nenter a command (q - quit and save, "
  35.             "p - quit and purge,\n                 "
  36.             "a - add, l - list):\n");
  37.  
  38.      // get a line of input
  39.      for(; fgets(buf, SHRT_MAX, stdin) == EOF;) ;
  40.  
  41.      // extract the first non-whitespace character
  42.      for(ch = ' ', wrk1 = 0; wrk1 < buf.occurs; ++wrk1)
  43.          {if(!isspace(buf[wrk1]))
  44.               {ch = buf[wrk1];
  45.                break;}}
  46.  
  47.      switch(ch)
  48.          {case 'p': case 'q': goto done;
  49.           case 'a': add();
  50.           case 'l': list();
  51.           default: printf("command '%c' is unrecognized\n", ch);}}
  52.  
  53. // write the database
  54. done:
  55. buf.occurs = 0;
  56. if(ch == 'q')
  57.     buf[0](struct ab_tag []) = ab;
  58.  
  59. printf("database size:%d   file size:%d\n", sizeof(ab), buf.occurs);
  60.  
  61. if(error = fputall(DB_FILE, BINARY, buf))
  62.     {printf("file '%s' : %s\n", DB_FILE, strerror(error));
  63.      status = EXIT_FAILURE;}
  64. else
  65.     status = EXIT_SUCCESS;
  66. }
  67.  
  68.  
  69. list()
  70. {
  71. int wrk1, wrk2;
  72.  
  73. for(wrk1 = 0; wrk1 < ab.occurs; ++wrk1)
  74.     {printf("\n");
  75.      printf(" %s\n", ab[wrk1].name);
  76.      printf(" %s\n", ab[wrk1].phone);
  77.      for(wrk2 = 0; wrk2 < ab[wrk1].addr.occurs; ++wrk2)
  78.          printf(" %s\n", ab[wrk1].addr[wrk2].line);}
  79.  
  80. printf("\n");
  81. }
  82.  
  83.  
  84. add()
  85. {
  86. char buf[1000];
  87. int wrk1, sub, step;
  88.  
  89. for(sub = ab.occurs, step = 1;; ++step)
  90.     {if(step == 1)
  91.          printf("enter the name (or q if done):\n");
  92.      else if(step == 2)
  93.          printf("enter the telephone number (or q if done):\n");
  94.      else
  95.          {if(step == 3)
  96.               wrk1 = 0;
  97.           else
  98.               ++wrk1;
  99.           printf("enter address-line %d (or q if done):\n", wrk1 + 1);}
  100.  
  101.      // get a line of input and trim trailing newline
  102.      for(; fgets(buf, SHRT_MAX, stdin) == EOF;) ;
  103.      --buf.occurs;
  104.  
  105.      if(strcmp(buf, "q") == 0)
  106.          break;
  107.  
  108.      // save the data
  109.      if(step == 1)
  110.           strcpy(ab[sub].name, buf);
  111.      else if(step == 2)
  112.           strcpy(ab[sub].phone, buf);
  113.      else
  114.           strcpy(ab[sub].addr[wrk1].line, buf);}
  115.  
  116. printf("\n");
  117. }