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

  1. #ifndef lint
  2. static char *RCSid = "$Header: /u5/davy/progs/index/RCS/selectdb.c,v 1.1 89/08/09 11:07:06 davy Exp $";
  3. #endif
  4. /*
  5.  * selectdb.c - database selection routines.
  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:    selectdb.c,v $
  15.  * Revision 1.1  89/08/09  11:07:06  davy
  16.  * Initial revision
  17.  * 
  18.  */
  19. #include <sys/param.h>
  20. #include <sys/dir.h>
  21. #include <curses.h>
  22. #include <stdio.h>
  23. #include "defs.h"
  24.  
  25. /*
  26.  * select_db - allow the user to select a database from the list of databases
  27.  *           he has, or to create a new database.
  28.  */
  29. char *
  30. select_db()
  31. {
  32.     char dbname[MAXPATHLEN];
  33.     char *dblist[MAXDBFILES];
  34.     register int ndbs, i, row, col, spread;
  35.  
  36.     /*
  37.      * Load the list of databases the user has.
  38.      */
  39.     ndbs = load_dblist(dblist);
  40.     spread = (ndbs + 3) / 4;
  41.  
  42.     /*
  43.      * Set tty modes, clear screen.
  44.      */
  45.     set_modes();
  46.     clear();
  47.  
  48.     /*
  49.      * Print the list of databases in four columns.
  50.      */
  51.     for (row = 0; row < spread; row++) {
  52.         for (col = 0; col < 4; col++) {
  53.             i = col * spread + row;
  54.  
  55.             if (dblist[i])
  56.                 mvaddstr(row, col * COLS/4, dblist[i]);
  57.         }
  58.     }
  59.  
  60.     *dbname = '\0';
  61.  
  62.     /*
  63.      * Prompt for the name of a database.
  64.      */
  65.     while (*dbname == '\0')
  66.         prompt_str(spread+2, 0, "Select a database: ", dbname);
  67.  
  68.     /*
  69.      * If the database exists, return its name.
  70.      */
  71.     for (i = 0; i < ndbs; i++) {
  72.         if (!strcmp(dbname, dblist[i]))
  73.             return(savestr(dbname));
  74.     }
  75.  
  76.     /*
  77.      * Doesn't exist - create it.
  78.      */
  79.     create_db(dbname);
  80.     return(savestr(dbname));
  81. }
  82.  
  83. /*
  84.  * load_dblist - load up a list of the databases the user has.
  85.  */
  86. load_dblist(dblist)
  87. char **dblist;
  88. {
  89.     DIR *dp;
  90.     int ndbs;
  91.     char *rindex();
  92.     register char *s;
  93.     extern int compare();
  94.     register struct direct *d;
  95.  
  96.     ndbs = 0;
  97.  
  98.     /*
  99.      * Open the database directory.
  100.      */
  101.     if ((dp = opendir(dbasedir)) == NULL) {
  102.         fprintf(stderr, "%s: cannot open \"%s\".\n", pname, dbasedir);
  103.         exit(1);
  104.     }
  105.  
  106.     /*
  107.      * Read entries from the directory...
  108.      */
  109.     while ((d = readdir(dp)) != NULL) {
  110.         /*
  111.          * Search for a "." in the name, which marks
  112.          * the suffix.
  113.          */
  114.         if ((s = rindex(d->d_name, '.')) == NULL)
  115.             continue;
  116.  
  117.         /*
  118.          * If this is an index definition file, save its
  119.          * name.
  120.          */
  121.         if (!strcmp(s, IDXFILE_SUFFIX)) {
  122.             if (ndbs < MAXDBFILES) {
  123.                 *s = '\0';
  124.                 dblist[ndbs++] = savestr(d->d_name);
  125.             }
  126.         }
  127.     }
  128.  
  129.     /*
  130.      * Sort the list.
  131.      */
  132.     qsort(dblist, ndbs, sizeof(char *), compare);
  133.     closedir(dp);
  134.  
  135.     return(ndbs);
  136. }
  137.  
  138. /*
  139.  * compare - comparis routine for qsort of dblist.
  140.  */
  141. static int
  142. compare(a, b)
  143. char **a, **b;
  144. {
  145.     return(strcmp(*a, *b));
  146. }
  147.