1 //==============================================================================
2 //
3 //  This file is part of GPSTk, the GPS Toolkit.
4 //
5 //  The GPSTk is free software; you can redistribute it and/or modify
6 //  it under the terms of the GNU Lesser General Public License as published
7 //  by the Free Software Foundation; either version 3.0 of the License, or
8 //  any later version.
9 //
10 //  The GPSTk is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU Lesser General Public License for more details.
14 //
15 //  You should have received a copy of the GNU Lesser General Public
16 //  License along with GPSTk; if not, write to the Free Software Foundation,
17 //  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
18 //
19 //  This software was developed by Applied Research Laboratories at the
20 //  University of Texas at Austin.
21 //  Copyright 2004-2020, The Board of Regents of The University of Texas System
22 //
23 //==============================================================================
24 
25 //==============================================================================
26 //
27 //  This software was developed by Applied Research Laboratories at the
28 //  University of Texas at Austin, under contract to an agency or agencies
29 //  within the U.S. Department of Defense. The U.S. Government retains all
30 //  rights to use, duplicate, distribute, disclose, or release this software.
31 //
32 //  Pursuant to DoD Directive 523024
33 //
34 //  DISTRIBUTION STATEMENT A: This software has been approved for public
35 //                            release, distribution is unlimited.
36 //
37 //==============================================================================
38 
39 /**
40  * @file Exception.cpp
41  * Exceptions for all of GPSTK, including location information
42  */
43 
44 #include <sstream>
45 #include "Exception.hpp"
46 
47 using std::ostream;
48 using std::ostringstream;
49 using std::streambuf;
50 using std::string;
51 using std::endl;
52 
53 namespace gpstk
54 {
55 
dump(ostream & s) const56    void ExceptionLocation::dump(ostream& s) const
57    {
58       s << getFileName() << ":"
59 #ifdef __FUNCTION__
60         << getFunctionName() << ":"
61 #endif
62         << getLineNumber();
63    }
64 
Exception()65    Exception::Exception()
66    {
67    }
68 
Exception(const string & errorText,const unsigned long & errId,const Severity & sever)69    Exception::Exception(const string& errorText,
70                         const unsigned long& errId,
71                         const Severity& sever)
72    {
73       text.push_back(errorText);
74       errorId = errId;
75       severity = sever;
76    }
77 
Exception(const char * errorText,const unsigned long & errId,const Severity & sever)78    Exception::Exception(const char* errorText,
79       const unsigned long& errId,
80       const Severity& sever)
81    {
82       text.push_back(string(errorText));
83       errorId = errId;
84       severity = sever;
85    }
86 
Exception(const Exception & e)87    Exception::Exception(const Exception& e)
88          : errorId(e.errorId),
89            locations(e.locations),
90            severity(e.severity),
91            text(e.text),
92            streamBuffer(e.streamBuffer)
93    {}
94 
operator =(const Exception & e)95    Exception& Exception::operator=(const Exception& e)
96    {
97       errorId = e.errorId;
98       locations = e.locations;
99       severity = e.severity;
100       text = e.text;
101          // reuse existing stream objects, no matter.
102          //streambuf(), ostream((streambuf*)this),
103       streamBuffer = e.streamBuffer;
104 
105       return *this;
106    }
107 
addLocation(const ExceptionLocation & location)108    Exception& Exception::addLocation(
109       const ExceptionLocation& location)
110    {
111       locations.push_back(location);
112       return *this;
113    }
114 
getLocation(const size_t & index) const115    const ExceptionLocation Exception::getLocation(
116       const size_t& index) const
117    {
118       if (index>=getLocationCount())
119       {
120          return ExceptionLocation();
121       }
122       else
123       {
124          return locations[index];
125       }
126    }
127 
getLocationCount() const128    size_t Exception::getLocationCount() const
129    {
130       return locations.size();
131    }
132 
addText(const string & errorText)133    Exception& Exception::addText(const string& errorText)
134    {
135       text.push_back(errorText);
136       return *this;
137    }
138 
getText(const size_t & index) const139    string Exception::getText(const size_t& index) const
140    {
141       if (index>=getTextCount())
142       {
143          string tmp;
144          return tmp;
145       }
146       else
147       {
148          return text[index];
149       }
150    }
151 
getTextCount() const152    size_t Exception::getTextCount() const
153    {
154       return text.size();
155    }
156 
dump(ostream & s) const157    void Exception::dump(ostream& s) const
158    {
159       size_t i;
160       for (i=0; i<getTextCount(); i++)
161       {
162          s << "text " << i << ":" << this->getText(i) << endl;
163       }
164       for (i=0; i<getLocationCount(); i++)
165       {
166          s << "location " << i << ":" << getLocation(i).what() << endl;
167       }
168    }
169 
overflow(int c)170    int Exception::overflow(int c)
171    {
172       if (c == '\n' || !c)
173       {
174          if (streamBuffer.length() == 0)
175          {
176             return c;
177          }
178          addText(streamBuffer);
179          streamBuffer = "";
180          return c;
181       }
182       streamBuffer.append(1, (char)c);
183       return c;
184    }
185 
what() const186    string ExceptionLocation::what() const
187    {
188       ostringstream oss;
189       this->dump(oss);
190       return oss.str();
191    }
192 
what() const193    string Exception::what() const
194    {
195       ostringstream oss;
196       this->dump(oss);
197       return oss.str();
198    }
199 
operator <<(ostream & s,const Exception & e)200     ostream& operator<<( ostream& s,
201                          const Exception& e )
202     {
203        e.dump(s);
204        return s;
205     }
206 
operator <<(ostream & s,const ExceptionLocation & e)207     ostream& operator<<( ostream& s,
208                          const ExceptionLocation& e )
209     {
210        e.dump(s);
211        return s;
212     }
213 
214 } // namespace gpstk
215