1 /*
2  *	HT Editor
3  *	except.cc
4  *
5  *	Copyright (C) 1999-2002 Stefan Weyergraf (stefan@weyergraf.de)
6  *
7  *	This program is free software; you can redistribute it and/or modify
8  *	it under the terms of the GNU General Public License version 2 as
9  *	published by the Free Software Foundation.
10  *
11  *	This program is distributed in the hope that it will be useful,
12  *	but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *	GNU General Public License for more details.
15  *
16  *	You should have received a copy of the GNU General Public License
17  *	along with this program; if not, write to the Free Software
18  *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20 
21 #include <cstdio>
22 #include <cstdarg>
23 #include <cstring>
24 
25 #include "except.h"
26 #include "snprintf.h"
27 #include "strtools.h"
28 
29 /*
30  *	Exception
31  */
reason(String & result) const32 String &Exception::reason(String &result) const
33 {
34 	result = "Unknown Exception";
35 	return result;
36 }
37 
38 
toString(char * buf,int buflen) const39 int Exception::toString(char *buf, int buflen) const
40 {
41 	String s;
42 	return reason(s).toString(buf, buflen);
43 }
44 
45 /*
46  *	MsgException
47  */
MsgException(const char * e)48 MsgException::MsgException(const char *e)
49 {
50 	ht_strlcpy(estr, e, sizeof estr);
51 }
52 
reason(String & result) const53 String &MsgException::reason(String &result) const
54 {
55 	result = estr;
56 	return result;
57 }
58 
59 /*
60  *	MsgfException
61  */
MsgfException(const char * e,...)62 MsgfException::MsgfException(const char *e,...)
63 : MsgException()
64 {
65 	va_list va;
66 	va_start(va, e);
67 	ht_vsnprintf(estr, sizeof estr, e, va);
68 	va_end(va);
69 }
70 
71 /*
72  *	IOException
73  */
74 //#include <signal.h>
IOException(int aPosixErrno)75 IOException::IOException(int aPosixErrno)
76 {
77 	mPosixErrno = aPosixErrno;
78 	errstr = strerror(mPosixErrno);
79 //	raise(SIGTRAP);
80 }
81 
reason(String & result) const82 String &IOException::reason(String &result) const
83 {
84 	result = "I/O error: " + errstr;
85 	return result;
86 }
87 
88 /*
89  *	EOFException
90  */
EOFException()91 EOFException::EOFException()
92 	: IOException(0)
93 {
94 }
95 
reason(String & result) const96 String &EOFException::reason(String &result) const
97 {
98 	return result="unexpected end of file";
99 }
100 
101 /*
102  *	NotImplementedException
103  */
NotImplementedException(const String & filename,int line_number)104 NotImplementedException::NotImplementedException(const String &filename, int line_number)
105 {
106 	if (line_number) {
107 		location.assignFormat("%y:%d", &filename, line_number);
108 	} else {
109 		location = filename;
110 	}
111 }
112 
reason(String & result) const113 String &NotImplementedException::reason(String &result) const
114 {
115 	result = "Function not implemented";
116 	if (!location.isEmpty()) {
117 		result += ": ";
118 		result += location;
119 	}
120 	return result;
121 }
122 
123 /*
124  *	IllegalArgumentException
125  */
IllegalArgumentException(const String & filename,int line_number)126 IllegalArgumentException::IllegalArgumentException(const String &filename, int line_number)
127 {
128 	if (line_number) {
129 		location.assignFormat("%y:%d", &filename, line_number);
130 	} else {
131 		location = filename;
132 	}
133 }
134 
reason(String & result) const135 String &IllegalArgumentException::reason(String &result) const
136 {
137 	result = "Illegal argument";
138 	if (!location.isEmpty()) {
139 		result += ": ";
140 		result += location;
141 	}
142 	return result;
143 }
144 
145 /*
146  *	IndexOutOfBoundsException
147  */
IndexOutOfBoundsException(const String & filename,int line_number)148 IndexOutOfBoundsException::IndexOutOfBoundsException(const String &filename, int line_number)
149 {
150 	if (line_number) {
151 		location.assignFormat("%y:%d", &filename, line_number);
152 	} else {
153 		location = filename;
154 	}
155 }
156 
reason(String & result) const157 String &IndexOutOfBoundsException::reason(String &result) const
158 {
159 	result = "Index out of bounds";
160 	if (!location.isEmpty()) {
161 		result += ": ";
162 		result += location;
163 	}
164 	return result;
165 }
166 
167 /*
168  *	TypeCastException
169  */
170 
TypeCastException(const String & cast_type,const String & obj_type)171 TypeCastException::TypeCastException(const String &cast_type, const String &obj_type)
172 {
173 	aresult.assignFormat("(%y) %y", &cast_type, &obj_type);
174 }
175 
reason(String & result) const176 String &TypeCastException::reason(String &result) const
177 {
178 	result = "Bad type cast";
179 	if (!aresult.isEmpty()) {
180 		result += ": ";
181 		result += aresult;
182 	}
183 	return result;
184 }
185 
186