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.

Reply via email to