home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 26 / AACD 26.iso / AACD / Programming / ace_gpl_release / src / lib / c / filereq.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-04  |  5.2 KB  |  214 lines

  1. /* 
  2. ** Get the full path of a file
  3. ** via a file requester.
  4. ** Copyright (C) 1998 David Benn
  5. ** 
  6. ** This program is free software; you can redistribute it and/or
  7. ** modify it under the terms of the GNU General Public License
  8. ** as published by the Free Software Foundation; either version 2
  9. ** of the License, or (at your option) any later version.
  10. **
  11. ** This program is distributed in the hope that it will be useful,
  12. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. ** GNU General Public License for more details.
  15. **
  16. ** You should have received a copy of the GNU General Public License
  17. ** along with this program; if not, write to the Free Software
  18. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19. **
  20. ** Author: David J Benn
  21. **   Date: 24th-28th December 1993,
  22. **       26th February 1994,
  23. **       10th September 1994
  24. */
  25.  
  26. #include <exec/types.h>
  27. #include <exec/execbase.h>
  28. #include <exec/libraries.h>
  29. #include <exec/memory.h>
  30. #include <intuition/intuition.h>
  31. #include <libraries/asl.h>
  32.  
  33. #define MAXSTRINGLEN 1024
  34.  
  35. /* Arp file requester structure declaration */
  36. typedef struct ArpFileRequester {
  37.     BYTE    *title;
  38.     BYTE    *file;
  39.     BYTE    *dir;    
  40.     struct    Window    *window;
  41.     UBYTE    funcflags;
  42.     UBYTE    flags2;    
  43.     void    (*userfunc)();
  44.     WORD    leftedge;    
  45.     WORD    topedge;                        
  46. } ARPREQ;
  47.  
  48. /* externals */
  49. extern    struct    ExecBase *AbsExecBase;
  50. extern    struct    Window *Wdw,*WBWdw;
  51. extern    BYTE    IntuiMode;
  52.     
  53. /* globals */
  54. struct    Library *AslBase=NULL;
  55. struct     Library *ArpBase=NULL;
  56. static     char    path[MAXSTRINGLEN];
  57.                                        
  58. /* function protos */
  59. ULONG    FileRequest();
  60.  
  61. /* external functions */
  62. extern    void    stringcopy();    
  63.  
  64. /* functions */
  65. char *asl_filerequest(defdir,title)
  66. char *defdir,*title;
  67. {
  68. /* 
  69. ** File requester for machines
  70. ** running version 2.04 or higher
  71. ** of the operating system.
  72. */
  73. struct    TagItem    frtags[4];
  74. struct     FileRequester *FileReq;
  75. char     *file,*dir,*tmp;
  76. int    tagnum=0;
  77.  
  78.  /* requester title */
  79.  frtags[tagnum].ti_Tag  = ASL_Hail; 
  80.  frtags[tagnum++].ti_Data = (ULONG)title;
  81.  
  82.  /* default directory? */
  83.  if (defdir != NULL)
  84.  {
  85.      frtags[tagnum].ti_Tag  = ASL_Dir;
  86.     frtags[tagnum++].ti_Data = (ULONG)defdir;
  87.  }
  88.  
  89.  /* specify window? */ 
  90.  if (IntuiMode == 1)
  91.  {
  92.    frtags[tagnum].ti_Tag = ASL_Window; 
  93.    frtags[tagnum++].ti_Data = (ULONG)Wdw;
  94.  }
  95.  
  96.  frtags[tagnum].ti_Tag = TAG_DONE;
  97.  
  98.  path[0] = '\0';
  99.  
  100.  AslBase = (struct Library *)OpenLibrary(AslName,0L);
  101.  if (AslBase == NULL) return(path);
  102.  
  103.  FileReq = (struct FileRequester *)AllocAslRequest(ASL_FileRequest,frtags);
  104.  
  105.  if (AslRequest(FileReq,NULL))  
  106.  {  
  107.    dir  = FileReq->rf_Dir;
  108.    file = FileReq->rf_File;
  109.    if (*dir != '\0') stringcopy(path,dir);
  110.    tmp = path;
  111.    while (*tmp) tmp++;
  112.    if (*(tmp-1) != ':' && *dir != '\0') *tmp++ = '/';
  113.    stringcopy(tmp,file); 
  114.  }
  115.  
  116.  FreeAslRequest(FileReq);
  117.  
  118.  if (AslBase) CloseLibrary(AslBase);
  119.  
  120.  return(path);
  121. }
  122.  
  123. char *custom_filerequest(defdir,title)
  124. char *defdir,*title;
  125. {
  126. /* 
  127. ** File requester for machines
  128. ** running version 1.3 or less
  129. ** of the operating system.
  130. **
  131. ** Arp.library must be installed
  132. ** in the LIBS: directory for this
  133. ** function to work.
  134. */
  135. ARPREQ    *arpreq;
  136. char    file[MAXSTRINGLEN],dir[MAXSTRINGLEN];
  137. char    *tmp;
  138. int    cc;
  139.  
  140.     dir[0] = '\0';
  141.     file[0] = '\0';
  142.     path[0] = '\0';                     
  143.  
  144.     /* default directory? */
  145.     if (defdir != NULL) stringcopy(dir,defdir);
  146.  
  147.     /* open arp library */
  148.     ArpBase = (struct Library *)OpenLibrary("arp.library",0L);
  149.     if (ArpBase == NULL) return(path);
  150.  
  151.     /* allocate memory for requester structure */
  152.     arpreq = (ARPREQ *)AllocMem(sizeof(ARPREQ),MEMF_ANY | MEMF_CLEAR);
  153.     if (arpreq == NULL)       
  154.     {
  155.       if (ArpBase) CloseLibrary(ArpBase);
  156.       return(path); 
  157.     }      
  158.             
  159.     /* fill ARPREQ structure */                             
  160.     arpreq->title    = (BYTE *)title;
  161.     arpreq->file      = (BYTE *)file;   
  162.     arpreq->dir       = (BYTE *)dir;
  163.     if (IntuiMode == 1)
  164.     arpreq->window    = Wdw;
  165.     else
  166.     arpreq->window    = NULL;
  167.     arpreq->funcflags    = 0;
  168.     arpreq->flags2    = 0;
  169.     arpreq->userfunc    = NULL;
  170.     arpreq->leftedge    = 20;
  171.     arpreq->topedge    = 20;
  172.                                            
  173.     /* invoke arp file requester */                                
  174.     if (FileRequest(arpreq) == 0L)           
  175.     {
  176.       if (arpreq) FreeMem(arpreq,sizeof(ARPREQ));
  177.       if (ArpBase) CloseLibrary(ArpBase);
  178.       return(path); 
  179.     }                  
  180.                   
  181.    /* extract full path from ARPREQ structure */ 
  182.    cc=0;   
  183.    tmp = arpreq->dir;
  184.    while (*tmp) { path[cc++] = *tmp++; }
  185.    path[cc] = '\0';
  186.  
  187.    if (path[0] != '\0')
  188.       if (path[cc-1] != ':') { path[cc++] = '/'; path[cc] = '\0'; }
  189.  
  190.    tmp = arpreq->file;                    
  191.    while (*tmp) { path[cc++] = *tmp++; }
  192.    path[cc] = '\0';
  193.  
  194.    if (arpreq) FreeMem(arpreq,sizeof(ARPREQ));
  195.    if (ArpBase) CloseLibrary(ArpBase);
  196.  
  197.    return(path);         
  198. }                           
  199.  
  200. char *filerequest(defdir,title)
  201. char *defdir,*title;
  202. {
  203. /* 
  204. ** Invoke a file requester
  205. ** and return the full path
  206. ** of the selected file.
  207. */
  208.  
  209.   if (AbsExecBase->LibNode.lib_Version <= 34)    /* 1.3 or less? */
  210.       return(custom_filerequest(defdir,title));
  211.   else
  212.       return(asl_filerequest(defdir,title));
  213. }
  214.