📄 test.c
字号:
/* -------------------------------------------------------------------------
Test.cpp
Console i/o interface to test resource manager
Written by Kevin Ray (c) 1996 Spectrum Holobyte
------------------------------------------------------------------------ */
#include "resmgr.h" /* exported prototypes & type definitions */
#include "memmgr.h"
#include <stdio.h> /* low-level file i/o (+io.h) */
#include <string.h>
#include <memory.h>
#include <sys/stat.h> /* _S_IWRITE */
#if USE_WINDOWS
#include <io.h>
#include <direct.h>
#include <process.h> /* _beginthread() MUST SET C++ OPTIONS UNDER
MSVC SETTINGS */
#include <windows.h> /* all this for MessageBox (may move to debug.cpp)*/
#endif
#include "unzip.h"
#define MAX_ARGS 25
#define MAX_COMMAND_LINE 255
#if( !RES_REPLACE_FTELL )
# define FTELL(a) ResFTell(a)
#else
# define FTELL(a) ftell(a)
#endif
#define HI_WORD(a) ((a)>>16)
#define LO_WORD(a) ((a)&0x0ffff)
#if( RES_DEBUG_VERSION )
# define IF_DEBUG(a) a
#else
# define IF_DEBUG(a)
#endif
#if( RES_STANDALONE )
# include <conio.h> /* cgets */
# define GETS _cgets
#else
# define GETS gets
#endif /*RES_STANDALONE */
#if( RES_STANDALONE )
char * command[] =
{
"dir",
"exit",
"attach",
"detach",
"analyze",
"dump",
"find",
"cd",
"add",
"read",
"path",
"map",
"stream",
"extract",
"run",
"help"
};
char * help[] =
{
"Prints the contents of a directory that is in the search path.",
"Exit the program.",
"Attach an archive file to the search path.",
"Detach an archive file that has already been added to the\n\t\tsearch path",
"Display an analysis of the hash table for the specified\n\t\tdirectory [blank pathname = GLOBAL_HASH_TABLE].",
"Display a complete analysis of all hash tables and hash\n\t\tentries.",
"Find an entry within the Resource Manager and give\n\t\tspecifics.",
"Change the current directory path to a path already defined\n\t\twithin the search path (already added).",
"Add a directory to the search path.",
"Spawns an asynchronous read of specified file. When finished\n\t\tnotification will appear.",
"Display all of the directories in the search path.",
"Display all of the devices on host computer.",
"Read from a file using stdio streaming functions.",
"Extract a file from an archive to c:\\.",
"Execute a custom function.",
"Any command followed by '?' will display an explanation of\n\t\tthat command."
};
char * syntax[] =
{
"dir <directory name>",
"exit",
"attach [<attach point>] <zip filename> [<true | false>]",
"detach",
"analyze <pathname>",
"dump",
"find <filename>",
"cd <pathname>",
"add <pathname> [<true | false>]",
"read <filename>",
"path",
"map <volume id ('A', 'B', etc)>",
"stream <filename>",
"extract <dst filename> <src filename>",
"run <any number of parameters>",
"help"
};
enum COMMAND_CODES
{
COMMAND_ERROR = -1,
COMMAND_DIR,
COMMAND_EXIT,
COMMAND_ATTACH,
COMMAND_DETACH,
COMMAND_ANALYZE,
COMMAND_DUMP,
COMMAND_FIND,
COMMAND_CD,
COMMAND_ADD,
COMMAND_READ,
COMMAND_PATH,
COMMAND_MAP,
COMMAND_STREAM,
COMMAND_EXTRACT,
COMMAND_RUN,
COMMAND_HELP
};
#define COMMAND_COUNT (sizeof(command)/sizeof(command[0]))
extern HASH_TABLE * GLOBAL_HASH_TABLE; /* root hash table */
extern LIST * GLOBAL_PATH_LIST; /* search path list */
extern char GLOBAL_CURRENT_PATH[]; /* current working directory */
extern char * GLOBAL_SEARCH_PATH[ MAX_DIRECTORIES ]; /* directories in fixed order */
extern int GLOBAL_SEARCH_INDEX; /* number of entries in search path */
extern DEVICE_ENTRY * RES_DEVICES; /* array of device_entry structs */
extern HASH_ENTRY * hash_find( const char *, HASH_TABLE * );
extern char * res_fullpath( char * abs_buffer, const char * rel_buffer, int maxlen );
extern void dbg_analyze_hash( HASH_TABLE * );
extern void dbg_device( DEVICE_ENTRY * );
/* =======================================================
FUNCTION: parse_args
PURPOSE: Parse a command line entry into its
argc and argv components.
PARAMETERS: Command line, char * array for argv[].
RETURNS: Count of arguments (argc).
======================================================= */
int parse_args( char * cmd, char ** argv )
{
int quote_flag,
writing_flag,
argc;
quote_flag = FALSE;
writing_flag = FALSE;
argc = 0;
while( *cmd ) {
if( quote_flag ) {
if( *cmd == ASCII_QUOTE ) {
quote_flag = 0;
*cmd = 0x00;
}
} else {
if( *cmd == ASCII_SPACE ) {
*cmd = 0x00;
writing_flag = 0;
} else {
if( !writing_flag ) {
if( *cmd == ASCII_QUOTE ) {
argv[ argc++ ] = ++cmd;
quote_flag = 1;
}
else
argv[ argc++ ] = cmd;
writing_flag = 1;
}
}
}
cmd++;
}
return( argc );
}
void print_bytes( char * buffer )
{
int count;
for( count = 0; count < 16; count++ )
printf( "%02X ", (unsigned char)(buffer[ count ]));
printf( " " );
for( count = 0; count < 16; count++ )
if( (buffer[ count ] > 0x1f) && (buffer[ count ] < 0x7f))
printf( "%c", (unsigned char)(buffer[ count ]));
else
printf( "." );
}
void print_heading( void )
{
printf( "\n\n" );
printf( "+-------------------------------------------------------------------------+\n" );
printf( "| COMMANDS ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: |\n" );
printf( "| |\n" );
printf( "| dir exit attach detach analyze dump find cd add read path |\n" );
printf( "| map stream extract run help |\n" );
printf( "+-------------------------------------------------------------------------+\n" );
}
void show_help( int cmd )
{
printf( " syntax : %s\n", syntax[ cmd ] );
printf( " description : %s\n", help[ cmd ] );
}
int is_bool( char * string )
{
if( !stricmp( string, "false" )) return( 0 );
if( !stricmp( string, "true" )) return( 1 );
if( strlen( string ) == 1 ) {
if( strchr( "nNfF0", *string )) return(0);
if( strchr( "yYTt1", *string )) return(1);
}
return( -1 ); /* can't determine */
}
/* =======================================================
FUNCTION: cmd_read "read"
PURPOSE: Reads a file and displays it to stdout.
PARAMETERS: Source file name.
RETURNS: None.
======================================================= */
void cmd_read( char * argv )
{
unsigned int size;
char * inbuf,
* ptr;
inbuf = ptr = ResLoadFile( argv, NULL, &size );
if( !inbuf ) {
printf( "File not found (%s).\n", argv );
return;
}
fwrite( inbuf, 1, size, stdout );
ResUnloadFile( inbuf );
}
/* =======================================================
FUNCTION: cmd_map "map"
PURPOSE: Checks a device to see if the media has
changed.
PARAMETERS: Logical drive id ('A', 'B', etc ).
RETURNS: None.
======================================================= */
void cmd_map( char * argv )
{
int id,
check;
if( !argv ) {
printf( "No device id specified.\n" );
return;
}
id = toupper(toupper(argv[0])) - 'A';
if( id < 0 || id > 25 ) {
printf( "Can't decipher drive. Use A-Z\n" );
return;
}
else {
check = ResCheckMedia( id );
switch( check ) {
case 0:
#if( RES_DEBUG_VERSION )
dbg_device( &RES_DEVICES[ id ]);
#endif
break;
case 1: printf( "Media has not changed.\n" ); break;
case -1: printf( "Media not available.\n" ); break;
}
}
}
/* =======================================================
FUNCTION: cmd_dir "dir"
PURPOSE: Display a familiar looking directory
listing.
PARAMETERS: Path to list (optional)
RETURNS: None.
======================================================= */
void cmd_dir( char * argv )
{
RES_DIR * dir;
char * file;
char fullpath[_MAX_PATH];
int ct = 0;
if( !GLOBAL_SEARCH_INDEX ) {
printf( "No path.\n" );
return;
}
if( !argv )
ResGetDirectory( fullpath );
else
res_fullpath( fullpath, argv, _MAX_PATH );
dir = ResOpenDirectory( fullpath );
if( !dir )
printf( "Directory not found.\n" );
else {
printf( "Directory of %s\n", fullpath );
file = ResReadDirectory( dir );
for( ct = 0; ct < dir -> num_entries; ct++ ) {
printf( "%-17s", file );
if( !((ct+1) % 4 ))
printf("\n");
file = ResReadDirectory( dir );
}
}
ResCloseDirectory( dir );
}
/* =======================================================
FUNCTION: cmd_find "find"
PURPOSE: Find a file and display verbose statistics
gathered about that file.
PARAMETERS: Filename.
RETURNS: None.
======================================================= */
void cmd_find( char * fullpath )
{
RES_STAT stat;
DEVICE_ENTRY dev;
int flag, count;
char path[_MAX_PATH],
arcname[_MAX_PATH];
if( ResStatusFile( fullpath, &stat )) {
ResGetPath( stat.directory, path );
ResGetArchive( stat.archive, arcname );
printf( "\n----------------\n" );
printf( "File Information\n" );
printf( "----------------\n" );
printf( "name : %s\n", fullpath );
printf( "size : %d\n", stat.size );
printf( "csize : %d\n", stat.csize );
printf( "directory : %s\n", path, stat.directory );
printf( "volume : %c\n", stat.volume + 'A' );
if( *arcname )
printf( "archive : %s\n", arcname );
else
printf( "archive : NOT IN AN ARCHIVE\n" );
printf( "attributes: %0x ", stat.attributes );
if( stat.attributes & _A_NORMAL ) printf( " NORMAL BIT," );
if( stat.attributes & _A_RDONLY ) printf( " READ ONLY BIT," );
if( stat.attributes & _A_HIDDEN ) printf( " HIDDEN BIT," );
if( stat.attributes & _A_SYSTEM ) printf( " SYSTEM BIT," );
if( stat.attributes & _A_SUBDIR ) printf( " DIRECTORY BIT," );
if( stat.attributes & _A_ARCH ) printf( " ARCHIVE BIT " );
printf( "\n" );
flag = ResWhereIs( fullpath, NULL );
printf( "\n------------------\n" );
printf( "Volume information\n" );
printf( "------------------\n" );
if( flag != -1 ) {
printf( "media : " );
if( flag & RES_HD ) printf( "HARD DRIVE, " );
if( flag & RES_CD ) printf( "CD-ROM, " );
if( flag & RES_NET ) printf( "NETWORK, " );
if( flag & RES_ARCHIVE ) printf( "ARCHIVE FILE, " );
if( flag & RES_FLOPPY ) printf( "REMOVEABLE MEDIA " );
printf( "\n" );
}
flag = ResDevice( stat.volume, &dev );
if( flag ) {
printf( "name : %s\n", dev.name );
printf( "serial : %x-%x\n", HI_WORD(dev.serial), LO_WORD(dev.serial));
}
printf( "\n------------\n" );
printf( "Header bytes\n" );
printf( "------------\n" );
flag = ResOpenFile( fullpath, _O_RDONLY | _O_BINARY );
if( flag != -1 ) {
ResReadFile( flag, path, _MAX_PATH );
for( count = 0; count < 5; count++ ) {
printf( " : " );
print_bytes( &path[16*count] );
printf( "\n" );
}
ResCloseFile( flag );
};
}
else
printf( "File not found.\n" );
}
/* ================================================
FUNCTION: cmd_run "run"
PURPOSE: Use to test code.
PARAMETERS: argc, argv[]
RETURNS: None.
NOTE: If you want to test your own throw-away,
do so here.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -