Points&Forces (core)
Software tools facilitating the task of surveying architecture
a_dxf_group.cxx
Go to the documentation of this file.
1 /*
2 Copyright 2011 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_dxf_group.h"
17 #include <sstream>
18 #include <string.h>
19 std::string a_dxf_group::eof_ = "EOF";
20 //---------------------------------------------------------------------------
22 {
23  const int buffer_size = 254;
24  if (file_->eof()) throw file_error();
25  char buf[buffer_size]; char c; //the size of the buffer is critical, check if problem
26  file_->getline(buf,buffer_size,'\n'); //gives problems when there's an empty line
27  int n = strlen(buf)-1;
28  if (buf[n]=='\r') //possibly eliminate the extra CR if file was created in windows and read in *nix
29  buf[n] = '\0';
30  std::istringstream ss(buf);
31  ss >> code_;
32  if (ss.fail())
33  {
34  std::cerr << "a_dxf_group::read() invalid code line" << std::endl;
35  throw file_error(); //not an integer
36  }
37  if (file_->eof()) throw file_error(); //file not properly finished
38  file_->getline(buf,buffer_size,'\n'); //gives problems when there's an empty line
39  n = strlen(buf)-1;
40  if (buf[n]=='\r') //possibly eliminate the extra CR if file was created in windows and read in *nix
41  buf[n] = '\0';
42  if (file_->fail()) {file_->clear();} //line is empty
43  data_ = std::string(buf);
44  if (data_ != "")
45  {
46  if (data_.length() == buffer_size-1) //long data line
47  {
48  while (c != '\n')
49  {
50  data_ = data_ + c;
51  file_->get(c);
52  }
53  }
54  std::string::size_type ii=data_.find_first_not_of(" ");
55  std::string::size_type ie=data_.find_last_not_of(" ");
56  data_ = data_.substr(ii,++ie); //suppress the spacies around
57  }
58  if (data_ == eof_) return false;
59  return true;
60 }
61 //---------------------------------------------------------------------------
63 {
64  std::cout << code_ << std::endl;
65  std::cout << data_ << std::endl;
66 }
67 //---------------------------------------------------------------------------
69 {
70  file_->putback('\n');
71  for (int i=data_.length()-1;i>=0; i--)
72  file_->putback(data_[i]);
73  std::ostringstream o;
74  o << code_;
75  std::string code(o.str());
76  file_->putback('\n');
77  for (int i=code.length()-1;i>=0; i--)
78  file_->putback(code[i]);
79 }
80 //---------------------------------------------------------------------------
82 {
83  int result;
84  std::istringstream ss(data_);
85  ss >> result;
86  if (ss.fail()) throw file_error();
87  return result;
88 }
89 //---------------------------------------------------------------------------
91 {
92  double result;
93  std::istringstream ss(data_);
94  ss >> result;
95  if (ss.fail()) throw file_error();
96  return result;
97 }
double data_double()
Definition: a_dxf_group.cxx:90
int code()
Definition: a_dxf_group.h:36
int data_int()
Definition: a_dxf_group.cxx:81
static std::string eof_
Definition: a_dxf_group.h:49
void write()
Definition: a_dxf_group.cxx:62
void putback()
Definition: a_dxf_group.cxx:68
std::istream * file_
Definition: a_dxf_group.h:46
std::string data_
Definition: a_dxf_group.h:48
char buf[256]
Definition: copy.h:17