home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 337_01 / l_dir.c < prev    next >
C/C++ Source or Header  |  1991-01-14  |  5KB  |  210 lines

  1. /* Copyright (c) James L. Pinson 1990,1991  */
  2.  
  3. /**********************    L_DIR.C    ***************************/
  4.  
  5. #include "mydef.h"
  6. #include "stddef.h"
  7. #include "dos.h"
  8. #include "stdio.h"
  9.  
  10. #define DIR_NAME_LENGTH 13  /* length of directory entry
  11.                                (includes '\0') */
  12. #if defined QUICKC
  13.  
  14. #include "malloc.h"
  15. #include "memory.h"
  16.  
  17. #endif
  18.  
  19. #if defined TURBOC
  20.  
  21. #include "alloc.h"
  22. #include "mem.h"
  23. #include "string.h"
  24. #include "stdlib.h"
  25. #include "dir.h"
  26.  
  27. #endif
  28.  
  29.  
  30. /*****************************************************************
  31.  
  32.  Usage:   int dir(char *filespec,char *selection);
  33.  
  34.  
  35.   char *filespec=  Wildcard specification for directory
  36.                    ("*.*" ,"*.c") etc.
  37.  
  38.   char *selection= Pointer to a location suitable for storage of
  39.                    a DIR_NAME_LENGTH character array.  The name of
  40.                    the selected file is copied to this location. If
  41.                    the user presses Escape, no selection is made
  42.                    and *selection = "".
  43.  
  44.   Returns a 1 if memory is unavailable, otherwise returns a 0.
  45.  
  46.   Allows list selection of directory files specified by filespec.
  47.   Makes use of the list_select() function. Allows point-and-shoot
  48.   or Speed key selection.   The list is displayed in the currently
  49.   active (top) window.
  50.  
  51. *****************************************************************/
  52.  
  53. int dir(char *filespec, char *selection)
  54. {
  55. extern struct screen_structure scr;
  56. extern struct window_structure w[];
  57.  
  58. int done;               /* flag */
  59. int filecount=file_count(filespec);
  60.  
  61. #if defined TURBOC
  62.    struct ffblk DTA;
  63. #endif
  64.  
  65. #if defined QUICKC
  66.    struct find_t DTA;
  67. #endif
  68.  
  69. char **list;      /* a pointer to a character pointer */
  70.  
  71. int i,j,number;
  72.  
  73. /* allocate array */
  74.  
  75. /* note: we allocate an extra list pointer for the null terminator */
  76.    list =(char **)malloc ((filecount+1)*sizeof(char *));
  77.  
  78.  if (list==NULL)return(1);
  79.  
  80.  /* null the list of pointers */
  81.  for(i=0;i<=filecount;i++) list[i]=NULL;
  82.  
  83.   /* get the first directory listing */
  84.   /* different techniques used by Turbo C an QuickC  */
  85.  
  86. #if defined TURBOC
  87.    done=findfirst(filespec,&DTA,0);
  88. #endif
  89.  
  90. #if defined QUICKC
  91.    done= _dos_findfirst(filespec,_A_NORMAL,&DTA);
  92. #endif
  93.  
  94. /* the first step is to set pointer array to memory allocated
  95.    for each directory entry */
  96.  
  97.  
  98. i=0;
  99.  while(!done){  /* loop to get any additional entries */
  100.  
  101.   if(i==filecount) break;    /* stop if we exceed our pointers */
  102.   /* allocate memory */
  103.   list[i]=  malloc (DIR_NAME_LENGTH*sizeof(char));
  104.  
  105.   if(list[i] !=NULL){ /* if we have memory for directory entry */
  106.  
  107. /* copy the directory listing to the allocated space */
  108.  
  109. #if defined TURBOC
  110.          sprintf(list[i],"%-12s",DTA.ff_name);
  111. #endif
  112.  
  113. #if defined QUICKC
  114.          sprintf(list[i],"%-12s",DTA.name);
  115. #endif
  116.  
  117.  
  118.        /* get the next directory listing */
  119. #if defined TURBOC
  120.         done=findnext(&DTA);
  121. #endif
  122.  
  123. #if defined QUICKC
  124.         done=_dos_findnext(&DTA);
  125. #endif
  126.  
  127.      }  /* end if != NULL */
  128.       else return (1);   /* error allocating memory */
  129.     i++;                   /* inc index */
  130.  
  131.  } /* end while(!done) */
  132.  
  133.  /* now we call on list_select() to process directory list */
  134.  
  135.     if(i>0){  /* if any entries were found */
  136.  
  137.      number=list_select(list);  /* Call list_select so user can
  138.                                    make selection.  Upon return,
  139.                                    "number" equals the file selected
  140.                                    or -1 if user "Escaped". */
  141.     if(number==-1) strcpy(selection,"");
  142.      else
  143.        strcpy(selection, list[number]);
  144.  
  145.     for(j=0;j<=filecount-1;j++){   /* free up allocated memory */
  146.      if (list[j]!= NULL){
  147.       free(list[j]);
  148.       list[j]=NULL;
  149.      }
  150.      else
  151.         break;
  152.     }
  153.      if (list!=NULL){
  154.        free (list);
  155.        list=NULL;
  156.      }
  157.   }
  158.   return(0);
  159. }
  160.  
  161.  
  162. int file_count(char *filespec)
  163. {
  164. extern struct screen_structure scr;
  165. extern struct window_structure w[];
  166.  
  167. int done;               /* flag */
  168. int count=0;
  169.  
  170. #if defined TURBOC
  171.    struct ffblk DTA;
  172. #endif
  173.  
  174. #if defined QUICKC
  175.    struct find_t DTA;
  176. #endif
  177.  
  178. #if defined TURBOC
  179.    done=findfirst(filespec,&DTA,0);
  180. #endif
  181.  
  182. #if defined QUICKC
  183.    done= _dos_findfirst(filespec,_A_NORMAL,&DTA);
  184. #endif
  185.    if(!done)count++;
  186.  
  187. /* first step is set pointer array to memory allocated
  188.    for each directory entry */
  189.  
  190.  
  191.  while(!done){       /* if we have at least one entry,
  192.                         loop to get the rest */
  193.  
  194. /* get the next directory listing */
  195.  
  196. #if defined TURBOC
  197.         done=findnext(&DTA);
  198. #endif
  199.  
  200. #if defined QUICKC
  201.         done=_dos_findnext(&DTA);
  202. #endif
  203.  
  204.   if(!done) count++;
  205.  
  206.  } /* end while(!done) */
  207.  
  208.  return (count);
  209. }
  210.