This repository has been archived by the owner on Sep 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpb_message_factory.cc
58 lines (45 loc) · 1.62 KB
/
pb_message_factory.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright (C) 2012 by wubenqi
// Distributable under the terms of either the Apache License (Version 2.0) or
// the GNU Lesser General Public License, as specified in the COPYING file.
//
// By: wubenqi<[email protected]>
//
#include "zsniffer/pb_message_factory.h"
void PBMessageFactory::MapPath(const std::string& virtual_path, const std::string& disk_path) {
disk_tree_.MapPath(virtual_path, disk_path);
}
void PBMessageFactory::MapPaths(const std::vector<std::string>& paths) {
for(size_t i = 0; i < paths.size(); i++) {
disk_tree_.MapPath("", paths[i]);
}
}
google::protobuf::Message* PBMessageFactory::CreateMessage(const std::string& proto_file, const std::string& type_name) {
const google::protobuf::FileDescriptor* f = importer_.Import(proto_file);
if(f == NULL)
return NULL;
const google::protobuf::Descriptor* d = f->FindMessageTypeByName(type_name);
if(d == NULL)
return NULL;
google::protobuf::Message* msg = message_factory_.GetPrototype(d)->New();
return msg;
}
/*
{
PBMessageFactory message_factory;
std::vector<std::string> map_paths;
xml_ini.GetStrings("MapPath", "Path", map_paths);
message_factory.MapPaths(map_paths);
scoped_ptr<google::protobuf::Message> message(message_factory.CreateMessage(pb_file, pb_name));
std::ifstream ifs;
ifs.open(pb_data.c_str(), std::ios_base::in | std::ios_base::binary);
if (!ifs.good()) {
LOG(ERROR) << "open data file " << pb_data << " error!";
return 0;
}
if (!message->ParseFromIstream(&ifs)) {
LOG(ERROR) << "Parse proto data file " << pb_data << " error!";
return 0;
}
std::cout << base::UTF8ToNativeMB(message->Utf8DebugString()) << std::endl;
}
*/