注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

liangxh2008的博客

 
 
 

日志

 
 

protobuf示例  

2010-04-05 17:42:18|  分类: AppMarket |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

protobuf示例

关键字: protobuf
1, 下载compiler和源代码
http://code.google.com/p/protobuf/downloads/

build protobuf:
C++代码 复制代码
  1. ./configure   
  2. make   
  3. make check   
  4. make install  
./configure  make  make check  make install  


2, 创建一个addressbook.proto
C++代码 复制代码
  1. package tutorial;   
  2.   
  3. message Person {   
  4.   required string name = 1;   
  5.   required int32 id = 2;   
  6.   optional string email = 3;   
  7.      
  8.   enum PhoneType {   
  9.     MOBILE = 0;   
  10.     HOME = 1;   
  11.     WORK = 2;   
  12.   }   
  13.      
  14.   message PhoneNumber {   
  15.     required string number = 1;   
  16.     optional PhoneType type = 2 [default = HOME];   
  17.   }   
  18.      
  19.   repeated PhoneNumber phone = 4;    
  20. }   
  21.   
  22. message AddressBook {   
  23.   repeated Person person = 1;   
  24. }  
package tutorial;    message Person {    required string name = 1;    required int32 id = 2;    optional string email = 3;        enum PhoneType {      MOBILE = 0;      HOME = 1;      WORK = 2;    }        message PhoneNumber {      required string number = 1;      optional PhoneType type = 2 [default = HOME];    }        repeated PhoneNumber phone = 4;   }    message AddressBook {    repeated Person person = 1;  }  


3, 生成C++的stub
C++代码 复制代码
  1. protoc --cpp_out=. ./addressbook.proto  
protoc --cpp_out=. ./addressbook.proto  

运行上面的命令将生成addressbook.pb.h和addressbook.pb.cc

4, 写
C++代码 复制代码
  1. // write.cc   
  2. #include <iostream>   
  3. #include <fstream>   
  4. #include <string>   
  5. #include "addressbook.pb.h"   
  6. using namespace std;   
  7.   
  8. void PromptForAddress(tutorial::Person* person) {   
  9.     cout << "Enter person ID number: ";   
  10.     int id;   
  11.     cin >> id;   
  12.     person->set_id(id);   
  13.     cin.ignore(256, '\n');   
  14.   
  15.     cout << "Enter name: ";   
  16.     getline(cin, *person->mutable_name());   
  17.   
  18.     cout << "Enter email address (blank for none): ";   
  19.     string email;   
  20.     getline(cin, email);   
  21.     if (!email.empty()) {   
  22.         person->set_email(email);   
  23.     }   
  24.   
  25.     while(true) {   
  26.         cout << "Enter a phone number (or leave blank to finish):";   
  27.         string number;   
  28.         getline(cin, number);   
  29.         if (number.empty()) {   
  30.             break;   
  31.         }   
  32.   
  33.         tutorial::Person::PhoneNumber *phone_number = person->add_phone();   
  34.         phone_number->set_number(number);   
  35.   
  36.         cout << "Is this a mobile, home, or work phone?";   
  37.         string type;   
  38.         getline(cin, type);   
  39.         if (type == "mobile") {   
  40.             phone_number->set_type(tutorial::Person::MOBILE);   
  41.         } else if (type == "home") {   
  42.             phone_number->set_type(tutorial::Person::HOME);   
  43.         } else {   
  44.             cout << "Unknown phone type. Using default." << endl;   
  45.         }   
  46.     }   
  47. }   
  48.   
  49. int main(int argc, char* argv[]) {   
  50.     GOOGLE_PROTOBUF_VERIFY_VERSION;   
  51.   
  52.     if(argc != 2) {   
  53.         cerr << "Usage: " << argv[0] << " ADDRESS_BOOK_FILE" << endl;   
  54.         return -1;   
  55.     }   
  56.   
  57.     tutorial::AddressBook address_book;   
  58.   
  59.     {   
  60.         fstream input(argv[1], ios::in | ios::binary);   
  61.         if (!input) {   
  62.             cout << argv[1] << ": File not found. Creating a new file." << endl;   
  63.         } else if (!address_book.ParseFromIstream(&input)) {   
  64.             cerr << "Failed to parse address book." << endl;   
  65.             return -1;   
  66.         }   
  67.     }   
  68.   
  69.     PromptForAddress(address_book.add_person());   
  70.     {   
  71.         fstream output(argv[1], ios::out | ios::trunc | ios::binary);   
  72.         if (!address_book.SerializeToOstream(&output)) {   
  73.             cerr << "Failed to write address book." << endl;   
  74.             return -1;   
  75.         }   
  76.     }   
  77.   
  78.     google::protobuf::ShutdownProtobufLibrary();   
  79.   
  80.     return 0;   
  81. }  
