MCPcopy Create free account
hub / github.com/USBGuard/usbguard / parseFromString

Method parseFromString

src/Library/UMockdevDeviceDefinition.cpp:334–364  ·  view source on GitHub ↗

* FIXME (easy): Deduplicate the code here a bit. The parseFrom* methods could be reimplemented to * share the parsing & sorting code. The difference is only the source. Let's create * a low-level parsing method template that will generate one or the other implementation. * Another option is to deduplicate this by creating a shared implementation t

Source from the content-addressed store, hash-verified

332 * the data only from memory and handle the source name from the specialized methods.
333 */
334 std::vector<std::unique_ptr<UMockdevDeviceDefinition>> UMockdevDeviceDefinition::parseFromString(
335 const std::string& definitions_string, bool sort_by_hierarchy)
336 {
337 const std::string umockdev_name = "<string>";
338 std::vector<std::unique_ptr<UMockdevDeviceDefinition>> definitions;
339
340 try {
341 tao::pegtl::string_input<> input(definitions_string, "<string>");
342#if TAO_PEGTL_VERSION_MAJOR >= 3
343 tao::pegtl::complete_trace<UMockdevParser::grammar, UMockdevParser::actions>(input, definitions, umockdev_name);
344#else
345 tao::pegtl::parse<UMockdevParser::grammar, UMockdevParser::actions, tao::pegtl::tracer>(input, definitions, umockdev_name);
346#endif
347 }
348 catch (...) {
349 USBGUARD_LOG(Error) << "UMockdevDeviceDefinition: " << "<string>" << ": parsing failed at line <LINE>";
350 throw;
351 }
352
353 const auto lambdaSysfsPathHierarchyCompare = [](const std::unique_ptr<UMockdevDeviceDefinition>& a,
354 const std::unique_ptr<UMockdevDeviceDefinition>& b) {
355 return (a->getSysfsPath().size() < b->getSysfsPath().size() ||
356 b->getSysfsPath() <= b->getSysfsPath());
357 };
358
359 if (sort_by_hierarchy) {
360 std::sort(definitions.begin(), definitions.end(), lambdaSysfsPathHierarchyCompare);
361 }
362
363 return definitions;
364 }
365}
366
367/* vim: set ts=2 sw=2 et */

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected