1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-18 17:31:57 +08:00
Files
CMake/Tests/FindProtobuf/Test/main-desc.cxx
Kitware Robot 0b96ae1f6a Revise C++ coding style using clang-format with "east const"
Run the `clang-format.bash` script to update all our C and C++ code to a
new style defined by `.clang-format`, now with "east const" enforcement.
Use `clang-format` version 18.

* If you reached this commit for a line in `git blame`, re-run the blame
  operation starting at the parent of this commit to see older history
  for the content.

* See the parent commit for instructions to rebase a change across this
  style transition commit.

Issue: #26123
2025-01-23 13:09:50 -05:00

59 lines
1.6 KiB
C++

#include <fstream>
#include <google/protobuf/descriptor.pb.h>
#include <iostream>
#include <string>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/dynamic_message.h>
#include <google/protobuf/text_format.h>
int main(int argc, char* argv[])
{
std::ifstream fs;
fs.open(argv[1], std::ifstream::in);
google::protobuf::FileDescriptorSet file_descriptor_set;
file_descriptor_set.ParseFromIstream(&fs);
google::protobuf::DescriptorPool const* compiled_pool =
google::protobuf::DescriptorPool::generated_pool();
if (compiled_pool == NULL) {
std::cerr << "compiled pool is NULL." << std::endl;
return 1;
}
google::protobuf::DescriptorPool pool(compiled_pool);
google::protobuf::DynamicMessageFactory dynamic_message_factory(&pool);
for (google::protobuf::FileDescriptorProto const& file_descriptor_proto :
file_descriptor_set.file()) {
google::protobuf::FileDescriptor const* file_descriptor =
pool.BuildFile(file_descriptor_proto);
if (file_descriptor == NULL) {
continue;
}
google::protobuf::Descriptor const* descriptor =
pool.FindMessageTypeByName("example.msgs.ExampleDesc");
if (descriptor == NULL) {
continue;
}
google::protobuf::Message* msg =
dynamic_message_factory.GetPrototype(descriptor)->New();
std::string data = "data: 1";
bool success = google::protobuf::TextFormat::ParseFromString(data, msg);
if (success) {
return 0;
} else {
std::cerr << "Failed to parse message." << std::endl;
return 2;
}
}
std::cerr << "No matching message found." << std::endl;
return 3;
}