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 .
20 #ifndef INCLUDED_OSL_PROFILE_HXX
21 #define INCLUDED_OSL_PROFILE_HXX
23 #include <osl/profile.h>
24 #include <rtl/ustring.hxx>
32 typedef oslProfileOption ProfileOption
;
34 const int Profile_DEFAULT
= osl_Profile_DEFAULT
;
35 const int Profile_SYSTEM
= osl_Profile_SYSTEM
; /* use system depended functinality */
36 const int Profile_READLOCK
= osl_Profile_READLOCK
; /* lock file for reading */
37 const int Profile_WRITELOCK
= osl_Profile_WRITELOCK
; /* lock file for writing */
46 /** Open or create a configuration profile.
47 @retval 0 if the profile could not be created, otherwise a handle to the profile.
49 Profile(const rtl::OUString
& strProfileName
, oslProfileOption Options
= Profile_DEFAULT
)
51 profile
= osl_openProfile(strProfileName
.pData
, Options
);
53 throw std::exception();
57 /** Close the opened profile an flush all data to the disk.
61 osl_closeProfile(profile
);
67 return osl_flushProfile(profile
);
70 rtl::OString
readString( const rtl::OString
& rSection
, const rtl::OString
& rEntry
,
71 const rtl::OString
& rDefault
)
74 return osl_readProfileString( profile
,
79 rDefault
.getStr() ) ? rtl::OString( aBuf
) : rtl::OString();
83 bool readBool( const rtl::OString
& rSection
, const rtl::OString
& rEntry
, bool bDefault
)
85 return osl_readProfileBool( profile
, rSection
.getStr(), rEntry
.getStr(), bDefault
);
88 sal_uInt32
readIdent(const rtl::OString
& rSection
, const rtl::OString
& rEntry
,
89 sal_uInt32 nFirstId
, const std::list
< rtl::OString
>& rStrings
,
92 size_t nItems
= rStrings
.size();
93 const sal_Char
** pStrings
= new const sal_Char
*[ nItems
+1 ];
94 std::list
< rtl::OString
>::const_iterator it
= rStrings
.begin();
96 while( it
!= rStrings
.end() )
98 pStrings
[ nItems
++ ] = it
->getStr();
101 pStrings
[ nItems
] = NULL
;
102 sal_uInt32 nRet
= osl_readProfileIdent(profile
, rSection
.getStr(), rEntry
.getStr(), nFirstId
, pStrings
, nDefault
);
107 bool writeString(const rtl::OString
& rSection
, const rtl::OString
& rEntry
,
108 const rtl::OString
& rString
)
110 return osl_writeProfileString(profile
, rSection
.getStr(), rEntry
.getStr(), rString
.getStr());
113 bool writeBool(const rtl::OString
& rSection
, const rtl::OString
& rEntry
, bool Value
)
115 return osl_writeProfileBool(profile
, rSection
.getStr(), rEntry
.getStr(), Value
);
118 bool writeIdent(const rtl::OString
& rSection
, const rtl::OString
& rEntry
,
119 sal_uInt32 nFirstId
, const std::list
< rtl::OString
>& rStrings
,
122 size_t nItems
= rStrings
.size();
123 const sal_Char
** pStrings
= new const sal_Char
*[ nItems
+1 ];
124 std::list
< rtl::OString
>::const_iterator it
= rStrings
.begin();
126 while( it
!= rStrings
.end() )
128 pStrings
[ nItems
++ ] = it
->getStr();
131 pStrings
[ nItems
] = NULL
;
133 osl_writeProfileIdent(profile
, rSection
.getStr(), rEntry
.getStr(), nFirstId
, pStrings
, nValue
);
138 /** Remove an entry from a section.
139 @param rSection Name of the section.
140 @param rEntry Name of the entry to remove.
141 @retval False if section or entry could not be found.
143 bool removeEntry(const rtl::OString
& rSection
, const rtl::OString
& rEntry
)
145 return osl_removeProfileEntry(profile
, rSection
.getStr(), rEntry
.getStr());
148 /** Get all entries belonging to the specified section.
149 @param rSection Name of the section.
150 @return Pointer to a array of pointers.
152 std::list
< rtl::OString
> getSectionEntries(const rtl::OString
& rSection
)
154 std::list
< rtl::OString
> aEntries
;
156 // count buffer size necessary
157 size_t n
= osl_getProfileSectionEntries( profile
, rSection
.getStr(), NULL
, 0 );
160 sal_Char
* pBuf
= new sal_Char
[ n
+1 ];
161 osl_getProfileSectionEntries( profile
, rSection
.getStr(), pBuf
, n
+1 );
163 for( n
= 0; ( nLen
= strlen( pBuf
+n
) ); n
+= nLen
+1 )
164 aEntries
.push_back( rtl::OString( pBuf
+n
) );
171 /** Get all section entries
172 @return Pointer to a array of pointers.
174 std::list
< rtl::OString
> getSections()
176 std::list
< rtl::OString
> aSections
;
178 // count buffer size necessary
179 size_t n
= osl_getProfileSections( profile
, NULL
, 0 );
182 sal_Char
* pBuf
= new sal_Char
[ n
+1 ];
183 osl_getProfileSections( profile
, pBuf
, n
+1 );
185 for( n
= 0; ( nLen
= strlen( pBuf
+n
) ); n
+= nLen
+1 )
186 aSections
.push_back( rtl::OString( pBuf
+n
) );
195 #endif // INCLUDED_OSL_PROFILE_HXX
198 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */