summaryrefslogtreecommitdiff
path: root/src/common/string_util.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/string_util.h')
-rw-r--r--src/common/string_util.h111
1 files changed, 111 insertions, 0 deletions
diff --git a/src/common/string_util.h b/src/common/string_util.h
new file mode 100644
index 000000000..fcbae4715
--- /dev/null
+++ b/src/common/string_util.h
@@ -0,0 +1,111 @@
1// Copyright 2013 Dolphin Emulator Project
2// Licensed under GPLv2
3// Refer to the license.txt file included.
4
5#ifndef _STRINGUTIL_H_
6#define _STRINGUTIL_H_
7
8#include <stdarg.h>
9
10#include <vector>
11#include <string>
12#include <sstream>
13#include <iomanip>
14
15#include "common.h"
16
17std::string StringFromFormat(const char* format, ...);
18// Cheap!
19bool CharArrayFromFormatV(char* out, int outsize, const char* format, va_list args);
20
21template<size_t Count>
22inline void CharArrayFromFormat(char (& out)[Count], const char* format, ...)
23{
24 va_list args;
25 va_start(args, format);
26 CharArrayFromFormatV(out, Count, format, args);
27 va_end(args);
28}
29
30// Good
31std::string ArrayToString(const u8 *data, u32 size, int line_len = 20, bool spaces = true);
32
33std::string StripSpaces(const std::string &s);
34std::string StripQuotes(const std::string &s);
35
36// Thousand separator. Turns 12345678 into 12,345,678
37template <typename I>
38std::string ThousandSeparate(I value, int spaces = 0)
39{
40 std::ostringstream oss;
41
42// std::locale("") seems to be broken on many platforms
43#if defined _WIN32 || (defined __linux__ && !defined __clang__)
44 oss.imbue(std::locale(""));
45#endif
46 oss << std::setw(spaces) << value;
47
48 return oss.str();
49}
50
51std::string StringFromInt(int value);
52std::string StringFromBool(bool value);
53
54bool TryParse(const std::string &str, bool *output);
55bool TryParse(const std::string &str, u32 *output);
56
57template <typename N>
58static bool TryParse(const std::string &str, N *const output)
59{
60 std::istringstream iss(str);
61
62 N tmp = 0;
63 if (iss >> tmp)
64 {
65 *output = tmp;
66 return true;
67 }
68 else
69 return false;
70}
71
72// TODO: kill this
73bool AsciiToHex(const char* _szValue, u32& result);
74
75std::string TabsToSpaces(int tab_size, const std::string &in);
76
77void SplitString(const std::string& str, char delim, std::vector<std::string>& output);
78
79// "C:/Windows/winhelp.exe" to "C:/Windows/", "winhelp", ".exe"
80bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _pFilename, std::string* _pExtension);
81
82void BuildCompleteFilename(std::string& _CompleteFilename, const std::string& _Path, const std::string& _Filename);
83std::string ReplaceAll(std::string result, const std::string& src, const std::string& dest);
84std::string UriDecode(const std::string & sSrc);
85std::string UriEncode(const std::string & sSrc);
86
87std::string CP1252ToUTF8(const std::string& str);
88std::string SHIFTJISToUTF8(const std::string& str);
89std::string UTF16ToUTF8(const std::wstring& str);
90
91#ifdef _WIN32
92
93std::wstring UTF8ToUTF16(const std::string& str);
94
95#ifdef _UNICODE
96inline std::string TStrToUTF8(const std::wstring& str)
97{ return UTF16ToUTF8(str); }
98
99inline std::wstring UTF8ToTStr(const std::string& str)
100{ return UTF8ToUTF16(str); }
101#else
102inline std::string TStrToUTF8(const std::string& str)
103{ return str; }
104
105inline std::string UTF8ToTStr(const std::string& str)
106{ return str; }
107#endif
108
109#endif
110
111#endif // _STRINGUTIL_H_