Windows下Command Line中的中文字符是采用ANSI編碼來處理的,而Perforce command line要求傳入的中文參數(shù)(文件(夾)名)為UTF-8編碼,所以需要將中文參數(shù)轉(zhuǎn)換為UTF-8后再做處理,下面介紹兩種處理方法。
一、系統(tǒng)調(diào)用
通過使用WinExec、ShellExecute或system,如:
_snprintf(cmdbuf , 1024 , "p4 -c %s add \"%s\"" , argv[1] , ANSIToUTF8(path.c_str()));
system(cmdbuf);
此方法的雖然簡單,但存在效率問題,因為有創(chuàng)建進程的巨大開銷。此外不同版本的Perforce也會出現(xiàn)不同的執(zhí)行結(jié)果,針對特定的中文會出現(xiàn)操作失敗的詭異問題。
二、Perforce SDK
Perforce提供有SDK用以擴展或集成到其他應(yīng)用中,雖然沒有詳細(xì)的文檔,但可以通過學(xué)習(xí)SDK中的sample文件來學(xué)習(xí),此方法最穩(wěn)定。
下面代碼展示了通過SDK中ClientAPI來遞歸添加指定文件夾下的所有文件:
# include "clientapi.h"
# include "i18napi.h"
# include "CharSetConvertUtil.h"
# include <string>
# include <vector>
# include <list>
# include <io.h>
using namespace std;
// structure to hold a directory and all its filenames.
struct FILELIST
{
string path;
vector<string> theList;
};
void TransverseDirectory(string path, list<FILELIST>& theList)
{
struct _finddatai64_t data;
string fname = path + "\\*.*";
long h = _findfirsti64(fname.c_str(),&data);
if(h >= 0)
{
FILELIST thisList;
theList.push_back(thisList);
list<FILELIST>::iterator it = theList.end();
it--;
(*it).path = path;
do {
if( (data.attrib & _A_SUBDIR) )
{
// make sure we skip "." and "..". Have to use strcmp here because
// some file names can start with a dot, so just testing for the
// first dot is not suffient.
if( strcmp(data.name,".") != 0 &&strcmp(data.name,"..") != 0)
{
// We found a sub-directory, so get the files in it too
fname = path + "\\" + data.name;
// recurrsion here!
TransverseDirectory(fname,theList);
}
}
else
{
// this is just a normal filename. So just add it to our vector
(*it).theList.push_back(data.name);
}
}while( _findnexti64(h,&data) == 0);
_findclose(h);
}
}
int main( int argc, char **argv )
{
list<FILELIST> MyList;
string path;
ClientUser ui;
ClientApi client;
StrBuf msg;
Error e;
if(argc < 4)
{
fprintf( stderr , "P4 Transverse Add: Arguments Error!\n");
return -1;
}
client.SetPort(argv[1]);
client.SetClient(argv[2]);
client.SetTrans(CharSetApi::UTF_8 , CharSetApi::UTF_8 ,
CharSetApi::UTF_8,
CharSetApi::UTF_8);
TransverseDirectory(argv[3],MyList);
// Connect to server
client.Init( &e );
if( e.Test() )
{
e.Fmt( &msg );
fprintf( stderr, "%s\n", msg.Text() );
return -1;
}
list<FILELIST>::iterator it;
for(it = MyList.begin(); it != MyList.end(); it++)
{
vector<string>::iterator its;
for(its = (*it).theList.begin(); its != (*it).theList.end(); its++)
{
path = (*it).path + "\\" + (*its);
char* pText = ANSIToUTF8(path.c_str());
client.SetArgv( 1 , &pText);
client.Run( "add" , &ui );
}
}
// Close connection
client.Final( &e );
if( e.Test() )
{
e.Fmt( &msg );
fprintf( stderr, "%s\n", msg.Text() );
return -1;
}
return 0;
}
此方法省去了創(chuàng)建p4進程的開銷,任務(wù)執(zhí)行效率會提高不少,而且也不會出現(xiàn)執(zhí)行結(jié)果不穩(wěn)定的問題。
附一:SDK下載地址
ftp://ftp.perforce.com/perforce/
附二:附上ANSI轉(zhuǎn)UTF-8代碼
wchar_t* ANSIToUnicode( const char* str )
{
int textlen ;
wchar_t * result;
textlen = MultiByteToWideChar( CP_ACP, 0, str,-1, NULL,0 );
result = (wchar_t *)malloc((textlen+1)*sizeof(wchar_t));
memset(result,0,(textlen+1)*sizeof(wchar_t));
MultiByteToWideChar(CP_ACP, 0,str,-1,(LPWSTR)result,textlen );
return result;
}
char* UnicodeToANSI( const wchar_t *str )
{
char * result;
int textlen;
// wide char to multi char
textlen = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, NULL, NULL );
result =(char *)malloc((textlen+1)*sizeof(char));
memset( result, 0, sizeof(char) * ( textlen + 1 ) );
WideCharToMultiByte( CP_ACP, 0, str, -1, result, textlen, NULL, NULL );
return result;
}
wchar_t* UTF8ToUnicode( const char* str )
{
int textlen ;
wchar_t * result;
textlen = MultiByteToWideChar( CP_UTF8, 0, str,-1, NULL,0 );
result = (wchar_t *)malloc((textlen+1)*sizeof(wchar_t));
memset(result,0,(textlen+1)*sizeof(wchar_t));
MultiByteToWideChar(CP_UTF8, 0,str,-1,(LPWSTR)result,textlen );
return result;
}
char* UnicodeToUTF8( const wchar_t *str )
{
char * result;
int textlen;
// wide char to multi char
textlen = WideCharToMultiByte( CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL );
result =(char *)malloc((textlen+1)*sizeof(char));
memset(result, 0, sizeof(char) * ( textlen + 1 ) );
WideCharToMultiByte( CP_UTF8, 0, str, -1, result, textlen, NULL, NULL );
return result;
}
char* ANSIToUTF8(const char* str)
{
wchar_t* pUnicodeBuff = ANSIToUnicode(str);
char* pUtf8Buff = UnicodeToUTF8(pUnicodeBuff);
free(pUnicodeBuff);
return pUtf8Buff;
}