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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 401 - (hide annotations) (download)
Fri Feb 25 17:31:09 2011 UTC (9 years, 5 months ago) by william
File MIME type: text/plain
File size: 8459 byte(s)
Auto Commited Import of: pcsx2-0.9.7-DEBUG (upstream: v0.9.7.4358 local: v0.9.7.313-latest) in ./trunk
1 william 31 /* 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 william 62 #include "SafeArray.h"
20 william 273 #include "ScopedAlloc.h"
21 william 31
22     #include <wx/tokenzr.h>
23    
24 william 62
25     // --------------------------------------------------------------------------------------
26     // pxToUTF8
27     // --------------------------------------------------------------------------------------
28     // Converts a string to UTF8 and provides an interface for getting its length.
29     class pxToUTF8
30     {
31     DeclareNoncopyableObject( pxToUTF8 );
32    
33     protected:
34     wxCharBuffer m_result;
35     int m_length;
36    
37     public:
38     explicit pxToUTF8(const wxString& src)
39     : m_result( src.ToUTF8() )
40     {
41     m_length = -1;
42     }
43    
44     size_t Length()
45     {
46     if( -1 == m_length )
47     m_length = strlen( m_result );
48     return m_length;
49     }
50    
51     void Convert( const wxString& src )
52     {
53     m_result = src.ToUTF8();
54     m_length = -1;
55     }
56    
57     const char* data() const { return m_result; }
58    
59     operator const char*() const
60     {
61     return m_result.data();
62     }
63     };
64    
65 william 31 extern void px_fputs( FILE* fp, const char* src );
66    
67     // wxWidgets lacks one of its own...
68     extern const wxRect wxDefaultRect;
69    
70     extern void SplitString( wxArrayString& dest, const wxString& src, const wxString& delims, wxStringTokenizerMode mode = wxTOKEN_RET_EMPTY_ALL );
71 william 62 extern wxString JoinString( const wxArrayString& src, const wxString& separator );
72     extern wxString JoinString( const wxChar** src, const wxString& separator );
73 william 31
74     extern wxString ToString( const wxPoint& src, const wxString& separator=L"," );
75     extern wxString ToString( const wxSize& src, const wxString& separator=L"," );
76     extern wxString ToString( const wxRect& src, const wxString& separator=L"," );
77    
78     extern bool TryParse( wxPoint& dest, const wxStringTokenizer& parts );
79     extern bool TryParse( wxSize& dest, const wxStringTokenizer& parts );
80    
81     extern bool TryParse( wxPoint& dest, const wxString& src, const wxPoint& defval=wxDefaultPosition, const wxString& separators=L",");
82     extern bool TryParse( wxSize& dest, const wxString& src, const wxSize& defval=wxDefaultSize, const wxString& separators=L",");
83     extern bool TryParse( wxRect& dest, const wxString& src, const wxRect& defval=wxDefaultRect, const wxString& separators=L",");
84    
85     // --------------------------------------------------------------------------------------
86     // ParsedAssignmentString
87     // --------------------------------------------------------------------------------------
88     // This class is a simple helper for parsing INI-style assignments, in the typical form of:
89     // variable = value
90     // filename = SomeString.txt
91     // integer = 15
92     //
93     // This parser supports both '//' and ';' at the head of a line as indicators of a commented
94     // line, and such a line will return empty strings for l- and r-value.
95     //
96     // No type handling is performed -- the user must manually parse strings into integers, etc.
97     // For advanced "fully functional" ini file parsing, consider using wxFileConfig instead.
98     //
99     struct ParsedAssignmentString
100     {
101     wxString lvalue;
102     wxString rvalue;
103     bool IsComment;
104    
105     ParsedAssignmentString( const wxString& src );
106     };
107    
108 william 273 // ======================================================================================
109     // FastFormatAscii / FastFormatUnicode (overview!)
110     // ======================================================================================
111     // Fast formatting of ASCII or Unicode text. These classes uses a series of thread-local
112     // format buffers that are allocated only once and grown to accommodate string formatting
113     // needs. Because the buffers are thread-local, no thread synch objects are required in
114     // order to format strings, allowing for multi-threaded string formatting operations to be
115     // performed with maximum efficiency. This class also reduces the overhead typically required
116     // to allocate string buffers off the heap.
117     //
118     // Drawbacks:
119     // * Some overhead is added to the creation and destruction of threads, however since thread
120     // construction is a typically slow process, and often avoided to begin with, this should
121     // be a sound trade-off.
122     //
123     // Notes:
124     // * conversion to wxString requires a heap allocation.
125     // * FastFormatUnicode can accept either UTF8 or UTF16/32 (wchar_t) input, but FastFormatAscii
126     // accepts Ascii/UTF8 only.
127     //
128    
129 william 62 // --------------------------------------------------------------------------------------
130 william 273 // FastFormatAscii
131 william 62 // --------------------------------------------------------------------------------------
132 william 273
133 william 62 class FastFormatAscii
134     {
135     protected:
136 william 273 ScopedAlignedAlloc<char,16>* m_dest;
137 william 62 bool m_deleteDest;
138 william 273 uint m_Length;
139 william 62
140     public:
141     FastFormatAscii();
142     ~FastFormatAscii() throw();
143     FastFormatAscii& Write( const char* fmt, ... );
144     FastFormatAscii& WriteV( const char* fmt, va_list argptr );
145 william 31
146 william 273 void Clear();
147 william 62 bool IsEmpty() const;
148 william 273 uint Length() const { return m_Length; }
149 william 31
150 william 62 const char* c_str() const { return m_dest->GetPtr(); }
151     operator const char*() const { return m_dest->GetPtr(); }
152    
153     const wxString GetString() const;
154     //operator wxString() const;
155 william 273
156     FastFormatAscii& operator+=(const wxString& s)
157     {
158     Write( "%ls", s.c_str() );
159     return *this;
160     }
161    
162     FastFormatAscii& operator+=(const wxChar* psz )
163     {
164     Write( "%ls", psz );
165     return *this;
166     }
167    
168     FastFormatAscii& operator+=(const char* psz )
169     {
170     Write( "%s", psz );
171     return *this;
172     }
173 william 62 };
174    
175 william 273 // --------------------------------------------------------------------------------------
176     // FastFormatUnicode
177     // --------------------------------------------------------------------------------------
178 william 62 class FastFormatUnicode
179     {
180     protected:
181 william 273 ScopedAlignedAlloc<char,16>* m_dest;
182 william 62 bool m_deleteDest;
183 william 273 uint m_Length;
184 william 62
185     public:
186     FastFormatUnicode();
187     ~FastFormatUnicode() throw();
188    
189     FastFormatUnicode& Write( const char* fmt, ... );
190     FastFormatUnicode& Write( const wxChar* fmt, ... );
191     FastFormatUnicode& WriteV( const char* fmt, va_list argptr );
192     FastFormatUnicode& WriteV( const wxChar* fmt, va_list argptr );
193    
194 william 273 void Clear();
195 william 62 bool IsEmpty() const;
196 william 273 uint Length() const { return m_Length; }
197 william 62
198 william 273 FastFormatUnicode& ToUpper();
199     FastFormatUnicode& ToLower();
200    
201 william 62 const wxChar* c_str() const { return (const wxChar*)m_dest->GetPtr(); }
202     operator const wxChar*() const { return (const wxChar*)m_dest->GetPtr(); }
203     operator wxString() const { return (const wxChar*)m_dest->GetPtr(); }
204 william 273
205     FastFormatUnicode& operator+=(const wxString& s)
206     {
207     Write( L"%s", s.c_str() );
208     return *this;
209     }
210    
211     FastFormatUnicode& operator+=(const wxChar* psz )
212     {
213     Write( L"%s", psz );
214     return *this;
215     }
216    
217     FastFormatUnicode& operator+=(const char* psz );
218 william 62 };
219    
220     extern bool pxParseAssignmentString( const wxString& src, wxString& ldest, wxString& rdest );
221    
222 william 273 #define pxsFmt FastFormatUnicode().Write
223     #define pxsFmtV FastFormatUnicode().WriteV
224 william 62
225 william 280 #define pxsPtr(ptr) pxsFmt("0x%08X", (ptr)).c_str()
226    
227 william 273 extern wxString& operator+=(wxString& str1, const FastFormatUnicode& str2);
228     extern wxString operator+(const wxString& str1, const FastFormatUnicode& str2);
229     extern wxString operator+(const wxChar* str1, const FastFormatUnicode& str2);
230 william 401 extern wxString operator+(const FastFormatUnicode& str1, const wxString& str2);
231     extern wxString operator+(const FastFormatUnicode& str1, const wxChar* str2);
232 william 273
233    
234 william 31 //////////////////////////////////////////////////////////////////////////////////////////
235     // Custom internal sprintf functions, which are ASCII only (even in UNICODE builds)
236     //
237     // These functions are useful since they are ASCII always, even under Unicode. Typically
238     // even in a unicode app.
239    
240     extern void ssprintf(std::string& dest, const char* fmt, ...);
241     extern void ssappendf(std::string& dest, const char* format, ...);
242     extern void vssprintf(std::string& dest, const char* format, va_list args);
243     extern void vssappendf(std::string& dest, const char* format, va_list args);
244    
245     extern std::string fmt_string( const char* fmt, ... );
246     extern std::string vfmt_string( const char* fmt, va_list args );

  ViewVC Help
Powered by ViewVC 1.1.22