日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux下使用protobuf实现简单配置功能

發(fā)布時(shí)間:2024/9/30 linux 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux下使用protobuf实现简单配置功能 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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

前言:

??? 程序一般需要load一些參數(shù)列表,一般來說我們可以通過linux自帶的命令行解析函數(shù)來搞定(getopt_long,如果需要了解的man一 下,manpage里面是有example的),但是對(duì)于參數(shù)太多,我們不可能寫滿一屏幕進(jìn)行傳參吧,當(dāng)然,我們的輸入在linux里面也是有限制的。所以呢,一般的做法是load一個(gè)配置文件,進(jìn)行解析;最近在研究了一下protobuf的使用,我們都知道protobuf在網(wǎng)絡(luò)中作為協(xié)議發(fā)送比較爽,但是發(fā)現(xiàn)用protobuf來實(shí)現(xiàn)程序的配置文件管理也是一個(gè) 不錯(cuò)的思路。

?

protobuf是何物?

大名鼎鼎的google公司的開源軟件,被N多程序員推薦使用,個(gè)人認(rèn)為優(yōu)點(diǎn)是:

1:api接口很友好,這點(diǎn)很重要

2:serilize之后的包比較小,速度快,這個(gè)在網(wǎng)絡(luò)傳輸中至關(guān)重要

3:可擴(kuò)展性強(qiáng),加入和刪除字段都是透明的。這個(gè)在互聯(lián)網(wǎng)開發(fā)中,經(jīng)常變更協(xié)議的時(shí)候十分重要

