summaryrefslogtreecommitdiff
path: root/src/utilcode/sstring_com.cpp
blob: ada4c9c40673c428ab76e0d8712963d11e87610c (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
98
99
100
101
102
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
// ---------------------------------------------------------------------------
// SString_COM.cpp

// ---------------------------------------------------------------------------

#include "stdafx.h"
#include "sstring.h"
#include "ex.h"
#include "holder.h"

#define DEFAULT_RESOURCE_STRING_SIZE 255

//----------------------------------------------------------------------------
// Load the string resource into this string.
//----------------------------------------------------------------------------
BOOL SString::LoadResource(CCompRC::ResourceCategory eCategory, int resourceID)
{
    return SUCCEEDED(LoadResourceAndReturnHR(eCategory, resourceID));
}

HRESULT SString::LoadResourceAndReturnHR(CCompRC::ResourceCategory eCategory, int resourceID)
{
    WRAPPER_NO_CONTRACT;
    return LoadResourceAndReturnHR(NULL, eCategory,resourceID);
}

HRESULT SString::LoadResourceAndReturnHR(CCompRC* pResourceDLL, CCompRC::ResourceCategory eCategory, int resourceID)
{
    CONTRACT(BOOL)
    {
        INSTANCE_CHECK;
        NOTHROW;
    }
    CONTRACT_END;

    HRESULT hr = E_FAIL;

#ifndef FEATURE_UTILCODE_NO_DEPENDENCIES
    if (pResourceDLL == NULL) 
    {
        pResourceDLL = CCompRC::GetDefaultResourceDll();
    }
    
    if (pResourceDLL != NULL)
    {
 
        int size = 0;

        EX_TRY
        {
            if (GetRawCount() == 0)
                Resize(DEFAULT_RESOURCE_STRING_SIZE, REPRESENTATION_UNICODE);

            while (TRUE)
            {
                // First try and load the string in the amount of space that we have.
                // In fatal error reporting scenarios, we may not have enough memory to 
                // allocate a larger buffer.
            
                hr = pResourceDLL->LoadString(eCategory, resourceID, GetRawUnicode(), GetRawCount()+1, &size);
                if (hr != HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER))
                {
                    if (FAILED(hr))
                    {
                        Clear();
                        break;
                    }

                    // Although we cannot generally detect truncation, we can tell if we
                    // used up all the space (in which case we will assume truncation.)
                    if (size < (int)GetRawCount())
                    {
                        break;
                    }
                }

                // Double the size and try again.
                Resize(size*2, REPRESENTATION_UNICODE);

            }

            if (SUCCEEDED(hr))
            {
                Truncate(Begin() + (COUNT_T) wcslen(GetRawUnicode()));
            }

            Normalize();
            
        }
        EX_CATCH
        {
            hr = E_FAIL;
        }
        EX_END_CATCH(SwallowAllExceptions);
    }
#endif //!FEATURE_UTILCODE_NO_DEPENDENCIES
    
    RETURN hr;
} // SString::LoadResourceAndReturnHR