-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCStr.cpp
More file actions
89 lines (78 loc) · 1.83 KB
/
CStr.cpp
File metadata and controls
89 lines (78 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//
// Stand-alone mini-CString
//
// Copyright (c) 2004, Algin Technology LLC
// Written by Alan Klietz
// Distributed under GNU General Public License version 2.
//
// $Id: CStr.cpp,v 1.2 2007/11/06 00:30:04 cvsalan Exp $
//
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <crtdbg.h>
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
#define NEED_CSTR_H
#include "windows-support.h"
EXPORT CString::CString(const CString& strSrc) {
*this = strSrc.GetData(); // invokes CString::operator=(LPCTSTR sz)
}
EXPORT CString::CString(LPCSTR sz)
{
int iLen = (sz == NULL ? 0 : (int)strlen(sz));
if (iLen == 0) {
m_szBuf = NULL;
m_iLen = 0;
} else {
_alloc(iLen);
memcpy(m_szBuf, sz, iLen);
}
}
void CString::_alloc(int iLen)
{
if (iLen <= 0) {
m_szBuf = NULL;
m_iLen = 0 ;
} else {
m_iLen = iLen;
m_szBuf = new char[iLen+1]; // add space for terminating \0
m_szBuf[iLen] = '\0';
}
}
EXPORT CString::~CString()
{
if (m_szBuf == NULL) return;
delete [] m_szBuf; m_szBuf = NULL;
}
EXPORT CString& CString::operator=(const CString& stringSrc)
{
if (m_szBuf != stringSrc.GetData()) {
if (m_szBuf != NULL) {
delete [] m_szBuf; m_szBuf = NULL;
}
int nSrcLen = stringSrc.GetLength();
_alloc(nSrcLen);
if (nSrcLen > 0) {
memcpy(m_szBuf, stringSrc.GetData(), nSrcLen);
}
}
return *this;
}
EXPORT CString& CString::operator=(LPCSTR sz)
{
int nSrcLen = (sz == NULL ? 0 : strlen(sz));
_alloc(nSrcLen);
if (nSrcLen > 0) {
memcpy(m_szBuf, sz, nSrcLen);
}
return *this;
}
EXPORT BOOL operator==(const CString& s1, const CString& s2)
{ return s1.Equal(s2) == 0; }
EXPORT BOOL operator!=(const CString& s1, const CString& s2)
{ return s1.Equal(s2) != 0; }
/*
vim:tabstop=4:shiftwidth=4:noexpandtab
*/