• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Heath's Blog

            There is no end, it is just the beginning! - A Game Developer's Notes

            Windows下Perforce Command Line處理中文參數的兩種方法

                Windows下Command Line中的中文字符是采用ANSI編碼來處理的,而Perforce command line要求傳入的中文參數(文件(夾)名)為UTF-8編碼,所以需要將中文參數轉換為UTF-8后再做處理,下面介紹兩種處理方法。

            一、系統調用

                通過使用WinExec、ShellExecute或system,如:

            _snprintf(cmdbuf , 1024 , "p4 -c %s add \"%s\"" , argv[1] , ANSIToUTF8(path.c_str()));
            system(cmdbuf);

                此方法的雖然簡單,但存在效率問題,因為有創建進程的巨大開銷。此外不同版本的Perforce也會出現不同的執行結果,針對特定的中文會出現操作失敗的詭異問題。

            二、Perforce SDK

                Perforce提供有SDK用以擴展或集成到其他應用中,雖然沒有詳細的文檔,但可以通過學習SDK中的sample文件來學習,此方法最穩定。

                下面代碼展示了通過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;
            }

                此方法省去了創建p4進程的開銷,任務執行效率會提高不少,而且也不會出現執行結果不穩定的問題。   

             

            附一:SDK下載地址

            ftp://ftp.perforce.com/perforce/

            附二:附上ANSI轉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;
            }

            posted on 2010-11-15 17:58 Heath 閱讀(4301) 評論(7)  編輯 收藏 引用 所屬分類: Game Development

            Feedback

            # re: Windows下Perforce Command Line處理中文參數的兩種方法[未登錄] 2010-11-16 10:02 johnson

            perforce sdk本質上也是調用命令行吧  回復  更多評論   

            # re: Windows下Perforce Command Line處理中文參數的兩種方法[未登錄] 2010-11-16 12:25 Heath

            @johnson
            NO  回復  更多評論   

            # re: Windows下Perforce Command Line處理中文參數的兩種方法 2010-11-20 11:17 Condor

            Windows 有專門解析的API,名字忘記了,可以直接轉成UINCODE的。  回復  更多評論   

            # re: Windows下Perforce Command Line處理中文參數的兩種方法 2010-11-28 11:18 Condor

            GetCommandLine CommandLineToArgvW   回復  更多評論   

            # re: Windows下Perforce Command Line處理中文參數的兩種方法[未登錄] 2010-12-08 20:04 Heath

            @Condor
            注意寬字符和utf-8的區別  回復  更多評論   

            # re: Windows下Perforce Command Line處理中文參數的兩種方法 2014-10-29 21:15 xiami

            perforce 支持中文命名嗎?  回復  更多評論   

            # re: Windows下Perforce Command Line處理中文參數的兩種方法[未登錄] 2014-10-30 19:40 Heath

            @xiami
            Yes
              回復  更多評論   

            久久精品国产精品亚洲精品| 精品少妇人妻av无码久久| jizzjizz国产精品久久| 久久99国产综合精品女同| 国产福利电影一区二区三区久久老子无码午夜伦不 | 久久婷婷国产剧情内射白浆| 久久99热精品| 观看 国产综合久久久久鬼色 欧美 亚洲 一区二区 | 少妇被又大又粗又爽毛片久久黑人| 久久人人爽人人爽人人AV| 精品无码久久久久久午夜| 91久久精品国产成人久久| 久久国产一区二区| 久久国产AVJUST麻豆| 99久久免费只有精品国产| 国产精品久久久久久久久软件| www.久久热.com| 国产精品久久一区二区三区| 麻豆久久| 亚洲精品午夜国产VA久久成人| 久久精品国产乱子伦| 久久国产午夜精品一区二区三区| 国产精自产拍久久久久久蜜| 久久久久久综合网天天| 久久久久国产精品三级网| 久久国产精品国产自线拍免费| 午夜不卡久久精品无码免费| 青青草原综合久久大伊人导航| 香蕉久久夜色精品国产小说| 日产精品久久久久久久| 久久精品亚洲AV久久久无码| 日日狠狠久久偷偷色综合96蜜桃| 精品久久一区二区三区| 久久久无码精品亚洲日韩按摩| 7777久久久国产精品消防器材| 久久精品国产精品亚洲毛片| 亚洲美日韩Av中文字幕无码久久久妻妇| 国产偷久久久精品专区| 九九精品久久久久久噜噜| 亚洲欧美国产日韩综合久久| 香蕉99久久国产综合精品宅男自 |