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