生活随笔
收集整理的這篇文章主要介紹了
linux下使用protobuf实现简单配置功能
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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
文件定義test.proto:??
[plain]?view plaincopy
message?student{?? ????required?string?name?=?1;???//姓名?? ????required?int32??age?=?2;????//年齡?? ????optional?string?addr?=?3;?? }?? ?? //班級?? message?class{?? ????required?string?name?=?1;???//班級名稱?? ????repeated?student?member?=?2;????//班級成員??
protoc --cpp_out=.test.proto? 可以生成test.pb.htest.pb.cc這兩個文件
在寫程序運行之前,我們直觀的看一下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"?? }??
這份配置表明:一個叫做 "Communication 2004"的班級,有3個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);?? ????}?? ?? ?????? ????classes?c;?? ????c.set_name("Communication?2004");?? ?????? ????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");?? ?? ?? ?????? ????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;?? }??
只是簡單的進行一些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;?? }??
程序運行的截圖為:
這樣一個基于protobuf的配置搞定:
以后程序員可以自定義一個proto文件,然后要么按照特定的格式填寫save_file,要么直接寫程序生成一個save_file,
有了這個save_file,只需要load一下就搞定了,然后可以很方便的使用protobuf內置的各種函數,個人覺得是個不錯的選擇
總結
以上是生活随笔為你收集整理的linux下使用protobuf实现简单配置功能的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。