winadapter.cpp
来自「funambol window mobile客户端源代码」· C++ 代码 · 共 779 行 · 第 1/2 页
CPP
779 行
//*len=msglen;
ret = true;
finally:
delete [] wpath;
return ret;
}
bool saveFile(const char *filename, const char *buffer, size_t len, bool binary)
{
bool ret = false;
// Convert to wide-char to be compatible with international chars.
// If using char and UTF-8, fopen() is not working correctly and
// will fail with special chars. (with UTF-8... why?)
// So we use _wfopen() and a wide-char path.
const WCHAR* mode = binary ? TEXT("wb") : TEXT("w");
WCHAR* wpath = toWideChar(filename);
FILE *f = _wfopen(wpath, mode);
if(!f) {
goto finally;
}
if (fwrite(buffer, sizeof(char), len, f) != len) {
fclose(f);
goto finally;
}
fclose(f);
ret = true;
finally:
delete [] wpath;
return ret;
}
#if defined(WIN32) && !defined(_WIN32_WCE)
unsigned long getFileModTime(const char* name) {
struct _stat buffer;
return _stat(name, &buffer) ? 0 : (unsigned long)buffer.st_mtime;
}
#endif
#if defined(WIN32) || defined(_WIN32_WCE)
/// Returns a file list from a directory, as char**.
char** readDir(char* name, int *count, bool onlyCount) {
*count = 0;
char** fileNames = NULL;
WIN32_FIND_DATA FileData;
HANDLE hFind;
DWORD dwAttrs;
WCHAR toFind [512];
WCHAR szNewPath [512];
szNewPath[0] = 0;
bool fFinished = false;
//
// Get number of files
//
if (!onlyCount) {
int i=0;
readDir(name, &i, true);
if (i>0)
fileNames = new char*[i];
else
return NULL;
}
WCHAR* wname = toWideChar(name);
wsprintf(toFind, TEXT("%s\\*.*"), wname);
//
// Get file names from dir
//
hFind = FindFirstFile(toFind, &FileData);
if (hFind == INVALID_HANDLE_VALUE) {
LOG.error("Invalid handle for retrieve files from %s", name);
*count = 0;
fileNames = NULL;
goto finally;
}
else {
while (!fFinished) {
wsprintf(szNewPath, TEXT("%s/%s"), wname, FileData.cFileName);
dwAttrs = GetFileAttributes(szNewPath);
if ( (dwAttrs & FILE_ATTRIBUTE_DIRECTORY)
|| (dwAttrs & FILE_ATTRIBUTE_HIDDEN)
|| (dwAttrs & FILE_ATTRIBUTE_SYSTEM) ) {
}// nothing
else {
if (!onlyCount) {
fileNames[*count] = toMultibyte(FileData.cFileName);
}
(*count) ++;
}
if (!FindNextFile(hFind, &FileData)) {
if (GetLastError() == ERROR_NO_MORE_FILES){
fFinished = true;
}
}
}
// Close the search handle.
FindClose(hFind);
}
finally:
if (wname) {
delete [] wname;
wname = NULL;
}
return fileNames;
}
bool removeFileInDir(const char* d, const char* fname) {
WIN32_FIND_DATA FileData;
HANDLE hFind;
wchar_t toFind [512];
wchar_t szNewPath [512];
bool ret = false;
DWORD dwAttrs;
BOOL fFinished = FALSE;
WCHAR* dir = toWideChar(d);
if (fname) {
WCHAR* filename = toWideChar(fname);
wsprintf(toFind, TEXT("%s/%s"), dir, filename);
delete [] filename; filename = NULL;
}
else {
wsprintf(toFind, TEXT("%s/*.*"), dir);
}
hFind = FindFirstFile(toFind, &FileData);
if (hFind != INVALID_HANDLE_VALUE) {
while (!fFinished) {
wsprintf(szNewPath, TEXT("%s/%s"), dir, FileData.cFileName);
dwAttrs = GetFileAttributes(szNewPath);
if (dwAttrs == FILE_ATTRIBUTE_DIRECTORY) { }
// do anything for subdirectory
else {
DeleteFile(szNewPath);
}
if (!FindNextFile(hFind, &FileData)) {
if (GetLastError() == ERROR_NO_MORE_FILES) {
fFinished = TRUE;
}
else {
goto finally;
}
}
}
FindClose(hFind);
}
ret = true;
finally:
if (dir) { delete [] dir; }
return ret;
}
StringBuffer getCacheDirectory() {
StringBuffer ret = ".";
wchar_t p[260];
SHGetSpecialFolderPath(NULL, p, CSIDL_PERSONAL, 0);
wcscat(p, TEXT("/"));
wcscat(p, TEXT(CACHE_REP));
DWORD attr = CreateDirectory(p, NULL);
if (attr == ERROR_ALREADY_EXISTS) {
LOG.info("The %S directory exists", p);
} else if (attr == ERROR_PATH_NOT_FOUND) {
LOG.info("The %S has an error in the path", p);
return ret;
} else if (attr == 0) {
//
}
char* t = toMultibyte(p);
ret = t;
delete [] t;
return ret;
}
#else
// TBD: dummy implementation!
char** readDir(char* name, int *count, bool onlyCount) {
return NULL;
}
bool removeFileInDir(const char* dir, const char* filename);
#endif // #if defined(WIN32) && !defined(_WIN32_WCE)
static int findCodePage(const char *encoding)
{
if (encoding){
for(int i=0; encodings[i].name; i++) {
if(_stricmp(encodings[i].name, encoding) == 0) {
// Found
return encodings[i].codepage_id;
}
}
// Not found
LOG.error("Invalid encoding: %s", encoding);
}
// Default encoding
return CP_UTF8;
}
static size_t getLenEncoding(const WCHAR* s, int codepage)
{
if (!s)
return 0;
int len = wcslen(s);
if (!len)
return 0;
long k = WideCharToMultiByte (codepage, 0, s, len, 0, 0, 0, 0);
return (k != 0) ? (long)k : -1;
}
size_t getLenEncoding(const WCHAR* s, const char* encoding)
{
return getLenEncoding( s, findCodePage(encoding) );
}
char* toMultibyte(const WCHAR *wc, const char *encoding)
{
if (!wc) {
return NULL;
}
char *ret;
size_t wlen = wcslen(wc);
if (!wlen) {
ret = new char[1];
ret[0] = 0;
return ret;
}
int codepage = findCodePage(encoding);
size_t blen = getLenEncoding(wc, codepage);
if(blen <= 0) {
LOG.error("toMultibyte: invalid encoding");
return NULL;
}
ret = new char[blen+1];
blen = WideCharToMultiByte(codepage, 0, wc, wlen, ret, blen, 0, 0);
ret[blen] = 0;
return ret;
}
WCHAR* toWideChar(const char *mb, const char *encoding) {
if (mb == NULL) {
return NULL;
}
unsigned long dsize = strlen(mb);
WCHAR *ret = new WCHAR[dsize+2];
memset(ret, 0, (dsize + 1)*sizeof(WCHAR));
if (!dsize)
return ret;
int codepage = findCodePage(encoding);
unsigned long k = 0;
k = MultiByteToWideChar(codepage, 0, mb, -1, ret, dsize + 1);
if( !k ) {
LOG.error("toWideChar: error %d \n\tConverting: %s\n\tWith encoding %s",
GetLastError(), mb, encoding);
LOG.error("toWideChar: try to use default codepage.");
k = MultiByteToWideChar(CP_UTF8, 0, mb, -1, ret, dsize + 1);
if( !k ){
LOG.error("toWideChar: error %d converting the string using default codepage.");
delete [] ret; ret = 0;
}
}
return ret;
}
int round(double val) {
int v = (int)val;
return ((val - v) > 0.5) ? v+1 : v;
}
#if defined(WIN32) && !defined(_WIN32_WCE)
// ----------------------------------------------------
// REDEFINITION OF NEW / DELETE -> debug for memory leaks
//
// WARNING: this sloooooowwwwssss doooowwwwnnnn things!
// ----------------------------------------------------
#ifdef MALLOC_DEBUG
//
// This is required since in debug mode, new is rewritten
// as new(__FILE__, __LINE__). See utils.h for details
//
#undef new
#include "base/memTracker.h"
#include "base/globalsdef.h"
USE_NAMESPACE
MemTracker m = MemTracker(true);
void *operator new(size_t s, char* file, int line) {
void* p = malloc(s);
//fprintf(stderr, "new - p:%lx s:%ld, %s:%d\n", p, s, file, line);
if (m.isMemTracking()) {
m.disableMemTracker();
m.addTrack((DWORD)p, s, file, line);
m.enableMemTracker();
}
return p;
}
void *operator new(size_t s) {
return ::operator new(s, "", 0);
}
void *operator new[](size_t s) {
return ::operator new(s, "", 0);
}
void *operator new[](size_t s, char* file, int line) {
return ::operator new(s, file, line);
}
void operator delete(void* p) {
//fprintf(stderr, "delete - p:%lx\n", (long)p);
if (m.isMemTracking()) {
m.disableMemTracker();
m.removeTrack((DWORD)p);
m.enableMemTracker();
}
if (p) {
free(p);
}
}
void operator delete[] (void* p) {
::operator delete(p);
}
void printMemLeaks() {
if (m.isMemTracking())
m.dumpUnfreed();
}
#endif // #ifdef MALLOC_DEBUG
#endif // #if defined(WIN32) && !defined(_WIN32_WCE)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?