CASM
AClustersApproachtoStatisticalMechanics
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
DataFormatterTools_impl.hh
Go to the documentation of this file.
1 #include <algorithm>
2 #include "casm/external/boost.hh"
3 namespace CASM {
4 
5  template<typename ValueType, typename ArgType, typename DataObject>
6  bool DataFormatterOperator<ValueType,
7  ArgType,
8  DataObject>::parse_args(const std::string &_args) {
9 
10  if(_args.size() == 0)
11  return true;
12  if(!m_arg_formatter.empty())
13  return false;
14 
15  std::vector<std::string> format_tags, subexprs;
16  split_formatter_expression(_args, format_tags, subexprs);
17 
18  //std::cout << "_args: " << _args << "\n";
19  //std::cout << "format_tabs: " << format_tags << "\n"
20  //<< "subexprs: '" << subexprs << "'\n";
21  for(Index i = 0; i < format_tags.size(); i++) {
22  std::string ttag(format_tags[i].size(), ' ');
23  std::transform(format_tags[i].cbegin(), format_tags[i].cend(), ttag.begin(), tolower);
24 
25  char ch = ttag[0];
26  if(boost::is_any_of("-+.0123456789")(ch)) { // tag is a constant number
27  if(std::any_of(ttag.cbegin(),
28  ttag.cend(),
29  boost::is_any_of(".e"))) {
30  double val;
31  try {
32  val = std::stod(ttag);
33  }
34  catch(...) {
35  throw std::runtime_error("Unable to parse '" + ttag + "' from subexpression " + _args + "\n");
36  }
37  m_arg_formatter.push_back(ConstantValueFormatter<double, DataObject>(ttag, val));
38  }
39  else {
40  long val;
41  try {
42  val = std::stol(ttag);
43  }
44  catch(...) {
45  throw std::runtime_error("Unable to parse '" + ttag + "' from subexpression " + _args + "\n");
46  }
47  m_arg_formatter.push_back(ConstantValueFormatter<long, DataObject>(ttag, val));
48  }
49  }
50  else if(ttag == "false")
51  m_arg_formatter.push_back(ConstantValueFormatter<bool, DataObject>(ttag, false));
52  else if(ttag == "true")
53  m_arg_formatter.push_back(ConstantValueFormatter<bool, DataObject>(ttag, true));
54  else if((ch == '\'' || ch == '\"') && ch == ttag.back() && ttag.size() > 1) {
55  m_arg_formatter.push_back(ConstantValueFormatter<std::string, DataObject>(format_tags[i], std::string((++format_tags[i].cbegin()), (--format_tags[i].cend()))));
56  }
57  else {
58  //std::cout << "About to push_back parsed formatter!!\n";
59  //can throw, should be handled by caller
60  const BaseDatumFormatter<DataObject> &proto_format = *this->home().lookup(ttag);
61 
62  m_arg_formatter.push_back(proto_format, subexprs[i]);
63 
64  }
65  }
66  return true;
67  }
68 
69  //******************************************************************************
70 
71  template<typename ValueType, typename ArgType, typename DataObject>
72  std::string DataFormatterOperator<ValueType,
73  ArgType,
74  DataObject>::short_header(const DataObject &_obj)const {
75  std::stringstream t_ss;
76  m_arg_formatter.print_header(_obj, t_ss);
77 
78  std::vector<std::string> format_tags, subexprs;
79  split_formatter_expression(t_ss.str(), format_tags, subexprs);
80  t_ss.str("");
81  t_ss.clear();
82  t_ss << name();
83  if(format_tags.size()) {
84  t_ss << '(';
85  for(Index i = 0; i < format_tags.size(); i++) {
86  t_ss << format_tags[i];
87  if(subexprs[i].size() > 0)
88  t_ss << '(' << subexprs[i] << ')';
89  if(i + 1 < format_tags.size())
90  t_ss << ',';
91  }
92  t_ss << ')';
93  }
94  return t_ss.str();
95  }
96 
97 }
void split_formatter_expression(const std::string &input_expr, std::vector< std::string > &tag_names, std::vector< std::string > &sub_exprs)
Main CASM namespace.
Definition: complete.cpp:8
Prints a string value specified at construction. A header string can also be passed.
EigenIndex Index
For long integer indexing:
DataFormatters that operate on the results of other DataFormatters.