home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / pctech / hlsrc.arc / FSELECT.C next >
C/C++ Source or Header  |  1988-09-09  |  3KB  |  89 lines

  1. /*+
  2.     Name:    fselect.c
  3.     Date:    06-Jun-1988
  4.     Author:    Kent J. Quirk
  5.         (c) Copyright 1988 Ziff Communications Co.
  6.     Abstract:    Displays a list of files and allows user to select one
  7.         of them.
  8.     History:    09-Sep-88   kjq     Version 1.00
  9. -*/
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <dos.h>
  15. #include <graph.h>
  16. #include "winmenu.h"
  17.  
  18. /**** s t r c m p p ****
  19.     Abstract:    Compares two pointers to string pointers using strcmp.
  20.         Used by qsort.
  21.     Parameters: Pointers to pointers to the two strings to be compared.
  22.     Returns:    Whatever strcmp would for the two strings.
  23.     Comments:    Necessary because qsort sorts an array of pointers, not
  24.         the strings themselves.
  25. ****************************/
  26. int strcmpp(sp, tp)
  27. char **sp, **tp;
  28. {
  29.     return(strcmp(*sp, *tp));
  30. }
  31.  
  32. #define MAXFILES 256
  33. /**** s e l e c t _ 1 _ f i l e ****
  34.     Abstract:    Displays a sorted list of files in a menu and allows user to
  35.         select one. The files displayed meet the specifications
  36.         of the ambiguous file name passed in (such as *.c, etc).  
  37.     Parameters: char *afn - an ambiguous file name supported by DOS
  38.     Returns:    char * -- a pointer to a malloc'd string containing the 
  39.                 filename chosen, or NULL.
  40.     Comments:    Uses the scroll_menu function to display the names.
  41.         Code to trim off the extensions has been commented out.
  42.         The window size and location is hard-coded.
  43. ****************************/
  44. char *select_1_file(afn)
  45. char *afn;
  46. {
  47.     char **filelist;
  48.     struct find_t buffer;
  49.     char *choice;
  50.  
  51.     int i;
  52.  
  53.     filelist = malloc(MAXFILES * sizeof(char *));
  54.     
  55.     if (_dos_findfirst(afn, _A_NORMAL, &buffer) == 0)
  56.     {
  57.     /*    *strchr(buffer.name, '.') = 0;         */
  58.     filelist[0] = strdup(buffer.name);    /* read the first filename */
  59.     }
  60.  
  61.     for (i=1; _dos_findnext(&buffer) == 0; i++)     /* and all the rest */
  62.     {
  63.     if (i > MAXFILES)
  64.     {
  65.         printf("Sorry, too many files.\n");     /* ugly */
  66.         break;
  67.     }
  68.     else
  69.     {
  70.         /*         *strchr(buffer.name, '.') = 0;    */
  71.         filelist[i] = strdup(buffer.name);
  72.     }
  73.     }
  74.  
  75.     filelist[i] = NULL;             /* the end of the list */
  76.     qsort(filelist, i, sizeof(filelist[0]), strcmpp);
  77.  
  78.     if ((i = scroll_menu(filelist, afn, "", 5, 3, 17, 1)) >= 0)
  79.     choice = strdup(filelist[i]);        /* dup the one chosen */
  80.     else
  81.     choice = NULL;
  82.  
  83.     for (i=0; filelist[i] != NULL; i++)     /* free them all */
  84.     free(filelist[i]);
  85.     
  86.     free(filelist);                /* and the list itself */
  87.     return(choice);
  88. }
  89.