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

            戰魂小筑

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

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

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

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

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

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

            下面是在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[]) 函數中添加代碼:

            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_ );

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

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

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

            函數中,找到:

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

            加黑部分為解析開關

             

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

              // 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_;
            
            轉載請注明: 戰魂小筑http://www.shnenglu.com/sunicdavy
            posted on 2011-08-16 11:38 戰魂小筑 閱讀(3903) 評論(1)  編輯 收藏 引用 所屬分類: C++/ 編程語言工具使用及設計

            評論

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

            亚洲国产成人久久综合一区77 | 欧美精品丝袜久久久中文字幕| 2020久久精品国产免费| 国产一区二区三区久久精品| 久久精品国产亚洲麻豆| 青草久久久国产线免观| 久久婷婷成人综合色综合| 久久中文字幕一区二区| 久久久久久国产精品美女| 久久精品国产清高在天天线| 久久亚洲天堂| 狠狠色丁香久久综合五月| 久久精品综合网| 国产精品无码久久久久| 国产精品久久久久jk制服| 亚洲国产小视频精品久久久三级| www性久久久com| 一本色道久久综合| 久久精品国产99久久香蕉| 欧美噜噜久久久XXX| 一本色综合久久| 99久久精品费精品国产| 国产精品一久久香蕉国产线看| 思思久久精品在热线热| 久久伊人亚洲AV无码网站| 伊人色综合久久| 久久99精品国产一区二区三区 | 99久久国产亚洲综合精品| 品成人欧美大片久久国产欧美...| 少妇内射兰兰久久| 久久久亚洲AV波多野结衣| 亚洲综合久久夜AV | 合区精品久久久中文字幕一区| 久久久久无码精品| 午夜福利91久久福利| 久久亚洲高清综合| 伊人情人综合成人久久网小说| 四虎国产精品成人免费久久| 久久久久亚洲AV无码观看| 亚洲中文字幕无码久久2020| 亚洲中文字幕久久精品无码喷水|