xref: /aoo41x/main/sal/inc/osl/profile.hxx (revision 565d668c)
1*565d668cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*565d668cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*565d668cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*565d668cSAndrew Rist  * distributed with this work for additional information
6*565d668cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*565d668cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*565d668cSAndrew Rist  * "License"); you may not use this file except in compliance
9*565d668cSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*565d668cSAndrew Rist  *
11*565d668cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*565d668cSAndrew Rist  *
13*565d668cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*565d668cSAndrew Rist  * software distributed under the License is distributed on an
15*565d668cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*565d668cSAndrew Rist  * KIND, either express or implied.  See the License for the
17*565d668cSAndrew Rist  * specific language governing permissions and limitations
18*565d668cSAndrew Rist  * under the License.
19*565d668cSAndrew Rist  *
20*565d668cSAndrew Rist  *************************************************************/
21*565d668cSAndrew Rist 
22*565d668cSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef _OSL_PROFILE_HXX_
25cdf0e10cSrcweir #define _OSL_PROFILE_HXX_
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "profile.h"
28cdf0e10cSrcweir #include <rtl/ustring.hxx>
29cdf0e10cSrcweir #include <string.h>
30cdf0e10cSrcweir #include <list>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir namespace osl {
33cdf0e10cSrcweir 
34cdf0e10cSrcweir 	typedef oslProfileOption ProfileOption;
35cdf0e10cSrcweir 
36cdf0e10cSrcweir 	const int Profile_DEFAULT   = osl_Profile_DEFAULT;
37cdf0e10cSrcweir 	const int Profile_SYSTEM    = osl_Profile_SYSTEM;    /* use system depended functinality */
38cdf0e10cSrcweir 	const int Profile_READLOCK  = osl_Profile_READLOCK;  /* lock file for reading            */
39cdf0e10cSrcweir 	const int Profile_WRITELOCK = osl_Profile_WRITELOCK; /* lock file for writing            */
40cdf0e10cSrcweir 
41cdf0e10cSrcweir     /** Deprecated API.
42cdf0e10cSrcweir         @deprecated
43cdf0e10cSrcweir     */
44cdf0e10cSrcweir 	class Profile {
45cdf0e10cSrcweir 		oslProfile profile;
46cdf0e10cSrcweir 
47cdf0e10cSrcweir 	public:
48cdf0e10cSrcweir 		/** Open or create a configuration profile.
49cdf0e10cSrcweir 			@return 0 if the profile could not be created, otherwise a handle to the profile.
50cdf0e10cSrcweir 		*/
51cdf0e10cSrcweir 		Profile(const rtl::OUString strProfileName, oslProfileOption Options = Profile_DEFAULT )
52cdf0e10cSrcweir 		{
53cdf0e10cSrcweir 			profile = osl_openProfile(strProfileName.pData, Options);
54cdf0e10cSrcweir 			if( ! profile )
55cdf0e10cSrcweir 				throw std::exception();
56cdf0e10cSrcweir 		}
57cdf0e10cSrcweir 
58cdf0e10cSrcweir 
59cdf0e10cSrcweir 		/** Close the opened profile an flush all data to the disk.
60cdf0e10cSrcweir 			@param Profile handle to a opened profile.
61cdf0e10cSrcweir 		*/
62cdf0e10cSrcweir 		~Profile()
63cdf0e10cSrcweir 		{
64cdf0e10cSrcweir 			osl_closeProfile(profile);
65cdf0e10cSrcweir 		}
66cdf0e10cSrcweir 
67cdf0e10cSrcweir 
68cdf0e10cSrcweir 		sal_Bool flush()
69cdf0e10cSrcweir 		{
70cdf0e10cSrcweir 			return osl_flushProfile(profile);
71cdf0e10cSrcweir 		}
72cdf0e10cSrcweir 
73cdf0e10cSrcweir 		rtl::OString readString( const rtl::OString& rSection, const rtl::OString& rEntry,
74cdf0e10cSrcweir 								 const rtl::OString& rDefault)
75cdf0e10cSrcweir 		{
76cdf0e10cSrcweir 			sal_Char aBuf[1024];
77cdf0e10cSrcweir 			return osl_readProfileString( profile,
78cdf0e10cSrcweir 										  rSection,
79cdf0e10cSrcweir 										  rEntry,
80cdf0e10cSrcweir 										  aBuf,
81cdf0e10cSrcweir 										  sizeof( aBuf ),
82cdf0e10cSrcweir 										  rDefault ) ? rtl::OString( aBuf ) : rtl::OString();
83cdf0e10cSrcweir 
84cdf0e10cSrcweir 		}
85cdf0e10cSrcweir 
86cdf0e10cSrcweir 		sal_Bool readBool( const rtl::OString& rSection, const rtl::OString& rEntry, sal_Bool bDefault )
87cdf0e10cSrcweir 		{
88cdf0e10cSrcweir 			return osl_readProfileBool( profile, rSection, rEntry, bDefault );
89cdf0e10cSrcweir 		}
90cdf0e10cSrcweir 
91cdf0e10cSrcweir 		sal_uInt32 readIdent(const rtl::OString& rSection, const rtl::OString& rEntry,
92cdf0e10cSrcweir 							 sal_uInt32 nFirstId, const std::list< rtl::OString >& rStrings,
93cdf0e10cSrcweir 							 sal_uInt32 nDefault)
94cdf0e10cSrcweir 		{
95cdf0e10cSrcweir 			int nItems = rStrings.size();
96cdf0e10cSrcweir 			const sal_Char** pStrings = new const sal_Char*[ nItems+1 ];
97cdf0e10cSrcweir 			std::list< rtl::OString >::const_iterator it = rStrings.begin();
98cdf0e10cSrcweir 			nItems = 0;
99cdf0e10cSrcweir 			while( it != rStrings.end() )
100cdf0e10cSrcweir 			{
101cdf0e10cSrcweir 				pStrings[ nItems++ ] = *it;
102cdf0e10cSrcweir 				++it;
103cdf0e10cSrcweir 			}
104cdf0e10cSrcweir 			pStrings[ nItems ] = NULL;
105cdf0e10cSrcweir 			sal_uInt32 nRet = osl_readProfileIdent(profile, rSection, rEntry, nFirstId, pStrings, nDefault);
106cdf0e10cSrcweir 			delete pStrings;
107cdf0e10cSrcweir 			return nRet;
108cdf0e10cSrcweir 		}
109cdf0e10cSrcweir 
110cdf0e10cSrcweir 		sal_Bool writeString(const rtl::OString& rSection, const rtl::OString& rEntry,
111cdf0e10cSrcweir 							 const rtl::OString& rString)
112cdf0e10cSrcweir 		{
113cdf0e10cSrcweir 			return osl_writeProfileString(profile, rSection, rEntry, rString);
114cdf0e10cSrcweir 		}
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 		sal_Bool writeBool(const rtl::OString& rSection, const rtl::OString& rEntry, sal_Bool Value)
117cdf0e10cSrcweir 		{
118cdf0e10cSrcweir 			return osl_writeProfileBool(profile, rSection, rEntry, Value);
119cdf0e10cSrcweir 		}
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 		sal_Bool writeIdent(const rtl::OString& rSection, const rtl::OString& rEntry,
122cdf0e10cSrcweir 							sal_uInt32 nFirstId, const std::list< rtl::OString >& rStrings,
123cdf0e10cSrcweir 							sal_uInt32 nValue)
124cdf0e10cSrcweir 		{
125cdf0e10cSrcweir 			int nItems = rStrings.size();
126cdf0e10cSrcweir 			const sal_Char** pStrings = new const sal_Char*[ nItems+1 ];
127cdf0e10cSrcweir 			std::list< rtl::OString >::const_iterator it = rStrings.begin();
128cdf0e10cSrcweir 			nItems = 0;
129cdf0e10cSrcweir 			while( it != rStrings.end() )
130cdf0e10cSrcweir 			{
131cdf0e10cSrcweir 				pStrings[ nItems++ ] = *it;
132cdf0e10cSrcweir 				++it;
133cdf0e10cSrcweir 			}
134cdf0e10cSrcweir 			pStrings[ nItems ] = NULL;
135cdf0e10cSrcweir 			sal_Bool bRet =
136cdf0e10cSrcweir 				osl_writeProfileIdent(profile, rSection, rEntry, nFirstId, pStrings, nValue );
137cdf0e10cSrcweir 			delete pStrings;
138cdf0e10cSrcweir 			return bRet;
139cdf0e10cSrcweir 		}
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 		/** Acquire the mutex, block if already acquired by another thread.
142cdf0e10cSrcweir 			@param Profile handle to a opened profile.
143cdf0e10cSrcweir 			@return False if section or entry could not be found.
144cdf0e10cSrcweir 		*/
145cdf0e10cSrcweir 		sal_Bool removeEntry(const rtl::OString& rSection, const rtl::OString& rEntry)
146cdf0e10cSrcweir 		{
147cdf0e10cSrcweir 			return osl_removeProfileEntry(profile, rSection, rEntry);
148cdf0e10cSrcweir 		}
149cdf0e10cSrcweir 
150cdf0e10cSrcweir 		/** Get all entries belonging to the specified section.
151cdf0e10cSrcweir 			@param Profile handle to a opened profile.
152cdf0e10cSrcweir 			@return Pointer to a array of pointers.
153cdf0e10cSrcweir 		*/
154cdf0e10cSrcweir 		std::list< rtl::OString > getSectionEntries(const rtl::OString& rSection )
155cdf0e10cSrcweir 		{
156cdf0e10cSrcweir 			std::list< rtl::OString > aEntries;
157cdf0e10cSrcweir 
158cdf0e10cSrcweir 			// count buffer size necessary
159cdf0e10cSrcweir 			int n = osl_getProfileSectionEntries( profile, rSection, NULL, 0 );
160cdf0e10cSrcweir 			if( n > 1 )
161cdf0e10cSrcweir 			{
162cdf0e10cSrcweir 				sal_Char* pBuf = new sal_Char[ n+1 ];
163cdf0e10cSrcweir 				osl_getProfileSectionEntries( profile, rSection, pBuf, n+1 );
164cdf0e10cSrcweir 				int nLen;
165cdf0e10cSrcweir 				for( n = 0; ( nLen = strlen( pBuf+n ) ); n += nLen+1 )
166cdf0e10cSrcweir 					aEntries.push_back( rtl::OString( pBuf+n ) );
167cdf0e10cSrcweir 				delete pBuf;
168cdf0e10cSrcweir 			}
169cdf0e10cSrcweir 
170cdf0e10cSrcweir 			return aEntries;
171cdf0e10cSrcweir 		}
172cdf0e10cSrcweir 
173cdf0e10cSrcweir 		/** Get all section entries
174cdf0e10cSrcweir 			@param Profile handle to a opened profile.
175cdf0e10cSrcweir 			@return Pointer to a array of pointers.
176cdf0e10cSrcweir 		*/
177cdf0e10cSrcweir 		std::list< rtl::OString > getSections()
178cdf0e10cSrcweir 		{
179cdf0e10cSrcweir 			std::list< rtl::OString > aSections;
180cdf0e10cSrcweir 
181cdf0e10cSrcweir 			// count buffer size necessary
182cdf0e10cSrcweir 			int n = osl_getProfileSections( profile, NULL, 0 );
183cdf0e10cSrcweir 			if( n > 1 )
184cdf0e10cSrcweir 			{
185cdf0e10cSrcweir 				sal_Char* pBuf = new sal_Char[ n+1 ];
186cdf0e10cSrcweir 				osl_getProfileSections( profile, pBuf, n+1 );
187cdf0e10cSrcweir 				int nLen;
188cdf0e10cSrcweir 				for( n = 0; ( nLen = strlen( pBuf+n ) ); n += nLen+1 )
189cdf0e10cSrcweir 					aSections.push_back( rtl::OString( pBuf+n ) );
190cdf0e10cSrcweir 				delete pBuf;
191cdf0e10cSrcweir 			}
192cdf0e10cSrcweir 
193cdf0e10cSrcweir 			return aSections;
194cdf0e10cSrcweir 		}
195cdf0e10cSrcweir 	};
196cdf0e10cSrcweir }
197cdf0e10cSrcweir 
198cdf0e10cSrcweir #endif	/* _OSL_PROFILE_HXX_ */
199cdf0e10cSrcweir 
200cdf0e10cSrcweir 
201