• <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 開源項(xiàng)目:https://github.com/davyxu

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

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

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

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

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

            下面是在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_ );

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

            同時(shí),我們還需要添加命令行解析開關(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)魂小筑 閱讀(3892) 評論(1)  編輯 收藏 引用 所屬分類: C++/ 編程語言 、工具使用及設(shè)計(jì)

            評論

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

            久久精品国产亚洲AV大全| 久久精品国产亚洲αv忘忧草| 亚洲精品NV久久久久久久久久| 久久久综合九色合综国产| 2021久久精品国产99国产精品| 亚洲国产一成人久久精品| 亚洲中文字幕无码久久2020| 一日本道伊人久久综合影| 思思久久99热只有频精品66| 成人综合久久精品色婷婷| 欧美日韩精品久久免费| 久久人人爽人人爽人人片AV高清| 青青草原综合久久大伊人| 久久笫一福利免费导航| 国产亚洲精久久久久久无码77777| 久久婷婷五月综合国产尤物app| 久久精品综合网| 国产∨亚洲V天堂无码久久久| 国产精品久久久天天影视| 亚洲国产精品一区二区久久| 观看 国产综合久久久久鬼色 欧美 亚洲 一区二区 | 久久久久久亚洲精品不卡| 久久天天婷婷五月俺也去| 亚洲精品乱码久久久久久蜜桃不卡| 久久久久亚洲AV无码永不| 亚洲一本综合久久| 亚洲婷婷国产精品电影人久久| 亚洲色大成网站www久久九| 免费国产99久久久香蕉| 久久亚洲精品无码观看不卡| 熟妇人妻久久中文字幕| 亚洲午夜久久久精品影院| 久久亚洲国产成人影院| 欧美777精品久久久久网| 无码任你躁久久久久久老妇App| 国产麻豆精品久久一二三| 久久综合成人网| 亚洲综合久久综合激情久久| 欧美日韩精品久久久久| 成人午夜精品久久久久久久小说| 波多野结衣AV无码久久一区|