LCOV - code coverage report
Current view: top level - src/outputs/formatters - InputFileFormatter.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: 419b9d Lines: 69 88 78.4 %
Date: 2025-08-08 20:01:16 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //* This file is part of the MOOSE framework
       2             : //* https://mooseframework.inl.gov
       3             : //*
       4             : //* All rights reserved, see COPYRIGHT for full restrictions
       5             : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
       6             : //*
       7             : //* Licensed under LGPL 2.1, please see LICENSE for details
       8             : //* https://www.gnu.org/licenses/lgpl-2.1.html
       9             : 
      10             : #include "InputFileFormatter.h"
      11             : #include "MooseUtils.h"
      12             : #include "InputParameters.h"
      13             : 
      14             : #include <sstream>
      15             : #include <vector>
      16             : #include <iomanip>
      17             : 
      18       69008 : InputFileFormatter::InputFileFormatter(bool dump_mode) : SyntaxTree(), _dump_mode(dump_mode) {}
      19             : 
      20             : std::string
      21      882637 : InputFileFormatter::printBlockOpen(const std::string & name,
      22             :                                    short depth,
      23             :                                    const std::string & /*doc*/)
      24             : {
      25      882637 :   std::string indent(depth * 2, ' ');
      26      882637 :   std::string opening_string;
      27             : 
      28      882637 :   if (depth)
      29      408222 :     opening_string = "./";
      30             : 
      31     2647911 :   return std::string("\n") + indent + "[" + opening_string + name + "]\n";
      32      882637 : }
      33             : 
      34             : std::string
      35      882637 : InputFileFormatter::printBlockClose(const std::string & /*name*/, short depth) const
      36             : {
      37      882637 :   std::string indent(depth * 2, ' ');
      38      882637 :   std::string closing_string;
      39             : 
      40      882637 :   if (depth)
      41      408222 :     closing_string = "../";
      42             : 
      43     2647911 :   return std::string("") + indent + "[" + closing_string + "]\n";
      44      882637 : }
      45             : 
      46             : std::string
      47     5465439 : InputFileFormatter::printParams(const std::string & /*prefix*/,
      48             :                                 const std::string & fully_qualified_name,
      49             :                                 InputParameters & params,
      50             :                                 short depth,
      51             :                                 const std::string & search_string,
      52             :                                 bool & found)
      53             : {
      54     5465439 :   std::stringstream oss;
      55             : 
      56     5465439 :   std::string quotes = "";
      57     5465439 :   std::string spacing = "";
      58     5465439 :   std::string forward = "";
      59     5465439 :   std::string backdots = "";
      60     5465439 :   int offset = 30;
      61     6475536 :   for (int i = 0; i < depth; ++i)
      62             :   {
      63     1010097 :     spacing += "  ";
      64     1010097 :     forward = ".";
      65     1010097 :     offset -= 2;
      66             :   }
      67             : 
      68   137687261 :   for (const auto & iter : params)
      69             :   {
      70             :     // We only want non-private params and params that we haven't already seen
      71   132221822 :     if (params.isPrivate(iter.first) || haveSeenIt(fully_qualified_name, iter.first))
      72   114145587 :       continue;
      73             : 
      74    22850672 :     std::string value = "INVALID";
      75    22850672 :     if (params.isParamValid(iter.first))
      76             :     {
      77             :       // Print the parameter's value to a stringstream.
      78    18211147 :       std::ostringstream toss;
      79    18211147 :       iter.second->print(toss);
      80    18211147 :       value = MooseUtils::trim(toss.str());
      81    18211147 :     }
      82     4639525 :     else if (params.hasDefaultCoupledValue(iter.first))
      83             :     {
      84         224 :       std::ostringstream toss;
      85         224 :       toss << params.defaultCoupledValue(iter.first);
      86         224 :       value = toss.str();
      87         224 :     }
      88             : 
      89             :     // See if we match the search string
      90    45701344 :     if (MooseUtils::wildCardMatch(iter.first, search_string) ||
      91    22850672 :         MooseUtils::wildCardMatch(value, search_string))
      92             :     {
      93             :       // Don't print active if it is the default all, that means it's not in the input file - unless
      94             :       // of course we are in dump mode
      95    22850672 :       if (!_dump_mode && iter.first == "active")
      96             :       {
      97     4774498 :         if (params.have_parameter<std::vector<std::string>>(iter.first))
      98             :         {
      99     4774498 :           const auto & active = params.get<std::vector<std::string>>(iter.first);
     100     4774498 :           if (active.size() == 1 && active[0] == "__all__")
     101     4774437 :             continue;
     102             :         }
     103             :       }
     104             : 
     105             :       // Mark it as "seen"
     106    18076235 :       seenIt(fully_qualified_name, iter.first);
     107             : 
     108             :       // Don't print type if it is blank
     109    18076235 :       if (iter.first == "type")
     110             :       {
     111      490803 :         if (params.have_parameter<std::string>(iter.first))
     112             :         {
     113      456284 :           const auto & active = params.get<std::string>(iter.first);
     114      456284 :           if (active == "")
     115           0 :             continue;
     116             :         }
     117             :       }
     118             : 
     119    18076235 :       found = true;
     120    18076235 :       oss << spacing << "  " << std::left << std::setw(offset) << iter.first << " = ";
     121             :       // std::setw() takes an int
     122    18076235 :       int l_offset = 30;
     123             : 
     124    18076235 :       if (!_dump_mode || value != "INVALID")
     125             :       {
     126             :         // If the value has spaces, surround it with quotes, otherwise no quotes
     127    18076235 :         if (value.find(' ') != std::string::npos)
     128             :         {
     129      349832 :           quotes = "'";
     130      349832 :           l_offset -= 2;
     131             :         }
     132             :         else
     133    17726403 :           quotes = "";
     134             : 
     135    18076235 :         if (value.size() == 0)
     136     1656737 :           value = "(no_default)";
     137    18076235 :         oss << quotes << value << quotes;
     138    18076235 :         l_offset -= value.size();
     139             :       }
     140           0 :       else if (_dump_mode && params.isParamRequired(iter.first))
     141             :       {
     142           0 :         oss << "(required)";
     143           0 :         l_offset -= 10;
     144             :       }
     145             : 
     146             :       // Documentation string
     147    18076235 :       if (_dump_mode)
     148             :       {
     149           0 :         std::vector<std::string> elements;
     150           0 :         std::string doc = params.getDocString(iter.first);
     151           0 :         if (MooseUtils::trim(doc) != "")
     152             :         {
     153           0 :           MooseUtils::tokenize(doc, elements, 68, " \t");
     154             : 
     155           0 :           for (auto & element : elements)
     156           0 :             MooseUtils::escape(element);
     157             : 
     158           0 :           oss << std::right << std::setw(l_offset) << "# " << elements[0];
     159           0 :           for (unsigned int i = 1; i < elements.size(); ++i)
     160             :             oss << " ...\n"
     161           0 :                 << "  " << std::setw(63) << "# " << elements[i];
     162             :         }
     163           0 :         const std::string group = params.getGroupName(iter.first);
     164           0 :         if (!group.empty())
     165             :         {
     166           0 :           if (MooseUtils::trim(doc) != "")
     167             :             oss << " ...\n"
     168           0 :                 << "  " << std::setw(70) << "# Group: " << group;
     169             :           else
     170           0 :             oss << std::right << std::setw(l_offset) << "# Group: " << group;
     171             :         }
     172           0 :       }
     173    18076235 :       oss << std::endl;
     174             :     }
     175    22850672 :   }
     176             : 
     177    10930878 :   return oss.str();
     178     5465439 : }

Generated by: LCOV version 1.14