dbgfile.c
来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 685 行 · 第 1/2 页
C
685 行
/****************************************************************************
*
* Open Watcom Project
*
* Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
*
* ========================================================================
*
* This file contains Original Code and/or Modifications of Original
* Code as defined in and that are subject to the Sybase Open Watcom
* Public License version 1.0 (the 'License'). You may not use this file
* except in compliance with the License. BY USING THIS FILE YOU AGREE TO
* ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
* provided with the Original Code and Modifications, and is also
* available at www.sybase.com/developer/opensource.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
* ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
* NON-INFRINGEMENT. Please see the License for the specific language
* governing rights and limitations under the License.
*
* ========================================================================
*
* Description: Local and remote file access routines.
*
****************************************************************************/
#include "dbgdefn.h"
#include "dbglit.h"
#include "dbgmem.h"
#include "dbgio.h"
#include "dbgtoggl.h"
#include "trpfile.h"
#include "dui.h"
#include <string.h>
extern int DUIEnvLkup( char *, char *, int );
extern char *StrCopy( char const *, char * );
extern void FreeRing( char_ring * );
extern unsigned RemoteStringToFullName( bool, char *, char *, unsigned );
extern void StartupErr( char * );
extern bool HaveRemoteFiles( void );
extern unsigned RemoteErase( char const * );
extern void RemoteErrMsg( sys_error, char * );
extern unsigned RemoteRead( sys_handle, void *, unsigned );
extern unsigned RemoteWrite( sys_handle, const void *, unsigned );
extern unsigned long RemoteSeek( sys_handle, unsigned long, seek_method );
extern sys_handle RemoteOpen( char const *, open_access );
extern unsigned RemoteClose( sys_handle );
extern unsigned RemoteWriteConsole( void *, unsigned );
extern unsigned LocalErase( char const * );
extern void LocalErrMsg( sys_error, char * );
extern unsigned LocalRead( sys_handle, void *, unsigned );
extern unsigned LocalWrite( sys_handle, const void *, unsigned );
extern unsigned long LocalSeek( sys_handle, unsigned long, seek_method );
extern sys_handle LocalOpen( char const *, open_access );
extern unsigned LocalClose( sys_handle );
extern sys_handle LocalHandle( handle );
extern void DUIWndUser( void );
extern file_components RemFile;
extern file_components LclFile;
extern char LclPathSep;
static char_ring *LclPath;
#define LOC_ESCAPE '@'
#define REMOTE_LOC 'r'
#define LOCAL_LOC 'l'
#define REMOTE_IND 0x8000
#define MAX_OPENS 100
#define MAX_ERRORS 10
static sys_handle SysHandles[MAX_OPENS];
static sys_error SysErrors[MAX_ERRORS];
static unsigned ErrRover;
static unsigned LastErr;
handle PathOpen( char const *name, unsigned len, char *ext );
char *RealFName( char const *name, open_access *loc )
{
*loc &= ~(OP_REMOTE|OP_LOCAL);
if( name[0] == LOC_ESCAPE ) {
if( (name[1] | 0x20) == REMOTE_LOC ) {
*loc |= OP_REMOTE;
name += 2;
} else if( (name[1] | 0x20) == LOCAL_LOC ) {
*loc |= OP_LOCAL;
name += 2;
} else if( name[1] == LOC_ESCAPE ) {
name += 1;
}
}
return( (char*)name );
}
static open_access DefaultLoc( open_access loc )
{
if( (loc & (OP_REMOTE|OP_LOCAL) ) == 0 ) {
if( _IsOn( SW_REMOTE_FILES ) ) {
loc |= OP_REMOTE;
} else {
loc |= OP_LOCAL;
}
}
if( (loc & OP_REMOTE) && !HaveRemoteFiles() ) {
loc &= ~OP_REMOTE;
loc |= OP_LOCAL;
}
return( loc );
}
char *FileLoc( char const *name, open_access *loc )
{
open_access ind;
ind = 0;
name = RealFName( name, &ind );
if( ind != 0 ) {
*loc &= ~(OP_LOCAL|OP_REMOTE);
*loc |= ind;
}
*loc = DefaultLoc( *loc );
return( (char*)name );
}
static file_components *PathInfo( char const *path, open_access loc )
{
file_components *info;
FileLoc( path, &loc );
if( loc & OP_LOCAL ) {
info = &LclFile;
} else {
info = &RemFile;
}
return( info );
}
static handle FindFreeHandle( void )
{
handle i;
for( i = 0; i < MAX_OPENS; ++i ) {
if( SysHandles[i] == NIL_SYS_HANDLE ) return( i );
}
return( NIL_HANDLE );
}
unsigned ReadStream( handle h, void *b, unsigned l )
{
sys_handle sys = SysHandles[ h & ~REMOTE_IND ];
if( h & REMOTE_IND ) {
return( RemoteRead( sys, b, l ) );
} else {
return( LocalRead( sys, b, l ) );
}
}
unsigned ReadText( handle h, void *b, unsigned l )
{
return( ReadStream( h, b, l ) );
}
unsigned WriteStream( handle h, const void *b, unsigned l)
{
sys_handle sys = SysHandles[ h & ~REMOTE_IND ];
if( h & REMOTE_IND ) {
return( RemoteWrite( sys, b, l ) );
} else {
return( LocalWrite( sys, b, l ) );
}
}
unsigned WriteText( handle h, const void *b, unsigned len )
{
char *nl;
len = WriteStream( h, b, len );
if( h & REMOTE_IND ) {
nl = RemFile.newline;
} else {
nl = LclFile.newline;
}
WriteStream( h, nl, (nl[1] != NULLCHAR) ? 2 : 1 );
return( len ); /* not including the newline sequence */
}
unsigned long SeekStream( handle h, long p, seek_method m )
{
sys_handle sys = SysHandles[ h & ~REMOTE_IND ];
if( h & REMOTE_IND ) {
return( RemoteSeek( sys, p, m ) );
} else {
return( LocalSeek( sys, p, m ) );
}
}
handle FileOpen( char const *name, open_access o )
{
sys_handle sys;
handle h;
if( o & OP_SEARCH ) {
return( PathOpen( name, strlen( name ), "" ) );
}
name = FileLoc( name, &o );
h = FindFreeHandle();
if( h == NIL_HANDLE ) return( NIL_HANDLE );
if( o & OP_REMOTE ) {
h |= REMOTE_IND;
sys = RemoteOpen( name, o );
} else {
sys = LocalOpen( name, o );
}
if( sys == NIL_SYS_HANDLE ) return( NIL_HANDLE );
SysHandles[ h & ~REMOTE_IND ] = sys;
if( o & OP_APPEND ) SeekStream( h, 0, DIO_SEEK_END );
return( h );
}
unsigned FileClose( handle h )
{
sys_handle sys = SysHandles[ h & ~REMOTE_IND ];
SysHandles[ h & ~REMOTE_IND ] = NIL_SYS_HANDLE;
if( h & REMOTE_IND ) {
return( RemoteClose( sys ) );
} else {
return( LocalClose( sys ) );
}
}
unsigned FileRemove( char const *name, open_access loc )
{
name = FileLoc( name, &loc );
if( loc & OP_REMOTE ) {
return( RemoteErase( name ) );
} else {
return( LocalErase( name ) );
}
}
void WriteToPgmScreen( void *buff, unsigned len )
{
#if !defined( BUILD_RFX )
DUIWndUser();
#endif
RemoteWriteConsole( buff, len );
}
open_access FileHandleInfo( handle h )
{
if( h & REMOTE_IND ) return( OP_REMOTE );
return( OP_LOCAL );
}
char *SysErrMsg( unsigned code, char *buff )
{
sys_error sys = SysErrors[ (code & ~REMOTE_IND) - 1];
if( code & REMOTE_IND ) {
RemoteErrMsg( sys, buff );
} else {
LocalErrMsg( sys, buff );
}
return( &buff[ strlen( buff ) ] );
}
unsigned StashErrCode( sys_error sys, open_access loc )
{
unsigned code;
if( sys == 0 ) return( 0 );
if( ++ErrRover >= MAX_ERRORS ) ErrRover = 0;
code = ErrRover;
SysErrors[code] = sys;
++code;
if( loc & OP_REMOTE ) code |= REMOTE_IND;
LastErr = code;
return( code );
}
/* for RFX */
unsigned GetLastErr( void )
{
return( LastErr );
}
/* for RFX */
sys_error GetSystemErrCode( unsigned code )
{
if( code == 0 ) return( 0 );
return( SysErrors[ (code & ~REMOTE_IND) - 1] );
}
/* for RFX */
sys_handle GetSystemHandle( unsigned h )
{
return( SysHandles[ h & ~REMOTE_IND ] );
}
bool IsAbsolutePath( char *path )
{
file_components *info;
char *p;
open_access loc;
p = RealFName( path, &loc );
info = PathInfo( p, loc );
if( strlen( p ) == 0 ) return( FALSE );
return( p[0] == info->path_separator[0]
|| p[0] == info->path_separator[1]
|| p[0] == info->path_separator[2] );
}
char *AppendPathDelim( char *path, open_access loc )
{
file_components *info;
unsigned len;
char *end;
info = PathInfo( path, loc );
len = strlen( path );
end = &path[len];
if( len == 0 ||
( end[-1] != info->path_separator[0]
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?