📄 stdlibx.cpp
字号:
//
// (c) Yuri Kiryanov, openh323@kiryanov.com and
// Yuriy Gorvitovskiy
//
// for Openh323, www.Openh323.org
//
// Windows CE Port
//
// stdlib routines implemented through Windows CE API
//
#include <ptlib.h>
#include <Atlconv.h>
#include <winbase.h>
#include <winnt.h>
#include <snmp.h>
#include <ptlib/sockets.h>
#define DELETE (0x00010000L) // defined in <winnt.h> and undef "msos/ptlib/contain.h"
#ifndef __cplusplus
#include <tchar.h>
int isprint(int c) { return _istprint(c);}
int isxdigit(int c) { return _istxdigit(c); }
int isspace( int c ) { return _istspace(c); }
int isupper( int c ) { return _istupper(c); }
int islower( int c ) { return _istlower(c); }
int isalnum( int c ) { return _istalnum(c); }
int isalpha( int c ) { return _istalpha(c); }
int iscntrl( int c ) { return _istcntrl(c); }
int isdigit( int c ) { return _istdigit(c); }
int ispunct( int c ) { return _istpunct(c); }
#endif
void __cdecl abort(void) {
if(IDYES == MessageBox(NULL, _T("Abort?"), _T("stdlibx"), MB_YESNO))
exit(3);
}
void __cdecl perror(const char * s) {
USES_CONVERSION;
MessageBox(NULL, s && *s ? A2T(s) : _T("null"), _T("stdlibx"), MB_OK);
}
long _lseek(int nHandle, long off, int orig)
{
DWORD dwMoveMethod=FILE_BEGIN;
switch(orig)
{
case SEEK_SET: dwMoveMethod=FILE_BEGIN; break;
case SEEK_CUR: dwMoveMethod=FILE_CURRENT; break;
case SEEK_END: dwMoveMethod=FILE_END; break;
}
return SetFilePointer((HANDLE)nHandle,off,NULL,dwMoveMethod);
}
int _close(int nHandle)
{
FlushFileBuffers((HANDLE)nHandle);
return (CloseHandle((HANDLE)nHandle)) ? 0 : -1;
}
int _read(int nHandle, void *p, unsigned int s)
{
DWORD size=0;
ReadFile((HANDLE)nHandle,p,s,&size,NULL);
return size;
}
int _write(int nHandle, const void *p, unsigned int s)
{
DWORD size=0;
WriteFile((HANDLE)nHandle,p,s,&size,NULL);
//[YG]???? FlushFileBuffers((HANDLE)nHandle);
return size;
}
int _open( const char *filename, int oflag , int pmode)
{
USES_CONVERSION;
HANDLE osfh; /* OS handle of opened file */
DWORD fileaccess; /* OS file access (requested) */
DWORD fileshare; /* OS file sharing mode */
DWORD filecreate; /* OS method of opening/creating */
DWORD fileattrib; /* OS file attribute flags */
/*
* decode the access flags
*/
switch( oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR) )
{
case _O_RDONLY: /* read access */
fileaccess = GENERIC_READ;
break;
case _O_WRONLY: /* write access */
fileaccess = GENERIC_WRITE;
break;
case _O_RDWR: /* read and write access */
fileaccess = GENERIC_READ | GENERIC_WRITE;
break;
default: /* error, bad oflag */
set_errno(EINVAL);
return -1;
}
/*
* decode sharing flags
*/
fileshare = FILE_SHARE_READ | FILE_SHARE_WRITE;
/*
* decode open/create method flags
*/
switch ( oflag & (_O_CREAT | _O_EXCL | _O_TRUNC) )
{
case 0:
case _O_EXCL: // ignore EXCL w/o CREAT
filecreate = OPEN_EXISTING;
break;
case _O_CREAT:
filecreate = OPEN_ALWAYS;
break;
case _O_CREAT | _O_EXCL:
case _O_CREAT | _O_TRUNC | _O_EXCL:
filecreate = CREATE_NEW;
break;
case _O_TRUNC:
case _O_TRUNC | _O_EXCL: // ignore EXCL w/o CREAT
filecreate = TRUNCATE_EXISTING;
break;
case _O_CREAT | _O_TRUNC:
filecreate = CREATE_ALWAYS;
break;
default:
// this can't happen ... all cases are covered
set_errno(EINVAL);
return -1;
}
/*
* decode file attribute flags if _O_CREAT was specified
*/
fileattrib = FILE_ATTRIBUTE_NORMAL; /* default */
if ( oflag & _O_CREAT )
{
/*
* set up variable argument list stuff
*/
if ( oflag & _O_RDONLY )
fileattrib = FILE_ATTRIBUTE_READONLY;
}
/*
* Set temporary file (delete-on-close) attribute if requested.
*/
if ( oflag & _O_TEMPORARY )
{
fileattrib |= FILE_FLAG_DELETE_ON_CLOSE;
fileaccess |= DELETE;
}
/*
* Set temporary file (delay-flush-to-disk) attribute if requested.
*/
if ( oflag & _O_SHORT_LIVED )
fileattrib |= FILE_ATTRIBUTE_TEMPORARY;
/*
* Set sequential or random access attribute if requested.
*/
if ( oflag & _O_SEQUENTIAL )
fileattrib |= FILE_FLAG_SEQUENTIAL_SCAN;
else if ( oflag & _O_RANDOM )
fileattrib |= FILE_FLAG_RANDOM_ACCESS;
/*
* get an available handle.
*
* multi-thread note: the returned handle is locked!
*/
/*
* try to open/create the file
*/
if ( (osfh = CreateFile( A2T(filename),
fileaccess,
fileshare,
NULL,
filecreate,
fileattrib,
NULL ))
== INVALID_HANDLE_VALUE )
{
return -1; /* return error to caller */
}
return (int)osfh;
}
int _sopen(const char *filename, int oflag , int pmode, ...)
{
return _open(filename, oflag , pmode);
}
int _chsize( int nHandle, long size )
{
if ((DWORD)size!=SetFilePointer((HANDLE)nHandle,size,NULL,FILE_BEGIN)) return -1;
return (SetEndOfFile((HANDLE)nHandle)) ? 0 : -1;
}
int _mkdir(const char *sDir)
{
USES_CONVERSION;
PString folderName = sDir;
if (folderName[folderName.GetLength() - 1] == PDIR_SEPARATOR) {
folderName.Delete(folderName.GetLength() - 1, 1);
}
return (CreateDirectory(A2T(folderName),NULL) ? 0 : -1);
}
int _rmdir(const char *sDir)
{
USES_CONVERSION;
PString folderName = sDir;
if (folderName[folderName.GetLength() - 1] == PDIR_SEPARATOR) {
folderName.Delete(folderName.GetLength() - 1, 1);
}
return (RemoveDirectory(A2T(folderName)) ? 0 : -1);
}
int _access(const char *sName, int mode)
{
USES_CONVERSION;
WIN32_FIND_DATA FindFileData;
PString test(sName);
if (test[test.GetLength() - 1] == '.' && test[test.GetLength() - 2] == PDIR_SEPARATOR)
test.Delete(test.GetLength() - 2, 2);
HANDLE file = FindFirstFile(A2T((const char*) test), &FindFileData);
if (file == INVALID_HANDLE_VALUE ) return -1;
FindClose(file);
switch(mode)
{
//checking for the existance
case 0: return 0;
//checking for read permission
case 4: return 0;
//checking for write permission
case 2: return (FindFileData.dwFileAttributes&FILE_ATTRIBUTE_READONLY) ? -1 : 0;
//checking for read and write permission
case 6: return (FindFileData.dwFileAttributes&FILE_ATTRIBUTE_READONLY) ? -1 : 0;
}
return -1;
}
int remove(const char *name)
{
USES_CONVERSION;
return (DeleteFile(A2T(name)) ? 0 : -1);
}
int _chmod( const char *filename, int pmode )
{
USES_CONVERSION;
TCHAR* pFileName=A2T(filename);
DWORD attr = GetFileAttributes(pFileName);
if (pmode&_S_IWRITE)
attr|=FILE_ATTRIBUTE_READONLY;
else
attr&=~FILE_ATTRIBUTE_READONLY;
return (SetFileAttributes(pFileName,attr) ? 0: -1);
}
int rename( const char *oldname, const char *newname )
{
USES_CONVERSION;
return (DeleteAndRenameFile( A2T(newname), A2T(oldname)) ? 0 : -1);
}
//used by regex.cxx
void printchar (char n)
{
printf(" %d ",n);
}
long strtol (const char *nptr,char **endptr,int ibase)
{
USES_CONVERSION;
TCHAR* tnptr = A2T(nptr);
TCHAR* tendptr = NULL;
long res= _tcstoul(tnptr,&tendptr,ibase);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -