当前位置:编程学习 > C/C++ >>

让protobuf生成器支持时间戳检查

使用protobuf的生成器可以对proto文件进行解析后生成指定的目标语言代码.随着项目的不断扩大, 协议修改变的非常频繁, 因此每次重编变的异常耗时. 模仿C/C++编译器的时间戳检查生成机制,我给protobuf生成器添加了时间戳检查功能. 下面说下原理:

此功能不是必须的, 可以通过命令行指定—timestampfile FILE 来指定要生成的proto文件对应的时间戳

每次运行解析器时, 解析器通过命令行获取proto文件, 我们可以先读取之前保存的时间戳, 然后与这个文件的当前修改时间进行对比

如果时间戳不相等,说明文件已经被修改, 需要重新生成.之后更新时间戳文件即可.

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

command_line_inte易做图ce.cc

void CommandLineInte易做图ce::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( ×tamp, 1, sizeof(timestamp), f );

        if ( read == 0 )
            break;

        time_stamp_map_[filename] = timestamp;
    }

    fclose( f );
}

void CommandLineInte易做图ce::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( ×tamp, 1, sizeof( timestamp ), f );
    }
   
    fclose( f );
}

bool CommandLineInte易做图ce::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 CommandLineInte易做图ce::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 CommandLineInte易做图ce::Run(int argc, const char* const argv[]) 函数中添加代码:

int CommandLineInte易做图ce::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 CommandLineInte易做图ce::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 {
   &

补充:软件开发 , C语言 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,