9601ca75b26dc98b00d89c22f986de1d728292ec
2 ** NetXMS - Network Management System
3 ** NetXMS Foundation Library
4 ** Copyright (C) 2003, 2004, 2005, 2006, 2007 Victor Kirhenshtein
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 of the License, or
9 ** (at your option) any later version.
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "libnetxms.h"
31 const int String::npos
= -1;
51 safe_free(m_pszBuffer
);
59 const String
& String::operator =(const TCHAR
*pszStr
)
61 safe_free(m_pszBuffer
);
62 m_pszBuffer
= _tcsdup(CHECK_NULL_EX(pszStr
));
63 m_dwBufSize
= (DWORD
)_tcslen(CHECK_NULL_EX(pszStr
)) + 1;
72 const String
& String::operator +=(const TCHAR
*pszStr
)
78 dwLen
= (DWORD
)_tcslen(pszStr
);
79 m_pszBuffer
= (TCHAR
*)realloc(m_pszBuffer
, (m_dwBufSize
+ dwLen
) * sizeof(TCHAR
));
80 _tcscpy(&m_pszBuffer
[m_dwBufSize
- 1], pszStr
);
88 // Add formatted string to the end of buffer
91 void String::AddFormattedString(const TCHAR
*pszFormat
, ...)
97 nLen
= (int)_tcslen(pszFormat
) + NumChars(pszFormat
, _T('%')) * 1024;
98 pszBuffer
= (TCHAR
*)malloc(nLen
* sizeof(TCHAR
));
99 va_start(args
, pszFormat
);
100 _vsntprintf(pszBuffer
, nLen
, pszFormat
, args
);
108 // Add formatted string to the end of buffer
111 void String::AddString(const TCHAR
*pStr
, DWORD dwSize
)
113 m_pszBuffer
= (TCHAR
*)realloc(m_pszBuffer
, (m_dwBufSize
+ dwSize
) * sizeof(TCHAR
));
114 memcpy(&m_pszBuffer
[m_dwBufSize
- 1], pStr
, dwSize
* sizeof(TCHAR
));
115 m_dwBufSize
+= dwSize
;
116 m_pszBuffer
[m_dwBufSize
- 1] = 0;
121 // Escape given character
124 void String::EscapeCharacter(int ch
, int esc
)
129 if (m_pszBuffer
== NULL
)
132 nCount
= NumChars(m_pszBuffer
, ch
);
136 m_dwBufSize
+= nCount
;
137 m_pszBuffer
= (TCHAR
*)realloc(m_pszBuffer
, m_dwBufSize
* sizeof(TCHAR
));
138 for(i
= 0; m_pszBuffer
[i
] != 0; i
++)
140 if (m_pszBuffer
[i
] == ch
)
142 memmove(&m_pszBuffer
[i
+ 1], &m_pszBuffer
[i
], (m_dwBufSize
- i
- 1) * sizeof(TCHAR
));
143 m_pszBuffer
[i
] = esc
;
151 // Set dynamically allocated string as a new buffer
154 void String::SetBuffer(TCHAR
*pszBuffer
)
156 safe_free(m_pszBuffer
);
157 m_pszBuffer
= pszBuffer
;
158 m_dwBufSize
= (m_pszBuffer
!= NULL
) ? (DWORD
)_tcslen(m_pszBuffer
) + 1 : 1;
163 // Translate given substring
166 void String::Translate(const TCHAR
*pszSrc
, const TCHAR
*pszDst
)
168 DWORD i
, dwLenSrc
, dwLenDst
, dwDelta
;
170 if (m_pszBuffer
== NULL
)
173 dwLenSrc
= (DWORD
)_tcslen(pszSrc
);
174 dwLenDst
= (DWORD
)_tcslen(pszDst
);
176 if (m_dwBufSize
<= dwLenSrc
)
179 for(i
= 0; i
< m_dwBufSize
- dwLenSrc
; i
++)
181 if (!memcmp(pszSrc
, &m_pszBuffer
[i
], dwLenSrc
* sizeof(TCHAR
)))
183 if (dwLenSrc
== dwLenDst
)
185 memcpy(&m_pszBuffer
[i
], pszDst
, dwLenDst
* sizeof(TCHAR
));
188 else if (dwLenSrc
> dwLenDst
)
190 memcpy(&m_pszBuffer
[i
], pszDst
, dwLenDst
* sizeof(TCHAR
));
192 dwDelta
= dwLenSrc
- dwLenDst
;
193 m_dwBufSize
-= dwDelta
;
194 memmove(&m_pszBuffer
[i
], &m_pszBuffer
[i
+ dwDelta
], (m_dwBufSize
- i
) * sizeof(TCHAR
));
199 dwDelta
= dwLenDst
- dwLenSrc
;
200 m_pszBuffer
= (TCHAR
*)realloc(m_pszBuffer
, (m_dwBufSize
+ dwDelta
) * sizeof(TCHAR
));
201 memmove(&m_pszBuffer
[i
+ dwLenDst
], &m_pszBuffer
[i
+ dwLenSrc
], (m_dwBufSize
- i
- dwLenSrc
) * sizeof(TCHAR
));
202 m_dwBufSize
+= dwDelta
;
203 memcpy(&m_pszBuffer
[i
], pszDst
, dwLenDst
* sizeof(TCHAR
));
212 // Extract substring into buffer
215 TCHAR
*String::SubStr(int nStart
, int nLen
, TCHAR
*pszBuffer
)
220 if ((nStart
< (int)m_dwBufSize
- 1) && (nStart
>= 0))
224 nCount
= (int)m_dwBufSize
- nStart
- 1;
228 nCount
= min(nLen
, (int)m_dwBufSize
- nStart
- 1);
230 pszOut
= (pszBuffer
!= NULL
) ? pszBuffer
: (TCHAR
*)malloc((nCount
+ 1) * sizeof(TCHAR
));
231 memcpy(pszOut
, &m_pszBuffer
[nStart
], nCount
* sizeof(TCHAR
));
236 pszOut
= (pszBuffer
!= NULL
) ? pszBuffer
: (TCHAR
*)malloc(sizeof(TCHAR
));
244 // Find substring in a string
247 int String::Find(const TCHAR
*pszStr
, int nStart
)
251 if ((nStart
>= (int)m_dwBufSize
- 1) || (nStart
< 0))
254 p
= _tcsstr(&m_pszBuffer
[nStart
], pszStr
);
255 return (p
!= NULL
) ? (int)(((char *)p
- (char *)m_pszBuffer
) / sizeof(TCHAR
)) : npos
;
260 // Strip leading and trailing spaces
265 if (m_pszBuffer
!= NULL
)
267 StrStrip(m_pszBuffer
);
268 m_dwBufSize
= (DWORD
)_tcslen(m_pszBuffer
) + 1;
274 // Shring string by removing trailing characters
277 void String::Shrink(int chars
)
281 m_dwBufSize
-= min(m_dwBufSize
- 1, (DWORD
)chars
);
282 if (m_pszBuffer
!= NULL
)
283 m_pszBuffer
[m_dwBufSize
- 1] = 0;