home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / utility / misc / reqchang.run / ReqChange / Extra / GetFile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-01  |  4.7 KB  |  215 lines

  1. /*
  2. GetFile. Public Domain by Magnus Holmgren in 1992.
  3.  
  4. Compile with DICE:
  5. dcc -pr -2.0 -ms -proto -mRR -o GetFile GetFile.c
  6.  
  7. Note that in order to compile with other compilers, changes are neccessary.
  8. */
  9.  
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <exec/types.h>
  13. #include <exec/execbase.h>
  14. #include <exec/memory.h>
  15. #include <dos/rdargs.h>
  16. #include <dos/dos.h>
  17. #include <libraries/asl.h>
  18. #include <clib/asl_protos.h>
  19. #include <clib/dos_protos.h>
  20. #include <clib/exec_protos.h>
  21.  
  22. extern struct DOSBase *DOSBase;
  23. extern struct AslBase *AslBase;
  24. extern struct ExecBase *SysBase;
  25.  
  26. #define DIR     0
  27. #define FILE    1
  28. #define TITLE   2
  29. #define VAR     3
  30. #define PATTERN 4
  31. #define GLOBAL  5
  32. #define NOFILES 6
  33. #define SAVE    7
  34. #define MULTI   8
  35.  
  36. VOID quotename( STRPTR );
  37.  
  38. /* Expands string 'name' to be quoted, if it contains spaces. */
  39.  
  40. VOID quotename( STRPTR name )
  41. {
  42.   if( strchr( name, ' ' ) )
  43.   {
  44.     LONG i;
  45.  
  46.     for( i = strlen( name ); i >= 0; i-- )
  47.       *( name + i + 1 ) = *( name + i );
  48.  
  49.     *name = '\"';
  50.     strcat( name, "\"" );
  51.   }
  52. }
  53.  
  54. _main()
  55. {
  56.   struct RDArgs *rdargs;
  57.   struct FileRequester *req = NULL;
  58.   STRPTR title, var, pattern = "#?", buf = NULL;
  59.   LONG extflags = 0, funcflags = 0;
  60.  
  61.   static const TEXT VersTag[] = "$VER: GetFile 1.2 (27.04.92)";
  62.   static const TEXT OS2Required_Msg[] = "GetFile: You need OS 2.04+\n";
  63.   static ULONG flags[ 9 ];
  64.   static TEXT file[ 128 ], dir[ 256 ], fullpath[ 512 ];
  65.  
  66.   LONG ret = RETURN_FAIL;
  67.  
  68.   dir[ 0 ] = fullpath[ 0 ] = '\0';
  69.  
  70.   if( SysBase -> LibNode . lib_Version < 37 )
  71.   {
  72.       Write( ( BPTR ) Output(),
  73.              OS2Required_Msg,
  74.              sizeof( OS2Required_Msg ) - 1 );
  75.       _exit( RETURN_FAIL );
  76.   }
  77.  
  78.   flags[ TITLE ] = "Select a file";
  79.   rdargs = ReadArgs( "DIR,FILE,TITLE,VAR,PATTERN,GLOBAL/S,NOFILES/S,"
  80.                      "SAVE/S,MULTISELECT/S", flags, NULL );
  81.  
  82.   if( rdargs == NULL )
  83.   {
  84.     PrintFault( IoErr(), "GetFile:\nError in argument line", );
  85.     _exit( RETURN_ERROR );
  86.   }
  87.  
  88.   if( !flags[ DIR ] )
  89.   {
  90.     if( !GetCurrentDirName( dir, 255 ) )
  91.     {
  92.       PutStr( "GetFile: " );
  93.  
  94.       if( IoErr() == ERROR_OBJECT_WRONG_TYPE )
  95.         PutStr( "No CLI structure available\n" );
  96.       else
  97.         PutStr( "Error getting name of current directory\n" );
  98.  
  99.       goto fail;
  100.     }
  101.   }
  102.   else
  103.     strncpy( dir, ( STRPTR ) flags[ DIR ], 255 );
  104.  
  105.   if( flags[ FILE ] )
  106.     strncpy( file, ( STRPTR ) flags[ FILE ], 127 );
  107.  
  108.   if( !flags[ VAR ] || *( ( STRPTR ) flags[ VAR ] ) == '\0' )
  109.     flags[ VAR ] = "GetFileResult";
  110.  
  111.   var = ( STRPTR ) flags[ VAR ];
  112.   title = ( STRPTR ) flags[ TITLE ];
  113.  
  114.   if( flags[ SAVE ] )
  115.     funcflags = FILF_SAVE;
  116.   else
  117.     if( flags[ MULTI ] )
  118.       funcflags = FILF_MULTISELECT;
  119.  
  120.   if( flags[ PATTERN ] )
  121.   {
  122.     pattern = ( STRPTR ) flags[ PATTERN ];
  123.     funcflags |= FILF_PATGAD;
  124.   }
  125.  
  126.   if( flags[ NOFILES ] )
  127.     extflags = FIL1F_NOFILES;
  128.  
  129.   if( !( req = AllocAslRequestTags( ASL_FileRequest,
  130.                                     ASL_Hail, title,
  131.                                     ASL_File, file,
  132.                                     ASL_Dir, dir,
  133.                                     ASL_FuncFlags, funcflags,
  134.                                     ASL_ExtFlags1, extflags,
  135.                                     ASL_Pattern, pattern,
  136.                                     TAG_END ) ) )
  137.   {
  138.     PutStr( "GetFile: Couldn't allocate FileRequester structure\n" );
  139.     ret = RETURN_FAIL;
  140.     goto fail1;
  141.   }
  142.  
  143.   if( !RequestFile( req ) )
  144.   {
  145.     ret = RETURN_WARN;
  146.     goto fail;
  147.   }
  148.  
  149.   fullpath[ 0 ] = '\0';
  150.  
  151.   if( funcflags & FILF_MULTISELECT )
  152.   {
  153.     ULONG i, len = 1, dirlen = 0;
  154.  
  155.     dirlen = strlen( req -> rf_Dir );
  156.  
  157.     for( i = 0; i < req -> rf_NumArgs; i++ )
  158.       len += strlen( req -> rf_ArgList[ i ] . wa_Name ) + 3 + dirlen;
  159.  
  160.     if( !( buf = AllocVec( len, MEMF_CLEAR | MEMF_PUBLIC ) ) )
  161.     {
  162.       PutStr( "GetFile: Ran out of memory\n" );
  163.       ret = RETURN_FAIL;
  164.       goto fail;
  165.     }
  166.  
  167.     for( i = 0; i < req -> rf_NumArgs; i++ )
  168.     {
  169.       strncpy( fullpath, req -> rf_Dir, 511 );
  170.  
  171.       if( !AddPart( fullpath, req -> rf_ArgList[ i ] . wa_Name, 511 ) )
  172.         goto builderror;
  173.  
  174.       quotename( fullpath );
  175.       strcat( buf, fullpath );
  176.       strcat( buf, " " );
  177.     }
  178.   }
  179.   else
  180.   {
  181.     strncpy( fullpath, req -> rf_Dir, 511 );
  182.  
  183. builderror:
  184.     if( !AddPart( fullpath, req -> rf_File, 511 ) )
  185.     {
  186.       PutStr( "GetFile: Buffer overflow building filename\n" );
  187.       ret = RETURN_FAIL;
  188.       goto fail;
  189.     }
  190.  
  191.     quotename( fullpath );
  192.   }
  193.  
  194.   if( !SetVar( var,
  195.                buf ? buf : fullpath, -1,
  196.                flags[ GLOBAL ] ? GVF_GLOBAL_ONLY : GVF_LOCAL_ONLY ) )
  197.   {
  198.     PutStr( "GetFile: Error setting result variable\n" );
  199.     ret = RETURN_FAIL;
  200.     goto fail;
  201.   }
  202.  
  203.   ret = RETURN_OK;
  204.  
  205. fail:
  206.  
  207.   FreeAslRequest( req );
  208.  
  209. fail1:
  210.  
  211.   FreeVec( buf );
  212.   FreeArgs( rdargs );
  213.   _exit( ret );
  214. }
  215.