FACT++  1.0
template<class T >
vector<T> Converter::Get ( const std::string &  str) const

Converts the provided format string into a vector.

Template Parameters
TKind of data to be returned. This can either be boost::any objects or a bnary data-block (char).
Parameters
strData to be converted. For details see class reference
Returns
A vector of the given template type containing the arguments. In case of failure an empty vector is returned.
Exceptions
std::runtime_errorif the conversion was not successfull

Definition at line 525 of file Converter.cc.

References data, empty(), fFormat, fList, GetBinImp(), GetBinString(), GetBool(), GetString(), GetStringEol(), i, valid(), and wout.

526 {
527  if (!valid())
528  throw runtime_error("Compiled format invalid!");
529 
530  // If the format is empty we are already done
531  if (empty() && str.empty())
532  {
533  wout << endl;
534  return vector<T>();
535  }
536 
537  int arg = 0;
538  stringstream line(str);
539 
540  vector<T> data;
541 
542  for (Converter::FormatList::const_iterator i=fList.begin(); i<fList.end()-1; i++)
543  {
544  if (*i->first.first == typeid(string))
545  {
546  GetBinString(data, GetStringEol(line));
547  line.clear(ios::eofbit);
548  continue;
549  }
550 
551  // Get as many items from the input line as requested
552  for (int j=0; j<i->second.first; j++)
553  {
554  switch (i->first.first->name()[0])
555  {
556  case 'b': GetBinImp(data, GetBool(line)); break;
557  case 's': GetBinImp(data, Get<short> (line)); break;
558  case 'i': GetBinImp(data, Get<int> (line)); break;
559  case 'l': GetBinImp(data, Get<long> (line)); break;
560  case 'f': GetBinImp(data, Get<float> (line)); break;
561  case 'd': GetBinImp(data, Get<double> (line)); break;
562  case 'x': GetBinImp(data, Get<long long>(line)); break;
563  case 'c':
564  {
565  const unsigned short val = Get<unsigned short>(line);
566  if (val>255)
567  line.setstate(ios::failbit);
568  GetBinImp(data, static_cast<unsigned char>(val));
569  }
570  break;
571  case 'N':
572  GetBinString(data, GetString(line));
573  if (*i->first.first == typeid(O))
574  line.clear(ios::goodbit|(line.rdstate()&ios::eofbit));
575  break;
576  default:
577  // This should never happen!
578  throw runtime_error("Format '"+string(i->first.first->name())+" not supported!");
579  }
580 
581  arg++;
582  }
583 
584  if (!line)
585  break;
586  }
587  wout << endl;
588 
589  // Something wrong with the conversion (e.g. 5.5 for an int)
590  if (line.fail() && !line.eof())
591  {
592  line.clear(); // This is necesasary to get a proper response from tellg()
593 
594  ostringstream err;
595  err << "Error converting argument at " << arg << " [fmt=" << fFormat << "]!\n";
596  err << line.str() << "\n";
597  err << setw(int(line.tellg())) << " " << "^\n";
598  throw runtime_error(err.str());
599  }
600 
601  // Not enough arguments, we have not reached the end
602  if (line.fail() && line.eof())
603  {
604  line.clear();
605 
606  ostringstream err;
607  err << "Not enough arguments [fmt=" << fFormat << "]!\n";
608  err << line.str() << "\n";
609  err << setw(int(line.tellg())+1) << " " << "^\n";
610  throw runtime_error(err.str());
611  }
612 
613  // Too many arguments, we have not reached the end
614  // Unfortunately, this can also mean that there is something
615  // wrong with the last argument
616  if (line.good() && !line.eof())
617  {
618  ostringstream err;
619  err << "More arguments available than expected [fmt=" << fFormat << "]!\n";
620  err << line.str() << "\n";
621  err << setw(int(line.tellg())+1) << " " << "^\n";
622  throw runtime_error(err.str());
623  }
624 
625  return data;
626 
627 }
int i
Definition: db_dim_client.c:21
char str[80]
Definition: test_client.c:7
bool valid() const
Definition: Converter.h:83
std::string GetString(std::stringstream &line) const
Definition: Converter.cc:294
std::ostream & wout
Definition: Converter.h:30
void GetBinImp(std::vector< char > &v, const T &val) const
Definition: Converter.cc:123
void GetBinString(std::vector< char > &v, const std::string &val) const
Definition: Converter.cc:169
bool GetBool(std::stringstream &line) const
Definition: Converter.cc:263
float data[4 *1440]
std::string GetStringEol(std::stringstream &line) const
Definition: Converter.cc:326
const FormatList fList
Original format string.
Definition: Converter.h:33
bool empty() const
Definition: Converter.h:80
const std::string fFormat
ostream to which output is redirected
Definition: Converter.h:32

+ Here is the call graph for this function: