MCPcopy Create free account
hub / github.com/SuperElastix/elastix / ParseTomlFile

Function ParseTomlFile

Common/ParameterFileParser/itkParameterFileParser.cxx:354–427  ·  view source on GitHub ↗

Parses the specified TOML file, and returns its key-value pairs as an elastix ParameterMap.

Source from the content-addressed store, hash-verified

352
353// Parses the specified TOML file, and returns its key-value pairs as an elastix ParameterMap.
354auto
355ParseTomlFile(const std::string & fileName)
356{
357 ParameterFileParser::ParameterMapType parameterMap;
358
359 const auto fileContents = [&fileName] {
360 std::ifstream inputFileStream(fileName);
361 return std::string(std::istreambuf_iterator<char>(inputFileStream), std::istreambuf_iterator<char>());
362 }();
363
364 // Retrieves an elastix parameter value from the specified TOML node.
365 const auto getParameterValue = [&fileContents, &fileName](const toml::node & tomlNode) -> std::string {
366 // When the TOML node holds a string, just use it as it was produced by the TOML parser. (The TOML parser removes
367 // surrounding double-quotes from string values.)
368 if (const auto * const result = tomlNode.as_string())
369 {
370 return result->get();
371 }
372
373 const auto convertTomlValueToString = [](const auto & tomlValue) {
374 return elx::Conversion::ToString(tomlValue.get());
375 };
376
377 if (const auto * const result = tomlNode.as_boolean())
378 {
379 return convertTomlValueToString(*result);
380 }
381 if (const auto * const result = tomlNode.as_integer())
382 {
383 return convertTomlValueToString(*result);
384 }
385 if (const auto * const result = tomlNode.as_floating_point())
386 {
387 return convertTomlValueToString(*result);
388 }
389
390 const toml::source_position sourceRegionBegin = tomlNode.source().begin;
391 itkGenericExceptionMacro("Unsupported TOML value type `" << tomlNode.type() << "` in \"" << fileName << "\" "
392 << sourceRegionBegin << ", at the following line:\n\""
393 << GetLine(fileContents, sourceRegionBegin.line) << "\"");
394 };
395
396 try
397 {
398 for (const auto & [tomlKey, tomlNode] : toml::parse(fileContents))
399 {
400 const auto parameterName = tomlKey.str();
401 auto & parameterValues = parameterMap[std::string(parameterName)];
402
403 if (const auto tomlArray = tomlNode.as_array())
404 {
405 // An elastix parameter that may have multiple values.
406 for (const toml::node & tomlArrayElement : *tomlArray)
407 {
408 parameterValues.push_back(getParameterValue(tomlArrayElement));
409 }
410 }
411 else

Callers 1

ReadParameterFileMethod · 0.85

Calls 5

GetLineFunction · 0.85
parseFunction · 0.85
strMethod · 0.80
push_backMethod · 0.80
whatMethod · 0.80

Tested by

no test coverage detected