summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Common/Code/StrUtilities.h
blob: c56d4b25cea3b48ff0c6817dd92de14628472a27 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/****************************************************************************
**
** Copyright (C) 1999-2001 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$
**
****************************************************************************/
//==============================================================================
//	Prefixes
//==============================================================================
#ifndef STRUTILITIES_H
#define STRUTILITIES_H

//==============================================================================
//	Includes
//==============================================================================
#include <string>
#include <ctype.h>
#include <stddef.h>

//==============================================================================
//	String Conversion Functions
//==============================================================================
//==============================================================================
/**
 *	@functions bunch of stream ins to facilitate putting various different
 *	types into std::string and std::wstring
 *
 */
std::wstring &operator<<(std::wstring &inStr, bool inBool);
std::wstring &operator<<(std::wstring &inStr, long inLong);
std::wstring &operator<<(std::wstring &inStr, double inDouble);
std::wstring &operator<<(std::wstring &inStr, const char *inStr02);
std::wstring &operator<<(std::wstring &inStr, const wchar_t *inStr02);
std::wstring &operator<<(std::wstring &inStr, const std::string &inStr02);
std::wstring &operator<<(std::wstring &inStr, const std::wstring &inStr02);

std::string &operator<<(std::string &inStr, bool inBool);
std::string &operator<<(std::string &inStr, long inLong);
std::string &operator<<(std::string &inStr, double inDouble);
std::string &operator<<(std::string &inStr, const char *inStr02);
std::string &operator<<(std::string &inStr, const wchar_t *inStr02);
std::string &operator<<(std::string &inStr, const std::string &inStr02);
std::string &operator<<(std::string &inStr, const std::wstring &inStr02);

//==============================================================================
/**
 *	Compares 2 strings in a case-insensitive way.
 *	operator== should be reserved for case sensitive compare,
 *	so this leaves us with this ugly function name to use.
 *
 *	@param		inString1		The first string to be compared
 *	@param		inString2		The second string to be compared
 *
 *	@return		Returns true if both strings are equal, false otherwise
 */
template <class T, class U>
bool CaseInsensitiveEqual(const std::basic_string<T> &inString1,
                          const std::basic_string<U> &inString2)
{
    bool theResult = true;

    typename std::basic_string<T>::const_iterator theIterator1 = inString1.begin();
    typename std::basic_string<U>::const_iterator theIterator2 = inString2.begin();

    while (theIterator1 != inString1.end() && theIterator2 != inString2.end()) {
        if (toupper(*theIterator1++) != toupper(*theIterator2++)) {
            theResult = false;
            theIterator1 = inString1.end(); // break
        }
    }

    if (theIterator1 != inString1.end() || theIterator2 != inString2.end()) {
        theResult = false;
    }

    return theResult;
}

//==============================================================================
/**
 *	Converts a string to upper-case.
 *
 *	@param		ioString		The string
 *
 */
template <class T>
void ToUpperCase(std::basic_string<T> &ioString)
{
    typename std::basic_string<T>::iterator theIterator;
    for (theIterator = ioString.begin(); theIterator != ioString.end(); ++theIterator) {
        *theIterator = static_cast<T>(toupper(*theIterator));
    }
}

//==============================================================================
/**
 *	Finds a string in a case-insensitive way.
 *
 *	@param		inString1		The source string
 *	@param		inString2		The string to find
 *
 *	@return		Returns the pos in inString1 where inString2 is found, npos if not found.
 */
template <class T, class U>
size_t CaseInsensitiveFind(const std::basic_string<T> &inString1,
                           const std::basic_string<U> &inString2)
{
    size_t theResult = std::basic_string<T>::npos;

    std::basic_string<T> theString1(inString1);
    std::basic_string<T> theString2;
    theString2 << inString2;

    // convert both to upper-case
    ToUpperCase<T>(theString1);
    ToUpperCase<T>(theString2);

    theResult = theString1.find(theString2);

    return theResult;
}

//==============================================================================
/**
 * Returns the actual path of the string.
 *
 *	@param	inString		the string to extract the path
 *	@param	outString		the string to gett the path
 *
 *	@return	the path, if there is one, '.' otherwise.
 */
template <class T, class U>
void GetActualPath(const std::basic_string<T> &inString, std::basic_string<U> &outString)
{
    outString.clear();

    size_t thePos = inString.find_last_of("/\\");
    if (thePos != std::string::npos) {
        for (std::string::const_iterator theIter = inString.begin();
             theIter != inString.begin() + thePos; ++theIter) {
            outString.push_back(*theIter);
        }
    } else {
        outString.push_back('.');
    }
}

//==============================================================================
/**
 * Search for tokens and replace them.
 *
 *	@param	inToBeReplaced		the tokens to be replaced
 *	@param	inToBeReplacedBy	the tokens to be replaced with
 *	@param	outString			the string to be operated on
 *
 */
template <class T>
void ReplaceString(const std::basic_string<T> &inToBeReplaced,
                   const std::basic_string<T> &inToBeReplacedBy, std::basic_string<T> &outString)
{
    size_t thePos = outString.find_first_of(inToBeReplaced);
    while (thePos != std::basic_string<T>::npos) {
        outString.replace(outString.begin() + thePos, outString.begin() + thePos + 1,
                          inToBeReplacedBy.begin(), inToBeReplacedBy.end());
        thePos = outString.find_first_of(inToBeReplaced, thePos);
    }
}

#endif