HaikuDepot: notify work status from main window
[haiku.git] / src / kits / shared / HashString.cpp
blobecbaf340dce8fca2c2045c47b7856667d48e9ed1
1 /*
2 * Copyright 2004-2007, Ingo Weinhold, bonefish@users.sf.net. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
5 #include <new>
6 #include <string.h>
8 #include "HashString.h"
10 /*!
11 \class HashString
12 \brief A very simple string class.
15 // constructor
16 HashString::HashString()
17 : fLength(0),
18 fString(NULL)
22 // copy constructor
23 HashString::HashString(const HashString &string)
24 : fLength(0),
25 fString(NULL)
27 *this = string;
30 // constructor
31 HashString::HashString(const char *string, int32 length)
32 : fLength(0),
33 fString(NULL)
35 SetTo(string, length);
38 // destructor
39 HashString::~HashString()
41 Unset();
44 // SetTo
45 bool
46 HashString::SetTo(const char *string, int32 maxLength)
48 if (string) {
49 if (maxLength > 0)
50 maxLength = strnlen(string, maxLength);
51 else if (maxLength < 0)
52 maxLength = strlen(string);
54 return _SetTo(string, maxLength);
57 // Unset
58 void
59 HashString::Unset()
61 if (fString) {
62 delete[] fString;
63 fString = NULL;
65 fLength = 0;
68 // Truncate
69 void
70 HashString::Truncate(int32 newLength)
72 if (newLength < 0)
73 newLength = 0;
74 if (newLength < fLength) {
75 char *string = fString;
76 fString = NULL;
77 if (!_SetTo(string, newLength)) {
78 fString = string;
79 fLength = newLength;
80 fString[fLength] = '\0';
81 } else
82 delete[] string;
86 // GetString
87 const char *
88 HashString::GetString() const
90 if (fString)
91 return fString;
92 return "";
95 // =
96 HashString &
97 HashString::operator=(const HashString &string)
99 if (&string != this)
100 _SetTo(string.fString, string.fLength);
101 return *this;
104 // ==
105 bool
106 HashString::operator==(const HashString &string) const
108 return (fLength == string.fLength
109 && (fLength == 0 || !strcmp(fString, string.fString)));
112 // _SetTo
113 bool
114 HashString::_SetTo(const char *string, int32 length)
116 bool result = true;
117 Unset();
118 if (string && length > 0) {
119 fString = new(std::nothrow) char[length + 1];
120 if (fString) {
121 memcpy(fString, string, length);
122 fString[length] = '\0';
123 fLength = length;
124 } else
125 result = false;
127 return result;