| 
									
										
										
										
											2009-03-28 08:57:34 +00:00
										 |  |  | // Copyright (C) 2003-2009 Dolphin Project.
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // This program is free software: you can redistribute it and/or modify
 | 
					
						
							|  |  |  | // it under the terms of the GNU General Public License as published by
 | 
					
						
							|  |  |  | // the Free Software Foundation, version 2.0.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful,
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
					
						
							|  |  |  | // GNU General Public License 2.0 for more details.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // A copy of the GPL 2.0 should have been included with the program.
 | 
					
						
							|  |  |  | // If not, see http://www.gnu.org/licenses/
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Official SVN repository and contact information can be found at
 | 
					
						
							|  |  |  | // http://code.google.com/p/dolphin-emu/
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "StringUtil.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // faster than sscanf
 | 
					
						
							|  |  |  | bool AsciiToHex(const char* _szValue, u32& result) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	u32 value = 0; | 
					
						
							|  |  |  | 	size_t finish = strlen(_szValue); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (finish > 8) | 
					
						
							|  |  |  | 		finish = 8;  // Max 32-bit values are supported.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (size_t count = 0; count < finish; count++) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		value <<= 4; | 
					
						
							|  |  |  | 		switch (_szValue[count]) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 		    case '0': break; | 
					
						
							|  |  |  | 		    case '1': value += 1; break; | 
					
						
							|  |  |  | 		    case '2': value += 2; break; | 
					
						
							|  |  |  | 		    case '3': value += 3; break; | 
					
						
							|  |  |  | 		    case '4': value += 4; break; | 
					
						
							|  |  |  | 		    case '5': value += 5; break; | 
					
						
							|  |  |  | 		    case '6': value += 6; break; | 
					
						
							|  |  |  | 		    case '7': value += 7; break; | 
					
						
							|  |  |  | 		    case '8': value += 8; break; | 
					
						
							|  |  |  | 		    case '9': value += 9; break; | 
					
						
							|  |  |  | 		    case 'A': | 
					
						
							|  |  |  | 		    case 'a': value += 10; break; | 
					
						
							|  |  |  | 		    case 'B': | 
					
						
							|  |  |  | 		    case 'b': value += 11; break; | 
					
						
							|  |  |  | 		    case 'C': | 
					
						
							|  |  |  | 		    case 'c': value += 12; break; | 
					
						
							|  |  |  | 		    case 'D': | 
					
						
							|  |  |  | 		    case 'd': value += 13; break; | 
					
						
							|  |  |  | 		    case 'E': | 
					
						
							|  |  |  | 		    case 'e': value += 14; break; | 
					
						
							|  |  |  | 		    case 'F': | 
					
						
							|  |  |  | 		    case 'f': value += 15; break; | 
					
						
							|  |  |  | 		    default: | 
					
						
							|  |  |  | 			    return false; | 
					
						
							|  |  |  | 			    break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	result = value; | 
					
						
							|  |  |  | 	return (true); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-20 03:13:22 +00:00
										 |  |  | // Convert AB to it's ascii table entry numbers 0x4142
 | 
					
						
							|  |  |  | u32 Ascii2Hex(std::string _Text) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	// Reset the return value zero
 | 
					
						
							|  |  |  | 	u32 Result = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Max 32-bit values are supported
 | 
					
						
							| 
									
										
										
										
											2009-02-22 12:43:25 +00:00
										 |  |  | 	size_t Length = _Text.length(); | 
					
						
							| 
									
										
										
										
											2009-02-22 11:01:08 +00:00
										 |  |  | 	if (Length > 4) | 
					
						
							|  |  |  | 		Length = 4; | 
					
						
							| 
									
										
										
										
											2009-02-20 03:13:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	for (int i = 0; i < (int)Length; i++) | 
					
						
							| 
									
										
										
										
											2009-02-20 03:13:22 +00:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		// Add up the values, for example RSPE becomes, 0x52000000, then 0x52530000 and so on
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 		Result += _Text.c_str()[i] << ((Length - 1 - i) * 8); | 
					
						
							| 
									
										
										
										
											2009-02-20 03:13:22 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	// Return the value
 | 
					
						
							|  |  |  | 	return Result; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2009-02-22 11:01:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-20 03:13:22 +00:00
										 |  |  | // Convert it back again
 | 
					
						
							|  |  |  | std::string Hex2Ascii(u32 _Text) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	// Create temporary storate
 | 
					
						
							| 
									
										
										
										
											2009-02-22 11:01:08 +00:00
										 |  |  | 	char Result[5];  // need space for the final \0
 | 
					
						
							| 
									
										
										
										
											2009-02-20 03:13:22 +00:00
										 |  |  | 	// Go through the four characters
 | 
					
						
							|  |  |  | 	sprintf(Result, "%c%c%c%c", _Text >> 24, _Text >> 16, _Text >> 8, _Text); | 
					
						
							|  |  |  | 	// Return the string
 | 
					
						
							|  |  |  | 	std::string StrResult = Result; | 
					
						
							|  |  |  | 	return StrResult; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | bool CharArrayFromFormatV(char* out, int outsize, const char* format, va_list args) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int writtenCount = vsnprintf(out, outsize, format, args); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (writtenCount > 0 && writtenCount < outsize) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		out[writtenCount] = '\0'; | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		out[outsize - 1] = '\0'; | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Expensive!
 | 
					
						
							| 
									
										
										
										
											2008-12-10 22:36:26 +00:00
										 |  |  | void ToStringFromFormat(std::string* out, const char* format, ...) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	int writtenCount = -1; | 
					
						
							| 
									
										
										
										
											2008-12-10 22:36:26 +00:00
										 |  |  | 	int newSize = (int)strlen(format) + 4; | 
					
						
							|  |  |  | 	char *buf = 0; | 
					
						
							|  |  |  | 	va_list args; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	while (writtenCount < 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		delete [] buf; | 
					
						
							|  |  |  | 		buf = new char[newSize + 1]; | 
					
						
							| 
									
										
										
										
											2008-12-10 22:36:26 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	    va_start(args, format); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		writtenCount = vsnprintf(buf, newSize, format, args); | 
					
						
							| 
									
										
										
										
											2008-12-10 22:36:26 +00:00
										 |  |  | 		va_end(args); | 
					
						
							|  |  |  | 		if (writtenCount >= (int)newSize) { | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 			writtenCount = -1; | 
					
						
							| 
									
										
										
										
											2008-12-10 22:36:26 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		// ARGH! vsnprintf does no longer return -1 on truncation in newer libc!
 | 
					
						
							|  |  |  | 		// WORKAROUND! let's fake the old behaviour (even though it's less efficient).
 | 
					
						
							|  |  |  | 		// TODO: figure out why the fix causes an invalid read in strlen called from vsnprintf :(
 | 
					
						
							|  |  |  | //		if (writtenCount >= (int)newSize)
 | 
					
						
							|  |  |  | //			writtenCount = -1;
 | 
					
						
							|  |  |  | 		newSize *= 2; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	buf[writtenCount] = '\0'; | 
					
						
							|  |  |  | 	*out = buf; | 
					
						
							|  |  |  | 	delete[] buf; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | std::string StringFromFormat(const char* format, ...) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2008-12-10 22:36:26 +00:00
										 |  |  | 	int writtenCount = -1; | 
					
						
							|  |  |  | 	int newSize = (int)strlen(format) + 4; | 
					
						
							|  |  |  | 	char *buf = 0; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	va_list args; | 
					
						
							| 
									
										
										
										
											2008-12-10 22:36:26 +00:00
										 |  |  | 	while (writtenCount < 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		delete [] buf; | 
					
						
							|  |  |  | 		buf = new char[newSize + 1]; | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	    va_start(args, format); | 
					
						
							|  |  |  | 		writtenCount = vsnprintf(buf, newSize, format, args); | 
					
						
							|  |  |  | 		va_end(args); | 
					
						
							|  |  |  | 		if (writtenCount >= (int)newSize) { | 
					
						
							|  |  |  | 			writtenCount = -1; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		// ARGH! vsnprintf does no longer return -1 on truncation in newer libc!
 | 
					
						
							|  |  |  | 		// WORKAROUND! let's fake the old behaviour (even though it's less efficient).
 | 
					
						
							|  |  |  | 		// TODO: figure out why the fix causes an invalid read in strlen called from vsnprintf :(
 | 
					
						
							|  |  |  | //		if (writtenCount >= (int)newSize)
 | 
					
						
							|  |  |  | //			writtenCount = -1;
 | 
					
						
							|  |  |  | 		newSize *= 2; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	buf[writtenCount] = '\0'; | 
					
						
							|  |  |  | 	std::string temp = buf; | 
					
						
							|  |  |  | 	return temp; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-28 08:57:34 +00:00
										 |  |  | // For Debugging. Read out an u8 array.
 | 
					
						
							| 
									
										
										
										
											2009-02-03 00:59:26 +00:00
										 |  |  | std::string ArrayToString(const u8 *data, u32 size, u32 offset, int line_len, bool Spaces) | 
					
						
							| 
									
										
										
										
											2008-12-09 05:37:15 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-02-07 03:16:41 +00:00
										 |  |  | 	std::string Tmp, Spc; | 
					
						
							|  |  |  | 	if (Spaces) Spc = " "; else Spc = ""; | 
					
						
							| 
									
										
										
										
											2008-12-09 05:37:15 +00:00
										 |  |  | 	for (u32 i = 0; i < size; i++) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-02-07 03:16:41 +00:00
										 |  |  | 		Tmp += StringFromFormat("%02x%s", data[i + offset], Spc.c_str()); | 
					
						
							|  |  |  | 		if(i > 1 && (i + 1) % line_len == 0) Tmp.append("\n"); // break long lines
 | 
					
						
							| 
									
										
										
										
											2008-12-09 05:37:15 +00:00
										 |  |  | 	}	 | 
					
						
							| 
									
										
										
										
											2009-02-07 03:16:41 +00:00
										 |  |  | 	return Tmp; | 
					
						
							| 
									
										
										
										
											2008-12-09 05:37:15 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-25 13:31:13 +00:00
										 |  |  | // Turns "  hej " into "hej". Also handles tabs.
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | std::string StripSpaces(const std::string &str) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-02-25 13:31:13 +00:00
										 |  |  | 	std::string s = str; | 
					
						
							|  |  |  | 	int i; | 
					
						
							|  |  |  | 	for (i = 0; i < (int)s.size(); i++) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if ((s[i] != ' ') && (s[i] != 9)) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-25 13:31:13 +00:00
										 |  |  | 	s = s.substr(i); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-25 13:31:13 +00:00
										 |  |  | 	for (i = (int)s.size() - 1; i > 0; i--) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if ((s[i] != ' ') && (s[i] != 9)) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-25 13:31:13 +00:00
										 |  |  | 	return s.substr(0, i + 1); | 
					
						
							| 
									
										
										
										
											2009-02-25 10:33:09 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-25 13:31:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | // "\"hello\"" is turned to "hello"
 | 
					
						
							|  |  |  | // This one assumes that the string has already been space stripped in both
 | 
					
						
							|  |  |  | // ends, as done by StripSpaces above, for example.
 | 
					
						
							|  |  |  | std::string StripQuotes(const std::string& s) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if ((s[0] == '\"') && (s[s.size() - 1] == '\"')) | 
					
						
							|  |  |  | 		return s.substr(1, s.size() - 2); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		return s; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-25 13:31:13 +00:00
										 |  |  | // "\"hello\"" is turned to "hello"
 | 
					
						
							|  |  |  | // This one assumes that the string has already been space stripped in both
 | 
					
						
							|  |  |  | // ends, as done by StripSpaces above, for example.
 | 
					
						
							|  |  |  | std::string StripNewline(const std::string& s) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (!s.size()) | 
					
						
							|  |  |  | 		return s; | 
					
						
							|  |  |  | 	else if (s[s.size() - 1] == '\n') | 
					
						
							|  |  |  | 		return s.substr(0, s.size() - 1); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		return s; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | bool TryParseInt(const char* str, int* outVal) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	const char* s = str; | 
					
						
							|  |  |  | 	int value = 0; | 
					
						
							|  |  |  |     bool negativ = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (*s == '-') | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         negativ = true; | 
					
						
							|  |  |  |         s++; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	while (*s) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		char c = *s++; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if ((c < '0') || (c > '9')) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			return false; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		value = value * 10 + (c - '0'); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  |     if (negativ) | 
					
						
							|  |  |  |         value = -value; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	*outVal = value; | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool TryParseBool(const char* str, bool* output) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if ((str[0] == '1') || !strcmp(str, "true") || !strcmp(str, "True") || !strcmp(str, "TRUE")) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		*output = true; | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else if (str[0] == '0' || !strcmp(str, "false") || !strcmp(str, "False") || !strcmp(str, "FALSE")) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		*output = false; | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | std::string StringFromInt(int value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	char temp[16]; | 
					
						
							|  |  |  | 	sprintf(temp, "%i", value); | 
					
						
							|  |  |  | 	return std::string(temp); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | std::string StringFromBool(bool value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return value ? "True" : "False"; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef _WIN32
 | 
					
						
							|  |  |  | bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _pFilename, std::string* _pExtension) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	char drive[_MAX_DRIVE]; | 
					
						
							|  |  |  | 	char dir[_MAX_DIR]; | 
					
						
							|  |  |  | 	char fname[_MAX_FNAME]; | 
					
						
							|  |  |  | 	char ext[_MAX_EXT]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (_splitpath_s(full_path.c_str(), drive, _MAX_DRIVE, dir, _MAX_DIR, fname, _MAX_FNAME, ext, _MAX_EXT) == 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (_pPath) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			*_pPath = std::string(drive) + std::string(dir); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (_pFilename != 0) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			*_pFilename = fname; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (_pExtension != 0) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			*_pExtension = ext; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _pFilename, std::string* _pExtension) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	size_t last_slash = full_path.rfind('/'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (last_slash == std::string::npos) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-06-17 23:36:04 +00:00
										 |  |  | 		return false; // FIXME return the filename
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	size_t last_dot = full_path.rfind('.'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if ((last_dot == std::string::npos) || (last_dot < last_slash)) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-06-17 23:36:04 +00:00
										 |  |  | 		return false; // FIXME why missing . is critical?
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (_pPath) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		*_pPath = full_path.substr(0, last_slash + 1); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (_pFilename) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		*_pFilename = full_path.substr(last_slash + 1, last_dot - (last_slash + 1)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (_pExtension) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		*_pExtension = full_path.substr(last_dot + 1); | 
					
						
							|  |  |  | 		_pExtension->insert(0, "."); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else if (_pFilename) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		*_pFilename += full_path.substr(last_dot); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void BuildCompleteFilename(std::string& _CompleteFilename, const std::string& _Path, const std::string& _Filename) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	_CompleteFilename = _Path; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// check for seperator
 | 
					
						
							| 
									
										
										
										
											2009-01-14 16:36:43 +00:00
										 |  |  | 	if (_CompleteFilename[_CompleteFilename.size() - 1] != DIR_SEP_CHR) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-07-04 04:24:52 +00:00
										 |  |  | #ifdef _WIN32
 | 
					
						
							|  |  |  | 		if (_CompleteFilename[_CompleteFilename.size() - 1] != '\\') | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 			_CompleteFilename += DIR_SEP_CHR; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// add the filename
 | 
					
						
							|  |  |  | 	_CompleteFilename += _Filename; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void SplitString(const std::string& str, const std::string& delim, std::vector<std::string>& output) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	output.clear(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	size_t offset = 0; | 
					
						
							|  |  |  | 	size_t delimIndex = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	delimIndex = str.find(delim, offset); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	while (delimIndex != std::string::npos) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		output.push_back(str.substr(offset, delimIndex - offset)); | 
					
						
							|  |  |  | 		offset += delimIndex - offset + delim.length(); | 
					
						
							|  |  |  | 		delimIndex = str.find(delim, offset); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	output.push_back(str.substr(offset)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool TryParseUInt(const std::string& str, u32* output) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (!strcmp(str.substr(0, 2).c_str(), "0x") || !strcmp(str.substr(0, 2).c_str(), "0X")) | 
					
						
							|  |  |  | 		return sscanf(str.c_str() + 2, "%x", output) > 0; | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		return sscanf(str.c_str(), "%d", output) > 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int ChooseStringFrom(const char* str, const char* * items) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int i = 0; | 
					
						
							|  |  |  | 	while (items[i] != 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (!strcmp(str, items[i])) | 
					
						
							|  |  |  | 			return i; | 
					
						
							|  |  |  | 		i++; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Thousand separator. Turns 12345678 into 12,345,678.
 | 
					
						
							| 
									
										
										
										
											2009-02-16 06:18:18 +00:00
										 |  |  | std::string ThS(int Integer, bool Unsigned) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-02-16 06:18:18 +00:00
										 |  |  | 	// Create storage space
 | 
					
						
							|  |  |  | 	char cbuf[20]; | 
					
						
							|  |  |  | 	// Determine treatment of signed or unsigned
 | 
					
						
							|  |  |  | 	if(Unsigned) sprintf(cbuf, "%u", Integer); else sprintf(cbuf, "%i", Integer); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	std::string sbuf = cbuf; | 
					
						
							|  |  |  | 	for (u32 i = 0; i < sbuf.length(); ++i) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if((i & 3) == 3) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			sbuf.insert(sbuf.length() - i, ","); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return sbuf; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2009-04-06 06:58:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | void NormalizeDirSep(std::string* str) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #ifdef _WIN32
 | 
					
						
							|  |  |  | 	int i; | 
					
						
							|  |  |  | 	while ((i = (int)str->find_first_of('\\')) >= 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		str->replace(i, 1, DIR_SEP); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2009-06-17 23:36:04 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-06-21 08:39:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | std::string TabsToSpaces(int tab_size, const std::string &in) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	std::string out; | 
					
						
							|  |  |  | 	int len = 0; | 
					
						
							|  |  |  | 	// First, compute the size of the new string.
 | 
					
						
							| 
									
										
										
										
											2009-06-28 20:53:26 +00:00
										 |  |  | 	for (unsigned i = 0; i < in.size(); i++) | 
					
						
							| 
									
										
										
										
											2009-06-21 08:39:21 +00:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		if (in[i] == '\t') | 
					
						
							|  |  |  | 			len += tab_size; | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			len += 1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	out.resize(len); | 
					
						
							|  |  |  | 	int out_ctr = 0; | 
					
						
							| 
									
										
										
										
											2009-06-28 20:53:26 +00:00
										 |  |  | 	for (unsigned i = 0; i < in.size(); i++) | 
					
						
							| 
									
										
										
										
											2009-06-21 08:39:21 +00:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		if (in[i] == '\t') | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			for (int j = 0; j < tab_size; j++) | 
					
						
							|  |  |  | 				out[out_ctr++] = ' '; | 
					
						
							|  |  |  | 		}  | 
					
						
							|  |  |  | 		else  | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			out[out_ctr++] = in[i]; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return out; | 
					
						
							| 
									
										
										
										
											2009-06-28 20:53:26 +00:00
										 |  |  | } |