www.mooseframework.org
InputFileFormatter.C
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://www.mooseframework.org
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 
13 #include <sstream>
14 #include <vector>
15 #include <iomanip>
16 
17 InputFileFormatter::InputFileFormatter(bool dump_mode) : SyntaxTree(), _dump_mode(dump_mode) {}
18 
19 std::string
20 InputFileFormatter::printBlockOpen(const std::string & name,
21  short depth,
22  const std::string & /*doc*/)
23 {
24  std::string indent(depth * 2, ' ');
25  std::string opening_string;
26 
27  if (depth)
28  opening_string = "./";
29 
30  return std::string("\n") + indent + "[" + opening_string + name + "]\n";
31 }
32 
33 std::string
34 InputFileFormatter::printBlockClose(const std::string & /*name*/, short depth) const
35 {
36  std::string indent(depth * 2, ' ');
37  std::string closing_string;
38 
39  if (depth)
40  closing_string = "../";
41 
42  return std::string("") + indent + "[" + closing_string + "]\n";
43 }
44 
45 std::string
46 InputFileFormatter::printParams(const std::string & /*prefix*/,
47  const std::string & fully_qualified_name,
48  InputParameters & params,
49  short depth,
50  const std::string & search_string,
51  bool & found)
52 {
53  std::stringstream oss;
54 
55  std::string quotes = "";
56  std::string spacing = "";
57  std::string forward = "";
58  std::string backdots = "";
59  int offset = 30;
60  for (int i = 0; i < depth; ++i)
61  {
62  spacing += " ";
63  forward = ".";
64  offset -= 2;
65  }
66 
67  for (const auto & iter : params)
68  {
69  // We only want non-private params and params that we haven't already seen
70  if (params.isPrivate(iter.first) || haveSeenIt(fully_qualified_name, iter.first))
71  continue;
72 
73  std::string value = "INVALID";
74  if (params.isParamValid(iter.first))
75  {
76  // Print the parameter's value to a stringstream.
77  std::ostringstream toss;
78  iter.second->print(toss);
79  value = MooseUtils::trim(toss.str());
80  }
81  else if (params.hasDefaultCoupledValue(iter.first))
82  {
83  std::ostringstream toss;
84  toss << params.defaultCoupledValue(iter.first);
85  value = toss.str();
86  }
87 
88  // See if we match the search string
89  if (MooseUtils::wildCardMatch(iter.first, search_string) ||
90  MooseUtils::wildCardMatch(value, search_string))
91  {
92  // Don't print active if it is the default all, that means it's not in the input file - unless
93  // of course we are in dump mode
94  if (!_dump_mode && iter.first == "active")
95  {
96  libMesh::Parameters::Parameter<std::vector<std::string>> * val =
97  dynamic_cast<libMesh::Parameters::Parameter<std::vector<std::string>> *>(iter.second);
98  const std::vector<std::string> & active = val->get();
99  if (val != NULL && active.size() == 1 && active[0] == "__all__")
100  continue;
101  }
102 
103  // Mark it as "seen"
104  seenIt(fully_qualified_name, iter.first);
105 
106  // Don't print type if it is blank
107  if (iter.first == "type")
108  {
109  libMesh::Parameters::Parameter<std::string> * val =
110  dynamic_cast<libMesh::Parameters::Parameter<std::string> *>(iter.second);
111  const std::string & active = val->get();
112  if (val != NULL && active == "")
113  continue;
114  }
115 
116  found = true;
117  oss << spacing << " " << std::left << std::setw(offset) << iter.first << " = ";
118  // std::setw() takes an int
119  int l_offset = 30;
120 
121  if (!_dump_mode || value != "INVALID")
122  {
123  // If the value has spaces, surround it with quotes, otherwise no quotes
124  if (value.find(' ') != std::string::npos)
125  {
126  quotes = "'";
127  l_offset -= 2;
128  }
129  else
130  quotes = "";
131 
132  if (value.size() == 0)
133  value = "(no_default)";
134  oss << quotes << value << quotes;
135  l_offset -= value.size();
136  }
137  else if (_dump_mode && params.isParamRequired(iter.first))
138  {
139  oss << "(required)";
140  l_offset -= 10;
141  }
142 
143  // Documentation string
144  if (_dump_mode)
145  {
146  std::vector<std::string> elements;
147  std::string doc = params.getDocString(iter.first);
148  if (MooseUtils::trim(doc) != "")
149  {
150  MooseUtils::tokenize(doc, elements, 68, " \t");
151 
152  for (auto & element : elements)
153  MooseUtils::escape(element);
154 
155  oss << std::right << std::setw(l_offset) << "# " << elements[0];
156  for (unsigned int i = 1; i < elements.size(); ++i)
157  oss << " ...\n"
158  << " " << std::setw(63) << "# " << elements[i];
159  }
160  const std::string group = params.getGroupName(iter.first);
161  if (!group.empty())
162  {
163  if (MooseUtils::trim(doc) != "")
164  oss << " ...\n"
165  << " " << std::setw(70) << "# Group: " << group;
166  else
167  oss << std::right << std::setw(l_offset) << "# Group: " << group;
168  }
169  }
170  oss << "\n";
171  }
172  }
173 
174  return oss.str();
175 }
std::string indent(unsigned int spaces)
Create empty string for indenting.
Definition: ConsoleUtils.C:30
void tokenize(const std::string &str, std::vector< T > &elements, unsigned int min_len=1, const std::string &delims="/")
This function will split the passed in string on a set of delimiters appending the substrings to the ...
Definition: MooseUtils.h:532
InputFileFormatter(bool dump_mode)
virtual std::string printBlockOpen(const std::string &name, short depth, const std::string &) override
This method is called at the beginning of each Node in the tree.
virtual std::string printBlockClose(const std::string &name, short depth) const override
This method is called at the end of of each Node in the tree.
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
std::string trim(const std::string &str, const std::string &white_space=" \\\)
Standard scripting language trim function.
Definition: MooseUtils.C:113
bool wildCardMatch(std::string name, std::string search_string)
Definition: MooseUtils.C:512
virtual std::string printParams(const std::string &prefix, const std::string &fully_qualified_name, InputParameters &params, short depth, const std::string &search_string, bool &found) override
This function is called for each InputParameters object stored at a particular node.
bool haveSeenIt(const std::string &prefix, const std::string &item) const
Definition: SyntaxTree.C:63
void seenIt(const std::string &prefix, const std::string &item)
Definition: SyntaxTree.C:57
void escape(std::string &str)
This function will escape all of the standard C++ escape characters so that they can be printed...
Definition: MooseUtils.C:95