1 /* AbiWord
2  * Copyright (C) 1998 AbiSource, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA.
18  */
19 
20 #ifndef UT_WIN32LOCALESTRING_H
21 #define UT_WIN32LOCALESTRING_H
22 
23 #include "ut_string_class.h"
24 #include <windows.h>
25 #include <wchar.h>
26 
27 
28 
29 class UT_UCS2Stringbuf
30 {
31 public:
32 	typedef UT_UCS2Char char_type;
33 
34 	UT_UCS2Stringbuf();
35 	UT_UCS2Stringbuf(const UT_UCS2Stringbuf& rhs);
36 	UT_UCS2Stringbuf(const char_type* sz, size_t n);
37 	~UT_UCS2Stringbuf();
38 
39 	void		operator=(const UT_UCS2Stringbuf& rhs);
40 
41 	void		assign(const char_type* sz, size_t n);
42 	void		append(const char_type* sz, size_t n);
43 	void		append(const UT_UCS2Stringbuf& rhs);
44 
45 	void		clear();
46 
empty()47 	bool				empty()		const { return m_psz == m_pEnd; }
size()48 	size_t				size()		const { return m_pEnd - m_psz; }
capacity()49 	size_t				capacity()	const { return m_size; }
data()50 	const char_type*	data()		const { return m_psz; }
data()51 	char_type*			data() 			  { return m_psz; }
52 
53 private:
54 	void	grow_nocopy(size_t n);
55 	void	grow_copy(size_t n);
56 	void	grow_common(size_t n, bool bCopy);
57 
58 	static void copy(char_type* pDest, const char_type* pSrc, size_t n);
59 
60 	char_type*	m_psz;
61 	char_type*	m_pEnd;
62 	size_t		m_size;
63 };
64 
65 
66 class ABI_EXPORT UT_UCS2String
67 {
68 public:
69 	UT_UCS2String();
70 	UT_UCS2String(const UT_UCS2Char * sz, size_t n = 0 /* 0 == zero-terminate */);
71 	UT_UCS2String(const UT_UCS2String& rhs);
72 	~UT_UCS2String();
73 
74 	size_t		size() const;
75 	bool		empty() const;
76 	void        clear() const;
length()77 	size_t		length() { return size(); }
78 
79 	UT_UCS2String	substr(size_t iStart, size_t nChars) const;
80 
81 	UT_UCS2String&	operator=(const UT_UCS2String&  rhs);
82 	UT_UCS2String&	operator=(const UT_UCS2Char *    rhs);
83 
84 	// The returned pointer is valid until the next non-const
85 	// operation. You will _always_ get a legal pointer back,
86 	// even if to an empty (0) string.
87 	const UT_UCS2Char* ucs2_str() const;
88 
89 protected:
90 	class UT_UCS2Stringbuf* pimpl;
91 };
92 
93 
94 class ABI_EXPORT UT_Win32LocaleString : public UT_UCS2String
95 {
96 public:
97 
98 	UT_Win32LocaleString ();
99 
100 	void fromUCS2 (const UT_UCS2Char * szIn);
101 	void fromUCS4 (const UT_UCS4Char * szIn);
102 	void fromUTF8 (const char* szUTF8);
103 	void fromASCII (const char* szASCII, size_t size = -1);
104 	void fromLocale (const wchar_t* szLocale);
105 
106 	void appendASCII (const char* szASCII);
107 	void appendLocale (const wchar_t* szLocale);
108 	const wchar_t * c_str() const;
109 	UT_UTF8String utf8_str() const;
110 	UT_UCS4String ucs4_str() const;
111 	UT_Win32LocaleString substr(size_t iStart, size_t nChars) const;
112 
113 	wchar_t operator[](size_t iPos) const
114 	{
115 		UT_ASSERT(iPos <= size());
116 		if (iPos == size())
117 			return L'\0';
118 		return (wchar_t) pimpl->data()[iPos];
119 	}
120 
121 	wchar_t& operator[](size_t iPos)
122 	{
123 		UT_ASSERT(iPos <= size());
124 		return (wchar_t &) pimpl->data()[iPos];
125 	}
126 };
127 
128 bool operator==(const UT_Win32LocaleString& s1, const wchar_t* s2);
129 
130 #endif /* UT_WIN32LOCALESTRING_H */
131