1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 * This file is part of LibreOffice published API.
24 #ifndef INCLUDED_OSL_PROFILE_HXX
25 #define INCLUDED_OSL_PROFILE_HXX
27 #include "osl/profile.h"
28 #include "rtl/ustring.hxx"
36 typedef oslProfileOption ProfileOption
;
38 const int Profile_DEFAULT
= osl_Profile_DEFAULT
;
39 const int Profile_SYSTEM
= osl_Profile_SYSTEM
; /* use system depended functionality */
40 const int Profile_READLOCK
= osl_Profile_READLOCK
; /* lock file for reading */
41 const int Profile_WRITELOCK
= osl_Profile_WRITELOCK
; /* lock file for writing */
50 /** Open or create a configuration profile.
52 Sets a handle to the profile, otherwise if the profile could not be opened then throw an exception.
54 Profile(const rtl::OUString
& strProfileName
, oslProfileOption Options
= Profile_DEFAULT
)
56 profile
= osl_openProfile(strProfileName
.pData
, Options
);
58 throw std::exception();
62 /** Close the opened profile an flush all data to the disk.
66 osl_closeProfile(profile
);
72 return osl_flushProfile(profile
);
75 rtl::OString
readString( const rtl::OString
& rSection
, const rtl::OString
& rEntry
,
76 const rtl::OString
& rDefault
)
79 return osl_readProfileString( profile
,
84 rDefault
.getStr() ) ? rtl::OString( aBuf
) : rtl::OString();
88 bool readBool( const rtl::OString
& rSection
, const rtl::OString
& rEntry
, bool bDefault
)
90 return osl_readProfileBool( profile
, rSection
.getStr(), rEntry
.getStr(), bDefault
);
93 sal_uInt32
readIdent(const rtl::OString
& rSection
, const rtl::OString
& rEntry
,
94 sal_uInt32 nFirstId
, const std::list
< rtl::OString
>& rStrings
,
97 size_t nItems
= rStrings
.size();
98 const char** pStrings
= new const char*[ nItems
+1 ];
99 std::list
< rtl::OString
>::const_iterator it
= rStrings
.begin();
101 while( it
!= rStrings
.end() )
103 pStrings
[ nItems
++ ] = it
->getStr();
106 pStrings
[ nItems
] = NULL
;
107 sal_uInt32 nRet
= osl_readProfileIdent(profile
, rSection
.getStr(), rEntry
.getStr(), nFirstId
, pStrings
, nDefault
);
112 bool writeString(const rtl::OString
& rSection
, const rtl::OString
& rEntry
,
113 const rtl::OString
& rString
)
115 return osl_writeProfileString(profile
, rSection
.getStr(), rEntry
.getStr(), rString
.getStr());
118 bool writeBool(const rtl::OString
& rSection
, const rtl::OString
& rEntry
, bool Value
)
120 return osl_writeProfileBool(profile
, rSection
.getStr(), rEntry
.getStr(), Value
);
123 bool writeIdent(const rtl::OString
& rSection
, const rtl::OString
& rEntry
,
124 sal_uInt32 nFirstId
, const std::list
< rtl::OString
>& rStrings
,
127 size_t nItems
= rStrings
.size();
128 const char** pStrings
= new const char*[ nItems
+1 ];
129 std::list
< rtl::OString
>::const_iterator it
= rStrings
.begin();
131 while( it
!= rStrings
.end() )
133 pStrings
[ nItems
++ ] = it
->getStr();
136 pStrings
[ nItems
] = NULL
;
138 osl_writeProfileIdent(profile
, rSection
.getStr(), rEntry
.getStr(), nFirstId
, pStrings
, nValue
);
143 /** Remove an entry from a section.
144 @param rSection Name of the section.
145 @param rEntry Name of the entry to remove.
146 @retval False if section or entry could not be found.
148 bool removeEntry(const rtl::OString
& rSection
, const rtl::OString
& rEntry
)
150 return osl_removeProfileEntry(profile
, rSection
.getStr(), rEntry
.getStr());
153 /** Get all entries belonging to the specified section.
154 @param rSection Name of the section.
155 @return Pointer to an array of pointers.
157 std::list
< rtl::OString
> getSectionEntries(const rtl::OString
& rSection
)
159 std::list
< rtl::OString
> aEntries
;
161 // count buffer size necessary
162 size_t n
= osl_getProfileSectionEntries( profile
, rSection
.getStr(), NULL
, 0 );
165 char* pBuf
= new char[ n
+1 ];
166 osl_getProfileSectionEntries( profile
, rSection
.getStr(), pBuf
, n
+1 );
168 for( n
= 0; ; n
+= nLen
+1 )
170 nLen
= strlen( pBuf
+n
);
173 aEntries
.push_back( rtl::OString( pBuf
+n
) );
181 /** Get all section entries
182 @return Pointer to an array of pointers.
184 std::list
< rtl::OString
> getSections()
186 std::list
< rtl::OString
> aSections
;
188 // count buffer size necessary
189 size_t n
= osl_getProfileSections( profile
, NULL
, 0 );
192 char* pBuf
= new char[ n
+1 ];
193 osl_getProfileSections( profile
, pBuf
, n
+1 );
195 for( n
= 0; ; n
+= nLen
+1 )
197 nLen
= strlen( pBuf
+n
);
200 aSections
.push_back( rtl::OString( pBuf
+n
) );
210 #endif // INCLUDED_OSL_PROFILE_HXX
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */