设为首页 加入收藏

TOP

让protobuf生成器支持时间戳检查 (一)
2014-11-23 22:57:33 来源: 作者: 【 】 浏览:4
Tags:protobuf 成器 支持 时间 检查

使用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( ×tamp, 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( ×tamp, 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::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 imp

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇基于顺序存储的多叉树实现: (4) .. 下一篇对象的消息模型

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: