xref: /aoo4110/main/sal/inc/osl/profile.hxx (revision b1cdbd2c)
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 
23 
24 #ifndef _OSL_PROFILE_HXX_
25 #define _OSL_PROFILE_HXX_
26 
27 #include "profile.h"
28 #include <rtl/ustring.hxx>
29 #include <string.h>
30 #include <list>
31 
32 namespace osl {
33 
34 	typedef oslProfileOption ProfileOption;
35 
36 	const int Profile_DEFAULT   = osl_Profile_DEFAULT;
37 	const int Profile_SYSTEM    = osl_Profile_SYSTEM;    /* use system depended functinality */
38 	const int Profile_READLOCK  = osl_Profile_READLOCK;  /* lock file for reading            */
39 	const int Profile_WRITELOCK = osl_Profile_WRITELOCK; /* lock file for writing            */
40 
41     /** Deprecated API.
42         @deprecated
43     */
44 	class Profile {
45 		oslProfile profile;
46 
47 	public:
48 		/** Open or create a configuration profile.
49 			@return 0 if the profile could not be created, otherwise a handle to the profile.
50 		*/
Profile(const rtl::OUString strProfileName,oslProfileOption Options=Profile_DEFAULT)51 		Profile(const rtl::OUString strProfileName, oslProfileOption Options = Profile_DEFAULT )
52 		{
53 			profile = osl_openProfile(strProfileName.pData, Options);
54 			if( ! profile )
55 				throw std::exception();
56 		}
57 
58 
59 		/** Close the opened profile an flush all data to the disk.
60 			@param Profile handle to a opened profile.
61 		*/
~Profile()62 		~Profile()
63 		{
64 			osl_closeProfile(profile);
65 		}
66 
67 
flush()68 		sal_Bool flush()
69 		{
70 			return osl_flushProfile(profile);
71 		}
72 
readString(const rtl::OString & rSection,const rtl::OString & rEntry,const rtl::OString & rDefault)73 		rtl::OString readString( const rtl::OString& rSection, const rtl::OString& rEntry,
74 								 const rtl::OString& rDefault)
75 		{
76 			sal_Char aBuf[1024];
77 			return osl_readProfileString( profile,
78 										  rSection.getStr(),
79 										  rEntry.getStr(),
80 										  aBuf,
81 										  sizeof( aBuf ),
82 										  rDefault.getStr() ) ? rtl::OString( aBuf ) : rtl::OString();
83 
84 		}
85 
readBool(const rtl::OString & rSection,const rtl::OString & rEntry,sal_Bool bDefault)86 		sal_Bool readBool( const rtl::OString& rSection, const rtl::OString& rEntry, sal_Bool bDefault )
87 		{
88 			return osl_readProfileBool( profile, rSection.getStr(), rEntry.getStr(), bDefault );
89 		}
90 
readIdent(const rtl::OString & rSection,const rtl::OString & rEntry,sal_uInt32 nFirstId,const std::list<rtl::OString> & rStrings,sal_uInt32 nDefault)91 		sal_uInt32 readIdent(const rtl::OString& rSection, const rtl::OString& rEntry,
92 							 sal_uInt32 nFirstId, const std::list< rtl::OString >& rStrings,
93 							 sal_uInt32 nDefault)
94 		{
95 			int nItems = rStrings.size();
96 			const sal_Char** pStrings = new const sal_Char*[ nItems+1 ];
97 			std::list< rtl::OString >::const_iterator it = rStrings.begin();
98 			nItems = 0;
99 			while( it != rStrings.end() )
100 			{
101 				pStrings[ nItems++ ] = (*it).getStr();
102 				++it;
103 			}
104 			pStrings[ nItems ] = NULL;
105 			sal_uInt32 nRet = osl_readProfileIdent( profile, rSection.getStr(), rEntry.getStr(), nFirstId, pStrings, nDefault);
106 			delete pStrings;
107 			return nRet;
108 		}
109 
writeString(const rtl::OString & rSection,const rtl::OString & rEntry,const rtl::OString & rString)110 		sal_Bool writeString(const rtl::OString& rSection, const rtl::OString& rEntry,
111 							 const rtl::OString& rString)
112 		{
113 			return osl_writeProfileString( profile, rSection.getStr(), rEntry.getStr(), rString.getStr());
114 		}
115 
writeBool(const rtl::OString & rSection,const rtl::OString & rEntry,sal_Bool Value)116 		sal_Bool writeBool(const rtl::OString& rSection, const rtl::OString& rEntry, sal_Bool Value)
117 		{
118 			return osl_writeProfileBool( profile, rSection.getStr(), rEntry.getStr(), Value);
119 		}
120 
writeIdent(const rtl::OString & rSection,const rtl::OString & rEntry,sal_uInt32 nFirstId,const std::list<rtl::OString> & rStrings,sal_uInt32 nValue)121 		sal_Bool writeIdent(const rtl::OString& rSection, const rtl::OString& rEntry,
122 							sal_uInt32 nFirstId, const std::list< rtl::OString >& rStrings,
123 							sal_uInt32 nValue)
124 		{
125 			int nItems = rStrings.size();
126 			const sal_Char** pStrings = new const sal_Char*[ nItems+1 ];
127 			std::list< rtl::OString >::const_iterator it = rStrings.begin();
128 			nItems = 0;
129 			while( it != rStrings.end() )
130 			{
131 				pStrings[ nItems++ ] = (*it).getStr();
132 				++it;
133 			}
134 			pStrings[ nItems ] = NULL;
135 			sal_Bool bRet =
136 				osl_writeProfileIdent( profile, rSection.getStr(), rEntry.getStr(), nFirstId, pStrings, nValue );
137 			delete pStrings;
138 			return bRet;
139 		}
140 
141 		/** Acquire the mutex, block if already acquired by another thread.
142 			@param Profile handle to a opened profile.
143 			@return False if section or entry could not be found.
144 		*/
removeEntry(const rtl::OString & rSection,const rtl::OString & rEntry)145 		sal_Bool removeEntry(const rtl::OString& rSection, const rtl::OString& rEntry)
146 		{
147 			return osl_removeProfileEntry( profile, rSection.getStr(), rEntry.getStr());
148 		}
149 
150 		/** Get all entries belonging to the specified section.
151 			@param Profile handle to a opened profile.
152 			@return Pointer to a array of pointers.
153 		*/
getSectionEntries(const rtl::OString & rSection)154 		std::list< rtl::OString > getSectionEntries(const rtl::OString& rSection )
155 		{
156 			std::list< rtl::OString > aEntries;
157 
158 			// count buffer size necessary
159 			int n = osl_getProfileSectionEntries( profile, rSection.getStr(), NULL, 0 );
160 			if( n > 1 )
161 			{
162 				sal_Char* pBuf = new sal_Char[ n+1 ];
163 				osl_getProfileSectionEntries( profile, rSection.getStr(), pBuf, n+1 );
164 				int nLen;
165 				for( n = 0; ( nLen = strlen( pBuf+n ) ); n += nLen+1 )
166 					aEntries.push_back( rtl::OString( pBuf+n ) );
167 				delete pBuf;
168 			}
169 
170 			return aEntries;
171 		}
172 
173 		/** Get all section entries
174 			@param Profile handle to a opened profile.
175 			@return Pointer to a array of pointers.
176 		*/
getSections()177 		std::list< rtl::OString > getSections()
178 		{
179 			std::list< rtl::OString > aSections;
180 
181 			// count buffer size necessary
182 			int n = osl_getProfileSections( profile, NULL, 0 );
183 			if( n > 1 )
184 			{
185 				sal_Char* pBuf = new sal_Char[ n+1 ];
186 				osl_getProfileSections( profile, pBuf, n+1 );
187 				int nLen;
188 				for( n = 0; ( nLen = strlen( pBuf+n ) ); n += nLen+1 )
189 					aSections.push_back( rtl::OString( pBuf+n ) );
190 				delete pBuf;
191 			}
192 
193 			return aSections;
194 		}
195 	};
196 }
197 
198 #endif	/* _OSL_PROFILE_HXX_ */
199 
200 
201