home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 428_02 / libsrc / pickfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-13  |  2.5 KB  |  113 lines

  1. /*
  2. ** pickfile.c
  3. **
  4. ** Pictor, Version 1.51, Copyright (c) 1992-94 SoftCircuits
  5. ** Redistributed by permission.
  6. */
  7.  
  8. #include <dos.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <malloc.h>
  12. #include "pictor.h"
  13.  
  14. struct find_t fileinfo;
  15.  
  16. /* Note: a pointer to this structure is passed as a pointer to a */
  17. /* string to listbox(), so name[] must always be the first member */
  18. struct filename {
  19.     char name[14];
  20.     struct filename *next;
  21. };
  22.  
  23. static struct filename *head,*new,*curr;
  24. static struct filename **filearray;
  25.  
  26.  
  27. /*
  28. ** Frees pickfile() memory structures.
  29. */
  30. static void free_filemem(void)
  31. {
  32.     struct filename *ptr,*next;
  33.  
  34.     for(ptr = head;ptr != NULL;ptr = next) {
  35.         next = ptr->next;
  36.         free(ptr);
  37.     }
  38.     if(filearray != NULL) free(filearray);
  39.  
  40. } /* free_filemem */
  41.  
  42. /*
  43. ** Compare routine for qsort().
  44. */
  45. static int compare(const void *elem1,const void *elem2)
  46. {
  47.     return(strcmp(*(char **)elem1,*(char **)elem2));
  48.  
  49. } /* compare */
  50.  
  51. /*
  52. ** Uses listbox to select from the files matching filespec. Returns
  53. ** a pointer to the selected filename. Returns NULL if user pressed
  54. ** Escape, not enough memory, or no files matching filespec. Yo can
  55. ** use escpressed() to determine which occured.
  56. */
  57. char *pickfile(char *filespec,char *title,COLORSTRUCT *colors)
  58. {
  59.     int i,numfiles = 0,selection = 0;
  60.     static char buffer[13];
  61.  
  62.     head = NULL;
  63.     filearray = NULL;
  64.  
  65.     /* build linked list of files in current directory */
  66.     if(!_dos_findfirst(filespec,_A_NORMAL,&fileinfo)) {
  67.  
  68.         do {
  69.             new = (struct filename *)malloc(sizeof(struct filename));
  70.             if(new == NULL) {
  71.                 free_filemem();
  72.                 return(NULL);
  73.             }
  74.  
  75.             if(head == NULL)
  76.                 head = new;
  77.             else
  78.                 curr->next = new;
  79.             curr = new;
  80.             curr->next = NULL;
  81.  
  82.             strcpy(curr->name,fileinfo.name);
  83.             numfiles++;
  84.  
  85.         } while(!_dos_findnext(&fileinfo));
  86.     }
  87.     if(numfiles == 0) {
  88.         messagebox("No files found",title,MB_OK,colors);
  89.         return(NULL);
  90.     }
  91.  
  92.     /* build array of pointers */
  93.     filearray = (struct filename **)malloc(sizeof(struct filename)*numfiles);
  94.     if(filearray == NULL) {
  95.         free_filemem();
  96.         return(NULL);
  97.     }
  98.     for(i = 0,curr = head;i < numfiles;i++,curr = curr->next) {
  99.         filearray[i] = curr;
  100.     }
  101.  
  102.     qsort(filearray,numfiles,sizeof(struct filename *),compare);
  103.  
  104.     if(listbox((char **)filearray,numfiles,&selection,title,colors)) {
  105.         strcpy(buffer,filearray[selection]->name);
  106.         free_filemem();
  107.         return(buffer);
  108.     }
  109.     free_filemem();
  110.     return(NULL);
  111.  
  112. } /* pickfile */
  113.