You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
2.1 KiB
82 lines
2.1 KiB
|
|
#include "ProtoImporter.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
class MyMultiFileErrorCollector : public google::protobuf::compiler::MultiFileErrorCollector
|
|
{
|
|
virtual void AddError(
|
|
const std::string & filename,
|
|
int line,
|
|
int column,
|
|
const std::string & message)
|
|
{
|
|
fprintf(stderr, "%s:%d:%d:%s\n", filename.c_str(), line, column, message.c_str());
|
|
}
|
|
};
|
|
|
|
static MyMultiFileErrorCollector errorCollector;
|
|
static google::protobuf::compiler::DiskSourceTree sourceTree;
|
|
|
|
ProtoImporter::ProtoImporter():
|
|
importer(&sourceTree, &errorCollector)
|
|
{
|
|
char* protopath = getenv("PROTO_PATH");
|
|
if (!protopath)
|
|
{
|
|
sourceTree.MapPath("", "");
|
|
}
|
|
else
|
|
{
|
|
sourceTree.MapPath("", protopath);
|
|
}
|
|
printf("[ProtoImporter] protopath:%s\n", protopath);
|
|
}
|
|
|
|
void ProtoImporter::MapPath(const std::string& virtual_path, const std::string& disk_path)
|
|
{
|
|
sourceTree.MapPath(virtual_path, disk_path);
|
|
}
|
|
|
|
bool ProtoImporter::Import(const std::string& filename)
|
|
{
|
|
const google::protobuf::FileDescriptor* filedescriptor = importer.Import(filename);
|
|
if (!filedescriptor)
|
|
{
|
|
fprintf(stderr, "import (%s) file descriptor error\n", filename.c_str());
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
bool ProtoImporter::Import(const std::string&filename, const void * data, int size)
|
|
{
|
|
const google::protobuf::FileDescriptor* filedescriptor = importer.Import(filename,data,size);
|
|
if (!filedescriptor)
|
|
{
|
|
fprintf(stderr, "import (%s) file descriptor by data error\n", filename.c_str());
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
void ProtoImporter::CleanCacheFile()
|
|
{
|
|
importer.cleanCacheFile();
|
|
}
|
|
void ProtoImporter::CleanMapPath()
|
|
{
|
|
sourceTree.CleanMapPath();
|
|
}
|
|
google::protobuf::Message* ProtoImporter::createDynamicMessage(const std::string& typeName)
|
|
{
|
|
google::protobuf::Message* message = NULL;
|
|
const google::protobuf::Descriptor* descriptor = importer.pool()->FindMessageTypeByName(typeName);
|
|
if (descriptor)
|
|
{
|
|
const google::protobuf::Message* prototype = factory.GetPrototype(descriptor);
|
|
if (prototype)
|
|
{
|
|
message = prototype->New();
|
|
}
|
|
}
|
|
return message;
|
|
}
|
|
|