• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            woaidongmao

            文章均收錄自他人博客,但不喜標題前加-[轉貼],因其丑陋,見諒!~
            隨筆 - 1469, 文章 - 0, 評論 - 661, 引用 - 0
            數據加載中……

            在C++中使用Google的“Protocol Buffers”

            如果您已經初步了解了Protocol Buffers并打算試著使用它,本文符合您的胃口。如果您剛聽說Protocol Buffers,請先到本文末尾的附錄區看一看。

            1.下載并安裝Protocol Buffers

            可以從官方下載源碼包,linux下和Solaris下的安裝直接見源碼包中的“README.txt”。這里詳細說下Windows下的安裝,源碼包里有一個“vsprojects”目錄,其中就是vs的工程文件和解決方案。用vs(版本得高點)打開“protobuf.sln”解決方案,編譯。其中包括四個工程 libprotobuf(接口dll庫)、libprotoc(轉換器的實現庫)、protocwindows下轉換器的實現)、tests(使用gTest庫進行的測試)。編譯好之后在Debug目錄下可以找到“libprotobuf.dlllibprotobuf.lib”,這個是我們的程序要使用的動態鏈接庫和導入庫。“libprotoc.dlllibprotoc.lib”,這個是完成.proto文件到cppjavapython格式數據轉換的庫。“protoc.exe”,這個是windows下轉換程序(它使用了剛才的libprotoc),這個程序的靜態鏈接版本也在此項目老家提供下載。

            2.設置編譯環境

                linux下,只要將Protocol Buffers源碼包安裝到系統即可開始使用。而windows下需要設置一下編譯環境,將“src”目錄加入到編譯器的頭文件搜索路徑,將“vsprojects\Debug”目錄加入到編譯器的lib搜索路徑中。為了更方便的在windows命令行下使用protoc.exe轉換程序,可以將“vsprojects\Debug”目錄添加到系統PATH變量中。

            3.編寫.proto數據描述文件

                這里仿照源碼中例子,寫出“addressbook.proto”文件。內容如下:

            --code begin--

            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;

            }

            --code end--

            4.使用protoc(windows下是protoc.exe)生成c++頭文件及類文件。

                protoc.exe --cpp_out=./ addressbook.proto

            如果沒有錯誤,程序將沒有任何輸出。并且當前目錄下多出兩個文件“addressbook.pb.h”“addressbook.pb.cc”

            5.編寫C++程序使用它們

                新建vs工程,除了設置以上的頭文件搜索路徑和庫文件搜索路徑外,還要鏈接到庫“libprotobuf.lib”。將4步生成的一個.h文件和一個.cpp文件添加并拷貝到工程里,由于vs的特性(需要預編譯頭),所以在addressbook.pb.cc開頭添加“#include "stdafx.h"”,主代碼如下,然后編譯。這個演示程序需要一個參數用于指定數據文件文件名,第一次運行,會生成這個數據文件。它會先讓用戶輸入一條通訊錄信息并添加進數據文件,然后再顯示出指定的數據文件中所有的數據。注意,為了使DEMO程序可以運行,別忘了拷貝“vsprojects\Debug”目錄下的動態鏈接庫“libprotobuf.dll”到當前目錄。

            --code begin--

            // testprotocolbuffer.cpp : 定義控制臺應用程序的入口點。

            //

             

            #include "stdafx.h"

             

            // See README.txt for information and build instructions.

             

            #include <iostream>

            #include <fstream>

            #include <string>

            #include "addressbook.pb.h"

             

            using namespace std;

             

            // This function fills in a Person message based on user input.

            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 if (type == "work") {

                  phone_number->set_type(tutorial::Person::WORK);

                } else {

                  cout << "Unknown phone type.  Using default." << endl;

                }

              }

            }

             

            // Iterates though all people in the AddressBook and prints info about them.

            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;

                }

              }

            }

             

            // Main function:  Reads the entire address book from a file,

            //   adds one person based on user input, then writes it back out to the same

            //   file.

            int main(int argc, char* argv[]) {

              // Verify that the version of the library that we linked against is

              // compatible with the version of the headers we compiled against.

              GOOGLE_PROTOBUF_VERIFY_VERSION;

             

              if (argc != 2) {

                cerr << "使用方法:  " << argv[0] << " 想要生成的存儲數據的文件" << endl;

                return -1;

              }

             

              tutorial::AddressBook address_book;

             

              {

                // Read the existing address book.

                fstream input(argv[1], ios::in | ios::binary);

                if (!input) {

                  cout << argv[1] << ": 指定的文件沒找到,創建一個新文件." << endl;

                } else if (!address_book.ParseFromIstream(&input)) {

                  cerr << "解析addressbook數據文件失敗。" << endl;

                  return -1;

                }

              }

             

              // Add an address.

              PromptForAddress(address_book.add_person());

             

              {

                // Write the new address book back to disk.

                fstream output(argv[1], ios::out | ios::trunc | ios::binary);

                if (!address_book.SerializeToOstream(&output)) {

                  cerr << "寫入文件失敗。" << endl;

                  return -1;

                }

              }

             

              //再從文件中讀取剛才那個數據

              tutorial::AddressBook address_book2;

             

              {

                // Read the existing address book.

                fstream input(argv[1], ios::in | ios::binary);

                if (!address_book2.ParseFromIstream(&input)) {

                  cerr << "解析文件失敗。" << endl;

                  return -1;

                }

              }

             

              ListPeople(address_book2);

             

              return 0;

            }

            --code end--

            運行方式和結果如下圖:

            clip_image001

            備注:寫這東西好累啊。。。

            =====================附錄====================

            1.Protocol BuffersGoogle自己的一種數據交換格式。其簡介可以參考文章谷歌發布內部數據語言 比XML快近100

            2.如果想要了解Protocol Buffers相關的詳細信息,請訪問它的老家Protocol Buffers

             

            posted on 2009-08-20 23:58 肥仔 閱讀(3545) 評論(0)  編輯 收藏 引用 所屬分類: 網絡編程

            精品熟女少妇AV免费久久| 久久精品国产一区二区三区| 久久天堂电影网| 久久亚洲精品无码VA大香大香| 一本综合久久国产二区| 麻豆av久久av盛宴av| 久久成人18免费网站| 色婷婷久久久SWAG精品| 狠狠色婷婷久久综合频道日韩| 久久久无码人妻精品无码| AV狠狠色丁香婷婷综合久久| 91久久成人免费| 无码日韩人妻精品久久蜜桃| 久久国产免费观看精品| 无码任你躁久久久久久| 亚洲精品97久久中文字幕无码| 人妻少妇精品久久| 久久天天躁狠狠躁夜夜躁2014| 伊人久久无码中文字幕| 久久夜色精品国产欧美乱| 久久婷婷五月综合成人D啪| 热re99久久6国产精品免费| 蜜臀av性久久久久蜜臀aⅴ | 伊人色综合久久| 国产亚洲精久久久久久无码AV| 精品久久久无码中文字幕| 亚洲人成网站999久久久综合 | 久久91精品国产91| 久久久无码精品亚洲日韩按摩| 色偷偷888欧美精品久久久| 香港aa三级久久三级老师2021国产三级精品三级在 | 国产视频久久| 久久亚洲国产精品一区二区| 久久午夜综合久久| 久久久久免费看成人影片| 久久精品不卡| 91精品国产高清久久久久久io| 亚洲乱码精品久久久久..| 一本色道久久88加勒比—综合| 久久人人爽人人爽人人片AV麻烦| 久久夜色精品国产亚洲|