summaryrefslogtreecommitdiff
path: root/src/pal/src/misc/strutil.cpp
blob: 10aa6d179ef4c352e02b3deacdef1d5375fea940 (plain)
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
90
91
92
93
94
95
96
97
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information. 
//

/*++



Module Name:

    strutil.cpp

Abstract:
    Various string-related utility functions



--*/

#include "pal/corunix.hpp"
#include "pal/thread.hpp"
#include "pal/malloc.hpp"
#include "pal/dbgmsg.h"

SET_DEFAULT_DEBUG_CHANNEL(PAL);

using namespace CorUnix;

/*++
Function:
  CPalString::CopyString

  Copies a CPalString into a new (empty) instance, allocating buffer space
  as necessary

Parameters:
  psSource -- the string to copy from
--*/

PAL_ERROR
CPalString::CopyString(
    CPalString *psSource
    )
{
    PAL_ERROR palError = NO_ERROR;
        
    _ASSERTE(NULL != psSource);
    _ASSERTE(NULL == m_pwsz);
    _ASSERTE(0 == m_dwStringLength);
    _ASSERTE(0 == m_dwMaxLength);

    if (0 != psSource->GetStringLength())
    {
        _ASSERTE(psSource->GetMaxLength() > psSource->GetStringLength());
        
        WCHAR *pwsz = reinterpret_cast<WCHAR*>(
            InternalMalloc(psSource->GetMaxLength() * sizeof(WCHAR))
            );

        if (NULL != pwsz)
        {
            _ASSERTE(NULL != psSource->GetString());

            CopyMemory(
                pwsz,
                psSource->GetString(),
                psSource->GetMaxLength() * sizeof(WCHAR)
                );

            m_pwsz = pwsz;
            m_dwStringLength = psSource->GetStringLength();
            m_dwMaxLength = psSource->GetMaxLength();
        }
        else
        {
            palError = ERROR_OUTOFMEMORY;
        }
    }

    return palError;
}

/*++
Function:
  CPalString::FreeBuffer

  Frees the contained string buffer

--*/

void
CPalString::FreeBuffer()
{
    _ASSERTE(NULL != m_pwsz);
    InternalFree(const_cast<WCHAR*>(m_pwsz));
}