Avoid potential negative array index access to cached text.
[LibreOffice.git] / include / osl / profile.hxx
blobe83fef0c665c5a588ec5b37ce59f433e6e1c847a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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"
30 #include <string.h>
31 #include <exception>
32 #include <list>
34 namespace osl {
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 */
43 /** Deprecated API.
44 @deprecated
46 class Profile {
47 oslProfile profile;
49 public:
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);
57 if( ! profile )
58 throw std::exception();
62 /** Close the opened profile an flush all data to the disk.
64 ~Profile()
66 osl_closeProfile(profile);
70 bool flush()
72 return osl_flushProfile(profile);
75 rtl::OString readString( const rtl::OString& rSection, const rtl::OString& rEntry,
76 const rtl::OString& rDefault)
78 char aBuf[1024];
79 return osl_readProfileString( profile,
80 rSection.getStr(),
81 rEntry.getStr(),
82 aBuf,
83 sizeof( aBuf ),
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,
95 sal_uInt32 nDefault)
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();
100 nItems = 0;
101 while( it != rStrings.end() )
103 pStrings[ nItems++ ] = it->getStr();
104 ++it;
106 pStrings[ nItems ] = NULL;
107 sal_uInt32 nRet = osl_readProfileIdent(profile, rSection.getStr(), rEntry.getStr(), nFirstId, pStrings, nDefault);
108 delete[] pStrings;
109 return nRet;
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,
125 sal_uInt32 nValue)
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();
130 nItems = 0;
131 while( it != rStrings.end() )
133 pStrings[ nItems++ ] = it->getStr();
134 ++it;
136 pStrings[ nItems ] = NULL;
137 bool bRet =
138 osl_writeProfileIdent(profile, rSection.getStr(), rEntry.getStr(), nFirstId, pStrings, nValue );
139 delete[] pStrings;
140 return bRet;
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 );
163 if( n > 1 )
165 char* pBuf = new char[ n+1 ];
166 osl_getProfileSectionEntries( profile, rSection.getStr(), pBuf, n+1 );
167 size_t nLen;
168 for( n = 0; ; n += nLen+1 )
170 nLen = strlen( pBuf+n );
171 if (!nLen)
172 break;
173 aEntries.push_back( rtl::OString( pBuf+n ) );
175 delete[] pBuf;
178 return aEntries;
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 );
190 if( n > 1 )
192 char* pBuf = new char[ n+1 ];
193 osl_getProfileSections( profile, pBuf, n+1 );
194 size_t nLen;
195 for( n = 0; ; n += nLen+1 )
197 nLen = strlen( pBuf+n );
198 if (!nLen)
199 break;
200 aSections.push_back( rtl::OString( pBuf+n ) );
202 delete[] pBuf;
205 return aSections;
210 #endif // INCLUDED_OSL_PROFILE_HXX
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */