xref: /aoo41x/main/sal/inc/osl/profile.hxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef _OSL_PROFILE_HXX_
29 #define _OSL_PROFILE_HXX_
30 
31 #include "profile.h"
32 #include <rtl/ustring.hxx>
33 #include <string.h>
34 #include <list>
35 
36 namespace osl {
37 
38 	typedef oslProfileOption ProfileOption;
39 
40 	const int Profile_DEFAULT   = osl_Profile_DEFAULT;
41 	const int Profile_SYSTEM    = osl_Profile_SYSTEM;    /* use system depended functinality */
42 	const int Profile_READLOCK  = osl_Profile_READLOCK;  /* lock file for reading            */
43 	const int Profile_WRITELOCK = osl_Profile_WRITELOCK; /* lock file for writing            */
44 
45     /** Deprecated API.
46         @deprecated
47     */
48 	class Profile {
49 		oslProfile profile;
50 
51 	public:
52 		/** Open or create a configuration profile.
53 			@return 0 if the profile could not be created, otherwise a handle to the profile.
54 		*/
55 		Profile(const rtl::OUString strProfileName, oslProfileOption Options = Profile_DEFAULT )
56 		{
57 			profile = osl_openProfile(strProfileName.pData, Options);
58 			if( ! profile )
59 				throw std::exception();
60 		}
61 
62 
63 		/** Close the opened profile an flush all data to the disk.
64 			@param Profile handle to a opened profile.
65 		*/
66 		~Profile()
67 		{
68 			osl_closeProfile(profile);
69 		}
70 
71 
72 		sal_Bool flush()
73 		{
74 			return osl_flushProfile(profile);
75 		}
76 
77 		rtl::OString readString( const rtl::OString& rSection, const rtl::OString& rEntry,
78 								 const rtl::OString& rDefault)
79 		{
80 			sal_Char aBuf[1024];
81 			return osl_readProfileString( profile,
82 										  rSection,
83 										  rEntry,
84 										  aBuf,
85 										  sizeof( aBuf ),
86 										  rDefault ) ? rtl::OString( aBuf ) : rtl::OString();
87 
88 		}
89 
90 		sal_Bool readBool( const rtl::OString& rSection, const rtl::OString& rEntry, sal_Bool bDefault )
91 		{
92 			return osl_readProfileBool( profile, rSection, rEntry, bDefault );
93 		}
94 
95 		sal_uInt32 readIdent(const rtl::OString& rSection, const rtl::OString& rEntry,
96 							 sal_uInt32 nFirstId, const std::list< rtl::OString >& rStrings,
97 							 sal_uInt32 nDefault)
98 		{
99 			int nItems = rStrings.size();
100 			const sal_Char** pStrings = new const sal_Char*[ nItems+1 ];
101 			std::list< rtl::OString >::const_iterator it = rStrings.begin();
102 			nItems = 0;
103 			while( it != rStrings.end() )
104 			{
105 				pStrings[ nItems++ ] = *it;
106 				++it;
107 			}
108 			pStrings[ nItems ] = NULL;
109 			sal_uInt32 nRet = osl_readProfileIdent(profile, rSection, rEntry, nFirstId, pStrings, nDefault);
110 			delete pStrings;
111 			return nRet;
112 		}
113 
114 		sal_Bool writeString(const rtl::OString& rSection, const rtl::OString& rEntry,
115 							 const rtl::OString& rString)
116 		{
117 			return osl_writeProfileString(profile, rSection, rEntry, rString);
118 		}
119 
120 		sal_Bool writeBool(const rtl::OString& rSection, const rtl::OString& rEntry, sal_Bool Value)
121 		{
122 			return osl_writeProfileBool(profile, rSection, rEntry, Value);
123 		}
124 
125 		sal_Bool writeIdent(const rtl::OString& rSection, const rtl::OString& rEntry,
126 							sal_uInt32 nFirstId, const std::list< rtl::OString >& rStrings,
127 							sal_uInt32 nValue)
128 		{
129 			int nItems = rStrings.size();
130 			const sal_Char** pStrings = new const sal_Char*[ nItems+1 ];
131 			std::list< rtl::OString >::const_iterator it = rStrings.begin();
132 			nItems = 0;
133 			while( it != rStrings.end() )
134 			{
135 				pStrings[ nItems++ ] = *it;
136 				++it;
137 			}
138 			pStrings[ nItems ] = NULL;
139 			sal_Bool bRet =
140 				osl_writeProfileIdent(profile, rSection, rEntry, nFirstId, pStrings, nValue );
141 			delete pStrings;
142 			return bRet;
143 		}
144 
145 		/** Acquire the mutex, block if already acquired by another thread.
146 			@param Profile handle to a opened profile.
147 			@return False if section or entry could not be found.
148 		*/
149 		sal_Bool removeEntry(const rtl::OString& rSection, const rtl::OString& rEntry)
150 		{
151 			return osl_removeProfileEntry(profile, rSection, rEntry);
152 		}
153 
154 		/** Get all entries belonging to the specified section.
155 			@param Profile handle to a opened profile.
156 			@return Pointer to a array of pointers.
157 		*/
158 		std::list< rtl::OString > getSectionEntries(const rtl::OString& rSection )
159 		{
160 			std::list< rtl::OString > aEntries;
161 
162 			// count buffer size necessary
163 			int n = osl_getProfileSectionEntries( profile, rSection, NULL, 0 );
164 			if( n > 1 )
165 			{
166 				sal_Char* pBuf = new sal_Char[ n+1 ];
167 				osl_getProfileSectionEntries( profile, rSection, pBuf, n+1 );
168 				int nLen;
169 				for( n = 0; ( nLen = strlen( pBuf+n ) ); n += nLen+1 )
170 					aEntries.push_back( rtl::OString( pBuf+n ) );
171 				delete pBuf;
172 			}
173 
174 			return aEntries;
175 		}
176 
177 		/** Get all section entries
178 			@param Profile handle to a opened profile.
179 			@return Pointer to a array of pointers.
180 		*/
181 		std::list< rtl::OString > getSections()
182 		{
183 			std::list< rtl::OString > aSections;
184 
185 			// count buffer size necessary
186 			int n = osl_getProfileSections( profile, NULL, 0 );
187 			if( n > 1 )
188 			{
189 				sal_Char* pBuf = new sal_Char[ n+1 ];
190 				osl_getProfileSections( profile, pBuf, n+1 );
191 				int nLen;
192 				for( n = 0; ( nLen = strlen( pBuf+n ) ); n += nLen+1 )
193 					aSections.push_back( rtl::OString( pBuf+n ) );
194 				delete pBuf;
195 			}
196 
197 			return aSections;
198 		}
199 	};
200 }
201 
202 #endif	/* _OSL_PROFILE_HXX_ */
203 
204 
205