Don't import ogdf namespace
[TortoiseGit.git] / ext / OGDF / src / basic / String.cpp
blob3e9dfec97af089e0af835e491eaa234ece8ea0ba
1 /*
2 * $Revision: 2565 $
4 * last checkin:
5 * $Author: gutwenger $
6 * $Date: 2012-07-07 17:14:54 +0200 (Sa, 07. Jul 2012) $
7 ***************************************************************/
9 /** \file
10 * \brief Implementation of class String
12 * \author Carsten Gutwenger
14 * \par License:
15 * This file is part of the Open Graph Drawing Framework (OGDF).
17 * \par
18 * Copyright (C)<br>
19 * See README.txt in the root directory of the OGDF installation for details.
21 * \par
22 * This program is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU General Public License
24 * Version 2 or 3 as published by the Free Software Foundation;
25 * see the file LICENSE.txt included in the packaging of this file
26 * for details.
28 * \par
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * \par
35 * You should have received a copy of the GNU General Public
36 * License along with this program; if not, write to the Free
37 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
38 * Boston, MA 02110-1301, USA.
40 * \see http://www.gnu.org/copyleft/gpl.html
41 ***************************************************************/
45 #include <ogdf/basic/String.h>
46 #include <ogdf/basic/Hashing.h>
47 #include <stdarg.h>
48 #include <string.h>
51 namespace ogdf {
54 char String::s_pBuffer[OGDF_STRING_BUFFER_SIZE];
57 String::String()
59 m_pChar = new char[1];
60 if (m_pChar == 0) OGDF_THROW(InsufficientMemoryException);
62 m_pChar[0] = 0;
63 m_length = 0;
67 String::String(const char c)
69 m_length = 1;
70 m_pChar = new char[2];
71 if (m_pChar == 0) OGDF_THROW(InsufficientMemoryException);
73 m_pChar[0] = c;
74 m_pChar[1] = '\0';
78 String::String(const char *str)
80 m_length = strlen(str);
81 m_pChar = new char[m_length+1];
82 if (m_pChar == 0) OGDF_THROW(InsufficientMemoryException);
84 ogdf::strcpy(m_pChar,m_length+1,str);
88 String::String(size_t maxLen, const char *str)
90 m_length = maxLen;
91 m_pChar = new char[m_length+1];
92 if (m_pChar == 0) OGDF_THROW(InsufficientMemoryException);
94 ogdf::strncpy(m_pChar, m_length+1, str, m_length);
95 m_pChar[m_length] = '\0';
99 String::String(const char *format, ...)
101 va_list argList;
102 va_start(argList,format);
104 m_length = vsprintf(s_pBuffer,format,argList);
105 m_pChar = new char[m_length+1];
106 if (m_pChar == 0) OGDF_THROW(InsufficientMemoryException);
108 strcpy(m_pChar,s_pBuffer);
112 String::String(const String &str)
114 m_length = str.m_length;
115 m_pChar = new char[m_length+1];
116 if (m_pChar == 0) OGDF_THROW(InsufficientMemoryException);
118 ogdf::strcpy(m_pChar,m_length+1,str.m_pChar);
122 String::~String()
124 delete [] m_pChar;
128 String &String::operator =(const String &str)
130 if (&str == this) return *this;
132 delete [] m_pChar;
134 m_length = str.m_length;
135 m_pChar = new char[m_length+1];
136 if (m_pChar == 0) OGDF_THROW(InsufficientMemoryException);
138 ogdf::strcpy(m_pChar,m_length+1,str.m_pChar);
140 return *this;
144 String &String::operator =(const char *str)
146 delete [] m_pChar;
148 m_length = strlen(str);
149 m_pChar = new char[m_length+1];
150 if (m_pChar == 0) OGDF_THROW(InsufficientMemoryException);
152 ogdf::strcpy(m_pChar,m_length+1,str);
154 return *this;
158 String &String::operator +=(const String &str)
160 size_t oldLength = m_length;
161 char *pOldChar = m_pChar;
163 m_length += str.m_length;
164 m_pChar = new char[m_length+1];
165 if (m_pChar == 0) {
166 delete [] pOldChar;
167 OGDF_THROW(InsufficientMemoryException);
170 ogdf::strcpy(m_pChar,m_length+1,pOldChar);
171 ogdf::strcpy(m_pChar+oldLength,m_length+1-oldLength,str.m_pChar);
173 delete [] pOldChar;
175 return *this;
179 void String::sprintf(const char *format, ...)
181 delete [] m_pChar;
183 va_list argList;
184 va_start(argList,format);
186 m_length = ogdf::vsprintf(s_pBuffer,OGDF_STRING_BUFFER_SIZE,format,argList);
187 m_pChar = new char[m_length+1];
188 if (m_pChar == 0) OGDF_THROW(InsufficientMemoryException);
190 ogdf::strcpy(m_pChar,m_length+1,s_pBuffer);
194 int String::compare (const String &x, const String &y)
196 return strcmp(x.m_pChar, y.m_pChar);
200 istream& operator>>(istream& is, String &str)
202 is >> String::s_pBuffer;
203 str = String::s_pBuffer;
204 return is;
207 int DefHashFunc<String>::hash(const String &key) const
209 int hashValue = 0;
210 const char *pChar = key.cstr();
212 while (*pChar)
213 hashValue += int(*pChar++);
215 return hashValue;
219 } // end namespace ogdf