/[pcsx2_0.9.7]/trunk/common/src/Utilities/StringHelpers.cpp
ViewVC logotype

Contents of /trunk/common/src/Utilities/StringHelpers.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 280 - (show annotations) (download)
Thu Dec 23 12:02:12 2010 UTC (9 years, 1 month ago) by william
File size: 8847 byte(s)
re-commit (had local access denied errors when committing)
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 #include "PrecompiledHeader.h"
17 #include <wx/gdicmn.h> // for wxPoint/wxRect stuff
18
19 __fi wxString fromUTF8( const char* src )
20 {
21 // IMPORTANT: We cannot use wxString::FromUTF8 because it *stupidly* relies on a C++ global instance of
22 // wxMBConvUTF8(). C++ initializes and destroys these globals at random, so any object constructor or
23 // destructor that attempts to do logging may crash the app (either during startup or during exit) unless
24 // we use a LOCAL instance of wxMBConvUTF8(). --air
25
26 // Performance? No worries. wxMBConvUTF8() is virtually free. Initializing a stack copy of the class
27 // is just as efficient as passing a pointer to a pre-instanced global. (which makes me wonder wh wxWidgets
28 // uses the stupid globals in the first place!) --air
29
30 return wxString( src, wxMBConvUTF8() );
31 }
32
33 __fi wxString fromAscii( const char* src )
34 {
35 return wxString::FromAscii( src );
36 }
37
38 wxString u128::ToString() const
39 {
40 return pxsFmt( L"0x%08X.%08X.%08X.%08X", _u32[0], _u32[1], _u32[2], _u32[3] );
41 }
42
43 wxString u128::ToString64() const
44 {
45 return pxsFmt( L"0x%08X%08X.%08X%08X", _u32[0], _u32[1], _u32[2], _u32[3] );
46 }
47
48 wxString u128::ToString8() const
49 {
50 FastFormatUnicode result;
51 result.Write( L"0x%02X.%02X", _u8[0], _u8[1] );
52 for (uint i=2; i<16; i+=2)
53 result.Write( L".%02X.%02X", _u8[i], _u8[i+1] );
54 return result;
55 }
56
57 void u128::WriteTo( FastFormatAscii& dest ) const
58 {
59 dest.Write( "0x%08X.%08X.%08X.%08X", _u32[0], _u32[1], _u32[2], _u32[3] );
60 }
61
62 void u128::WriteTo64( FastFormatAscii& dest ) const
63 {
64 dest.Write( "0x%08X%08X.%08X%08X", _u32[0], _u32[1], _u32[2], _u32[3] );
65 }
66
67 void u128::WriteTo8( FastFormatAscii& dest ) const
68 {
69 dest.Write( "0x%02X.%02X", _u8[0], _u8[1] );
70 for (uint i=2; i<16; i+=2)
71 dest.Write( ".%02X.%02X", _u8[i], _u8[i+1] );
72 }
73
74 // Splits a string into parts and adds the parts into the given SafeList.
75 // This list is not cleared, so concatenating many splits into a single large list is
76 // the 'default' behavior, unless you manually clear the SafeList prior to subsequent calls.
77 //
78 // Note: wxWidgets 2.9 / 3.0 has a wxSplit function, but we're using 2.8 so I had to make
79 // my own.
80 void SplitString( wxArrayString& dest, const wxString& src, const wxString& delims, wxStringTokenizerMode mode )
81 {
82 wxStringTokenizer parts( src, delims, mode );
83 while( parts.HasMoreTokens() )
84 dest.Add( parts.GetNextToken() );
85 }
86
87 // Joins a list of strings into one larger string, using the given string concatenation
88 // character as a separator. If you want to be able to split the string later then the
89 // concatenation string needs to be a single character.
90 //
91 // Note: wxWidgets 2.9 / 3.0 has a wxJoin function, but we're using 2.8 so I had to make
92 // my own.
93 wxString JoinString( const wxArrayString& src, const wxString& separator )
94 {
95 wxString dest;
96 for( int i=0, len=src.GetCount(); i<len; ++i )
97 {
98 if( src[i].IsEmpty() ) continue;
99 if( !dest.IsEmpty() )
100 dest += separator;
101 dest += src[i];
102 }
103 return dest;
104 }
105
106 wxString JoinString( const wxChar** src, const wxString& separator )
107 {
108 wxString dest;
109 while( *src != NULL )
110 {
111 if( *src[0] == 0 ) continue;
112
113 if( !dest.IsEmpty() )
114 dest += separator;
115 dest += *src;
116 ++src;
117 }
118 return dest;
119 }
120
121
122 // Attempts to parse and return a value for the given template type, and throws a ParseError
123 // exception if the parse fails. The template type can be anything that is supported/
124 // implemented via one of the TryParse() method overloads.
125 //
126 // This, so far, include types such as wxPoint, wxRect, and wxSize.
127 //
128 template< typename T >
129 T Parse( const wxString& src, const wxString& separators=L",")
130 {
131 T retval;
132 if( !TryParse( retval, src, separators ) )
133 throw Exception::ParseError( "Parse failure on call to " + fromUTF8(__WXFUNCTION__) + ": " + src );
134 return retval;
135 }
136
137
138 // --------------------------------------------------------------------------------------
139 // ToString helpers for wxString!
140 // --------------------------------------------------------------------------------------
141
142 // Converts a wxPoint into a comma-delimited string!
143 wxString ToString( const wxPoint& src, const wxString& separator )
144 {
145 return wxString() << src.x << separator << src.y;
146 }
147
148 wxString ToString( const wxSize& src, const wxString& separator )
149 {
150 return wxString() << src.GetWidth() << separator << src.GetHeight();
151 }
152
153 // Converts a wxRect into a comma-delimited string!
154 // Example: 32,64,128,5
155 wxString ToString( const wxRect& src, const wxString& separator )
156 {
157 return ToString( src.GetLeftTop(), separator ) << separator << ToString( src.GetSize(), separator );
158 }
159
160 // --------------------------------------------------------------------------------------
161 // Parse helpers for wxString!
162 // --------------------------------------------------------------------------------------
163
164 bool TryParse( wxPoint& dest, wxStringTokenizer& parts )
165 {
166 long result[2];
167
168 if( !parts.HasMoreTokens() || !parts.GetNextToken().ToLong( &result[0] ) ) return false;
169 if( !parts.HasMoreTokens() || !parts.GetNextToken().ToLong( &result[1] ) ) return false;
170 dest.x = result[0];
171 dest.y = result[1];
172
173 return true;
174 }
175
176 bool TryParse( wxSize& dest, wxStringTokenizer& parts )
177 {
178 long result[2];
179
180 if( !parts.HasMoreTokens() || !parts.GetNextToken().ToLong( &result[0] ) ) return false;
181 if( !parts.HasMoreTokens() || !parts.GetNextToken().ToLong( &result[1] ) ) return false;
182 dest.SetWidth( result[0] );
183 dest.SetHeight( result[1] );
184
185 return true;
186 }
187
188 // Tries to parse the given string into a wxPoint value at 'dest.' If the parse fails, the
189 // method aborts and returns false.
190 bool TryParse( wxPoint& dest, const wxString& src, const wxPoint& defval, const wxString& separators )
191 {
192 dest = defval;
193 wxStringTokenizer parts( src, separators );
194 return TryParse( dest, parts );
195 }
196
197 bool TryParse( wxSize& dest, const wxString& src, const wxSize& defval, const wxString& separators )
198 {
199 dest = defval;
200 wxStringTokenizer parts( src, separators );
201 return TryParse( dest, parts );
202 }
203
204 bool TryParse( wxRect& dest, const wxString& src, const wxRect& defval, const wxString& separators )
205 {
206 dest = defval;
207
208 wxStringTokenizer parts( src, separators );
209
210 wxPoint point;
211 wxSize size;
212
213 if( !TryParse( point, parts ) ) return false;
214 if( !TryParse( size, parts ) ) return false;
215
216 dest = wxRect( point, size );
217 return true;
218 }
219
220 // returns TRUE if the parse is valid, or FALSE if it's a comment.
221 bool pxParseAssignmentString( const wxString& src, wxString& ldest, wxString& rdest )
222 {
223 if( src.StartsWith(L"--") || src.StartsWith( L"//" ) || src.StartsWith( L";" ) )
224 return false;
225
226 ldest = src.BeforeFirst(L'=').Trim(true).Trim(false);
227 rdest = src.AfterFirst(L'=').Trim(true).Trim(false);
228
229 return true;
230 }
231
232 ParsedAssignmentString::ParsedAssignmentString( const wxString& src )
233 {
234 IsComment = pxParseAssignmentString( src, lvalue, rvalue );
235 }
236
237 // Performs a cross-platform puts operation, which adds CRs to naked LFs on Win32 platforms,
238 // so that Notepad won't throw a fit and Rama can read the logs again! On Unix and Mac platforms,
239 // the input string is written unmodified.
240 //
241 // PCSX2 generally uses Unix-style newlines -- LF (\n) only -- hence there's no need to strip CRs
242 // from incoming data. Mac platforms may need an implementation of their own that converts
243 // newlines to CRs...?
244 //
245 void px_fputs( FILE* fp, const char* src )
246 {
247 if( fp == NULL ) return;
248
249 #ifdef _WIN32
250 // Windows needs CR's partnered with all newlines, or else notepad.exe can't view
251 // the stupid logfile. Best way is to write one char at a time.. >_<
252
253 const char* curchar = src;
254 bool prevcr = false;
255 while( *curchar != 0 )
256 {
257 if( *curchar == '\r' )
258 {
259 prevcr = true;
260 }
261 else
262 {
263 // Only write a CR/LF pair if the current LF is not prefixed nor
264 // post-fixed by a CR.
265 if( *curchar == '\n' && !prevcr && (*(curchar+1) != '\r') )
266 fputs( "\r\n", fp );
267 else
268 fputc( *curchar, fp );
269
270 prevcr = false;
271 }
272 ++curchar;
273 }
274
275 #else
276 // Linux is happy with plain old LFs. Not sure about Macs... does OSX still
277 // go by the old school Mac style of using Crs only?
278
279 fputs( src, fp ); // fputs does not do automatic newlines, so it's ok!
280 #endif
281 }

  ViewVC Help
Powered by ViewVC 1.1.22