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