home *** CD-ROM | disk | FTP | other *** search
- /*
- GetFile. Public Domain by Magnus Holmgren in 1992.
-
- Compile with DICE:
- dcc -pr -2.0 -ms -proto -mRR -o GetFile GetFile.c
-
- Note that in order to compile with other compilers, changes are neccessary.
- */
-
- #include <stdlib.h>
- #include <string.h>
- #include <exec/types.h>
- #include <exec/execbase.h>
- #include <exec/memory.h>
- #include <dos/rdargs.h>
- #include <dos/dos.h>
- #include <libraries/asl.h>
- #include <clib/asl_protos.h>
- #include <clib/dos_protos.h>
- #include <clib/exec_protos.h>
-
- extern struct DOSBase *DOSBase;
- extern struct AslBase *AslBase;
- extern struct ExecBase *SysBase;
-
- #define DIR 0
- #define FILE 1
- #define TITLE 2
- #define VAR 3
- #define PATTERN 4
- #define GLOBAL 5
- #define NOFILES 6
- #define SAVE 7
- #define MULTI 8
-
- VOID quotename( STRPTR );
-
- /* Expands string 'name' to be quoted, if it contains spaces. */
-
- VOID quotename( STRPTR name )
- {
- if( strchr( name, ' ' ) )
- {
- LONG i;
-
- for( i = strlen( name ); i >= 0; i-- )
- *( name + i + 1 ) = *( name + i );
-
- *name = '\"';
- strcat( name, "\"" );
- }
- }
-
- _main()
- {
- struct RDArgs *rdargs;
- struct FileRequester *req = NULL;
- STRPTR title, var, pattern = "#?", buf = NULL;
- LONG extflags = 0, funcflags = 0;
-
- static const TEXT VersTag[] = "$VER: GetFile 1.2 (27.04.92)";
- static const TEXT OS2Required_Msg[] = "GetFile: You need OS 2.04+\n";
- static ULONG flags[ 9 ];
- static TEXT file[ 128 ], dir[ 256 ], fullpath[ 512 ];
-
- LONG ret = RETURN_FAIL;
-
- dir[ 0 ] = fullpath[ 0 ] = '\0';
-
- if( SysBase -> LibNode . lib_Version < 37 )
- {
- Write( ( BPTR ) Output(),
- OS2Required_Msg,
- sizeof( OS2Required_Msg ) - 1 );
- _exit( RETURN_FAIL );
- }
-
- flags[ TITLE ] = "Select a file";
- rdargs = ReadArgs( "DIR,FILE,TITLE,VAR,PATTERN,GLOBAL/S,NOFILES/S,"
- "SAVE/S,MULTISELECT/S", flags, NULL );
-
- if( rdargs == NULL )
- {
- PrintFault( IoErr(), "GetFile:\nError in argument line", );
- _exit( RETURN_ERROR );
- }
-
- if( !flags[ DIR ] )
- {
- if( !GetCurrentDirName( dir, 255 ) )
- {
- PutStr( "GetFile: " );
-
- if( IoErr() == ERROR_OBJECT_WRONG_TYPE )
- PutStr( "No CLI structure available\n" );
- else
- PutStr( "Error getting name of current directory\n" );
-
- goto fail;
- }
- }
- else
- strncpy( dir, ( STRPTR ) flags[ DIR ], 255 );
-
- if( flags[ FILE ] )
- strncpy( file, ( STRPTR ) flags[ FILE ], 127 );
-
- if( !flags[ VAR ] || *( ( STRPTR ) flags[ VAR ] ) == '\0' )
- flags[ VAR ] = "GetFileResult";
-
- var = ( STRPTR ) flags[ VAR ];
- title = ( STRPTR ) flags[ TITLE ];
-
- if( flags[ SAVE ] )
- funcflags = FILF_SAVE;
- else
- if( flags[ MULTI ] )
- funcflags = FILF_MULTISELECT;
-
- if( flags[ PATTERN ] )
- {
- pattern = ( STRPTR ) flags[ PATTERN ];
- funcflags |= FILF_PATGAD;
- }
-
- if( flags[ NOFILES ] )
- extflags = FIL1F_NOFILES;
-
- if( !( req = AllocAslRequestTags( ASL_FileRequest,
- ASL_Hail, title,
- ASL_File, file,
- ASL_Dir, dir,
- ASL_FuncFlags, funcflags,
- ASL_ExtFlags1, extflags,
- ASL_Pattern, pattern,
- TAG_END ) ) )
- {
- PutStr( "GetFile: Couldn't allocate FileRequester structure\n" );
- ret = RETURN_FAIL;
- goto fail1;
- }
-
- if( !RequestFile( req ) )
- {
- ret = RETURN_WARN;
- goto fail;
- }
-
- fullpath[ 0 ] = '\0';
-
- if( funcflags & FILF_MULTISELECT )
- {
- ULONG i, len = 1, dirlen = 0;
-
- dirlen = strlen( req -> rf_Dir );
-
- for( i = 0; i < req -> rf_NumArgs; i++ )
- len += strlen( req -> rf_ArgList[ i ] . wa_Name ) + 3 + dirlen;
-
- if( !( buf = AllocVec( len, MEMF_CLEAR | MEMF_PUBLIC ) ) )
- {
- PutStr( "GetFile: Ran out of memory\n" );
- ret = RETURN_FAIL;
- goto fail;
- }
-
- for( i = 0; i < req -> rf_NumArgs; i++ )
- {
- strncpy( fullpath, req -> rf_Dir, 511 );
-
- if( !AddPart( fullpath, req -> rf_ArgList[ i ] . wa_Name, 511 ) )
- goto builderror;
-
- quotename( fullpath );
- strcat( buf, fullpath );
- strcat( buf, " " );
- }
- }
- else
- {
- strncpy( fullpath, req -> rf_Dir, 511 );
-
- builderror:
- if( !AddPart( fullpath, req -> rf_File, 511 ) )
- {
- PutStr( "GetFile: Buffer overflow building filename\n" );
- ret = RETURN_FAIL;
- goto fail;
- }
-
- quotename( fullpath );
- }
-
- if( !SetVar( var,
- buf ? buf : fullpath, -1,
- flags[ GLOBAL ] ? GVF_GLOBAL_ONLY : GVF_LOCAL_ONLY ) )
- {
- PutStr( "GetFile: Error setting result variable\n" );
- ret = RETURN_FAIL;
- goto fail;
- }
-
- ret = RETURN_OK;
-
- fail:
-
- FreeAslRequest( req );
-
- fail1:
-
- FreeVec( buf );
- FreeArgs( rdargs );
- _exit( ret );
- }
-