home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume20 / index-db / part01 / dbfunc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-23  |  3.6 KB  |  216 lines

  1. #ifndef lint
  2. static char *RCSid = "$Header: /u5/davy/progs/index/RCS/dbfunc.c,v 1.1 89/08/09 11:06:31 davy Exp $";
  3. #endif
  4. /*
  5.  * dbfunc.c - database functions selected from the main menu.
  6.  *
  7.  * David A. Curry
  8.  * Research Institute for Advanced Computer Science
  9.  * Mail Stop 230-5
  10.  * NASA Ames Research Center
  11.  * Moffett Field, CA 94035
  12.  * davy@riacs.edu
  13.  *
  14.  * $Log:    dbfunc.c,v $
  15.  * Revision 1.1  89/08/09  11:06:31  davy
  16.  * Initial revision
  17.  * 
  18.  */
  19. #include <curses.h>
  20. #include <stdio.h>
  21. #include "defs.h"
  22.  
  23. /*
  24.  * add_entry - add an entry to the database.
  25.  */
  26. add_entry()
  27. {
  28.     register int i;
  29.     struct dbfile *realloc();
  30.  
  31.     /*
  32.      * Search for an empty entry in the array.
  33.      */
  34.     for (i=0; i < dbsize; i++) {
  35.         /*
  36.          * Found one; use it.
  37.          */
  38.         if ((db[i].db_flag & DB_VALID) == 0) {
  39.             /*
  40.              * Clear out any old junk.
  41.              */
  42.             bzero(&db[i], sizeof(struct dbfile));
  43.  
  44.             /*
  45.              * Let the user edit the entry.
  46.              */
  47.             if (edit_entry(&db[i], "new")) {
  48.                 /*
  49.                  * Mark it valid, mark the database
  50.                  * as modified, and increase the
  51.                  * number of entries.
  52.                  */
  53.                 db[i].db_flag |= DB_VALID;
  54.                 dbmodified = 1;
  55.                 dbentries++;
  56.  
  57.                 /*
  58.                  * Sort the array, to get this
  59.                  * entry into its proper place.
  60.                  */
  61.                 qsort(db, dbentries, sizeof(struct dbfile),
  62.                       dbsort);
  63.             }
  64.  
  65.             return;
  66.         }
  67.     }
  68.  
  69.     /*
  70.      * Didn't find an empty slot, so we have to allocate
  71.      * some more.
  72.      */
  73.     dbsize *= 2;
  74.  
  75.     if ((db = realloc(db, dbsize * sizeof(struct dbfile))) == NULL) {
  76.         error("%s: out of memory.\n", pname, 0, 0);
  77.         exit(1);
  78.     }
  79.  
  80.     bzero(&db[dbentries], sizeof(struct dbfile));
  81.  
  82.     /*
  83.      * Let the user edit the new entry.
  84.      */
  85.     if (edit_entry(&db[dbentries], "new")) {
  86.         /*
  87.          * Mark the entry as valid, mark the
  88.          * database as modified, and increase
  89.          * the number of entries.
  90.          */
  91.         db[dbentries].db_flag |= DB_VALID;
  92.         dbmodified = 1;
  93.         dbentries++;
  94.  
  95.         qsort(db, dbentries, sizeof(struct dbfile), dbsort);
  96.     }
  97. }
  98.  
  99. /*
  100.  * del_entry - delete an entry from the database.
  101.  */
  102. del_entry(entry)
  103. struct dbfile *entry;
  104. {
  105.     char c;
  106.     int x, y;
  107.  
  108.     /*
  109.      * Prompt the user for confirmation.
  110.      */
  111.     getyx(curscr, y, x);
  112.     c = prompt_char(y, 0, "Really delete this entry? ", "YyNn");
  113.  
  114.     /*
  115.      * Return the status of the confirmation.
  116.      */
  117.     switch (c) {
  118.     case 'Y':
  119.     case 'y':
  120.         return(1);
  121.     case 'N':
  122.     case 'n':
  123.         return(0);
  124.     }
  125. }
  126.  
  127. /*
  128.  * find_entry - search for entries using a regular expression.
  129.  */
  130. find_entry()
  131. {
  132.     register int i;
  133.     char pattern[BUFSIZ];
  134.  
  135.     /*
  136.      * Clear the screen and prompt for the pattern to
  137.      * search for.
  138.      */
  139.     clear();
  140.     prompt_str(LINES/2, 0, "Pattern to search for: ", pattern);
  141.  
  142.     /*
  143.      * Search.  search_db will set DB_PRINT in the entries
  144.      * which match, and return non-zero if anything matched.
  145.      */
  146.     if (search_db(pattern)) {
  147.         /*
  148.          * Display the entries that matched.
  149.          */
  150.         disp_entries();
  151.  
  152.         /*
  153.          * Clear the DB_PRINT flags.
  154.          */
  155.         for (i=0; i < dbentries; i++)
  156.             db[i].db_flag &= ~DB_PRINT;
  157.     }
  158.     else {
  159.         /*
  160.          * Nothing matched.  Tell the user.
  161.          */
  162.         prompt_char(LINES/2, 0,
  163.             "No entries match pattern, type RETURN to continue: ",
  164.             "\n");
  165.     }
  166. }
  167.  
  168. /*
  169.  * read_db - run through the database entry by entry.
  170.  */
  171. read_db()
  172. {
  173.     register int i;
  174.  
  175.     /*
  176.      * Sort the database, so we're sure it's in order.
  177.      */
  178.     qsort(db, dbentries, sizeof(struct dbfile), dbsort);
  179.  
  180.     /*
  181.      * Set DB_PRINT in all entries.
  182.      */
  183.     for (i=0; i < dbentries; i++) {
  184.         if (db[i].db_flag & DB_VALID)
  185.             db[i].db_flag |= DB_PRINT;
  186.     }
  187.  
  188.     /*
  189.      * Display the entries.
  190.      */
  191.     disp_entries();
  192.  
  193.     /*
  194.      * Clear DB_PRINT.
  195.      */
  196.     for (i=0; i < dbentries; i++)
  197.         db[i].db_flag &= ~DB_PRINT;
  198. }
  199.  
  200. /*
  201.  * save_bye - save the database and exit.
  202.  */
  203. save_bye(dbname)
  204. char *dbname;
  205. {
  206.     /*
  207.      * Save the database.
  208.      */
  209.     save_db(dbname);
  210.  
  211.     /*
  212.      * Exit.
  213.      */
  214.     byebye();
  215. }
  216.