6 * $Date: 2012-07-07 17:14:54 +0200 (Sa, 07. Jul 2012) $
7 ***************************************************************/
10 * \brief Implementation of class String
12 * \author Carsten Gutwenger
15 * This file is part of the Open Graph Drawing Framework (OGDF).
19 * See README.txt in the root directory of the OGDF installation for details.
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
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.
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>
54 char String::s_pBuffer
[OGDF_STRING_BUFFER_SIZE
];
59 m_pChar
= new char[1];
60 if (m_pChar
== 0) OGDF_THROW(InsufficientMemoryException
);
67 String::String(const char c
)
70 m_pChar
= new char[2];
71 if (m_pChar
== 0) OGDF_THROW(InsufficientMemoryException
);
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
)
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, ...)
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
);
128 String
&String::operator =(const String
&str
)
130 if (&str
== this) return *this;
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
);
144 String
&String::operator =(const char *str
)
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
);
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];
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
);
179 void String::sprintf(const char *format
, ...)
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
;
207 int DefHashFunc
<String
>::hash(const String
&key
) const
210 const char *pChar
= key
.cstr();
213 hashValue
+= int(*pChar
++);
219 } // end namespace ogdf