]> git.ipfire.org Git - thirdparty/squid.git/blob - src/base/TextException.cc
Merged Postfix-Prefix branch: refactor inc/decrement operators from postfix to prefix...
[thirdparty/squid.git] / src / base / TextException.cc
1 #include "squid-old.h"
2 #include "base/TextException.h"
3 #include "Debug.h"
4 #include "util.h"
5
6 TextException::TextException()
7 {
8 message=NULL;
9 theFileName=NULL;
10 theLineNo=0;
11 theId=0;
12 }
13
14 TextException::TextException(const TextException& right) :
15 message((right.message?xstrdup(right.message):NULL)), theFileName(right.theFileName), theLineNo(right.theLineNo), theId(right.theId)
16 {
17 }
18
19 TextException::TextException(const char *aMsg, const char *aFileName, int aLineNo, unsigned int anId):
20 message(xstrdup(aMsg)), theFileName(aFileName), theLineNo(aLineNo), theId(anId)
21 {}
22
23 TextException::~TextException() throw()
24 {
25 if (message) xfree(message);
26 }
27
28 TextException& TextException::operator=(const TextException &right)
29 {
30 if (this==&right) return *this;
31 if (message) xfree(message);
32 message=(right.message?xstrdup(right.message):NULL);
33 theFileName=right.theFileName;
34 theLineNo=right.theLineNo;
35 theId=right.theId;
36 return *this;
37 }
38
39 const char *TextException::what() const throw()
40 {
41 /// \todo add file:lineno
42 return message ? message : "TextException without a message";
43 }
44
45 unsigned int TextException::FileNameHash(const char *fname)
46 {
47 const char *s = NULL;
48 unsigned int n = 0;
49 unsigned int j = 0;
50 unsigned int i = 0;
51 s = strrchr(fname, '/');
52
53 if (s)
54 ++s;
55 else
56 s = fname;
57
58 while (*s) {
59 ++j;
60 n ^= 271 * (unsigned) *s;
61 ++s;
62 }
63 i = n ^ (j * 271);
64 /*18bits of a 32 bit integer used for filename hash (max hash=262143),
65 and 14 bits for storing line number (16k max).
66 If you change this policy remember to update the FileNameHash function
67 in the scripts/calc-must-ids.pl script
68 */
69 return i % 262143;
70 }
71
72 void Throw(const char *message, const char *fileName, int lineNo, unsigned int id)
73 {
74
75 // or should we let the exception recepient print the exception instead?
76
77 if (fileName) {
78 debugs(0, 3, fileName << ':' << lineNo << ": exception" <<
79 (message ? ": " : ".") << (message ? message : ""));
80 } else {
81 debugs(0, 3, "exception" <<
82 (message ? ": " : ".") << (message ? message : ""));
83 }
84
85 throw TextException(message, fileName, lineNo, id);
86 }