• <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處理中文參數(shù)的兩種方法

                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;
            }

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

            Feedback

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

            perforce sdk本質(zhì)上也是調(diào)用命令行吧  回復(fù)  更多評論   

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

            @johnson
            NO  回復(fù)  更多評論   

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

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

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

            GetCommandLine CommandLineToArgvW   回復(fù)  更多評論   

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

            @Condor
            注意寬字符和utf-8的區(qū)別  回復(fù)  更多評論   

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

            perforce 支持中文命名嗎?  回復(fù)  更多評論   

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

            @xiami
            Yes
              回復(fù)  更多評論   

            久久WWW免费人成—看片| 亚洲AV无一区二区三区久久 | 久久久久一本毛久久久| 7国产欧美日韩综合天堂中文久久久久| 久久国产欧美日韩精品| 久久精品国产黑森林| 久久www免费人成看片| 久久精品成人免费看| 国内精品久久国产| 久久ZYZ资源站无码中文动漫| 国产2021久久精品| 久久天天躁狠狠躁夜夜躁2O2O| 中文字幕亚洲综合久久2| 亚洲精品乱码久久久久久久久久久久 | 欧美激情精品久久久久| 亚洲精品乱码久久久久久不卡| 久久99精品久久久久婷婷| 久久人妻少妇嫩草AV无码蜜桃 | 乱亲女H秽乱长久久久| 性做久久久久久久久老女人| 国产成人精品久久免费动漫| 2020久久精品亚洲热综合一本| 草草久久久无码国产专区| 麻豆亚洲AV永久无码精品久久 | 日本WV一本一道久久香蕉| 国产精品亚洲美女久久久| 国内精品久久久人妻中文字幕| 久久精品中文字幕大胸| 日本高清无卡码一区二区久久| 国产精品美女久久久久AV福利| 久久久久亚洲AV无码麻豆| 亚洲中文久久精品无码ww16| 欧美日韩精品久久久久| 久久久久一本毛久久久| 亚洲午夜福利精品久久| 久久久无码精品亚洲日韩软件| 国产精品丝袜久久久久久不卡| 伊人久久大香线焦综合四虎| 亚洲伊人久久大香线蕉苏妲己| 久久国产精品-国产精品| 久久精品人人槡人妻人人玩AV|