1 | // Licensed to the .NET Foundation under one or more agreements. |
---|---|
2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | // See the LICENSE file in the project root for more information. |
4 | |
5 | /*++ |
6 | |
7 | |
8 | |
9 | Module Name: |
10 | |
11 | strutil.cpp |
12 | |
13 | Abstract: |
14 | Various string-related utility functions |
15 | |
16 | |
17 | |
18 | --*/ |
19 | |
20 | #include "pal/corunix.hpp" |
21 | #include "pal/thread.hpp" |
22 | #include "pal/malloc.hpp" |
23 | #include "pal/dbgmsg.h" |
24 | |
25 | SET_DEFAULT_DEBUG_CHANNEL(PAL); |
26 | |
27 | using namespace CorUnix; |
28 | |
29 | /*++ |
30 | Function: |
31 | CPalString::CopyString |
32 | |
33 | Copies a CPalString into a new (empty) instance, allocating buffer space |
34 | as necessary |
35 | |
36 | Parameters: |
37 | psSource -- the string to copy from |
38 | --*/ |
39 | |
40 | PAL_ERROR |
41 | CPalString::CopyString( |
42 | CPalString *psSource |
43 | ) |
44 | { |
45 | PAL_ERROR palError = NO_ERROR; |
46 | |
47 | _ASSERTE(NULL != psSource); |
48 | _ASSERTE(NULL == m_pwsz); |
49 | _ASSERTE(0 == m_dwStringLength); |
50 | _ASSERTE(0 == m_dwMaxLength); |
51 | |
52 | if (0 != psSource->GetStringLength()) |
53 | { |
54 | _ASSERTE(psSource->GetMaxLength() > psSource->GetStringLength()); |
55 | |
56 | WCHAR *pwsz = reinterpret_cast<WCHAR*>( |
57 | InternalMalloc(psSource->GetMaxLength() * sizeof(WCHAR)) |
58 | ); |
59 | |
60 | if (NULL != pwsz) |
61 | { |
62 | _ASSERTE(NULL != psSource->GetString()); |
63 | |
64 | CopyMemory( |
65 | pwsz, |
66 | psSource->GetString(), |
67 | psSource->GetMaxLength() * sizeof(WCHAR) |
68 | ); |
69 | |
70 | m_pwsz = pwsz; |
71 | m_dwStringLength = psSource->GetStringLength(); |
72 | m_dwMaxLength = psSource->GetMaxLength(); |
73 | } |
74 | else |
75 | { |
76 | palError = ERROR_OUTOFMEMORY; |
77 | } |
78 | } |
79 | |
80 | return palError; |
81 | } |
82 | |
83 | /*++ |
84 | Function: |
85 | CPalString::FreeBuffer |
86 | |
87 | Frees the contained string buffer |
88 | |
89 | --*/ |
90 | |
91 | void |
92 | CPalString::FreeBuffer() |
93 | { |
94 | _ASSERTE(NULL != m_pwsz); |
95 | free(const_cast<WCHAR*>(m_pwsz)); |
96 | } |
97 |