• <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>

            戰(zhàn)魂小筑

            討論群:309800774 知乎關(guān)注:http://zhihu.com/people/sunicdavy 開源項目:https://github.com/davyxu

               :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              257 隨筆 :: 0 文章 :: 506 評論 :: 0 Trackbacks

            使用protobuf的生成器可以對proto文件進(jìn)行解析后生成指定的目標(biāo)語言代碼.隨著項目的不斷擴(kuò)大, 協(xié)議修改變的非常頻繁, 因此每次重編變的異常耗時. 模仿C/C++編譯器的時間戳檢查生成機(jī)制,我給protobuf生成器添加了時間戳檢查功能. 下面說下原理:

            此功能不是必須的, 可以通過命令行指定—timestampfile FILE 來指定要生成的proto文件對應(yīng)的時間戳

            每次運(yùn)行解析器時, 解析器通過命令行獲取proto文件, 我們可以先讀取之前保存的時間戳, 然后與這個文件的當(dāng)前修改時間進(jìn)行對比

            如果時間戳不相等,說明文件已經(jīng)被修改, 需要重新生成.之后更新時間戳文件即可.

            下面是在protobuf的protoc工程里添加的代碼:

            command_line_interface.cc

            void CommandLineInterface::ReadTimeStampFile( const string& timestampfile )
            {
                FILE* f = fopen( timestampfile.c_str(), "rb" );
            
                if ( f == NULL )
                    return;
            
                time_stamp_map_.clear( );
            
                while( !feof( f ) )
                {
                    size_t read = 0;
            
                    int strlen = 0;
                    read = fread( &strlen, 1 , sizeof strlen,  f );
            
                    if ( read == 0 )
                        break;
            
                    std::string filename;
                    filename.resize( strlen );
            
                    read = fread( (void*)filename.data(), 1, sizeof(char) * strlen, f );
            
                    if ( read == 0 )
                        break;
            
            
                    __time64_t timestamp;
                    read = fread( &timestamp, 1, sizeof(timestamp), f );
            
                    if ( read == 0 )
                        break;
            
                    time_stamp_map_[filename] = timestamp;
                }
            
                fclose( f );
            }
            
            void CommandLineInterface::WriteTimeStampFile( const string& timestampfile )
            {
                FILE* f = fopen( timestampfile.c_str(), "wb" );
            
                if ( f == NULL )
                    return;
            
                for ( time_stamp_map::iterator it = time_stamp_map_.begin();
                    it != time_stamp_map_.end();
                    it++)
                {
                    const std::string& filename = it->first;
                    __time64_t timestamp = it->second;
            
                    int strlen = filename.length();
                    fwrite( &strlen, 1, sizeof(strlen), f );
                    fwrite( (void*)filename.data(), 1, sizeof(char)*strlen, f );
                    fwrite( &timestamp, 1, sizeof( timestamp ), f );
                }
                
                fclose( f );
            }
            
            bool CommandLineInterface::NeedGenerate( const std::string& filename )
            {
                struct _stat buf;
            
                if ( _stat( filename.c_str(), &buf ) != 0 )
                {
                    // file error
                    return true;
                }
            
                time_stamp_map::iterator it = time_stamp_map_.find( filename );
            
                
                if ( it != time_stamp_map_.end() )
                {
                    __time64_t& timestamp = it->second;
            
                    if ( timestamp == buf.st_mtime )
                        return false;
                }
            
                // don't hold the time stamp or time stamp not match, generate it!
                
                // save to map , then write time stamp
                time_stamp_map_[filename] = buf.st_mtime;
            
                return true;
            }
            
            
            void CommandLineInterface::CheckFileStamp( const string& timestampfile )
            {
                
                ReadTimeStampFile( timestampfile );
            
                
            
                for (vector<string>::iterator it = input_files_.begin();
                    it != input_files_.end();) {
                    
                    if ( !NeedGenerate( *it) )
                    {
                        it = input_files_.erase( it );
                    }
                    else
                    {
                        ++it;
                    }
                }
            
            
                WriteTimeStampFile( timestampfile );
            }
             

            以上代碼為核心邏輯, 之后在int CommandLineInterface::Run(int argc, const char* const argv[]) 函數(shù)中添加代碼:

            int CommandLineInterface::Run(int argc, const char* const argv[]) {
              Clear();
              if (!ParseArguments(argc, argv)) return 1;
            
              // Set up the source tree.
              DiskSourceTree source_tree;
              for (int i = 0; i < proto_path_.size(); i++) {
                source_tree.MapPath(proto_path_[i].first, proto_path_[i].second);
              }
            
              // Map input files to virtual paths if necessary.
              if (!inputs_are_proto_path_relative_) {
                if (!MakeInputsBeProtoPathRelative(&source_tree)) {
                  return 1;
                }
              }
            
              // Allocate the Importer.
              ErrorPrinter error_collector(error_format_, &source_tree);
              Importer importer(&source_tree, &error_collector);
            
              vector<const FileDescriptor*> parsed_files;
            
            
              if ( time_stamp_filename_ != "" )
                CheckFileStamp( time_stamp_filename_ );

            加黑部分為添加的代碼, 這里是檢查時間戳的入口

            同時,我們還需要添加命令行解析開關(guān), 這里在

            bool CommandLineInterface::InterpretArgument(const string& name,
                                                         const string& value) {

            函數(shù)中,找到:

              } else if (name == "--error_format") {
                if (value == "gcc") {
                  error_format_ = ERROR_FORMAT_GCC;
                } else if (value == "msvs") {
                  error_format_ = ERROR_FORMAT_MSVS;
                } else {
                  cerr << "Unknown error format: " << value << endl;
                  return false;
                }
            
              } 
              else if ( name == "--timestampfile" ){
                    time_stamp_filename_ = value;
              }
              else if (name == "--plugin") {
                if (plugin_prefix_.empty()) {
                  cerr << "This compiler does not support plugins." << endl;
                  return false;
                }

            加黑部分為解析開關(guān)

             

            之后在頭文件中添加聲明代碼:

              // Time stamp function
              void ReadTimeStampFile( const string& timestampfile );
            
              void WriteTimeStampFile( const string& timestampfile );
            
              bool NeedGenerate( const std::string& filename );
            
              void CheckFileStamp( const string& timestampfile );
            
              typedef std::map<std::string, __time64_t> time_stamp_map;    // proto file name as key, timestamp as value
              time_stamp_map time_stamp_map_;
              string time_stamp_filename_;
            
            轉(zhuǎn)載請注明: 戰(zhàn)魂小筑http://www.shnenglu.com/sunicdavy
            posted on 2011-08-16 11:38 戰(zhàn)魂小筑 閱讀(3903) 評論(1)  編輯 收藏 引用 所屬分類: C++/ 編程語言 、工具使用及設(shè)計

            評論

            # re: 讓protobuf生成器支持時間戳檢查[未登錄] 2011-11-11 16:02 frank28_nfls
            為什么不用make來做這個呢?呵呵  回復(fù)  更多評論
              

            久久综合九色欧美综合狠狠| 欧美日韩精品久久久久| 亚洲综合熟女久久久30p| 国产亚洲精品自在久久| 国产成人精品久久| 青青青青久久精品国产h久久精品五福影院1421 | 色综合久久综精品| 亚洲中文字幕伊人久久无码| www性久久久com| 一极黄色视频久久网站| 久久免费小视频| 色偷偷偷久久伊人大杳蕉| 久久精品国产第一区二区| 久久久老熟女一区二区三区| 免费精品久久久久久中文字幕| 久久亚洲精精品中文字幕| 国产精品嫩草影院久久| 久久综合给合久久狠狠狠97色69| 亚洲а∨天堂久久精品9966| 亚洲一区中文字幕久久| 7777久久亚洲中文字幕| 无码AV中文字幕久久专区| 蜜桃麻豆WWW久久囤产精品| 九九久久精品无码专区| 狠狠色婷婷综合天天久久丁香 | 2021最新久久久视精品爱| 久久精品国产一区二区| 久久综合狠狠综合久久激情 | 精品久久无码中文字幕| 国产精品久久国产精麻豆99网站| 一本久久知道综合久久| 无码人妻久久一区二区三区免费| 亚洲精品成人网久久久久久| 亚洲成av人片不卡无码久久| 日本精品久久久久影院日本 | 武侠古典久久婷婷狼人伊人| 国产精品欧美亚洲韩国日本久久| 99麻豆久久久国产精品免费| 久久精品黄AA片一区二区三区| 精品久久久无码人妻中文字幕豆芽 | 日韩精品久久久久久免费|