1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "common/algorithm.h"
24 #include "titanic/support/string.h"
25 
26 namespace Titanic {
27 
CString(char c,uint32 len)28 CString::CString(char c, uint32 len) : Common::String() {
29 	ensureCapacity(len, false);
30 	for (uint idx = 0; idx < len; ++idx)
31 		(*this) += c;
32 }
33 
CString(int val)34 CString::CString(int val) : Common::String() {
35 	*this = CString::format("%d", val);
36 }
37 
left(uint count) const38 CString CString::left(uint count) const {
39 	return (count > size()) ? CString() : CString(c_str(), c_str() + count);
40 }
41 
right(uint count) const42 CString CString::right(uint count) const {
43 	uint strSize = size();
44 	return (count > strSize) ? CString() :
45 		CString(c_str() + strSize - count, c_str() + strSize);
46 }
47 
mid(uint start,uint count) const48 CString CString::mid(uint start, uint count) const {
49 	if (start >= size())
50 		return CString();
51 	else
52 		return CString(c_str() + start, MIN(count, size() - start));
53 }
54 
mid(uint start) const55 CString CString::mid(uint start) const {
56 	uint strSize = size();
57 	assert(start <= strSize);
58 	return mid(start, strSize - start);
59 }
60 
deleteRight(uint count) const61 CString CString::deleteRight(uint count) const {
62 	return (count >= size()) ? CString() : left(size() - count);
63 }
64 
indexOf(char c) const65 int CString::indexOf(char c) const {
66 	const char *charP = strchr(c_str(), c);
67 	return charP ? charP - c_str() : -1;
68 }
69 
indexOf(const char * s) const70 int CString::indexOf(const char *s) const {
71 	const char *strP = strstr(c_str(), s);
72 	return strP ? strP - c_str() : -1;
73 }
74 
lastIndexOf(char c) const75 int CString::lastIndexOf(char c) const {
76 	const char *charP = strrchr(c_str(), c);
77 	return charP ? charP - c_str() : -1;
78 }
79 
containsIgnoreCase(const CString & str) const80 bool CString::containsIgnoreCase(const CString &str) const {
81 	CString lowerStr = *this;
82 	CString subStr = str;
83 	lowerStr.toLowercase();
84 	subStr.toLowercase();
85 
86 	return lowerStr.contains(subStr);
87 }
88 
fileTypeSuffix() const89 FileType CString::fileTypeSuffix() const {
90 	CString ext = right(1);
91 	if (ext == "0" || ext == "4")
92 		return FILETYPE_IMAGE;
93 	else if (ext == "1")
94 		return FILETYPE_WAV;
95 	else if (ext == "2" || ext == "3")
96 		return FILETYPE_MOVIE;
97 
98 	ext = right(3);
99 	if (ext == "tga" || ext == "jpg")
100 		return FILETYPE_IMAGE;
101 	else if (ext == "wav")
102 		return FILETYPE_WAV;
103 	else if (ext == "avi" || ext == "mov")
104 		return FILETYPE_MOVIE;
105 	else if (ext == "dlg")
106 		return FILETYPE_DLG;
107 	else
108 		return FILETYPE_UNKNOWN;
109 }
110 
imageTypeSuffix() const111 ImageType CString::imageTypeSuffix() const {
112 	CString ext = right(1);
113 	if (ext == "0")
114 		return IMAGETYPE_TARGA;
115 	else if (ext == "4")
116 		return IMAGETYPE_JPEG;
117 
118 	ext = right(3);
119 	if (ext == "tga")
120 		return IMAGETYPE_TARGA;
121 	else if (ext == "jpg")
122 		return IMAGETYPE_JPEG;
123 	else
124 		return IMAGETYPE_UNKNOWN;
125 }
126 
format(const char * fmt,...)127 CString CString::format(const char *fmt, ...) {
128 	String output;
129 
130 	va_list va;
131 	va_start(va, fmt);
132 	output = String::vformat(fmt, va);
133 	va_end(va);
134 
135 	return output;
136 }
137 
operator ==(const CString & x) const138 bool CString::operator==(const CString &x) const {
139 	return compareToIgnoreCase(x) == 0;
140 }
141 
operator ==(const char * x) const142 bool CString::operator==(const char *x) const {
143 	return compareToIgnoreCase(x) == 0;
144 }
145 
operator !=(const CString & x) const146 bool CString::operator!=(const CString &x) const {
147 	return compareToIgnoreCase(x) != 0;
148 }
149 
operator !=(const char * x) const150 bool CString::operator!=(const char *x) const {
151 	return compareToIgnoreCase(x) != 0;
152 }
153 
154 } // End of namespace Titanic
155