KiCad PCB EDA Suite
exceptions.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2007-2016 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
5  * Copyright (C) 2017 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #include <wx/wx.h>
26 #include <ki_exception.h>
27 
28 
29 #define THROWERS_WHERE _( "from %s : %s() line:%d" )
30 #define PARSE_PROBLEM _( "%s in input/source\n'%s'\nline %d, offset %d" )
31 
32 
33 const wxString IO_ERROR::What() const
34 {
35 #ifdef DEBUG
36  return wxString( "IO_ERROR: " ) + Problem() + "\n\n" + Where();
37 #else
38  return Problem();
39 #endif
40 }
41 
42 
43 const wxString IO_ERROR::Where() const
44 {
45  return where;
46 }
47 
48 
49 const wxString IO_ERROR::Problem() const
50 {
51  return problem;
52 }
53 
54 
55 
56 void IO_ERROR::init( const wxString& aProblem, const char* aThrowersFile, const char* aThrowersFunction, int aThrowersLineNumber )
57 {
58  problem = aProblem;
59 
60  // The throwers filename is a full filename, depending on Kicad source location.
61  // a short filename will be printed (it is better for user, the full filename has no meaning).
62  wxString srcname = aThrowersFile;
63 
64  where.Printf( THROWERS_WHERE, srcname.AfterLast( '/' ).GetData(),
65  wxString( aThrowersFunction ).GetData(), aThrowersLineNumber );
66 }
67 
68 
69 void PARSE_ERROR::init( const wxString& aProblem, const char* aThrowersFile,
70  const char* aThrowersFunction, int aThrowersLineNumber,
71  const wxString& aSource,
72  const char* aInputLine,
73  int aLineNumber, int aByteIndex )
74 {
75  problem.Printf( PARSE_PROBLEM, aProblem.GetData(), aSource.GetData(), aLineNumber, aByteIndex );
76 
77  inputLine = aInputLine;
78  lineNumber = aLineNumber;
79  byteIndex = aByteIndex;
80 
81  // The throwers filename is a full filename, depending on Kicad source location.
82  // a short filename will be printed (it is better for user, the full filename has no meaning).
83  wxString srcname = aThrowersFile;
84 
85  where.Printf( THROWERS_WHERE, srcname.AfterLast( '/' ).GetData(),
86  wxString( aThrowersFunction ).GetData(), aThrowersLineNumber );
87 }
88 
89 
90 FUTURE_FORMAT_ERROR::FUTURE_FORMAT_ERROR( const PARSE_ERROR& aParseError, const wxString& aRequiredVersion ) :
91  PARSE_ERROR(), requiredVersion( aRequiredVersion )
92 {
93  // Avoid double-printing the error message
94  bool wrapped_same_type = !!( dynamic_cast<const FUTURE_FORMAT_ERROR *>( &aParseError ) );
95 
96  if( wrapped_same_type )
97  {
98  problem = aParseError.Problem();
99  }
100  else
101  {
102  problem.Printf( _(
103  "KiCad was unable to open this file, as it was created with a more "
104  "recent version than the one you are running. To open it, you'll need "
105  "to upgrade KiCad to a more recent version.\n\n"
106  "Date of KiCad version required (or newer): %s\n\n"
107  "Full error text:\n%s" ),
108  requiredVersion, aParseError.Problem() );
109  }
110 
111  lineNumber = aParseError.lineNumber;
112  byteIndex = aParseError.byteIndex;
113  inputLine = aParseError.inputLine;
114 }
virtual const wxString Where() const
where did the Problem() occur?
Definition: exceptions.cpp:43
void init(const wxString &aProblem, const char *aThrowersFile, const char *aThrowersFunction, int aThrowersLineNumber)
Definition: exceptions.cpp:56
FUTURE_FORMAT_ERROR(const PARSE_ERROR &aParseError, const wxString &aRequiredVersion)
Definition: exceptions.cpp:90
virtual const wxString Problem() const
what was the problem?
Definition: exceptions.cpp:49
void init(const wxString &aProblem, const char *aThrowersFile, const char *aThrowersFunction, int aThrowersLineNumber, const wxString &aSource, const char *aInputLine, int aLineNumber, int aByteIndex)
Definition: exceptions.cpp:69
int lineNumber
at which line number, 1 based index.
Definition: ki_exception.h:96
wxString requiredVersion
version or date of KiCad required to open file
Definition: ki_exception.h:145
#define THROWERS_WHERE
Definition: exceptions.cpp:29
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:33
Struct PARSE_ERROR contains a filename or source description, a problem input line, a line number, a byte offset, and an error message which contains the the caller's report and his call site information: CPP source file, function, and line number.
Definition: ki_exception.h:94
int byteIndex
at which byte offset within the line, 1 based index
Definition: ki_exception.h:97
wxString where
Definition: ki_exception.h:83
Struct FUTURE_FORMAT_ERROR variant of PARSE_ERROR indicating that a syntax or related error was likel...
Definition: ki_exception.h:143
std::string inputLine
problem line of input [say, from a LINE_READER].
Definition: ki_exception.h:103
wxString problem
Definition: ki_exception.h:82
#define PARSE_PROBLEM
Definition: exceptions.cpp:30