Home » U++ Library support » U++ Core » Path including non-English character, buglog and usrlog file cannot be deleted
Path including non-English character, buglog and usrlog file cannot be deleted [message #17572] |
Thu, 21 August 2008 09:16 |
kasome
Messages: 78 Registered: July 2008 Location: Taiwan
|
Member |
|
|
In the debug mode, when the path of execute file including non-English character, like Chinese, buglog and usrlog cannot be deleted after the execute file terminated.
the content of buglog and usrlog file is as the following:
36.MainMenu with layout.2008-08-21-14-21-45.buglog:
* c:\Program Files\OpenCV\MyCode\build\win32\debug\´ú¸ÕÀÉ\36.MainMenu with layout.exe 21.08.2008 14:21:45, user: kasome
36.MainMenu with layout.2008-08-21-14-21-45.usrlog:
* c:\Program Files\OpenCV\MyCode\build\win32\debug\´ú¸ÕÀÉ\36.MainMenu with layout.exe 21.08.2008 14:21:45, user: kasome
and i find out the reason may be the file
uppsrc\Core\Path.cpp
in the following function:
bool FileDelete(const char *filename)
{
#if defined(PLATFORM_WIN32)
if( IsWinNT() ){
return !!UnicodeWin32().DeleteFileW( ToSystemCharsetW(filename) );
}
else
return !!DeleteFile(ToSystemCharset(filename));
#elif defined(PLATFORM_POSIX)
return !unlink(ToSystemCharset(filename));
#else
#error
#endif//PLATFORM
}
i keep trying and finally fix the function as the following, this function WideString can be found in the attachment file got from the internet.
bool FileDelete(const char *filename)
{
#if defined(PLATFORM_WIN32)
if( IsWinNT() ){
return !!UnicodeWin32().DeleteFileW( ToSystemCharsetW( ToUtf8( WideString( filename ) ) ) );
}
else
return !!DeleteFile(ToSystemCharset(filename));
#elif defined(PLATFORM_POSIX)
return !unlink(ToSystemCharset(filename));
#else
#error
#endif//PLATFORM
}
WideString.h
#include <windows.h>
#include <CtrlLib/CtrlLib.h>
#include <iostream>
using namespace std;
using namespace Upp;
WString WideString( const String s );
WideString.cpp
#include "WideString.h"
WString WideString( const String s ){
int nIndex = MultiByteToWideChar( CP_ACP, 0, s, -1, NULL, 0 );
wchar_t *w = new wchar_t[ nIndex + 1 ];
MultiByteToWideChar( CP_ACP, 0, s, -1, w, nIndex );
WString wstr = w;
delete[] w; w = 0;
return wstr;
}
now everthing is work fine, but i can not explain why, may be there is a more better way to solve this.
OS: Microsoft Windows XP Professional SP2
Language: Taiwan (codepage 950 )
[Updated on: Thu, 21 August 2008 09:31] Report message to a moderator
|
|
|
Re: Path including non-English character, buglog and usrlog file cannot be deleted [message #17582 is a reply to message #17572] |
Thu, 21 August 2008 16:48 |
|
mirek
Messages: 13975 Registered: November 2005
|
Ultimate Member |
|
|
kasome wrote on Thu, 21 August 2008 03:16 | In the debug mode, when the path of execute file including non-English character, like Chinese, buglog and usrlog cannot be deleted after the execute file terminated.
the content of buglog and usrlog file is as the following:
36.MainMenu with layout.2008-08-21-14-21-45.buglog:
* c:\Program Files\OpenCV\MyCode\build\win32\debug\´ú¸ÕÀÉ\36.MainMenu with layout.exe 21.08.2008 14:21:45, user: kasome
36.MainMenu with layout.2008-08-21-14-21-45.usrlog:
* c:\Program Files\OpenCV\MyCode\build\win32\debug\´ú¸ÕÀÉ\36.MainMenu with layout.exe 21.08.2008 14:21:45, user: kasome
and i find out the reason may be the file
uppsrc\Core\Path.cpp
in the following function:
bool FileDelete(const char *filename)
{
#if defined(PLATFORM_WIN32)
if( IsWinNT() ){
return !!UnicodeWin32().DeleteFileW( ToSystemCharsetW(filename) );
}
else
return !!DeleteFile(ToSystemCharset(filename));
#elif defined(PLATFORM_POSIX)
return !unlink(ToSystemCharset(filename));
#else
#error
#endif//PLATFORM
}
i keep trying and finally fix the function as the following, this function WideString can be found in the attachment file got from the internet.
bool FileDelete(const char *filename)
{
#if defined(PLATFORM_WIN32)
if( IsWinNT() ){
return !!UnicodeWin32().DeleteFileW( ToSystemCharsetW( ToUtf8( WideString( filename ) ) ) );
}
else
return !!DeleteFile(ToSystemCharset(filename));
#elif defined(PLATFORM_POSIX)
return !unlink(ToSystemCharset(filename));
#else
#error
#endif//PLATFORM
}
WideString.h
#include <windows.h>
#include <CtrlLib/CtrlLib.h>
#include <iostream>
using namespace std;
using namespace Upp;
WString WideString( const String s );
WideString.cpp
#include "WideString.h"
WString WideString( const String s ){
int nIndex = MultiByteToWideChar( CP_ACP, 0, s, -1, NULL, 0 );
wchar_t *w = new wchar_t[ nIndex + 1 ];
MultiByteToWideChar( CP_ACP, 0, s, -1, w, nIndex );
WString wstr = w;
delete[] w; w = 0;
return wstr;
}
now everthing is work fine, but i can not explain why, may be there is a more better way to solve this.
OS: Microsoft Windows XP Professional SP2
Language: Taiwan (codepage 950 )
|
Thanks. Handling CJK is always full of surprises
Hm, if this works well, I believe that the construct should be embedded into ToSystemCharsetW, correct?
Can you try to put it there and create / delete a couple of files with CJK filenames?
Mirek
|
|
|
Re: Path including non-English character, buglog and usrlog file cannot be deleted [message #17773 is a reply to message #17582] |
Thu, 28 August 2008 14:50 |
kasome
Messages: 78 Registered: July 2008 Location: Taiwan
|
Member |
|
|
I am so sorry replying late. Here is my some result.
When i put "ToUtf8( WideString( filename )" to the function "ToSystemCharsetW". i found something wrong when i try to delete and create file with Chinese filenames, so i try to trace the code. Finally, i find out the better solution.
just find the code in
c:\upp\uppsrc\Core\Log.cpp
bool LogStream::Delete()
{
Close();
if(*filename) {
if( !FileDelete( filename ) ) {
BugLog() << "Error deleting " << filename << ": " << GetLastErrorMessage();
return false;
}
*filename = 0;
}
return true;
}
and fix as the following
bool LogStream::Delete()
{
Close();
if(*filename) {
if( !FileDelete( FromSystemCharset(filename) ) ) {
BugLog() << "Error deleting " << filename << ": " << GetLastErrorMessage();
return false;
}
*filename = 0;
}
return true;
}
The function "FromSystemCharset" embbed in U++ and the function "WideString" is equivilent.
Because "FileDelete" should take UTF8 string as argument, and filename seem to just the ansi string, covert ansi string to utf8 by the function "FromSystemCharset" should solve the problem (e.g. the problem when path including non-English character, buglog and usrlog file cannot be deleted).
Now everything is work fine. ( i hope so )
BTW, here is some test code i wrote to test the two function (in c:\upp\uppsrc\Core\Path.cpp ) "FileDelete" and "DirectoryDelete" in processing file with the path including chinese characters, and it works very well in my OS.These two function has no problem when i test.
U++ is an amazing Tool. Thank you.
[Updated on: Thu, 28 August 2008 15:03] Report message to a moderator
|
|
|
|
|
|
Goto Forum:
Current Time: Tue Apr 30 01:39:00 CEST 2024
Total time taken to generate the page: 0.07532 seconds
|