Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #ifndef INCLUDED_OSL_PROFILE_HXX
21 : #define INCLUDED_OSL_PROFILE_HXX
22 :
23 : #include <osl/profile.h>
24 : #include <rtl/ustring.hxx>
25 :
26 : #include <string.h>
27 : #include <exception>
28 : #include <list>
29 :
30 : namespace osl {
31 :
32 : typedef oslProfileOption ProfileOption;
33 :
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 */
38 :
39 : /** Deprecated API.
40 : @deprecated
41 : */
42 : class Profile {
43 : oslProfile profile;
44 :
45 : public:
46 : /** Open or create a configuration profile.
47 : @return 0 if the profile could not be created, otherwise a handle to the profile.
48 : */
49 2 : Profile(const rtl::OUString & strProfileName, oslProfileOption Options = Profile_DEFAULT )
50 : {
51 2 : profile = osl_openProfile(strProfileName.pData, Options);
52 2 : if( ! profile )
53 0 : throw std::exception();
54 2 : }
55 :
56 :
57 : /** Close the opened profile an flush all data to the disk.
58 : */
59 2 : ~Profile()
60 : {
61 2 : osl_closeProfile(profile);
62 2 : }
63 :
64 :
65 : bool flush()
66 : {
67 : return osl_flushProfile(profile);
68 : }
69 :
70 2 : rtl::OString readString( const rtl::OString& rSection, const rtl::OString& rEntry,
71 : const rtl::OString& rDefault)
72 : {
73 : sal_Char aBuf[1024];
74 : return osl_readProfileString( profile,
75 : rSection.getStr(),
76 : rEntry.getStr(),
77 : aBuf,
78 : sizeof( aBuf ),
79 2 : rDefault.getStr() ) ? rtl::OString( aBuf ) : rtl::OString();
80 :
81 : }
82 :
83 : bool readBool( const rtl::OString& rSection, const rtl::OString& rEntry, bool bDefault )
84 : {
85 : return osl_readProfileBool( profile, rSection.getStr(), rEntry.getStr(), bDefault );
86 : }
87 :
88 : sal_uInt32 readIdent(const rtl::OString& rSection, const rtl::OString& rEntry,
89 : sal_uInt32 nFirstId, const std::list< rtl::OString >& rStrings,
90 : sal_uInt32 nDefault)
91 : {
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();
95 : nItems = 0;
96 : while( it != rStrings.end() )
97 : {
98 : pStrings[ nItems++ ] = it->getStr();
99 : ++it;
100 : }
101 : pStrings[ nItems ] = NULL;
102 : sal_uInt32 nRet = osl_readProfileIdent(profile, rSection.getStr(), rEntry.getStr(), nFirstId, pStrings, nDefault);
103 : delete[] pStrings;
104 : return nRet;
105 : }
106 :
107 : bool writeString(const rtl::OString& rSection, const rtl::OString& rEntry,
108 : const rtl::OString& rString)
109 : {
110 : return osl_writeProfileString(profile, rSection.getStr(), rEntry.getStr(), rString.getStr());
111 : }
112 :
113 : bool writeBool(const rtl::OString& rSection, const rtl::OString& rEntry, bool Value)
114 : {
115 : return osl_writeProfileBool(profile, rSection.getStr(), rEntry.getStr(), Value);
116 : }
117 :
118 : bool writeIdent(const rtl::OString& rSection, const rtl::OString& rEntry,
119 : sal_uInt32 nFirstId, const std::list< rtl::OString >& rStrings,
120 : sal_uInt32 nValue)
121 : {
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();
125 : nItems = 0;
126 : while( it != rStrings.end() )
127 : {
128 : pStrings[ nItems++ ] = it->getStr();
129 : ++it;
130 : }
131 : pStrings[ nItems ] = NULL;
132 : bool bRet =
133 : osl_writeProfileIdent(profile, rSection.getStr(), rEntry.getStr(), nFirstId, pStrings, nValue );
134 : delete[] pStrings;
135 : return bRet;
136 : }
137 :
138 : /** Remove an entry from a section.
139 : @param rSection Name of the section.
140 : @param rEntry Name of the entry to remove.
141 : @return False if section or entry could not be found.
142 : */
143 : bool removeEntry(const rtl::OString& rSection, const rtl::OString& rEntry)
144 : {
145 : return osl_removeProfileEntry(profile, rSection.getStr(), rEntry.getStr());
146 : }
147 :
148 : /** Get all entries belonging to the specified section.
149 : @param rSection Name of the section.
150 : @return Pointer to a array of pointers.
151 : */
152 : std::list< rtl::OString > getSectionEntries(const rtl::OString& rSection )
153 : {
154 : std::list< rtl::OString > aEntries;
155 :
156 : // count buffer size necessary
157 : size_t n = osl_getProfileSectionEntries( profile, rSection.getStr(), NULL, 0 );
158 : if( n > 1 )
159 : {
160 : sal_Char* pBuf = new sal_Char[ n+1 ];
161 : osl_getProfileSectionEntries( profile, rSection.getStr(), pBuf, n+1 );
162 : size_t nLen;
163 : for( n = 0; ( nLen = strlen( pBuf+n ) ); n += nLen+1 )
164 : aEntries.push_back( rtl::OString( pBuf+n ) );
165 : delete[] pBuf;
166 : }
167 :
168 : return aEntries;
169 : }
170 :
171 : /** Get all section entries
172 : @return Pointer to a array of pointers.
173 : */
174 : std::list< rtl::OString > getSections()
175 : {
176 : std::list< rtl::OString > aSections;
177 :
178 : // count buffer size necessary
179 : size_t n = osl_getProfileSections( profile, NULL, 0 );
180 : if( n > 1 )
181 : {
182 : sal_Char* pBuf = new sal_Char[ n+1 ];
183 : osl_getProfileSections( profile, pBuf, n+1 );
184 : size_t nLen;
185 : for( n = 0; ( nLen = strlen( pBuf+n ) ); n += nLen+1 )
186 : aSections.push_back( rtl::OString( pBuf+n ) );
187 : delete[] pBuf;
188 : }
189 :
190 : return aSections;
191 : }
192 : };
193 : }
194 :
195 : #endif // INCLUDED_OSL_PROFILE_HXX
196 :
197 :
198 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|