// write.cc  #include <iostream>  #include <fstream>  #include <string>  #include "addressbook.pb.h"  using namespace std;    void PromptForAddress(tutorial::Person* person) {   cout << "Enter person ID number: ";   int id;   cin >> id;   person->set_id(id);   cin.ignore(256, '\n');     cout << "Enter name: ";   getline(cin, *person->mutable_name());     cout << "Enter email address (blank for none): ";   string email;   getline(cin, email);   if (!email.empty()) {    person->set_email(email);   }     while(true) {    cout << "Enter a phone number (or leave blank to finish):";    string number;    getline(cin, number);    if (number.empty()) {     break;    }      tutorial::Person::PhoneNumber *phone_number = person->add_phone();    phone_number->set_number(number);      cout << "Is this a mobile, home, or work phone?";    string type;    getline(cin, type);    if (type == "mobile") {     phone_number->set_type(tutorial::Person::MOBILE);    } else if (type == "home") {     phone_number->set_type(tutorial::Person::HOME);    } else {     cout << "Unknown phone type. Using default." << endl;    }   }  }    int main(int argc, char* argv[]) {   GOOGLE_PROTOBUF_VERIFY_VERSION;     if(argc != 2) {    cerr << "Usage: " << argv[0] << " ADDRESS_BOOK_FILE" << endl;    return -1;   }     tutorial::AddressBook address_book;     {    fstream input(argv[1], ios::in | ios::binary);    if (!input) {     cout << argv[1] << ": File not found. Creating a new file." << endl;    } else if (!address_book.ParseFromIstream(&input)) {     cerr << "Failed to parse address book." << endl;     return -1;    }   }     PromptForAddress(address_book.add_person());   {    fstream output(argv[1], ios::out | ios::trunc | ios::binary);    if (!address_book.SerializeToOstream(&output)) {     cerr << "Failed to write address book." << endl;     return -1;    }   }     google::protobuf::ShutdownProtobufLibrary();     return 0;  }  

编译生成write.exe
注意:
需要将protobuf的源码以及lib加到编译的PATH中

向文件写数据:
C++代码 复制代码
  1. write ADDRESS_BOOK_FILE   
  2.   
  3. Enter person ID number: 123   
  4. Enter name: Hideto   
  5. Enter email address (blank for none): hideto.bj@gmail.com   
  6. Enter a phone number (or leave blank to finish):159xxxxxxxx   
  7. Is this a mobile, home, or work phone?mobile   
  8. Enter a phone number (or leave blank to finish):   
  9.   
  10. write ADDRESS_BOOK_FILE   
  11. // ....  
write ADDRESS_BOOK_FILE    Enter person ID number: 123  Enter name: Hideto  Enter email address (blank for none): hideto.bj@gmail.com  Enter a phone number (or leave blank to finish):159xxxxxxxx  Is this a mobile, home, or work phone?mobile  Enter a phone number (or leave blank to finish):    write ADDRESS_BOOK_FILE  // ....  


