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/
22 #include "CommonPaths.h"
23 #include "StringUtil.h"
26 bool AsciiToHex(const char* _szValue
, u32
& result
)
29 const u32 value
= strtoul(_szValue
, &endptr
, 16);
31 if (!endptr
|| *endptr
)
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';
49 out
[outsize
- 1] = '\0';
54 std::string
StringFromFormat(const char* format
, ...)
56 int writtenCount
= -1;
57 size_t newSize
= strlen(format
) + 4;
60 while (writtenCount
< 0)
63 buf
= new char[newSize
+ 1];
65 va_start(args
, format
);
66 writtenCount
= vsnprintf(buf
, newSize
, format
, args
);
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
;
79 buf
[writtenCount
] = '\0';
80 std::string temp
= buf
;
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
)
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");
113 return str
.substr(s
, str
.find_last_not_of(" \t\r\n") - s
+ 1);
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);
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);
140 bool TryParse(const std::string
&str
, u32
*const output
)
143 u32 value
= strtoul(str
.c_str(), &endptr
, 0);
145 if (!endptr
|| *endptr
)
152 bool TryParse(const std::string
&str
, bool *const output
)
154 if ('1' == str
[0] || !strcasecmp("true", str
.c_str()))
156 else if ('0' == str
[0] || !strcasecmp("false", str
.c_str()))
164 std::string
StringFromInt(int value
)
167 sprintf(temp
, "%i", value
);
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())
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
187 if (std::string::npos
== dir_end
)
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();
197 *_pPath
= full_path
.substr(0, dir_end
);
200 *_pFilename
= full_path
.substr(dir_end
, fname_end
- dir_end
);
203 *_pExtension
= full_path
.substr(fname_end
);
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
;
224 _CompleteFilename
+= _Filename
;
227 void SplitString(const std::string
& str
, const char delim
, std::vector
<std::string
>& output
)
229 std::istringstream
iss(str
);
232 while (std::getline(iss
, *output
.rbegin(), delim
))
233 output
.push_back("");
238 std::string
TabsToSpaces(int tab_size
, const std::string
&in
)
240 const std::string
spaces(tab_size
, ' ');
244 while (out
.npos
!= (i
= out
.find('\t')))
245 out
.replace(i
, 1, spaces
);