home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / program / gempp15b / gemfs.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-23  |  3.3 KB  |  173 lines

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  This file is Copyright 1992,1993 by Warwick W. Allison.
  4. //  This file is part of the gem++ library.
  5. //  You are free to copy and modify these sources, provided you acknowledge
  6. //  the origin by retaining this notice, and adhere to the conditions
  7. //  described in the file COPYING.LIB.
  8. //
  9. /////////////////////////////////////////////////////////////////////////////
  10.  
  11. #include "gemfs.h"
  12. #include <osbind.h>
  13. #include <aesbind.h>
  14. #include <string.h>
  15.  
  16.  
  17. static const PATHLEN=128;
  18. static const FILELEN=32;
  19.  
  20. GEMfileselector::GEMfileselector(int maxlen=PATHLEN) :
  21.     filename(new char[maxlen]),
  22.     filespec(new char[maxlen]),
  23.     file(new char[FILELEN]),
  24.     len(maxlen)
  25. {
  26.     CWD();
  27. }
  28.  
  29.  
  30. GEMfileselector::GEMfileselector(char* fname) :
  31.     filename(fname),
  32.     filespec(new char[PATHLEN]),
  33.     file(new char[FILELEN]),
  34.     len(0)
  35. {
  36.     if (fname[0]) Filename(fname);
  37.     else CWD();
  38. }
  39.  
  40.  
  41. GEMfileselector::~GEMfileselector()
  42. {
  43.     if (len) delete filename;
  44.     delete filespec;
  45.     delete file;
  46. }
  47.  
  48.  
  49. const char* GEMfileselector::CWD()
  50. {
  51.     int drive=Dgetdrv();
  52.     filespec[0]=drive+'A';
  53.     filespec[1]=':';
  54.     Dgetpath(&filespec[2],drive+1); // +1 since drv0 = current drive!
  55.     strcat(filespec,"\\*.*");
  56.     strcpy(file,"");
  57.     Merge();
  58.  
  59.     return filename;
  60. }
  61.  
  62.  
  63. // Returns result both ways.  NULL if cancelled.
  64. const char* GEMfileselector::Get(const char* prompt=0, char* into=0)
  65. {
  66.     int okay;
  67.  
  68.     if (prompt)
  69.         fsel_exinput(filespec,file,&okay,(char*)prompt);
  70.     else
  71.         fsel_input(filespec,file,&okay);
  72.  
  73.     Merge();
  74.  
  75.     if (okay) {
  76.         if (into) strcpy(into,filename);
  77.         return filename;
  78.     } else {
  79.         return 0;
  80.     }
  81. }
  82.  
  83. void GEMfileselector::Merge()
  84. {
  85.     strcpy(filename,filespec);
  86.     for (int i=0; filename[i]; i++);
  87.     while (i && filename[i]!='\\') i--;
  88.     if (filename[i]=='\\') strcpy(&filename[i+1],file);
  89.     else strcpy(filename,file);
  90. }
  91.  
  92. void GEMfileselector::Path(const char* path)
  93. {
  94.     // Find '\'
  95.     for (int i=0; path[i]; i++);
  96.     int to=i-1;
  97.     for (; i && path[i]!='\\'; i--);
  98.  
  99.     if (path[i]=='\\') {
  100.         while (to && path[to]=='\\') to--; // ignore trailing '\'
  101.  
  102.         for (int j=0; filespec[j]; j++);
  103.         while (j && filespec[j]!='\\') j--;
  104.         if (j) {
  105.             strcpy(&filespec[to+2],&filespec[j+1]);
  106.             strncpy(filespec,path,to+1);
  107.             filespec[to+1]='\\';
  108.         } else {
  109.             strncpy(filespec,path,to+1);
  110.             strcpy(filespec+to+1,"\\*.*");
  111.         }
  112.     } else {
  113.         strncpy(filespec,path,to+1);
  114.         strcpy(filespec+to+1,"\\*.*");
  115.     }
  116.  
  117.     Merge();
  118. }
  119.  
  120. const char* GEMfileselector::File(const char* f=0)
  121. // eg. File("foo.bar"); oldgot=File();
  122. {
  123.     if (f) {
  124.         strcpy(file,f);
  125.         Merge();
  126.     }
  127.     return file;
  128. }
  129.  
  130. const char* GEMfileselector::Filespec(const char* fspec=0)
  131. // eg. Filespec("*.bar"); Filespec("E:\foo\*.bar"); oldspec=Filespec();
  132. {
  133.     if (fspec) {
  134.         for (int i=0; fspec[i]; i++);
  135.         while (i && fspec[i]!='\\') i--;
  136.  
  137.         if (fspec[i]=='\\') {
  138.             strcpy(filespec,fspec);
  139.         } else {
  140.             for (int j=0; filespec[j]; j++);
  141.             while (j && filespec[j]!='\\') j--;
  142.             if (filespec[j]=='\\') {
  143.                 strcpy(&filespec[j+1],fspec);
  144.             } else {
  145.                 strcpy(filespec,fspec);
  146.             }
  147.         }
  148.     }
  149.     return filespec;
  150. }
  151.  
  152. const char* GEMfileselector::Filename(const char* fname=0)
  153. // eg. Filename("foo.bar"); Filename("E:\foo\foo.bar"); oldgot=Filename();
  154. {
  155.     for (int i=0; fname[i]; i++);
  156.     while (i && fname[i]!='\\') i--;
  157.  
  158.     if (fname[i]!='\\') {
  159.         File(fname);
  160.     } else {
  161.         File(&fname[i+1]);
  162.         strcpy(filename,fname);
  163.         filename[i]=0;
  164.         Path(filename);
  165.         Merge();
  166.     }
  167.  
  168.     return filename;
  169. }
  170.  
  171.  
  172.  
  173.