• <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 戰魂小筑 閱讀(3892) 評論(1)  編輯 收藏 引用 所屬分類: C++/ 編程語言工具使用及設計

            評論

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

            欧美亚洲国产精品久久久久| 粉嫩小泬无遮挡久久久久久| 亚洲一区二区三区日本久久九| 亚洲精品午夜国产VA久久成人| 麻豆成人久久精品二区三区免费| 久久A级毛片免费观看| 51久久夜色精品国产| 久久亚洲精品国产精品婷婷| 精品久久久久久久无码| 久久夜色精品国产亚洲av| 精品熟女少妇av免费久久| 久久强奷乱码老熟女网站| 久久青青草原亚洲av无码app | 性欧美丰满熟妇XXXX性久久久| 精品久久一区二区三区| 热久久视久久精品18| 久久久久亚洲精品天堂久久久久久| 久久大香萑太香蕉av| 久久国产成人亚洲精品影院| 国内精品久久久久影院免费| 伊人久久久AV老熟妇色| 久久人人超碰精品CAOPOREN| 99久久国产综合精品五月天喷水 | 久久99久国产麻精品66| 久久精品国产99久久久香蕉| 国产精品九九九久久九九| 亚洲午夜久久久久久久久久| 亚洲国产成人久久综合一区77| 久久久久夜夜夜精品国产| 人妻无码αv中文字幕久久琪琪布| 久久久久久午夜精品| 久久免费99精品国产自在现线| 久久久久免费精品国产| 亚洲综合精品香蕉久久网97| 99国产欧美精品久久久蜜芽| 狠狠狠色丁香婷婷综合久久五月| 久久综合噜噜激激的五月天| 久久久亚洲欧洲日产国码二区| 97精品依人久久久大香线蕉97| 欧美一区二区三区久久综| 久久综合亚洲欧美成人|