Hi Brett,

Sorry it took me so long to respond.

I've attached a .cpp and and a .proto file. Just put these two files under 
the same directory, then compile and run parse.cpp.

After this point the implementation will depend on what you want to achieve.



On Wednesday, June 12, 2019 at 3:06:58 PM UTC+3, Brett Smith wrote:
>
> Would you happen to have example code on how to do this? I've looked at 
> the reference pages and am still confused on how to implement this. 
>
> On Tuesday, June 11, 2019 at 4:23:22 PM UTC-4, Ahmet Can ÜN wrote:
>>
>> With google::protobuf::compiler::Importer class you can parse the proto 
>> file at run time and get 
>> the filedescriptors.
>>
>> You can even encode/decode proto messages at runtime without compiling 
>> .proto files.
>>
>>
>> https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.compiler.importer#Importer
>>
>>
>> https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.descriptor#FileDescriptor
>>
>> On Tuesday, June 11, 2019 at 4:43:49 PM UTC+3, Brett Smith wrote:
>>>
>>> Hello all, I'm brand new to protobufs and I've been following the 
>>> AddressBook example given by Google (
>>> https://developers.google.com/protocol-buffers/docs/cpptutorial). I've 
>>> been able to read and write protobufs but I've been wondering if it is 
>>> possible to parse a proto file without  compiling it beforehand and if you 
>>> are still able to access the elements of that file. If it is possible, 
>>> would someone be able to give me some assistance on how to do this? I have 
>>> attached the proto file that I have been working with for reference. 
>>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/protobuf.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/protobuf/e83269fa-a12c-47a5-a84b-a4409d1c27de%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
#include <google/protobuf/compiler/importer.h>
#include <google/protobuf/descriptor.h>
#include <iostream>
#include <string>

using namespace google::protobuf;
using namespace google::protobuf::compiler;


class ErrorPrinter : public google::protobuf::compiler::MultiFileErrorCollector {
 public:
//    ErrorPrinter() {}

  void AddError(const std::string& filename, int line, int column,
                const std::string& message) override {
    std::cerr << "error " << filename << " " << line << " " << column << " "
        << message << '\n';
  }

  void AddWarning(const std::string& filename, int line, int column,
                  const std::string& message) override {
    std::cerr  << "warning " << filename << " " << line << " " << column << " "
              << message << '\n';
  }

};



int main()
{
	try {
		DiskSourceTree diskSourceTree;
		diskSourceTree.MapPath("", "/usr/include"); // to able to import google/protobuf/timestamp.proto
		diskSourceTree.MapPath("", "."); // sample.proto is in the current directory.

		ErrorPrinter errPrinter;

		Importer importer(&diskSourceTree, &errPrinter);  // errPrinter not neeeded, can pass it nullptr

		const FileDescriptor *fileDesc = importer.Import("sample.proto"); // Proto file is parsed.

		if (!fileDesc) {
			std::cerr << "importing proto file failed. Inherit from MultiFileErrorCollector class to find out more..\n";
		}
	}
	catch (const std::exception &e) {
		std::cerr << "exception: " << e.what() << '\n';
	}
}

Attachment: sample.proto
Description: Binary data

Reply via email to