不過protobuf持久化的東西是一個(gè)二進(jìn)制,基本不可讀(可以參考https://developers.google.com/protocol-buffers/docs/overview?hl=zh-CN)

不過最近看到了一個(gè)protobuf的接口google::protobuf::TextFormat,發(fā)現(xiàn)protobuf支持文本的輸出,這樣我們就能將protobuf做成一個(gè)簡單的配置管理庫了


[plain]?view plaincopy
  • 文件定義test.proto:??
  • [plain]?view plaincopy
  • message?student{??
  • ????required?string?name?=?1;???//姓名??
  • ????required?int32??age?=?2;????//年齡??
  • ????optional?string?addr?=?3;??
  • }??
  • ??
  • //班級(jí)??
  • message?class{??
  • ????required?string?name?=?1;???//班級(jí)名稱??
  • ????repeated?student?member?=?2;????//班級(jí)成員??
  • protoc --cpp_out=.test.proto? 可以生成test.pb.htest.pb.cc這兩個(gè)文件


    在寫程序運(yùn)行之前,我們直觀的看一下protobuf的TextFormat格式是什么樣子的吧:

    [plain]?view plaincopy
  • name:?"Communication?2004"??
  • member?{??
  • ??name:?"flyan338"??
  • ??age:?26??
  • ??addr:?"china"??
  • }??
  • member?{??
  • ??name:?"likeliu"??
  • ??age:?25??
  • ??addr:?"china"??
  • }??
  • member?{??
  • ??name:?"gaoy"??
  • ??age:?24??
  • ??addr:?"American"??
  • }??
  • 這份配置表明:一個(gè)叫做 "Communication 2004"的班級(jí),有3個(gè)student,你可以直接用protobuf來load出來

    這份文件怎么生成的呢?代碼如下:

    [cpp]?view plaincopy
  • #include?<test.pb.h>??
  • #include?<stdlib.h>??
  • #include?<stdio.h>??
  • #include?<string.h>??
  • #include?<iostream>??
  • #include?<fcntl.h>??
  • #include?<fstream>??
  • #include?<cstdio>??
  • #include?<google/protobuf/text_format.h>??
  • #include?<google/protobuf/io/zero_copy_stream_impl.h>??
  • ??
  • using?namespace?std;??
  • int?main(int?argc,?char**?argv){??
  • ????if?(argc?<2){??
  • ????????printf("programe?savefile\n");??
  • ????????exit(1);??
  • ????}??
  • ??
  • ????//聲明一個(gè)class結(jié)構(gòu)??
  • ????classes?c;??
  • ????c.set_name("Communication?2004");??
  • ????//添加學(xué)生??
  • ????student*?t?=?c.add_member();??
  • ????t->set_name("flyan338");??
  • ????t->set_age(26);??
  • ????t->set_addr("china");??
  • ??????
  • ????t?=?c.add_member();??
  • ????t->set_name("likeliu");??
  • ????t->set_age(25);??
  • ????t->set_addr("china");??
  • ???
  • ????t?=?c.add_member();??
  • ????t->set_name("gaoy");??
  • ????t->set_age(24);??
  • ????t->set_addr("American");??
  • ??
  • ??
  • ????//首先將protobuf輸出到一個(gè)string中??
  • ????std::string?p;??
  • ????google::protobuf::TextFormat::PrintToString(c,&p);??
  • ??????
  • ????//輸出到文件中??
  • ????ofstream?fout;??
  • ????fout.open(argv[1],?ios::out|?ios_base::ate);??
  • ????if?(!fout.is_open()){??
  • ????????fprintf(stderr,?"open?%s?fail\n",?argv[1]);??
  • ????????return?-1;??
  • ????}??
  • ????fout?<<p<<endl;??
  • ????fout.flush();??
  • ????fout.close();??
  • ??
  • ????return?0;??
  • }??

  • 只是簡單的進(jìn)行一些set操作,就可以生成這樣的配置文件

    ?

    解析的代碼更簡單:

    [cpp]?view plaincopy
  • #include?<test.pb.h>??
  • #include?<stdlib.h>??
  • #include?<stdio.h>??
  • #include?<string.h>??
  • #include?<iostream>??
  • #include?<fcntl.h>??
  • #include?<fstream>??
  • #include?<cstdio>??
  • #include?<google/protobuf/text_format.h>??
  • #include?<google/protobuf/io/zero_copy_stream_impl.h>??
  • ??
  • using?namespace?std;??
  • int?main(int?argc,?char**?argv){??
  • ????if?(argc?<2){??
  • ????????printf("programe?reloadfile\n");??
  • ????????exit(1);??
  • ????}??
  • ??
  • ????classes?c;??
  • ????int?fileDescriptor?=?open(argv[1],?O_RDONLY);??
  • ????if(?fileDescriptor?<?0?){??
  • ????????return?-1;??
  • ????}??
  • ????google::protobuf::io::FileInputStream?fileInput(fileDescriptor);??
  • ????fileInput.SetCloseOnDelete(?true?);??
  • ????if?(!google::protobuf::TextFormat::Parse(&fileInput,?&c)){??
  • ????????return?-2;??
  • ????}??
  • ????cout<<"classes?name:"?<<c.name()?<<endl;??
  • ????cout<<"student?number:"<<c.member_size()<<endl;??
  • ????for?(int?i?=?0?;?i?<?c.member_size();?i++){??
  • ????????cout?<<"student?name:"<<c.member(i).name()<<endl;??
  • ????????cout?<<"student?age:"?<<?c.member(i).age()<<endl;??
  • ????????cout?<<"student?addr:"?<<?c.member(i).addr()?<<endl;??
  • ????}??
  • ????return?0;??
  • }??


  • 程序運(yùn)行的截圖為:


    這樣一個(gè)基于protobuf的配置搞定:

    以后程序員可以自定義一個(gè)proto文件,然后要么按照特定的格式填寫save_file,要么直接寫程序生成一個(gè)save_file,

    有了這個(gè)save_file,只需要load一下就搞定了,然后可以很方便的使用protobuf內(nèi)置的各種函數(shù),個(gè)人覺得是個(gè)不錯(cuò)的選擇



    總結(jié)

    以上是生活随笔為你收集整理的linux下使用protobuf实现简单配置功能的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。