Linux build fix. stricmp does not exist on linux. Also a small tweak to the StringF...
[dolphin.git] / Source / Core / Common / Src / StringUtil.cpp
blob707d519577d3873daba3f5ee2c48a4dad420099d
1 // Copyright (C) 2003 Dolphin Project.
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, version 2.0.
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 // GNU General Public License 2.0 for more details.
12 // A copy of the GPL 2.0 should have been included with the program.
13 // If not, see http://www.gnu.org/licenses/
15 // Official SVN repository and contact information can be found at
16 // http://code.google.com/p/dolphin-emu/
18 #include <stdlib.h>
19 #include <stdio.h>
21 #include "Common.h"
22 #include "CommonPaths.h"
23 #include "StringUtil.h"
25 // faster than sscanf
26 bool AsciiToHex(const char* _szValue, u32& result)
28 char *endptr = NULL;
29 const u32 value = strtoul(_szValue, &endptr, 16);
31 if (!endptr || *endptr)
32 return false;
34 result = value;
35 return true;
38 bool CharArrayFromFormatV(char* out, int outsize, const char* format, va_list args)
40 int writtenCount = vsnprintf(out, outsize, format, args);
42 if (writtenCount > 0 && writtenCount < outsize)
44 out[writtenCount] = '\0';
45 return true;
47 else
49 out[outsize - 1] = '\0';
50 return false;
54 std::string StringFromFormat(const char* format, ...)
56 int writtenCount = -1;
57 size_t newSize = strlen(format) + 4;
58 char *buf = NULL;
59 va_list args;
60 while (writtenCount < 0)
62 delete[] buf;
63 buf = new char[newSize + 1];
65 va_start(args, format);
66 writtenCount = vsnprintf(buf, newSize, format, args);
67 va_end(args);
69 #ifndef _WIN32
70 // vsnprintf does not return -1 on truncation in linux!
71 // Instead it returns the size of the string we need.
72 if (writtenCount >= (int)newSize)
73 newSize = writtenCount;
74 #else
75 newSize *= 2;
76 #endif
79 buf[writtenCount] = '\0';
80 std::string temp = buf;
81 delete[] buf;
82 return temp;
85 // For Debugging. Read out an u8 array.
86 std::string ArrayToString(const u8 *data, u32 size, int line_len, bool spaces)
88 std::ostringstream oss;
89 oss << std::setfill('0') << std::hex;
91 for (int line = 0; size; ++data, --size)
93 oss << std::setw(2) << (int)*data;
95 if (line_len == ++line)
97 oss << '\n';
98 line = 0;
100 else if (spaces)
101 oss << ' ';
104 return oss.str();
107 // Turns " hej " into "hej". Also handles tabs.
108 std::string StripSpaces(const std::string &str)
110 const size_t s = str.find_first_not_of(" \t\r\n");
112 if (str.npos != s)
113 return str.substr(s, str.find_last_not_of(" \t\r\n") - s + 1);
114 else
115 return "";
118 // "\"hello\"" is turned to "hello"
119 // This one assumes that the string has already been space stripped in both
120 // ends, as done by StripSpaces above, for example.
121 std::string StripQuotes(const std::string& s)
123 if (s.size() && '\"' == s[0] && '\"' == *s.rbegin())
124 return s.substr(1, s.size() - 2);
125 else
126 return s;
129 // "\"hello\"" is turned to "hello"
130 // This one assumes that the string has already been space stripped in both
131 // ends, as done by StripSpaces above, for example.
132 std::string StripNewline(const std::string& s)
134 if (s.size() && '\n' == *s.rbegin())
135 return s.substr(0, s.size() - 1);
136 else
137 return s;
140 bool TryParse(const std::string &str, u32 *const output)
142 char *endptr = NULL;
143 u32 value = strtoul(str.c_str(), &endptr, 0);
145 if (!endptr || *endptr)
146 return false;
148 *output = value;
149 return true;
152 bool TryParse(const std::string &str, bool *const output)
154 if ('1' == str[0] || !strcasecmp("true", str.c_str()))
155 *output = true;
156 else if ('0' == str[0] || !strcasecmp("false", str.c_str()))
157 *output = false;
158 else
159 return false;
161 return true;
164 std::string StringFromInt(int value)
166 char temp[16];
167 sprintf(temp, "%i", value);
168 return temp;
171 std::string StringFromBool(bool value)
173 return value ? "True" : "False";
176 bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _pFilename, std::string* _pExtension)
178 if (full_path.empty())
179 return false;
181 size_t dir_end = full_path.find_last_of("/"
182 // windows needs the : included for something like just "C:" to be considered a directory
183 #ifdef _WIN32
185 #endif
187 if (std::string::npos == dir_end)
188 dir_end = 0;
189 else
190 dir_end += 1;
192 size_t fname_end = full_path.rfind('.');
193 if (fname_end < dir_end || std::string::npos == fname_end)
194 fname_end = full_path.size();
196 if (_pPath)
197 *_pPath = full_path.substr(0, dir_end);
199 if (_pFilename)
200 *_pFilename = full_path.substr(dir_end, fname_end - dir_end);
202 if (_pExtension)
203 *_pExtension = full_path.substr(fname_end);
205 return true;
208 std::string PathToFilename(const std::string &Path)
210 std::string Name, Ending;
211 SplitPath(Path, 0, &Name, &Ending);
212 return Name + Ending;
215 void BuildCompleteFilename(std::string& _CompleteFilename, const std::string& _Path, const std::string& _Filename)
217 _CompleteFilename = _Path;
219 // check for seperator
220 if (DIR_SEP_CHR != *_CompleteFilename.rbegin())
221 _CompleteFilename += DIR_SEP_CHR;
223 // add the filename
224 _CompleteFilename += _Filename;
227 void SplitString(const std::string& str, const char delim, std::vector<std::string>& output)
229 std::istringstream iss(str);
230 output.resize(1);
232 while (std::getline(iss, *output.rbegin(), delim))
233 output.push_back("");
235 output.pop_back();
238 std::string TabsToSpaces(int tab_size, const std::string &in)
240 const std::string spaces(tab_size, ' ');
241 std::string out(in);
243 size_t i = 0;
244 while (out.npos != (i = out.find('\t')))
245 out.replace(i, 1, spaces);
247 return out;