5, 读
C++代码 复制代码
  1. // read.cc   
  2. #include <iostream>   
  3. #include <fstream>   
  4. #include <string>   
  5. #include "addressbook.pb.h"   
  6. using namespace std;   
  7.   
  8. void ListPeople(const tutorial::AddressBook& address_book) {   
  9.     for (int i = 0; i < address_book.person_size(); i++) {   
  10.         const tutorial::Person& person = address_book.person(i);   
  11.   
  12.         cout << "Person ID: " << person.id() << endl;   
  13.         cout << "Name: " << person.name() << endl;   
  14.         if(person.has_email()) {   
  15.             cout << "E-mail address: " << person.email() << endl;   
  16.         }   
  17.   
  18.         for (int j = 0; j < person.phone_size(); j++) {   
  19.             const tutorial::Person::PhoneNumber& phone_number = person.phone(j);   
  20.   
  21.             switch (phone_number.type()) {   
  22.                 case tutorial::Person::MOBILE:   
  23.                     cout << "Mobile phone #: ";   
  24.                     break;   
  25.                 case tutorial::Person::HOME:   
  26.                     cout << "Home phone #: ";   
  27.                     break;   
  28.                 case tutorial::Person::WORK:   
  29.                     cout << "Work phone #: ";   
  30.                     break;   
  31.             }   
  32.             cout << phone_number.number() << endl;   
  33.         }   
  34.     }   
  35. }   
  36.   
  37. int main(int argc, char* argv[]) {   
  38.     GOOGLE_PROTOBUF_VERIFY_VERSION;   
  39.   
  40.     if (argc != 2) {   
  41.         cerr << "Usage: " << argv[0] << "ADDRESS_BOOK_FILE" << endl;   
  42.         return -1;   
  43.     }   
  44.   
  45.     tutorial::AddressBook address_book;   
  46.     {   
  47.         fstream input(argv[1], ios::in | ios::binary);   
  48.         if (!address_book.ParseFromIstream(&input)) {   
  49.             cerr << "Failed to parse address book." << endl;   
  50.             return -1;   
  51.         }   
  52.     }   
  53.   
  54.     ListPeople(address_book);   
  55.   
  56.     google::protobuf::ShutdownProtobufLibrary();   
  57.   
  58.     return 0;   
  59. }  
// read.cc  #include <iostream>  #include <fstream>  #include <string>  #include "addressbook.pb.h"  using namespace std;    void ListPeople(const tutorial::AddressBook& address_book) {      for (int i = 0; i < address_book.person_size(); i++) {          const tutorial::Person& person = address_book.person(i);            cout << "Person ID: " << person.id() << endl;          cout << "Name: " << person.name() << endl;          if(person.has_email()) {              cout << "E-mail address: " << person.email() << endl;          }            for (int j = 0; j < person.phone_size(); j++) {              const tutorial::Person::PhoneNumber& phone_number = person.phone(j);                switch (phone_number.type()) {                  case tutorial::Person::MOBILE:                      cout << "Mobile phone #: ";                      break;                  case tutorial::Person::HOME:                      cout << "Home phone #: ";                      break;                  case tutorial::Person::WORK:                      cout << "Work phone #: ";                      break;              }              cout << phone_number.number() << endl;          }      }  }    int main(int argc, char* argv[]) {      GOOGLE_PROTOBUF_VERIFY_VERSION;        if (argc != 2) {          cerr << "Usage: " << argv[0] << "ADDRESS_BOOK_FILE" << endl;          return -1;      }        tutorial::AddressBook address_book;      {          fstream input(argv[1], ios::in | ios::binary);          if (!address_book.ParseFromIstream(&input)) {              cerr << "Failed to parse address book." << endl;              return -1;          }      }        ListPeople(address_book);        google::protobuf::ShutdownProtobufLibrary();        return 0;  }  

编译生成read.exe
从文件读数据:
C++代码 复制代码
  1. read ADDRESS_BOOK_FILE   
  2.   
  3. // 以下为输出   
  4. Person ID: 123   
  5. Name: Hideto   
  6. E-mail address: hideto.bj@gmail.com   
  7. Mobile phone #: 159xxxxxxxx   
  8. Person ID: 456   
  9. Name: chuang   
  10. E-mail address: chuang@freewheel.tv   
  11. Home phone #: 22222222   
  12. Person ID: 1231231   
  13. Name: asdfadsf   
  14. E-mail address: fadsfdsf@dasfads.com   
  15. Mobile phone #: 12321321321   
  16. Home phone #: 123432432  
