http://blog.csdn.net/flyan338/article/details/8448518

前言:

程序一般需要load一些参数列表,一般来说我们可以通过linux自带的命令行解析函数来搞定(getopt_long,如果需要了解的man一 下,manpage里面是有example的),但是对于参数太多,我们不可能写满一屏幕进行传参吧,当然,我们的输入在linux里面也是有限制的。所以呢,一般的做法是load一个配置文件,进行解析;最近在研究了一下protobuf的使用,我们都知道protobuf在网络中作为协议发送比较爽,但是发现用protobuf来实现程序的配置文件管理也是一个 不错的思路。

protobuf是何物?

大名鼎鼎的google公司的开源软件,被N多程序员推荐使用,个人认为优点是:

1:api接口很友好,这点很重要

2:serilize之后的包比较小,速度快,这个在网络传输中至关重要

3:可扩展性强,加入和删除字段都是透明的。这个在互联网开发中,经常变更协议的时候十分重要

不过protobuf持久化的东西是一个二进制,基本不可读(可以参考https://developers.google.com/protocol-buffers/docs/overview?hl=zh-CN)

不过最近看到了一个protobuf的接口google::protobuf::TextFormat,发现protobuf支持文本的输出,这样我们就能将protobuf做成一个简单的配置管理库了

[plain] view plaincopy
  1. 文件定义test.proto:
[plain] view plaincopy
  1. message student{
  2. required string name = 1;   //姓名
  3. required int32  age = 2;    //年龄
  4. optional string addr = 3;
  5. }
  6. //班级
  7. message class{
  8. required string name = 1;   //班级名称
  9. repeated student member = 2;    //班级成员

protoc --cpp_out=.test.proto  可以生成test.pb.htest.pb.cc这两个文件

在写程序运行之前,我们直观的看一下protobuf的TextFormat格式是什么样子的吧:

[plain] view plaincopy
  1. name: "Communication 2004"
  2. member {
  3. name: "flyan338"
  4. age: 26
  5. addr: "china"
  6. }
  7. member {
  8. name: "likeliu"
  9. age: 25
  10. addr: "china"
  11. }
  12. member {
  13. name: "gaoy"
  14. age: 24
  15. addr: "American"
  16. }

这份配置表明:一个叫做 "Communication 2004"的班级,有3个student,你可以直接用protobuf来load出来

这份文件怎么生成的呢?代码如下:

[cpp] view plaincopy
  1. #include <test.pb.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <iostream>
  6. #include <fcntl.h>
  7. #include <fstream>
  8. #include <cstdio>
  9. #include <google/protobuf/text_format.h>
  10. #include <google/protobuf/io/zero_copy_stream_impl.h>
  11. using namespace std;
  12. int main(int argc, char** argv){
  13. if (argc <2){
  14. printf("programe savefile\n");
  15. exit(1);
  16. }
  17. //声明一个class结构
  18. classes c;
  19. c.set_name("Communication 2004");
  20. //添加学生
  21. student* t = c.add_member();
  22. t->set_name("flyan338");
  23. t->set_age(26);
  24. t->set_addr("china");
  25. t = c.add_member();
  26. t->set_name("likeliu");
  27. t->set_age(25);
  28. t->set_addr("china");
  29. t = c.add_member();
  30. t->set_name("gaoy");
  31. t->set_age(24);
  32. t->set_addr("American");
  33. //首先将protobuf输出到一个string中
  34. std::string p;
  35. google::protobuf::TextFormat::PrintToString(c,&p);
  36. //输出到文件中
  37. ofstream fout;
  38. fout.open(argv[1], ios::out| ios_base::ate);
  39. if (!fout.is_open()){
  40. fprintf(stderr, "open %s fail\n", argv[1]);
  41. return -1;
  42. }
  43. fout <<p<<endl;
  44. fout.flush();
  45. fout.close();
  46. return 0;
  47. }

只是简单的进行一些set操作,就可以生成这样的配置文件

解析的代码更简单:

[cpp] view plaincopy
  1. #include <test.pb.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <iostream>
  6. #include <fcntl.h>
  7. #include <fstream>
  8. #include <cstdio>
  9. #include <google/protobuf/text_format.h>
  10. #include <google/protobuf/io/zero_copy_stream_impl.h>
  11. using namespace std;
  12. int main(int argc, char** argv){
  13. if (argc <2){
  14. printf("programe reloadfile\n");
  15. exit(1);
  16. }
  17. classes c;
  18. int fileDescriptor = open(argv[1], O_RDONLY);
  19. if( fileDescriptor < 0 ){
  20. return -1;
  21. }
  22. google::protobuf::io::FileInputStream fileInput(fileDescriptor);
  23. fileInput.SetCloseOnDelete( true );
  24. if (!google::protobuf::TextFormat::Parse(&fileInput, &c)){
  25. return -2;
  26. }
  27. cout<<"classes name:" <<c.name() <<endl;
  28. cout<<"student number:"<<c.member_size()<<endl;
  29. for (int i = 0 ; i < c.member_size(); i++){
  30. cout <<"student name:"<<c.member(i).name()<<endl;
  31. cout <<"student age:" << c.member(i).age()<<endl;
  32. cout <<"student addr:" << c.member(i).addr() <<endl;
  33. }
  34. return 0;
  35. }

程序运行的截图为:

这样一个基于protobuf的配置搞定:

以后程序员可以自定义一个proto文件,然后要么按照特定的格式填写save_file,要么直接写程序生成一个save_file,

有了这个save_file,只需要load一下就搞定了,然后可以很方便的使用protobuf内置的各种函数,个人觉得是个不错的选择

linux下使用protobuf实现简单配置功能相关推荐

  1. Linux下mysql支持中文,linux下mysql环境支持中文配置步骤

    sql脚本执行前加上: CREATE DATABASE IF NOT EXISTS mydatabase DEFAULT CHARSET utf8 COLLATE UTF8_GENERAL_CI; u ...

  2. Linux下Nagios的安装与配置

    Linux下Nagios的安装与配置 2017-03-23 17:40:20     来源:    点击:0 Nagios是企业普遍使用的最具影响力的网络信息监视系统之一,它可以动态监视指定的网络状态 ...

  3. Linux 下UVCamp;V4L2技术简单介绍(二)

    通过前文Linux 下UVC&V4L2技术简单介绍(一)我们了解了UVC和V4L2的简单知识. 这里是USB设备的文档描写叙述:http://www.usb.org/developers/do ...

  4. Linux下samba的安装与配置

    physique 博客园 首页 新随笔 联系 管理 订阅 随笔- 203  文章- 0  评论- 33 Linux下samba的安装与配置 转载来源:http://blog.chinaunix.net ...

  5. linux使用flask设计网站,linux下Flask框架搭建简单网页

    开始安装FLASK需要创建一个虚拟环境,虚拟环境可以不干扰正在使用的系统环境,避免影响,并且也不需要完全的root权限,更加安全可靠. 搭建环境 Python3.4 进入到microblog目录下创建 ...

  6. linux 安装cvs,linux下cvs详细安装和配置.docx

    Linux 下cvs详细安装和配置 2009-03-18 14:37:12 标签:[推送到技术圈] 版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章原始岀处 作者信息和本声明.否则将追究 ...

  7. Linux下MySQL数据库主从同步配置

    操作系统:CentOS 6.x 64位 MySQL数据库版本:mysql-5.5.35 MySQL主服务器:192.168.21.128 MySQL从服务器:192.168.21.129 准备篇: 说 ...

  8. Linux下的tar归档及解压缩功能详解

    Linux下的tar归档及解压缩功能详解 一.Linux下解压缩工具 二.gzip工具的使用方法 三.其他解压缩工具 一.Linux下解压缩工具 二.gzip工具的使用方法 三.其他解压缩工具 一.L ...

  9. linux编写一个简单的端口扫描程序,小弟我在linux下写了个简单的多线程端口扫描程序,运行时出现有关问题,请问一下(2)...

    当前位置:我的异常网» Linux/Unix » 小弟我在linux下写了个简单的多线程端口扫描程序, 小弟我在linux下写了个简单的多线程端口扫描程序,运行时出现有关问题,请问一下(2) www. ...

最新文章

  1. 从风投看中国IT行业的发展
  2. Android新版NDK环境配置(免Cygwin)
  3. Visual C++ 2010 使用心得 和帮助文档问题
  4. 皮一皮:是不是年轻时候的你...
  5. createrepo命令安装_安装CDH6.3
  6. zabbix2.4域用户配置
  7. Devexpress VCL Build v2014 vol 14.1.4 发布
  8. mynt product model: D1000-IR-120标定相机和IMU外参之二
  9. [MyBatisPlus]Plus分页插件的配置和使用
  10. 杂七杂八(4): win10设置启动时创建系统还原点
  11. 为什么在线性模型中相互作用的变量要相乘
  12. vscode 调整行间距
  13. 深度解析卡尔曼滤波在IMU中的使用
  14. C# WPF开源控件库:Newbeecoder.UI使用指南(四)
  15. Android 9格锁屏
  16. 【Proteus仿真】555振荡电路+CD4017流水灯(频率可调)
  17. 如何降低客户流失率高的问题
  18. 知乎想做下沉,但天花板越来越明显,用户体验也大不如前!
  19. ios 企业证书申请及发布APP Mac
  20. MySQL Notes for Professionals-Stack Overflow出品

热门文章

  1. mysql分区跨机器_(转) mysql的分区技术 .
  2. ICCV 2021 | 通过显式寻找物体的extremity区域加快DETR的收敛
  3. 2021年下半年,你还可以把论文投给这 9 个国际会议
  4. CVPR 2021 | 天津大学提出PISE:形状与纹理解耦的人体图像生成与编辑方法
  5. 指哪分哪:交互式分割近期发展
  6. 岗位内推 | 微软亚洲互联网工程院自然语言处理组招聘算法研究实习生
  7. 线下沙龙 | 小身材大能量!用英伟达智能小车Jetbot玩转深度学习
  8. 本周有哪些值得读的 AI 论文?我们替你挑选了 18 篇
  9. 【Java基础】HashMap原理详解
  10. HDU1247 字典树 Hat’s Words(Tire Tree)