]> git.ipfire.org Git - thirdparty/squid.git/blob - src/base/TextException.cc
Docs: Copyright updates for 2018 (#114)
[thirdparty/squid.git] / src / base / TextException.cc
1 /*
2 * Copyright (C) 1996-2018 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #include "squid.h"
10 #include "base/TextException.h"
11 #include "Debug.h"
12 #include "sbuf/SBuf.h"
13 #include "util.h"
14
15 TextException::TextException()
16 {
17 message=NULL;
18 theFileName=NULL;
19 theLineNo=0;
20 theId=0;
21 }
22
23 TextException::TextException(const TextException& right) :
24 message((right.message?xstrdup(right.message):NULL)), theFileName(right.theFileName), theLineNo(right.theLineNo), theId(right.theId)
25 {
26 }
27
28 TextException::TextException(const char *aMsg, const char *aFileName, int aLineNo, unsigned int anId):
29 message(aMsg?xstrdup(aMsg):NULL), theFileName(aFileName), theLineNo(aLineNo), theId(anId)
30 {}
31
32 TextException::TextException(SBuf msg, const char *aFileName, int aLineNo, unsigned int anId):
33 TextException(msg.c_str(), aFileName, aLineNo, anId)
34 {}
35
36 TextException::~TextException() throw()
37 {
38 if (message) xfree(message);
39 }
40
41 TextException& TextException::operator=(const TextException &right)
42 {
43 if (this==&right) return *this;
44 if (message) xfree(message);
45 message=(right.message?xstrdup(right.message):NULL);
46 theFileName=right.theFileName;
47 theLineNo=right.theLineNo;
48 theId=right.theId;
49 return *this;
50 }
51
52 const char *TextException::what() const throw()
53 {
54 /// \todo add file:lineno
55 return message ? message : "TextException without a message";
56 }
57
58 unsigned int TextException::FileNameHash(const char *fname)
59 {
60 const char *s = NULL;
61 unsigned int n = 0;
62 unsigned int j = 0;
63 unsigned int i = 0;
64 s = strrchr(fname, '/');
65
66 if (s)
67 ++s;
68 else
69 s = fname;
70
71 while (*s) {
72 ++j;
73 n ^= 271 * (unsigned) *s;
74 ++s;
75 }
76 i = n ^ (j * 271);
77 /*18bits of a 32 bit integer used for filename hash (max hash=262143),
78 and 14 bits for storing line number (16k max).
79 If you change this policy remember to update the FileNameHash function
80 in the scripts/calc-must-ids.pl script
81 */
82 return i % 262143;
83 }
84
85 void Throw(const char *message, const char *fileName, int lineNo, unsigned int id)
86 {
87
88 // or should we let the exception recepient print the exception instead?
89
90 if (fileName) {
91 debugs(0, 3, fileName << ':' << lineNo << ": exception" <<
92 (message ? ": " : ".") << (message ? message : ""));
93 } else {
94 debugs(0, 3, "exception" <<
95 (message ? ": " : ".") << (message ? message : ""));
96 }
97
98 throw TextException(message, fileName, lineNo, id);
99 }
100