LibreOffice
LibreOffice 24.2 SDK C/C++ API Reference
profile.hxx
Go to the documentation of this file.
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 /*
21  * This file is part of LibreOffice published API.
22  */
23 
24 #ifndef INCLUDED_OSL_PROFILE_HXX
25 #define INCLUDED_OSL_PROFILE_HXX
26 
27 #include "osl/profile.h"
28 #include "rtl/ustring.hxx"
29 
30 #include <string.h>
31 #include <exception>
32 #include <list>
33 
34 namespace osl {
35 
37 
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 */
42 
46  class Profile {
47  oslProfile profile;
48 
49  public:
54  Profile(const rtl::OUString & strProfileName, oslProfileOption Options = Profile_DEFAULT )
55  {
56  profile = osl_openProfile(strProfileName.pData, Options);
57  if( ! profile )
58  throw std::exception();
59  }
60 
61 
65  {
66  osl_closeProfile(profile);
67  }
68 
69 
70  bool flush()
71  {
72  return osl_flushProfile(profile);
73  }
74 
75  rtl::OString readString( const rtl::OString& rSection, const rtl::OString& rEntry,
76  const rtl::OString& rDefault)
77  {
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();
85 
86  }
87 
88  bool readBool( const rtl::OString& rSection, const rtl::OString& rEntry, bool bDefault )
89  {
90  return osl_readProfileBool( profile, rSection.getStr(), rEntry.getStr(), bDefault );
91  }
92 
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)
96  {
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() )
102  {
103  pStrings[ nItems++ ] = it->getStr();
104  ++it;
105  }
106  pStrings[ nItems ] = NULL;
107  sal_uInt32 nRet = osl_readProfileIdent(profile, rSection.getStr(), rEntry.getStr(), nFirstId, pStrings, nDefault);
108  delete[] pStrings;
109  return nRet;
110  }
111 
112  bool writeString(const rtl::OString& rSection, const rtl::OString& rEntry,
113  const rtl::OString& rString)
114  {
115  return osl_writeProfileString(profile, rSection.getStr(), rEntry.getStr(), rString.getStr());
116  }
117 
118  bool writeBool(const rtl::OString& rSection, const rtl::OString& rEntry, bool Value)
119  {
120  return osl_writeProfileBool(profile, rSection.getStr(), rEntry.getStr(), Value);
121  }
122 
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)
126  {
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() )
132  {
133  pStrings[ nItems++ ] = it->getStr();
134  ++it;
135  }
136  pStrings[ nItems ] = NULL;
137  bool bRet =
138  osl_writeProfileIdent(profile, rSection.getStr(), rEntry.getStr(), nFirstId, pStrings, nValue );
139  delete[] pStrings;
140  return bRet;
141  }
142 
148  bool removeEntry(const rtl::OString& rSection, const rtl::OString& rEntry)
149  {
150  return osl_removeProfileEntry(profile, rSection.getStr(), rEntry.getStr());
151  }
152 
157  std::list< rtl::OString > getSectionEntries(const rtl::OString& rSection )
158  {
159  std::list< rtl::OString > aEntries;
160 
161  // count buffer size necessary
162  size_t n = osl_getProfileSectionEntries( profile, rSection.getStr(), NULL, 0 );
163  if( n > 1 )
164  {
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 )
169  {
170  nLen = strlen( pBuf+n );
171  if (!nLen)
172  break;
173  aEntries.push_back( rtl::OString( pBuf+n ) );
174  }
175  delete[] pBuf;
176  }
177 
178  return aEntries;
179  }
180 
184  std::list< rtl::OString > getSections()
185  {
186  std::list< rtl::OString > aSections;
187 
188  // count buffer size necessary
189  size_t n = osl_getProfileSections( profile, NULL, 0 );
190  if( n > 1 )
191  {
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 )
196  {
197  nLen = strlen( pBuf+n );
198  if (!nLen)
199  break;
200  aSections.push_back( rtl::OString( pBuf+n ) );
201  }
202  delete[] pBuf;
203  }
204 
205  return aSections;
206  }
207  };
208 }
209 
210 #endif // INCLUDED_OSL_PROFILE_HXX
211 
212 
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SAL_DLLPUBLIC sal_uInt32 osl_readProfileIdent(oslProfile Profile, const char *pszSection, const char *pszEntry, sal_uInt32 FirstId, const char *Strings[], sal_uInt32 Default) SAL_COLD
Deprecated API.
std::list< rtl::OString > getSectionEntries(const rtl::OString &rSection)
Get all entries belonging to the specified section.
Definition: profile.hxx:157
const int Profile_DEFAULT
Definition: profile.hxx:38
#define osl_Profile_SYSTEM
Definition: profile.h:40
oslProfileOption ProfileOption
Definition: profile.hxx:36
Deprecated API.
Definition: profile.hxx:46
SAL_DLLPUBLIC sal_Bool osl_removeProfileEntry(oslProfile Profile, const char *pszSection, const char *pszEntry) SAL_COLD
Deprecated API.
Definition: condition.hxx:31
const char * getStr() const SAL_RETURNS_NONNULL
Returns a pointer to the characters of this string.
Definition: string.hxx:685
bool readBool(const rtl::OString &rSection, const rtl::OString &rEntry, bool bDefault)
Definition: profile.hxx:88
void * oslProfile
Definition: profile.h:46
SAL_DLLPUBLIC sal_Bool osl_flushProfile(oslProfile Profile) SAL_COLD
Deprecated API.
sal_uInt32 readIdent(const rtl::OString &rSection, const rtl::OString &rEntry, sal_uInt32 nFirstId, const std::list< rtl::OString > &rStrings, sal_uInt32 nDefault)
Definition: profile.hxx:93
rtl::OString readString(const rtl::OString &rSection, const rtl::OString &rEntry, const rtl::OString &rDefault)
Definition: profile.hxx:75
#define osl_Profile_DEFAULT
Definition: profile.h:39
bool flush()
Definition: profile.hxx:70
bool removeEntry(const rtl::OString &rSection, const rtl::OString &rEntry)
Remove an entry from a section.
Definition: profile.hxx:148
SAL_DLLPUBLIC sal_Bool osl_writeProfileString(oslProfile Profile, const char *pszSection, const char *pszEntry, const char *pszString) SAL_COLD
Deprecated API.
Profile(const rtl::OUString &strProfileName, oslProfileOption Options=Profile_DEFAULT)
Open or create a configuration profile.
Definition: profile.hxx:54
SAL_DLLPUBLIC sal_Bool osl_readProfileString(oslProfile Profile, const char *pszSection, const char *pszEntry, char *pszString, sal_uInt32 MaxLen, const char *pszDefault) SAL_COLD
Deprecated API.
bool writeString(const rtl::OString &rSection, const rtl::OString &rEntry, const rtl::OString &rString)
Definition: profile.hxx:112
sal_uInt32 oslProfileOption
Definition: profile.h:37
SAL_DLLPUBLIC sal_Bool osl_writeProfileBool(oslProfile Profile, const char *pszSection, const char *pszEntry, sal_Bool Value) SAL_COLD
Deprecated API.
std::list< rtl::OString > getSections()
Get all section entries.
Definition: profile.hxx:184
SAL_DLLPUBLIC sal_Bool osl_writeProfileIdent(oslProfile Profile, const char *pszSection, const char *pszEntry, sal_uInt32 FirstId, const char *Strings[], sal_uInt32 Value) SAL_COLD
Deprecated API.
const int Profile_WRITELOCK
Definition: profile.hxx:41
SAL_DLLPUBLIC sal_Bool osl_closeProfile(oslProfile Profile) SAL_COLD
Deprecated API.
#define osl_Profile_WRITELOCK
Definition: profile.h:42
SAL_DLLPUBLIC oslProfile osl_openProfile(rtl_uString *strProfileName, oslProfileOption Options) SAL_COLD
Deprecated API.
This String class provide base functionality for C++ like 8-Bit character array handling.
Definition: string.hxx:191
This String class provides base functionality for C++ like Unicode character array handling...
Definition: ustring.hxx:170
SAL_DLLPUBLIC sal_uInt32 osl_getProfileSectionEntries(oslProfile Profile, const char *pszSection, char *pszBuffer, sal_uInt32 MaxLen) SAL_COLD
Deprecated API.
#define osl_Profile_READLOCK
Definition: profile.h:41
const int Profile_SYSTEM
Definition: profile.hxx:39
bool writeIdent(const rtl::OString &rSection, const rtl::OString &rEntry, sal_uInt32 nFirstId, const std::list< rtl::OString > &rStrings, sal_uInt32 nValue)
Definition: profile.hxx:123
bool writeBool(const rtl::OString &rSection, const rtl::OString &rEntry, bool Value)
Definition: profile.hxx:118
SAL_DLLPUBLIC sal_uInt32 osl_getProfileSections(oslProfile Profile, char *pszBuffer, sal_uInt32 MaxLen) SAL_COLD
Deprecated API.
SAL_DLLPUBLIC sal_Bool osl_readProfileBool(oslProfile Profile, const char *pszSection, const char *pszEntry, sal_Bool Default) SAL_COLD
Deprecated API.
~Profile()
Close the opened profile an flush all data to the disk.
Definition: profile.hxx:64
const int Profile_READLOCK
Definition: profile.hxx:40