xref: /trunk/main/sal/inc/osl/profile.hxx (revision 914d351e5f5b84e4342a86d6ab8d4aca7308b9bd)
1565d668cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3565d668cSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4565d668cSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5565d668cSAndrew Rist  * distributed with this work for additional information
6565d668cSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7565d668cSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8565d668cSAndrew Rist  * "License"); you may not use this file except in compliance
9565d668cSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11565d668cSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13565d668cSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14565d668cSAndrew Rist  * software distributed under the License is distributed on an
15565d668cSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16565d668cSAndrew Rist  * KIND, either express or implied.  See the License for the
17565d668cSAndrew Rist  * specific language governing permissions and limitations
18565d668cSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20565d668cSAndrew Rist  *************************************************************/
21565d668cSAndrew Rist 
22565d668cSAndrew 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;
37*67f7bfb1SJohn Bampton     const int Profile_SYSTEM    = osl_Profile_SYSTEM;    /* use system depended functionality */
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         */
Profile(const rtl::OUString strProfileName,oslProfileOption Options=Profile_DEFAULT)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         */
~Profile()62cdf0e10cSrcweir         ~Profile()
63cdf0e10cSrcweir         {
64cdf0e10cSrcweir             osl_closeProfile(profile);
65cdf0e10cSrcweir         }
66cdf0e10cSrcweir 
67cdf0e10cSrcweir 
flush()68cdf0e10cSrcweir         sal_Bool flush()
69cdf0e10cSrcweir         {
70cdf0e10cSrcweir             return osl_flushProfile(profile);
71cdf0e10cSrcweir         }
72cdf0e10cSrcweir 
readString(const rtl::OString & rSection,const rtl::OString & rEntry,const rtl::OString & rDefault)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,
7824c56ab9SHerbert Dürr                                           rSection.getStr(),
7924c56ab9SHerbert Dürr                                           rEntry.getStr(),
80cdf0e10cSrcweir                                           aBuf,
81cdf0e10cSrcweir                                           sizeof( aBuf ),
8224c56ab9SHerbert Dürr                                           rDefault.getStr() ) ? rtl::OString( aBuf ) : rtl::OString();
83cdf0e10cSrcweir 
84cdf0e10cSrcweir         }
85cdf0e10cSrcweir 
readBool(const rtl::OString & rSection,const rtl::OString & rEntry,sal_Bool bDefault)86cdf0e10cSrcweir         sal_Bool readBool( const rtl::OString& rSection, const rtl::OString& rEntry, sal_Bool bDefault )
87cdf0e10cSrcweir         {
8824c56ab9SHerbert Dürr             return osl_readProfileBool( profile, rSection.getStr(), rEntry.getStr(), bDefault );
89cdf0e10cSrcweir         }
90cdf0e10cSrcweir 
readIdent(const rtl::OString & rSection,const rtl::OString & rEntry,sal_uInt32 nFirstId,const std::list<rtl::OString> & rStrings,sal_uInt32 nDefault)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             {
10124c56ab9SHerbert Dürr                 pStrings[ nItems++ ] = (*it).getStr();
102cdf0e10cSrcweir                 ++it;
103cdf0e10cSrcweir             }
104cdf0e10cSrcweir             pStrings[ nItems ] = NULL;
10524c56ab9SHerbert Dürr             sal_uInt32 nRet = osl_readProfileIdent( profile, rSection.getStr(), rEntry.getStr(), nFirstId, pStrings, nDefault);
1064078a611SDon Lewis             delete[] pStrings;
107cdf0e10cSrcweir             return nRet;
108cdf0e10cSrcweir         }
109cdf0e10cSrcweir 
writeString(const rtl::OString & rSection,const rtl::OString & rEntry,const rtl::OString & rString)110cdf0e10cSrcweir         sal_Bool writeString(const rtl::OString& rSection, const rtl::OString& rEntry,
111cdf0e10cSrcweir                              const rtl::OString& rString)
112cdf0e10cSrcweir         {
11324c56ab9SHerbert Dürr             return osl_writeProfileString( profile, rSection.getStr(), rEntry.getStr(), rString.getStr());
114cdf0e10cSrcweir         }
115cdf0e10cSrcweir 
writeBool(const rtl::OString & rSection,const rtl::OString & rEntry,sal_Bool Value)116cdf0e10cSrcweir         sal_Bool writeBool(const rtl::OString& rSection, const rtl::OString& rEntry, sal_Bool Value)
117cdf0e10cSrcweir         {
11824c56ab9SHerbert Dürr             return osl_writeProfileBool( profile, rSection.getStr(), rEntry.getStr(), Value);
119cdf0e10cSrcweir         }
120cdf0e10cSrcweir 
writeIdent(const rtl::OString & rSection,const rtl::OString & rEntry,sal_uInt32 nFirstId,const std::list<rtl::OString> & rStrings,sal_uInt32 nValue)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             {
13124c56ab9SHerbert Dürr                 pStrings[ nItems++ ] = (*it).getStr();
132cdf0e10cSrcweir                 ++it;
133cdf0e10cSrcweir             }
134cdf0e10cSrcweir             pStrings[ nItems ] = NULL;
135cdf0e10cSrcweir             sal_Bool bRet =
13624c56ab9SHerbert Dürr                 osl_writeProfileIdent( profile, rSection.getStr(), rEntry.getStr(), nFirstId, pStrings, nValue );
1374078a611SDon Lewis             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         */
removeEntry(const rtl::OString & rSection,const rtl::OString & rEntry)145cdf0e10cSrcweir         sal_Bool removeEntry(const rtl::OString& rSection, const rtl::OString& rEntry)
146cdf0e10cSrcweir         {
14724c56ab9SHerbert Dürr             return osl_removeProfileEntry( profile, rSection.getStr(), rEntry.getStr());
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         */
getSectionEntries(const rtl::OString & rSection)154cdf0e10cSrcweir         std::list< rtl::OString > getSectionEntries(const rtl::OString& rSection )
155cdf0e10cSrcweir         {
156cdf0e10cSrcweir             std::list< rtl::OString > aEntries;
157cdf0e10cSrcweir 
158cdf0e10cSrcweir             // count buffer size necessary
15924c56ab9SHerbert Dürr             int n = osl_getProfileSectionEntries( profile, rSection.getStr(), NULL, 0 );
160cdf0e10cSrcweir             if( n > 1 )
161cdf0e10cSrcweir             {
162cdf0e10cSrcweir                 sal_Char* pBuf = new sal_Char[ n+1 ];
16324c56ab9SHerbert Dürr                 osl_getProfileSectionEntries( profile, rSection.getStr(), 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 ) );
1674078a611SDon Lewis                 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         */
getSections()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 ) );
1904078a611SDon Lewis                 delete[] pBuf;
191cdf0e10cSrcweir             }
192cdf0e10cSrcweir 
193cdf0e10cSrcweir             return aSections;
194cdf0e10cSrcweir         }
195cdf0e10cSrcweir     };
196cdf0e10cSrcweir }
197cdf0e10cSrcweir 
198cdf0e10cSrcweir #endif  /* _OSL_PROFILE_HXX_ */
199