1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 #ifndef _DBASHARED_CONSTASCIISTRING_HXX_
23 #define _DBASHARED_CONSTASCIISTRING_HXX_
24 
25 #ifndef CONSTASCII_INCLUDED_INDIRECT
26 #error "don't include this file directly! use stringconstants.hrc instead!"
27 #endif
28 
29 // no namespaces. This file is included from several other files _within_ a namespace.
30 
31 //============================================================
32 //= a helper for static ascii pseudo-unicode strings
33 //============================================================
34 // string constants
35 struct ConstAsciiString
36 {
37 	const sal_Char*	ascii;
38 	sal_Int32		length;
39 
40 	inline	operator const ::rtl::OUString& () const;
41 	inline	operator const sal_Char* () const { return ascii; }
42 
43 	inline ConstAsciiString(const sal_Char* _pAsciiZeroTerminated, const sal_Int32 _nLength);
44 	inline ~ConstAsciiString();
45 
46 private:
47 	mutable ::rtl::OUString*	ustring;
48 };
49 
50 //------------------------------------------------------------
ConstAsciiString(const sal_Char * _pAsciiZeroTerminated,const sal_Int32 _nLength)51 inline ConstAsciiString::ConstAsciiString(const sal_Char* _pAsciiZeroTerminated, const sal_Int32 _nLength)
52 	:ascii(_pAsciiZeroTerminated)
53 	,length(_nLength)
54 	,ustring(NULL)
55 {
56 }
57 
58 //------------------------------------------------------------
~ConstAsciiString()59 inline ConstAsciiString::~ConstAsciiString()
60 {
61 	delete ustring;
62 	ustring = NULL;
63 }
64 
65 //------------------------------------------------------------
66 inline ConstAsciiString::operator const ::rtl::OUString& () const
67 {
68 	if (!ustring)
69 		ustring = new ::rtl::OUString(ascii, length, RTL_TEXTENCODING_ASCII_US);
70 	return *ustring;
71 }
72 
73 //============================================================
74 
75 #define DECLARE_CONSTASCII_USTRING( name ) \
76 	extern const ConstAsciiString name
77 
78 #define IMPLEMENT_CONSTASCII_USTRING( name, string ) \
79 	const ConstAsciiString name(string, sizeof(string)-1)
80 
81 
82 #endif // _DBASHARED_CONSTASCIISTRING_HXX_
83