I believe I get the piece about constructing a message for a specific type given a descriptor file.
About your last statement: "If person.pb contains the serialized (binary) data for your proto, you should be using one of the Message::ParseFrom* routines. " My understanding was I needed a "Person" object, before I could use the "Parse" routines and hence I went through this trail of getting a descriptor file and then constructing a Message(of type Person) and then using that to Parse. Are you saying that I do not need any descriptors etc at all. Given ANY data file (which is a serialized proto buf file) I can use Message::Parse and print it out ??? For e.g. I see that protoc --decode_raw < person.pb does indeed print it out correctly (without any descriptor OR any .proto file), so I think this may be possible. In summary all I want to do is given ANY serialized message in a file, I want to load it and print its contents out. That all. Thanks again. On Apr 2, 1:42 pm, Jason Hsueh <jas...@google.com> wrote: > On Fri, Apr 2, 2010 at 9:13 AM, Navin <ng06d...@gmail.com> wrote: > > 1. For the person example, I create a descriptor as follows: > > "E:\Desktop\prototype_MAIN\protoc.exe" person.proto -- > > descriptor_set_out=msg.protocomp > > > 2. I create a Person object and serialized it to file. "person.pb" > > > 3. I want to write a program to "print" the Person object using ONLY > > its descriptor. I know there is a way, but I cannot seem to get all > > the APIs correct. > > > 4. Here is what I have done. > > ifstream desc_file("E:\\Visual Studio 2008\\Projects\ > > \proto_buf1\\msg.protocomp",ios::in); > > if (desc_file.bad()) { > > std::cout << "desc file not read...exiting" << std::endl; > > return -1; > > } > > > FileDescriptorSet f; > > f.ParseFromIstream(&desc_file); > > f.PrintDebugString(); > > > > > FileDescriptorProto fdp = f.file(0); > > fdp.PrintDebugString(); > > > DescriptorProto dp = fdp.message_type(0); > > dp.PrintDebugString(); > > > Now what ? I need a copy of "Message" that I can pass as input to > > Parse, I think. I also may need DynamicMessage etc, and possibly also > > the following: > > Yes, you should construct a DynamicMessage. To do this, you'll need to build > the descriptor protos into a pool. For instance: > > DescriptorPool descriptor_pool; > for (int i = 0; i < f.file_size(); ++i) { > descriptor_pool.BuildFile(f.file(i));} > > const Descriptor* descriptor = descriptor_pool.FindMessageTypeByName("..."); > DynamicMessageFactory factory; > Message* my_message = factory.GetPrototype(descriptor)->New(); > ... > delete my_message; > > (You should probably check the return values of most of these calls - > BuildFile() may return NULL, for instance. You also probably want to have > some mechanism for specifying the message type, since you generally won't be > able to assume that it's the first message in the first file) > > > > > ifstream input_file("E:\\Visual Studio 2008\\Projects\ > > \proto_buf1\\Debug\\person.pb",ios::in); > > if (input_file.bad()) { > > std::cout << "file not read...exiting" << std::endl; > > return -1; > > } > > > IstreamInputStream is(&input_file); > > > TextFormat::Parse(&is,(msg)); > > If person.pb contains the serialized (binary) data for your proto, you > should be using one of the Message::ParseFrom* routines. > > > msg->PrintDebugString(); > > > Any simple example is GREATLY appreciated. Thanks. > > > -- > > You received this message because you are subscribed to the Google Groups > > "Protocol Buffers" group. > > To post to this group, send email to proto...@googlegroups.com. > > To unsubscribe from this group, send email to > > protobuf+unsubscr...@googlegroups.com<protobuf%2bunsubscr...@googlegroups.com> > > . > > For more options, visit this group at > >http://groups.google.com/group/protobuf?hl=en. -- You received this message because you are subscribed to the Google Groups "Protocol Buffers" group. To post to this group, send email to proto...@googlegroups.com. To unsubscribe from this group, send email to protobuf+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.