Google Protocolbuf 文本格式的解析

Google Protocolbuf 文本格式的解析

protobuf就是为懒人而造的工具, 啥协议, 啥配置文件, 统统定义proto, 解析就ok, 非常方便

 

文本格式的解析错误不能使用捕获错误来获取, 因此,我们需要使用自定义的错误收集器进行收集, 看代码:

#include <google/protobuf/text_format.h>
#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
#include <google/protobuf/io/tokenizer.h>
 
class PBTextErrorCollector : public google::protobuf::io::ErrorCollector
{
public:
    PBTextErrorCollector( const std::string& FileName )
        : mFileName( FileName )
    {
 
    }
 
    virtual void AddError(int line, int column, const string& message)
    {
        CCLog("%s(%d:%d) %s ", mFileName.c_str(), line, column, message.c_str() );
    }
 
    virtual void AddWarning(int line, int column, const string& message) 
    {
        CCLog("%s(%d:%d) %s ", mFileName.c_str(), line, column, message.c_str() );
    }
 
private:
    std::string mFileName;
};
 

解析代码

google::protobuf::TextFormat::Parser P;        
    P.RecordErrorsTo( &PEC );
    P.Parse( &AIS, &AF );

 

另外: 文本格式的注释使用unix shell风格: 以#开头

下面是我的文本格式的配置文件

 

AnchorPointX: 0.5
AnchorPointY: 0

SpriteScale: 2

ComponentName: "ActorActionManager"
ComponentName: "ActorFrameEventDispatcher"
#ComponentName: "SoundFXController"
ComponentName: "RoleDeltaMoveController"
ComponentName: "RoleBehaviorDirector"

InitAction: AA_Idle

Animations 
{
  AnimationName: "mai_idle"
  AnimationInterval: 0.067
}

你可能感兴趣的:(Google Protocolbuf 文本格式的解析)