📄 stream.cpp
字号:
int CharSize = StringInfo->CharSize ;
size_t CharSizeBitMask = CharSize - 1 ;
for ( size_t nLength = StringInfo->Current - StringInfo->Start ; nLength & CharSizeBitMask ; nLength ++ ) StreamWrite ( 0, Stream ) ;
for ( int nCount = 0 ; nCount < CharSize ; nCount ++ ) StreamWrite ( 0, Stream ) ;
// 僄儔乕偑専弌偝傟偨傜
if ( StringInfo->Flags & STRING_ERROR ) {
StringInfo->Flags &= ~ STRING_ERROR ; // 堦帪揑偵僄儔乕傪夝彍
StringInfo->End = StringInfo->Start + ( ( StringInfo->End - StringInfo->Start ) & ~ CharSizeBitMask ) ;
StringInfo->Current = StringInfo->Start + ( ( StringInfo->Current - StringInfo->Start ) & ~ CharSizeBitMask ) ;
if ( StringInfo->End - StringInfo->Current >= CharSize ) {
for ( int nCount = 0 ; nCount < CharSize ; nCount ++ ) StreamWrite ( 0, Stream ) ;
}
else if ( StringInfo->End - StringInfo->Start >= CharSize ) {
StringInfo->Current = StringInfo->End - CharSize ;
for ( int nCount = 0 ; nCount < CharSize ; nCount ++ ) StreamWrite ( 0, Stream ) ;
}
StringInfo->Flags |= STRING_ERROR ; // 堦帪揑偵夝彍偟偨僄儔乕傪栠偡
}
}
if ( pSize ) *pSize = StringInfo->Current - StringInfo->Start ;
return ( StringInfo->Flags & STRING_ERROR ) ? EOF : 0 ;
}
////////////////////////////////////////////
// 弶婜壔乮僼傽僀儖乯 //
////////////////////////////////////////////
static STREAM *InitFileStream ( STREAM *Stream, FILE *File ) ;
static FILE *wfsopen_for_all_platform ( const wchar_t *filename, const wchar_t *mode, int shflag ) ;
static inline FILE *CheckValidDevice ( FILE *File ) ;
#define SHARE_MODE_FOR_READING _SH_DENYNO
#define SHARE_MODE_FOR_WRITING _SH_DENYWR
// 撉傒庢傝梡偵僼傽僀儖傪奐偔乮ANSI斉乯
// 僼傽僀儖柤偑 NULL 側傜僐儞僜乕儖傪奐偔
// 惉岟偟偨傜 STREAM 傊偺億僀儞僞傪曉偡
// 幐攕偟偨傜 NULL 傪曉偡
STREAM *OpenFileStreamForReadingA ( const char *FileName ) {
STREAM *Stream = (STREAM*) malloc ( sizeof(STREAM) ) ;
if ( Stream ) {
FILE *File ;
if ( FileName == NULL ) File = CheckValidDevice ( stdin ) ;
else if ( FileName == (char*) -1 ) File = NULL ;
else File = fsopen ( FileName, "r", SHARE_MODE_FOR_READING ) ;
if ( File ) return InitFileStream ( Stream, File ) ;
free ( Stream ) ;
}
return NULL ;
}
// 撉傒庢傝梡偵僼傽僀儖傪奐偔乮UNICODE斉乯
// 僼傽僀儖柤偑 NULL 側傜僐儞僜乕儖傪奐偔
// 惉岟偟偨傜 STREAM 傊偺億僀儞僞傪曉偡
// 幐攕偟偨傜 NULL 傪曉偡
STREAM *OpenFileStreamForReadingW ( const wchar_t *FileName ) {
STREAM *Stream = (STREAM*) malloc ( sizeof(STREAM) ) ;
if ( Stream ) {
FILE *File ;
if ( FileName == NULL ) File = CheckValidDevice ( stdin ) ;
else if ( FileName == (wchar_t*) -1 ) File = NULL ;
else File = wfsopen_for_all_platform ( FileName, L"r", SHARE_MODE_FOR_READING ) ;
if ( File ) return InitFileStream ( Stream, File ) ;
free ( Stream ) ;
}
return NULL ;
}
// 彂偒崬傒梡偵僼傽僀儖傪奐偔乮ANSI斉乯
// 僼傽僀儖柤偑 NULL 側傜僐儞僜乕儖傪奐偔
// 惉岟偟偨傜 STREAM 傊偺億僀儞僞傪曉偡
// 幐攕偟偨傜 NULL 傪曉偡
STREAM *OpenFileStreamForWritingA ( const char *FileName ) {
STREAM *Stream = (STREAM*) malloc ( sizeof(STREAM) ) ;
if ( Stream ) {
FILE *File ;
if ( FileName == NULL ) File = CheckValidDevice ( stdout ) ;
else if ( FileName == (char*) -1 ) File = CheckValidDevice ( stderr ) ;
else File = fsopen ( FileName, "w", SHARE_MODE_FOR_WRITING ) ;
if ( File ) return InitFileStream ( Stream, File ) ;
free ( Stream ) ;
}
return NULL ;
}
// 彂偒崬傒梡偵僼傽僀儖傪奐偔乮UNICODE斉乯
// 僼傽僀儖柤偑 NULL 側傜僐儞僜乕儖傪奐偔
// 惉岟偟偨傜 STREAM 傊偺億僀儞僞傪曉偡
// 幐攕偟偨傜 NULL 傪曉偡
STREAM *OpenFileStreamForWritingW ( const wchar_t *FileName ) {
STREAM *Stream = (STREAM*) malloc ( sizeof(STREAM) ) ;
if ( Stream ) {
FILE *File ;
if ( FileName == NULL ) File = CheckValidDevice ( stdout ) ;
else if ( FileName == (wchar_t*) -1 ) File = CheckValidDevice ( stderr ) ;
else File = wfsopen_for_all_platform ( FileName, L"w", SHARE_MODE_FOR_WRITING ) ;
if ( File ) return InitFileStream ( Stream, File ) ;
free ( Stream ) ;
}
return NULL ;
}
static STREAM *InitFileStream ( STREAM *Stream, FILE *File ) {
memzero ( Stream, sizeof(STREAM) ) ;
Stream->File = File ;
Stream->Read = fgetc ;
Stream->Write = fputc ;
Stream->WriteString = fputs ;
Stream->WriteFormat = vfprintf ;
Stream->Error = ferror ;
Stream->Close = CloseFileStream ;
int prevmode = fsetmode ( File, _O_BINARY ) ;
if ( prevmode == _O_TEXT ) Stream->Flag |= IS_PREVMODE_TEXT ;
return Stream ;
}
// 僼傽僀儖僴儞僪儖偑桳岠側傜 堷偒悢傪偦偺傑傑曉偡
// 僼傽僀儖僴儞僪儖偑柍岠側傜 NULL 傪曉偡
static inline FILE *CheckValidDevice ( FILE *File ) {
if ( _fileno ( File ) < 0 ) return NULL ;
if ( fget_osfhandle ( File ) == INVALID_HANDLE_VALUE ) return NULL ;
return File ;
}
////////////////////////////////////////////
// 弶婜壔乮暥帤楍乯 //
////////////////////////////////////////////
static STREAM *InitStringStream ( STREAM *Stream, STRINGINFO *StringInfo ) ;
static const void *SearchEnd ( const void *String, int nCharSize ) ;
static inline int IsValidCharSize ( int n ) ;
// 撉傒庢傝梡偵暥帤楍傪奐偔
// nLength 偵偼挿偝乮僶僀僩乯傪巜掕
// nLength 偑 (size_t) -1 側傜丄nCharSize 偱巜掕偟偨僒僀僘乮僶僀僩乯偺僰儖暥帤偐傜挿偝傪寁嶼
// 惉岟偟偨傜 STREAM 傊偺億僀儞僞傪曉偡
// 幐攕偟偨傜 NULL 傪曉偡
STREAM *OpenStringStreamForReading ( const void *String, size_t nLength, int nCharSize ) {
STREAM *Stream = (STREAM*) malloc ( sizeof(STREAM) ) ;
if ( Stream ) {
STRINGINFO *StringInfo = (STRINGINFO*) malloc ( sizeof(STRINGINFO) ) ;
if ( StringInfo ) {
memzero ( StringInfo, sizeof(STRINGINFO) ) ;
if ( IsValidCharSize ( nCharSize ) ) StringInfo->CharSize = nCharSize ;
else StringInfo->CharSize = 1 ;
StringInfo->Current = (char*) String ;
StringInfo->Start = (char*) String ;
StringInfo->Flags = STRING_READ ;
if ( nLength != (size_t) -1 ) StringInfo->End = (char*) String + nLength ;
else StringInfo->End = (char*) SearchEnd ( String, StringInfo->CharSize ) ;
return InitStringStream ( Stream, StringInfo ) ;
}
free ( Stream ) ;
}
return NULL ;
}
// 彂偒崬傒梡偵暥帤楍傪奐偔
// 暥帤楍偑 NULL 側傜幚嵺偵偼彂偒崬傑側偄
// nLength 偵偼嵟戝挿乮僶僀僩乯傪巜掕
// nCharSize 偵偼侾暥帤偺僒僀僘乮僶僀僩乯傪巜掕
// 惉岟偟偨傜 STREAM 傊偺億僀儞僞傪曉偡
// 幐攕偟偨傜 NULL 傪曉偡
STREAM *OpenStringStreamForWriting ( void *String, size_t nLength, int nCharSize ) {
STREAM *Stream = (STREAM*) malloc ( sizeof(STREAM) ) ;
if ( Stream ) {
STRINGINFO *StringInfo = (STRINGINFO*) malloc ( sizeof(STRINGINFO) ) ;
if ( StringInfo ) {
memzero ( StringInfo, sizeof(STRINGINFO) ) ;
if ( IsValidCharSize ( nCharSize ) ) StringInfo->CharSize = nCharSize ;
else StringInfo->CharSize = 1 ;
StringInfo->Current = (char*) String ;
StringInfo->Start = (char*) String ;
StringInfo->Flags = STRING_WRITE ;
StringInfo->End = (char*) String + nLength ;
return InitStringStream ( Stream, StringInfo ) ;
}
free ( Stream ) ;
}
return NULL ;
}
static STREAM *InitStringStream ( STREAM *Stream, STRINGINFO *StringInfo ) {
memzero ( Stream, sizeof(STREAM) ) ;
Stream->File = (FILE*) StringInfo ;
Stream->Read = (int (__cdecl *)(FILE*)) StringRead ;
Stream->Error = (int (__cdecl *)(FILE*)) StringError ;
Stream->Close = CloseStringStream ;
if ( StringInfo->Start ) {
Stream->Write = (int (__cdecl *)(int,FILE*)) StringWrite ;
Stream->WriteString = (int (__cdecl *)(const char*,FILE*)) StringWriteString ;
Stream->WriteFormat = (int (__cdecl *)(FILE*,const char *, va_list)) StringWriteFormat ;
}
else {
Stream->Write = (int (__cdecl *)(int,FILE*)) StringWriteOnlyCount ;
Stream->WriteString = (int (__cdecl *)(const char*,FILE*)) StringWriteStringOnlyCount ;
Stream->WriteFormat = (int (__cdecl *)(FILE*,const char *, va_list)) StringWriteFormatOnlyCount ;
}
return Stream ;
}
// 僰儖暥帤偺億僀儞僞傪曉偡
static const void *SearchEnd ( const void *String, int nCharSize ) {
const char *Current = (const char*) String ;
while ( 1 ) {
int C = 0 ;
for ( int nCount = 0 ; nCount < nCharSize ; nCount ++ ) ( *Current ++ ) ? C ++ : 0 ;
if ( ! C ) break ;
}
return (const void*) ( Current - nCharSize ) ;
}
// 0 偱側偔丄偐偮 2 偺檖悢側傜 0 埲奜傪曉偡
static inline int IsValidCharSize ( int n ) {
if ( n && ! ( n & ( n - 1 ) ) ) return 1 ;
return 0 ;
}
////////////////////////////////////////////
// wfsopen for Win95 //
////////////////////////////////////////////
// wfsopen() 偲摨偠乮Windows 95宯丒NT宯偺椉曽偱幚峴壜擻乯
static FILE *wfsopen_for_all_platform ( const wchar_t *filename, const wchar_t *mode, int shflag ) {
if ( IsNT () ) return wfsopen ( filename, mode, shflag ) ;
char *filename_ansi = w2adup ( filename ) ;
char *mode_ansi = w2adup ( mode ) ;
FILE *File = NULL ;
if ( filename_ansi && mode_ansi ) File = fsopen ( filename_ansi, mode_ansi, shflag ) ;
free ( filename_ansi ) ;
free ( mode_ansi ) ;
return File ;
}
////////////////////////////////////////////
// VSCPRINTF //
////////////////////////////////////////////
#if ! defined _MSC_VER || _MSC_VER < 1300
// Printf曄姺偺弌椡偵昁梫側僶僢僼傽偺挿偝傪庢摼偡傞
// 暥帤悢傪曉偡乮廔抂偺 NULL 傪娷傑側偄乯
static int __cdecl vscprintf ( const char *format, va_list args ) {
FILE *stdnul = fopen ( "NUL", "wb" ) ;
if ( ! stdnul ) return EOF ;
int retvalue = vfprintf ( stdnul, format, args ) ;
fclose ( stdnul ) ;
return retvalue ;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -