LCOV - code coverage report
Current view: top level - src/outputs/formatters - InputFileFormatter.C (source / functions) Hit Total Coverage
Test: idaholab/moose framework: #31730 (e8b711) with base e0c998 Lines: 69 88 78.4 %
Date: 2025-10-29 16:49:47 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       71056 : InputFileFormatter::InputFileFormatter(bool dump_mode) : SyntaxTree(), _dump_mode(dump_mode) {}
      19             : 
      20             : std::string
      21      875860 : InputFileFormatter::printBlockOpen(const std::string & name,
      22             :                                    short depth,
      23             :                                    const std::string & /*doc*/)
      24             : {
      25      875860 :   std::string indent(depth * 2, ' ');
      26      875860 :   std::string opening_string;
      27             : 
      28      875860 :   if (depth)
      29      421433 :     opening_string = "./";
      30             : 
      31     1751720 :   return std::string("\n") + indent + "[" + opening_string + name + "]\n";
      32      875860 : }
      33             : 
      34             : std::string
      35      875860 : InputFileFormatter::printBlockClose(const std::string & /*name*/, short depth) const
      36             : {
      37      875860 :   std::string indent(depth * 2, ' ');
      38      875860 :   std::string closing_string;
      39             : 
      40      875860 :   if (depth)
      41      421433 :     closing_string = "../";
      42             : 
      43     1751720 :   return std::string("") + indent + "[" + closing_string + "]\n";
      44      875860 : }
      45             : 
      46             : std::string
      47     4343221 : 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     4343221 :   std::stringstream oss;
      55             : 
      56     8686442 :   std::string quotes = "";
      57     8686442 :   std::string spacing = "";
      58     8686442 :   std::string forward = "";
      59     4343221 :   std::string backdots = "";
      60     4343221 :   int offset = 30;
      61     5384112 :   for (int i = 0; i < depth; ++i)
      62             :   {
      63     1040891 :     spacing += "  ";
      64     1040891 :     forward = ".";
      65     1040891 :     offset -= 2;
      66             :   }
      67             : 
      68   117232009 :   for (const auto & iter : params)
      69             :   {
      70             :     // We only want non-private params and params that we haven't already seen
      71   112888788 :     if (params.isPrivate(iter.first) || haveSeenIt(fully_qualified_name, iter.first))
      72    95125992 :       continue;
      73             : 
      74    21393064 :     std::string value = "INVALID";
      75    21393064 :     if (params.isParamValid(iter.first))
      76             :     {
      77             :       // Print the parameter's value to a stringstream.
      78    16745926 :       std::ostringstream toss;
      79    16745926 :       iter.second->print(toss);
      80    16745926 :       value = MooseUtils::trim(toss.str());
      81    16745926 :     }
      82     4647138 :     else if (params.hasDefaultCoupledValue(iter.first))
      83             :     {
      84         280 :       std::ostringstream toss;
      85         280 :       toss << params.defaultCoupledValue(iter.first);
      86         280 :       value = toss.str();
      87         280 :     }
      88             : 
      89             :     // See if we match the search string
      90    42786128 :     if (MooseUtils::wildCardMatch(iter.first, search_string) ||
      91    21393064 :         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    21393064 :       if (!_dump_mode && iter.first == "active")
      96             :       {
      97     3630329 :         if (params.have_parameter<std::vector<std::string>>(iter.first))
      98             :         {
      99     3630329 :           const auto & active = params.get<std::vector<std::string>>(iter.first);
     100     3630329 :           if (active.size() == 1 && active[0] == "__all__")
     101     3630268 :             continue;
     102             :         }
     103             :       }
     104             : 
     105             :       // Mark it as "seen"
     106    17762796 :       seenIt(fully_qualified_name, iter.first);
     107             : 
     108             :       // Don't print type if it is blank
     109    17762796 :       if (iter.first == "type")
     110             :       {
     111      470699 :         if (params.have_parameter<std::string>(iter.first))
     112             :         {
     113      470331 :           const auto & active = params.get<std::string>(iter.first);
     114      470331 :           if (active == "")
     115           0 :             continue;
     116             :         }
     117             :       }
     118             : 
     119    17762796 :       found = true;
     120    17762796 :       oss << spacing << "  " << std::left << std::setw(offset) << iter.first << " = ";
     121             :       // std::setw() takes an int
     122    17762796 :       int l_offset = 30;
     123             : 
     124    17762796 :       if (!_dump_mode || value != "INVALID")
     125             :       {
     126             :         // If the value has spaces, surround it with quotes, otherwise no quotes
     127    17762796 :         if (value.find(' ') != std::string::npos)
     128             :         {
     129      360454 :           quotes = "'";
     130      360454 :           l_offset -= 2;
     131             :         }
     132             :         else
     133    17402342 :           quotes = "";
     134             : 
     135    17762796 :         if (value.size() == 0)
     136     1553492 :           value = "(no_default)";
     137    17762796 :         oss << quotes << value << quotes;
     138    17762796 :         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    17762796 :       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    17762796 :       oss << std::endl;
     174             :     }
     175    21393064 :   }
     176             : 
     177     8686442 :   return oss.str();
     178     4343221 : }

Generated by: LCOV version 1.14