summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Studio/Utils/StringLoader.cpp
blob: 73a5324fdb699d6a502d2ecf3aeaaa8e6c633d39 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/****************************************************************************
**
** Copyright (C) 2016 NVIDIA Corporation.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "stdafx.h"
#include "Strings.h"
#include "StringLoader.h"
#include <QtCore/qfile.h>
#include <QtCore/qxmlstream.h>

CStringLoader CStringLoader::s_GlobalInstance;

CStringLoader::CStringLoader()
    : m_Strings(nullptr)
    , m_StringCount(0)
{
}

CStringLoader::~CStringLoader()
{
    UnloadResourceStrings();
}

//=============================================================================
/**
 * Static function to load the string resource specified by inStringID.
 * inStringID should have been specified in the Resource.h header file.
 * This will load the string from the global string resource object.
 */
Q3DStudio::CString CStringLoader::LoadString(long inStringID)
{
    return s_GlobalInstance.LoadResourceString(inStringID);
}

//=============================================================================
/**
 * Load the string resource specified by inStringID.
 * inStringID should have been specified in the Resource.h header file.
 * @param inID the ID of the string to be loaded.
 * @return theString specified by inStringID.
 */
Q3DStudio::CString CStringLoader::LoadResourceString(long inStringID)
{
    Q3DStudio::CString theName;
    // Make sure we aren't going off into la-la land.
    if (inStringID > 0 && inStringID < m_StringCount) {
        theName = m_Strings[inStringID];
    }
    return theName;
}

//=============================================================================
/**
 * Load the string resources from the specified directory into the global table.
 * All .stro files in the directory will be processed for strings.
 * @param inDirectory the directory that should be read for strings.
 */
void CStringLoader::LoadStrings(const CUICFile &inDirectory)
{
    s_GlobalInstance.LoadResourceStrings(inDirectory);
}

//=============================================================================
/**
 * Load the string resources from the specified directory into this string table.
 * All .stro files in the directory will be processed for strings.
 * @param inDirectory the directory that should be read for strings.
 */
void CStringLoader::LoadResourceStrings(const CUICFile &inDirectory)
{
    UnloadResourceStrings();

    // Sure hope we don't get an absolute ton of strings here...
    m_Strings = new Q3DStudio::CString[STRING_RESOURCE_COUNT + 1];
    m_StringCount = STRING_RESOURCE_COUNT;

    Q3DStudio::CString theExtension = ".stro";

    // Go through all the files in the directory and process them.
    CFileIterator theFiles = inDirectory.GetSubItems();
    for (; !theFiles.IsDone(); ++theFiles) {
        CUICFile theFile = theFiles.GetCurrent();
        Q3DStudio::CString theFilename = theFile.GetAbsolutePath();
        // Only process .stro files.
        if (theFilename.Find(theExtension) == theFilename.Length() - theExtension.Length()) {
            QFile file(theFilename.toQString());
            file.open(QFile::ReadOnly);
            QXmlStreamReader reader(&file);
            while (!reader.atEnd()) {
                reader.readNextStartElement();
                if (reader.name() == "string") {
                    Q3DStudio::CString theValue;
                    long theIndex = -1;
                    for (auto attrib : reader.attributes()) {
                        if (attrib.name() == "value")
                            theValue = attrib.value().toUtf8().constData();
                        else if (attrib.name() == "ID")
                            theIndex = attrib.value().toInt();
                    }
                    if (theIndex > 0 && theIndex < STRING_RESOURCE_COUNT) {
                        theValue.Replace("\\n", "\n");
                        theValue.Replace("\\t", "\t");
                        m_Strings[theIndex] = theValue;
                    }
                }
            }
        }
    }
}

void CStringLoader::UnloadStrings()
{
    s_GlobalInstance.UnloadResourceStrings();
}

void CStringLoader::UnloadResourceStrings()
{
    delete[] m_Strings;
    m_Strings = nullptr;
    m_StringCount = 0;
}

Q3DStudio::CString LoadResourceString(long inID)
{
    return CStringLoader::LoadString(inID);
}