Points&Forces (core)
Software tools facilitating the task of surveying architecture
a_text.cxx
Go to the documentation of this file.
1 /*
2  Copyright 2010-2012 Pierre SMARS (smars@yuntech.edu.tw)
3  This program is free software: you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation, either version 2 of the License, or
6  (at your option) any later version.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 #include "a_text.h"
17 #include <fstream>
18 #include <sstream>
19 //---------------------------------------------------------------------------
21 {
22  endl_ = "";
23 }
24 //---------------------------------------------------------------------------
25 const std::string a_text::help()
26 {
27  std::ostringstream o;
28  o << "********" << std::endl;
29  o << "a_text:" << std::endl;
30  o << "********" << std::endl;
31  o << "This is a 'text' class" << std::endl;
32  o << std::endl;
33  o << "Create a text:" << std::endl;
34  o << "a_text t" << std::endl;
35  o << std::endl;
36  o << "Commands:" << std::endl;
37  o << "file _name: redirect stream stream to a file" << std::endl;
38  o << "on: output on" << std::endl;
39  o << "off: output off" << std::endl;
40  o << "user_unit: get the user units" << std::endl;
41  o << "user_unit _unit: set the user units (see a_unit package)" << std::endl;
42  o << "dimension: get the dimension" << std::endl;
43  o << "dimension _val: set the dimension" << std::endl;
44  o << "put _string: output a string" << std::endl;
45  o << "putd _val: output a number" << std::endl;
46  o << "unit: check whether user units are used" << std::endl;
47  o << "unit (on/off): use or do not use user units" << std::endl;
48  o << std::endl;
49  o << a_text_base::help();
50  return o.str();
51 }
52 //---------------------------------------------------------------------------
54 {
55  file_ = 0;
56  user_unit_ = 0;
57  use_user_unit_ = false;
58  on_ = true;
59  standard_ = true;
60  dimension_ = 1;
61  this->init();
62 }
63 //---------------------------------------------------------------------------
64 a_text::a_text(const a_text& text)
65 {
66  file_ = text.file();
67  user_unit_ = 0;
68  use_user_unit_ = false;
69  on_ = true;
70  standard_ = true;
71  dimension_ = 1;
72  this->init();
73 }
74 //---------------------------------------------------------------------------
76 {
77  if (file_) ((std::ofstream *)(file_))->close();
78 }
79 //---------------------------------------------------------------------------
80 void a_text::file(const std::string& file)
81 {
82  if (file_) ((std::ofstream *)(file_))->close();
83  if (file == "standard")
84  {
85  if (file_) file_ = NULL;
86  standard_ = true;
87  }
88  else if (file == "none")
89  {
90  file_ = 0;
91  standard_ = false;
92  }
93  else
94  {
95  static std::ofstream ff; //static to let ff open out of the constructor scope+Stroustrup, p.639
96  //append file
97  // ff.open(file.c_str());
98  ff.open(file.c_str(),std::ios_base::app);
99  file_ = &ff;
100  standard_ = false;
101  }
102 }
103 //---------------------------------------------------------------------------
105 {
106  if (!standard_)
107  ((std::ofstream *)(file_))->flush();
108 }
109 //---------------------------------------------------------------------------
110 a_text& a_text::operator<<(const char v)
111 {
112  if ((standard_)&&(on_)) std::cout << v << endl_;
113  else if ((file_)&&(on_)) *file_ << v << endl_;
114  return *this;
115 }
116 //---------------------------------------------------------------------------
118 {
119  if ((standard_)&&(on_)) std::cout << v;
120  else if ((file_)&&(on_)) *file_ << v;
121  return *this;
122 }
123 //---------------------------------------------------------------------------
124 a_text& a_text::operator<< (const double v)
125 {
126  if ((standard_)&&(on_))
127  {
128  if (use_user_unit_)
129  {
130  if (dimension_ == 1)
131  std::cout << user_unit_->dim1(v) << " (" << v << ")";
132  else
133  std::cout << user_unit_->dim2(v) << " (" << v << ")";
134  }
135  else
136  std::cout << v;
137  std::cout << endl_;
138  }
139  else if ((file_)&&(on_))
140  {
141  if (use_user_unit_)
142  {
143  if (dimension_ == 1)
144  *file_ << user_unit_->dim1(v) << " (" << v << ")";
145  else
146  *file_ << user_unit_->dim2(v) << " (" << v << ")";
147  }
148  else
149  *file_ << v;
150  }
151  return *this;
152 }
153 //---------------------------------------------------------------------------
155 {
156  if ((standard_)&&(on_))
157  {
158  if (use_user_unit_)
159  std::cout << user_unit_->dim1(v) << " (" << v << ")";
160  else
161  std::cout << v;
162  std::cout << endl_;
163  }
164  else if ((file_)&&(on_))
165  {
166  if (use_user_unit_)
167  *file_ << user_unit_->dim1(v) << " (" << v << ")";
168  else
169  *file_ << v;
170  }
171  return *this;
172 }
173 //---------------------------------------------------------------------------
174 a_text& a_text::operator<<(const float v)
175 {
176  this->operator<<((const double)(v));
177  return *this;
178 }
179 //---------------------------------------------------------------------------
180 a_text& a_text::operator<<(std::string& v)
181 {
182  if ((standard_)&&(on_)) std::cout << v;
183  else if ((file_)&&(on_)) *file_ << v;
184  return *this;
185 }
186 //---------------------------------------------------------------------------
187 a_text& a_text::operator<<(const std::string& v)
188 {
189  if ((standard_)&&(on_)) std::cout << v;
190  else if ((file_)&&(on_)) *file_ << v;
191  return *this;
192 }
193 //---------------------------------------------------------------------------
194 a_text& a_text::operator<<(std::ostream& (*f) (std::ostream&))
195 {
196  if ((standard_)&&(on_))
197  file_ = &std::cout;
198  if ((on_)&&(file_)) f(*file_);
199  if ((standard_)&&(on_))
200  file_ = 0;
201  return *this;
202 }
static const std::string help()
Definition: a_text_base.cxx:21
a text class
Definition: a_text.h:31
int dimension_
Definition: a_text.h:65
void file(const std::string &file)
Definition: a_text.cxx:80
a_text()
Definition: a_text.cxx:53
~a_text()
Definition: a_text.cxx:75
bool standard_
Definition: a_text.h:63
a_unit_system * user_unit_
Definition: a_text.h:61
void flush()
Definition: a_text.cxx:104
bool on_
Definition: a_text.h:62
void init()
Definition: a_text.cxx:20
a_text & operator<<(const char v)
Definition: a_text.cxx:110
bool use_user_unit_
Definition: a_text.h:64
std::string endl_
Definition: a_text.h:66
std::ostream * file() const
Definition: a_text.h:39
std::ostream * file_
Definition: a_text.h:60
static const std::string help()
Definition: a_text.cxx:25
std::string dim1(const double v)
std::string dim2(const double v)