利用protobuf进行读写配置文件

利用protobuf进行读写配置文件

1、编写protobuf 的proto文件

在程序中经常会用配置文件,而利用protobuf可以很方便的进行配置文件的读写。
先编写好protobuf的proto文件

syntax = "proto3";
package msgType;

enum EnumTest
{
    TEST0 = 0x00;
    TEST1 = 0x01;
    TEST2 = 0x02;
    TEST3 = 0x03;
}

message ProtoTestSub
{
    int32 test1 = 1;
    string test2 = 2;
}

message ProtoTest
{
    int32 int32_test = 1;
    string str_test = 2;
    repeated double dou_test = 3;
    repeated ProtoTestSub sub_test = 4;
    EnumTest eunm_test = 5;
    bytes bytes_test = 6;
}

2、生成相对应的源文件

root@root:/root/protobuf# protoc --cpp_out=./ ./test.proto.proto
root@root:/root/protobuf# ls
a.out  include  mybuild  protobuf_main.cpp  test.pb.cc  test.pb.h  test.proto

3、示例程序(读写配置文件)

#include <iostream>
#include <vector>
#include <algorithm>

#include "test.pb.h"
#include <google/protobuf/text_format.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>

#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <string>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>

int main(void )
{
    //1、序列化 填写基本的配置项
    msgType:: ProtoTest cls_proto_test1;
    cls_proto_test1.set_int32_test(123);
    cls_proto_test1.set_str_test("wanxuexiang");
    cls_proto_test1.add_dou_test(56.023);
    cls_proto_test1.add_dou_test(78.023);
    msgType:: ProtoTestSub * pcls_temp = cls_proto_test1.add_sub_test();
    pcls_temp->set_test1(12);
    pcls_temp->set_test2("zxcvbnm");
    pcls_temp = cls_proto_test1.add_sub_test();
    pcls_temp->set_test1(34);
    pcls_temp->set_test2("asdfghjkl");
    cls_proto_test1.set_eunm_test(msgType::TEST0);
    //protobuf调试打印函数 打印成字符串
    cls_proto_test1.PrintDebugString();
    //2、将配置写入文件之中
    std::string str_proto_test1;
    google::protobuf::TextFormat::PrintToString(cls_proto_test1, &str_proto_test1);
    std::ofstream file_proto_test1;
    file_proto_test1.open("file_proto_test1.cfg", std::ios::out| std:: ios_base::ate);

    if (!file_proto_test1.is_open())
    {
        fprintf(stderr, "open file_proto_test1.cfg fail\n");
        return -1;
    }
    file_proto_test1 << str_proto_test1;
    file_proto_test1.flush();
    file_proto_test1.close();
    system("cat file_proto_test1.cfg");
    //2、从配置文件中读取数据
    int fd = open("file_proto_test1.cfg", O_RDONLY);
    if (fd < 0)
    {
        printf("file_proto_test1.cfg:%s \n",strerror(errno));
        return false;
    }
    google::protobuf::io::FileInputStream fileInput(fd);
    fileInput.SetCloseOnDelete(true);
    msgType:: ProtoTest cls_proto_test2;;
    google::protobuf::TextFormat::Parse(&fileInput, &cls_proto_test2);
    //protobuf调试打印函数 打印成字符串
    cls_proto_test2.PrintDebugString();
    msgType:: ProtoTestSub cls_temp ;
    for(int i = 0;i < cls_proto_test2.sub_test_size();i++)
    {
        cls_temp = cls_proto_test2.sub_test(i);
        cls_temp.PrintDebugString();
    }
    while(1)
    {
        sleep(1);
    }       
    return 0;
}

4、运行结果

root@ubuntu:/wan/protobuf# ./a.out 
int32_test: 123
str_test: "wanxuexiang"
dou_test: 56.023
dou_test: 78.023
sub_test {
  test1: 12
  test2: "zxcvbnm"
}
sub_test {
  test1: 34
  test2: "asdfghjkl"
}
int32_test: 123
str_test: "wanxuexiang"
dou_test: 56.023
dou_test: 78.023
sub_test {
  test1: 12
  test2: "zxcvbnm"
}
sub_test {
  test1: 34
  test2: "asdfghjkl"
}
int32_test: 123
str_test: "wanxuexiang"
dou_test: 56.023
dou_test: 78.023
sub_test {
  test1: 12
  test2: "zxcvbnm"
}
sub_test {
  test1: 34
  test2: "asdfghjkl"
}
test1: 12
test2: "zxcvbnm"
test1: 34
test2: "asdfghjkl"
^C
root@ubuntu:/wan/protobuf# ls
a.out  file_proto_test1.cfg  include  mybuild  protobuf_main.cpp  test.pb.cc  test.pb.h  test.proto
root@ubuntu:/wan/protobuf# cat file_proto_test1.cfg 
int32_test: 123
str_test: "wanxuexiang"
dou_test: 56.023
dou_test: 78.023
sub_test {
  test1: 12
  test2: "zxcvbnm"
}
sub_test {
  test1: 34
  test2: "asdfghjkl"
}

猜你喜欢

转载自www.cnblogs.com/wanxuexiang/p/10943433.html