Debian Codegraph @

src/bin/geomutil/vrml2oogl/lib/QvString.c++

raw
1 #include <QvString.h>
2
3 QvString::~QvString()
4 {
5 if (string != staticStorage)
6 delete [] string;
7 }
8
9 void
10 QvString::expand(int bySize)
11 {
12 int newSize = strlen(string) + bySize + 1;
13
14 if (newSize >= QV_STRING_STATIC_STORAGE_SIZE &&
15 (string == staticStorage || newSize > storageSize)) {
16
17 char *newString = new char[newSize];
18
19 strcpy(newString, string);
20
21 if (string != staticStorage)
22 delete [] string;
23
24 string = newString;
25 storageSize = newSize;
26 }
27 }
28
29 u_long
30 QvString::hash(const char *s)
31 {
32 u_long total, shift;
33
34 total = shift = 0;
35 while (*s) {
36 total = total ^ ((*s) << shift);
37 shift+=5;
38 if (shift>24) shift -= 24;
39 s++;
40 }
41
42 return( total );
43 }
44
45 void
46 QvString::makeEmpty(QvBool freeOld)
47 {
48 if (string != staticStorage) {
49 if (freeOld)
50 delete [] string;
51 string = staticStorage;
52 }
53 string[0] = '\0';
54 }
55
56 QvString &
57 QvString::operator =(const char *str)
58 {
59 int size = strlen(str) + 1;
60
61 if (str >= string &&
62 str < string + (string != staticStorage ? storageSize :
63 QV_STRING_STATIC_STORAGE_SIZE)) {
64
65 QvString tmp = str;
66 *this = tmp;
67 return *this;
68 }
69
70 if (size < QV_STRING_STATIC_STORAGE_SIZE) {
71 if (string != staticStorage)
72 makeEmpty();
73 }
74
75 else if (string == staticStorage)
76 string = new char[size];
77
78 else if (size > storageSize) {
79 delete [] string;
80 string = new char[size];
81 }
82
83 strcpy(string, str);
84 storageSize = size;
85 return *this;
86 }
87
88 QvString &
89 QvString::operator +=(const char *str)
90 {
91 expand(strlen(str));
92 strcat(string, str);
93 return *this;
94 }
95
96 int
97 operator ==(const QvString &str, const char *s)
98 {
99 return (str.string[0] == s[0] && ! strcmp(str.string, s));
100 }
101
102 int
103 operator !=(const QvString &str, const char *s)
104 {
105 return (str.string[0] != s[0] || strcmp(str.string, s));
106 }