/[pcsx2_0.9.7]/trunk/common/include/Utilities/StringHelpers.h
ViewVC logotype

Contents of /trunk/common/include/Utilities/StringHelpers.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 62 - (show annotations) (download)
Tue Sep 7 11:08:22 2010 UTC (9 years, 10 months ago) by william
File MIME type: text/plain
File size: 6243 byte(s)
Auto Commited Import of: pcsx2-0.9.7-r3738-debug in ./trunk
1 /* PCSX2 - PS2 Emulator for PCs
2 * Copyright (C) 2002-2010 PCSX2 Dev Team
3 *
4 * PCSX2 is free software: you can redistribute it and/or modify it under the terms
5 * of the GNU Lesser General Public License as published by the Free Software Found-
6 * ation, either version 3 of the License, or (at your option) any later version.
7 *
8 * PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10 * PURPOSE. See the GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License along with PCSX2.
13 * If not, see <http://www.gnu.org/licenses/>.
14 */
15
16 #pragma once
17
18 #include "Dependencies.h"
19 #include "SafeArray.h"
20
21 #include <wx/tokenzr.h>
22
23
24 // --------------------------------------------------------------------------------------
25 // pxToUTF8
26 // --------------------------------------------------------------------------------------
27 // Converts a string to UTF8 and provides an interface for getting its length.
28 class pxToUTF8
29 {
30 DeclareNoncopyableObject( pxToUTF8 );
31
32 protected:
33 wxCharBuffer m_result;
34 int m_length;
35
36 public:
37 explicit pxToUTF8(const wxString& src)
38 : m_result( src.ToUTF8() )
39 {
40 m_length = -1;
41 }
42
43 size_t Length()
44 {
45 if( -1 == m_length )
46 m_length = strlen( m_result );
47 return m_length;
48 }
49
50 void Convert( const wxString& src )
51 {
52 m_result = src.ToUTF8();
53 m_length = -1;
54 }
55
56 const char* data() const { return m_result; }
57
58 operator const char*() const
59 {
60 return m_result.data();
61 }
62 };
63
64 extern void px_fputs( FILE* fp, const char* src );
65
66 extern wxString fromUTF8( const char* src );
67 extern wxString fromAscii( const char* src );
68
69 // wxWidgets lacks one of its own...
70 extern const wxRect wxDefaultRect;
71
72 extern void SplitString( wxArrayString& dest, const wxString& src, const wxString& delims, wxStringTokenizerMode mode = wxTOKEN_RET_EMPTY_ALL );
73 extern wxString JoinString( const wxArrayString& src, const wxString& separator );
74 extern wxString JoinString( const wxChar** src, const wxString& separator );
75
76 extern wxString ToString( const wxPoint& src, const wxString& separator=L"," );
77 extern wxString ToString( const wxSize& src, const wxString& separator=L"," );
78 extern wxString ToString( const wxRect& src, const wxString& separator=L"," );
79
80 extern bool TryParse( wxPoint& dest, const wxStringTokenizer& parts );
81 extern bool TryParse( wxSize& dest, const wxStringTokenizer& parts );
82
83 extern bool TryParse( wxPoint& dest, const wxString& src, const wxPoint& defval=wxDefaultPosition, const wxString& separators=L",");
84 extern bool TryParse( wxSize& dest, const wxString& src, const wxSize& defval=wxDefaultSize, const wxString& separators=L",");
85 extern bool TryParse( wxRect& dest, const wxString& src, const wxRect& defval=wxDefaultRect, const wxString& separators=L",");
86
87 // --------------------------------------------------------------------------------------
88 // ParsedAssignmentString
89 // --------------------------------------------------------------------------------------
90 // This class is a simple helper for parsing INI-style assignments, in the typical form of:
91 // variable = value
92 // filename = SomeString.txt
93 // integer = 15
94 //
95 // This parser supports both '//' and ';' at the head of a line as indicators of a commented
96 // line, and such a line will return empty strings for l- and r-value.
97 //
98 // No type handling is performed -- the user must manually parse strings into integers, etc.
99 // For advanced "fully functional" ini file parsing, consider using wxFileConfig instead.
100 //
101 struct ParsedAssignmentString
102 {
103 wxString lvalue;
104 wxString rvalue;
105 bool IsComment;
106
107 ParsedAssignmentString( const wxString& src );
108 };
109
110 // --------------------------------------------------------------------------------------
111 // FastFormatAscii
112 // --------------------------------------------------------------------------------------
113 // Fast formatting of ASCII text. This class uses a process-wide format buffer that is
114 // allocated only once and grown to accommodate string formatting needs. The buffer is
115 // thread-safe. This technique reduces the overhead of formatting strings by eliminating
116 // most or all heap allocation actions.
117 //
118 class FastFormatAscii
119 {
120 protected:
121 SafeArray<char>* m_dest;
122 bool m_deleteDest;
123
124 public:
125 FastFormatAscii();
126 ~FastFormatAscii() throw();
127 FastFormatAscii& Write( const char* fmt, ... );
128 FastFormatAscii& WriteV( const char* fmt, va_list argptr );
129
130 bool IsEmpty() const;
131
132 const char* c_str() const { return m_dest->GetPtr(); }
133 operator const char*() const { return m_dest->GetPtr(); }
134
135 const wxString GetString() const;
136 //operator wxString() const;
137 };
138
139 class FastFormatUnicode
140 {
141 protected:
142 SafeArray<char>* m_dest;
143 bool m_deleteDest;
144
145 public:
146 FastFormatUnicode();
147 ~FastFormatUnicode() throw();
148
149 FastFormatUnicode& Write( const char* fmt, ... );
150 FastFormatUnicode& Write( const wxChar* fmt, ... );
151 FastFormatUnicode& WriteV( const char* fmt, va_list argptr );
152 FastFormatUnicode& WriteV( const wxChar* fmt, va_list argptr );
153
154 bool IsEmpty() const;
155
156 const wxChar* c_str() const { return (const wxChar*)m_dest->GetPtr(); }
157 operator const wxChar*() const { return (const wxChar*)m_dest->GetPtr(); }
158 operator wxString() const { return (const wxChar*)m_dest->GetPtr(); }
159 };
160
161 extern bool pxParseAssignmentString( const wxString& src, wxString& ldest, wxString& rdest );
162
163 #define pxsFmt FastFormatUnicode().Write
164 #define pxsFmtV FastFormatUnicode().WriteV
165
166 //////////////////////////////////////////////////////////////////////////////////////////
167 // Custom internal sprintf functions, which are ASCII only (even in UNICODE builds)
168 //
169 // These functions are useful since they are ASCII always, even under Unicode. Typically
170 // even in a unicode app.
171
172 extern void ssprintf(std::string& dest, const char* fmt, ...);
173 extern void ssappendf(std::string& dest, const char* format, ...);
174 extern void vssprintf(std::string& dest, const char* format, va_list args);
175 extern void vssappendf(std::string& dest, const char* format, va_list args);
176
177 extern std::string fmt_string( const char* fmt, ... );
178 extern std::string vfmt_string( const char* fmt, va_list args );

  ViewVC Help
Powered by ViewVC 1.1.22