read ADDRESS_BOOK_FILE    // 以下为输出  Person ID: 123  Name: Hideto  E-mail address: hideto.bj@gmail.com  Mobile phone #: 159xxxxxxxx  Person ID: 456  Name: chuang  E-mail address: chuang@freewheel.tv  Home phone #: 22222222  Person ID: 1231231  Name: asdfadsf  E-mail address: fadsfdsf@dasfads.com  Mobile phone #: 12321321321  Home phone #: 123432432  


6, Ruby的protobuf库
http://code.google.com/p/ruby-protobuf/
安装:
Ruby代码 复制代码
  1. gem install ruby_protobuf  
gem install ruby_protobuf  

生成ruby的stub:
Ruby代码 复制代码
  1. rprotoc addressbook.proto  
rprotoc addressbook.proto  

从文件读数据:
Ruby代码 复制代码
  1. # read.rb   
  2. require 'addressbook.pb.rb'  
  3.   
  4. def list_people(address_book)   
  5.   address_book.person.each do | p |   
  6.     p "Person ID: #{p.id}"  
  7.     p "Name: #{p.name}"  
  8.     p "E-mail: #{p.email}" unless p.email.empty?   
  9.     p.phone.each do |phone|   
  10.       case phone.type   
  11.       when Tutorial::Person::PhoneType::MOBILE   
  12.         print "Mobile phone #: "  
  13.       when Tutorial::Person::PhoneType::HOME   
  14.         print "Home phone #: "  
  15.       when Tutorial::Person::PhoneType::WORK   
  16.         print "Work phone #: "  
  17.       end  
  18.       p phone.number   
  19.     end  
  20.   end  
  21. end  
  22.   
  23. address_book = Tutorial::AddressBook.new  
  24. address_book.parse_from_file ARGV[0]   
  25.   
  26. list_people address_book  
# read.rb  require 'addressbook.pb.rb'    def list_people(address_book)    address_book.person.each do | p |      p "Person ID: #{p.id}"      p "Name: #{p.name}"      p "E-mail: #{p.email}" unless p.email.empty?      p.phone.each do |phone|        case phone.type        when Tutorial::Person::PhoneType::MOBILE          print "Mobile phone #: "        when Tutorial::Person::PhoneType::HOME          print "Home phone #: "        when Tutorial::Person::PhoneType::WORK          print "Work phone #: "        end        p phone.number      end    end  end    http://hideto.javaeye.com/blog/440642
 
address_book = Tutorial::AddressBook.new  address_book.parse_from_file ARGV[0]    list_people address_book  

运行:
Ruby代码 复制代码
  1. ruby read.rb 'D:\cpp-projects\pb\bin\Release\ADDRESS_BOOK_FILE'  
  2.   
  3. // 输出   
  4. "Person ID: 123"  
  5. "Name: Hideto"  
  6. "E-mail: hideto.bj@gmail.com"  
  7. Mobile phone #: "159xxxxxxxx"   
  8. "Person ID: 456"  
  9. "Name: chuang"  
  10. "E-mail: chuang@freewheel.tv"  
  11. Home phone #: "22222222"   
  12. "Person ID: 1231231"  
  13. "Name: asdfadsf"  
  14. "E-mail: fadsfdsf@dasfads.com"  
  15. Mobile phone #: "12321321321"   
  16. Home phone #: "123432432"  
ruby read.rb 'D:\cpp-projects\pb\bin\Release\ADDRESS_BOOK_FILE'    // 输出  "Person ID: 123"  "Name: Hideto"  "E-mail: hideto.bj@gmail.com"  Mobile phone #: "159xxxxxxxx"  "Person ID: 456"  "Name: chuang"  "E-mail: chuang@freewheel.tv"  Home phone #: "22222222"  "Person ID: 1231231"  "Name: asdfadsf"  "E-mail: fadsfdsf@dasfads.com"  Mobile phone #: "12321321321"  Home phone #: "123432432"  
  评论这张
 
阅读(2944)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017