Points&Forces (core)
Software tools facilitating the task of surveying architecture
a_unit.cxx
Go to the documentation of this file.
1 #include "a_unit.h"
2 #include <sstream>
3 #include <vector>
4 
5 //---------------------------------------------------------------------------
6 const std::string a_unit::help()
7 {
8  std::ostringstream o;
9  o << "********" << std::endl;
10  o << "a_unit:" << std::endl;
11  o << "********" << std::endl;
12  o << "This is a 'unit' class" << std::endl;
13  o << std::endl;
14  o << "Create a unit:" << std::endl;
15  o << "a_unit u" << std::endl;
16  o << std::endl;
17  o << "Commands:" << std::endl;
18  o << "name _name: set full name of unit (example: meter)" << std::endl;
19  o << "name: get full name of unit" << std::endl;
20  o << "short_name _name: set short name of unit (example: m)" << std::endl;
21  o << "short_name: get short name of unit (example: m)" << std::endl;
22  o << "ratio _val: set value of 'basic unit' in this custom unit" << std::endl;
23  o << "ratio: get value of 'basic unit' in this custom unit" << std::endl;
24  o << "print: print parameters" << std::endl;
25  o << std::endl;
26  o << a_unit_base::help();
27  return o.str();
28 }
29 //***************************************************************************
30 //---------------------------------------------------------------------------
31 void a_unit::read(std::istream &in0)
32 {
33  std::string line;
34  std::getline(in0,line);
35  std::istringstream in(line.c_str());
36  std::vector<std::string> vals;
37  bool quote = false;
38  std::ostringstream out;
39  while (in)
40  {
41  std::string s2;
42  std::getline(in,s2,' ');
43  if ((!quote)&&(s2.substr(0,1)=="\""))
44  {
45  quote = true;
46  std::string s3("");
47  if (s2.length()>1)
48  {
49  //check whether their is an ending quote in text
50  s3 = s2.substr(1,s2.length()-1);
51  std::string::size_type ii=-1;
52  ii = s3.find_first_of("\"");
53  if (ii<s3.size())
54  {
55  s3 = s3.substr(0,ii);
56  quote = false;
57  out << s3;
58  vals.push_back(out.str());
59  out.str("");
60  out.clear();
61  }
62  else
63  out << s3;
64  }
65  }
66  else
67  {
68  if (quote)
69  {
70  std::string s3;
71  s3 = s2.substr(s2.length()-1,1);
72  if (s3=="\"")
73  {
74  quote = false;
75  if (s2!="\"")
76  out << " " << s2.substr(0,s2.length()-1);
77  vals.push_back(out.str());
78  out.str("");
79  out.clear();
80  }
81  else
82  out << " " << s2;
83  }
84  else
85  {
86  if (s2 != "")
87  vals.push_back(s2);
88  }
89  }
90  }
91  if (vals.size()<3)
92  return;
93  name_ = vals[0];
94  short_name_ = vals[1];
95  out.str("");
96  out.clear();
97  out << vals[2];
98  std::istringstream in2(out.str().c_str());
99  in2 >> ratio_;
100 }
101 //---------------------------------------------------------------------------
102 void a_unit::write(std::ostream &o) const
103 {
104  o << "\"" << this->name() << "\" \"";
105  o << this->short_name() << "\" ";
106  o << this->ratio() << " ";
107 }
108 //***************************************************************************
109 //---------------------------------------------------------------------------
111 {
112  name_ = "Basic Unit";
113  short_name_ = "_";
114  ratio_ = 1;
115 }
116 
117 //---------------------------------------------------------------------------
119 {
120  name_ = u.name();
121  short_name_ = u.short_name();
122  ratio_ = u.ratio();
123 }
124 
125 //---------------------------------------------------------------------------
126 a_unit::a_unit(std::string n,std::string ns,double r)
127 {
128  name_ = n;
129  short_name_ = ns;
130  ratio_ = r;
131 }
static const std::string help()
Definition: a_unit_base.cxx:21
unit
Definition: a_unit.h:29
virtual void write(std::ostream &o) const
Definition: a_unit.cxx:102
a_unit()
Definition: a_unit.cxx:110
static const std::string help()
Definition: a_unit.cxx:6
double ratio_
Definition: a_unit.h:50
std::string name_
Definition: a_unit.h:48
std::string short_name_
Definition: a_unit.h:49
std::string short_name() const
Definition: a_unit.h:38
std::string name() const
Definition: a_unit.h:36
double ratio() const
Definition: a_unit.h:40
virtual void read(std::istream &i)
Definition: a_unit.cxx:31
std::cin getline(buf, 256)