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 63286 : InputFileFormatter::InputFileFormatter(bool dump_mode) : SyntaxTree(), _dump_mode(dump_mode) {} 19 : 20 : std::string 21 810028 : InputFileFormatter::printBlockOpen(const std::string & name, 22 : short depth, 23 : const std::string & /*doc*/) 24 : { 25 810028 : std::string indent(depth * 2, ' '); 26 810028 : std::string opening_string; 27 : 28 810028 : if (depth) 29 374775 : opening_string = "./"; 30 : 31 2430084 : return std::string("\n") + indent + "[" + opening_string + name + "]\n"; 32 810028 : } 33 : 34 : std::string 35 810028 : InputFileFormatter::printBlockClose(const std::string & /*name*/, short depth) const 36 : { 37 810028 : std::string indent(depth * 2, ' '); 38 810028 : std::string closing_string; 39 : 40 810028 : if (depth) 41 374775 : closing_string = "../"; 42 : 43 2430084 : return std::string("") + indent + "[" + closing_string + "]\n"; 44 810028 : } 45 : 46 : std::string 47 4979089 : 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 4979089 : std::stringstream oss; 55 : 56 4979089 : std::string quotes = ""; 57 4979089 : std::string spacing = ""; 58 4979089 : std::string forward = ""; 59 4979089 : std::string backdots = ""; 60 4979089 : int offset = 30; 61 5905494 : for (int i = 0; i < depth; ++i) 62 : { 63 926405 : spacing += " "; 64 926405 : forward = "."; 65 926405 : offset -= 2; 66 : } 67 : 68 125709726 : for (const auto & iter : params) 69 : { 70 : // We only want non-private params and params that we haven't already seen 71 120730637 : if (params.isPrivate(iter.first) || haveSeenIt(fully_qualified_name, iter.first)) 72 104112889 : continue; 73 : 74 20962767 : std::string value = "INVALID"; 75 20962767 : if (params.isParamValid(iter.first)) 76 : { 77 : // Print the parameter's value to a stringstream. 78 16725046 : std::ostringstream toss; 79 16725046 : iter.second->print(toss); 80 16725046 : value = MooseUtils::trim(toss.str()); 81 16725046 : } 82 4237721 : else if (params.hasDefaultCoupledValue(iter.first)) 83 : { 84 206 : std::ostringstream toss; 85 206 : toss << params.defaultCoupledValue(iter.first); 86 206 : value = toss.str(); 87 206 : } 88 : 89 : // See if we match the search string 90 41925534 : if (MooseUtils::wildCardMatch(iter.first, search_string) || 91 20962767 : 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 20962767 : if (!_dump_mode && iter.first == "active") 96 : { 97 4345076 : if (params.have_parameter<std::vector<std::string>>(iter.first)) 98 : { 99 4345076 : const auto & active = params.get<std::vector<std::string>>(iter.first); 100 4345076 : if (active.size() == 1 && active[0] == "__all__") 101 4345019 : continue; 102 : } 103 : } 104 : 105 : // Mark it as "seen" 106 16617748 : seenIt(fully_qualified_name, iter.first); 107 : 108 : // Don't print type if it is blank 109 16617748 : if (iter.first == "type") 110 : { 111 450557 : if (params.have_parameter<std::string>(iter.first)) 112 : { 113 418899 : const auto & active = params.get<std::string>(iter.first); 114 418899 : if (active == "") 115 0 : continue; 116 : } 117 : } 118 : 119 16617748 : found = true; 120 16617748 : oss << spacing << " " << std::left << std::setw(offset) << iter.first << " = "; 121 : // std::setw() takes an int 122 16617748 : int l_offset = 30; 123 : 124 16617748 : if (!_dump_mode || value != "INVALID") 125 : { 126 : // If the value has spaces, surround it with quotes, otherwise no quotes 127 16617748 : if (value.find(' ') != std::string::npos) 128 : { 129 321062 : quotes = "'"; 130 321062 : l_offset -= 2; 131 : } 132 : else 133 16296686 : quotes = ""; 134 : 135 16617748 : if (value.size() == 0) 136 1520820 : value = "(no_default)"; 137 16617748 : oss << quotes << value << quotes; 138 16617748 : 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 16617748 : 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 16617748 : oss << std::endl; 174 : } 175 20962767 : } 176 : 177 9958178 : return oss.str(); 178 4979089 : }