LibreOffice
LibreOffice 5.3 SDK C/C++ API Reference
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 #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 
33 
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 
42  class Profile {
43  oslProfile profile;
44 
45  public:
49  Profile(const rtl::OUString & strProfileName, oslProfileOption Options = Profile_DEFAULT )
50  {
51  profile = osl_openProfile(strProfileName.pData, Options);
52  if( ! profile )
53  throw std::exception();
54  }
55 
56 
60  {
61  osl_closeProfile(profile);
62  }
63 
64 
65  bool flush()
66  {
67  return osl_flushProfile(profile);
68  }
69 
70  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  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 
143  bool removeEntry(const rtl::OString& rSection, const rtl::OString& rEntry)
144  {
145  return osl_removeProfileEntry(profile, rSection.getStr(), rEntry.getStr());
146  }
147 
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 
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: */
SAL_DLLPUBLIC sal_Bool osl_flushProfile(oslProfile Profile) SAL_COLD
Deprecated API.
bool removeEntry(const rtl::OString &rSection, const rtl::OString &rEntry)
Remove an entry from a section.
Definition: profile.hxx:143
sal_uInt32 oslProfileOption
Definition: profile.h:33
SAL_DLLPUBLIC sal_Bool osl_writeProfileBool(oslProfile Profile, const sal_Char *pszSection, const sal_Char *pszEntry, sal_Bool Value) SAL_COLD
Deprecated API.
char sal_Char
A legacy synonym for char.
Definition: types.h:130
std::list< rtl::OString > getSections()
Get all section entries.
Definition: profile.hxx:174
const int Profile_SYSTEM
Definition: profile.hxx:35
#define osl_Profile_DEFAULT
Definition: profile.h:35
bool writeString(const rtl::OString &rSection, const rtl::OString &rEntry, const rtl::OString &rString)
Definition: profile.hxx:107
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:88
SAL_DLLPUBLIC sal_Bool osl_writeProfileString(oslProfile Profile, const sal_Char *pszSection, const sal_Char *pszEntry, const sal_Char *pszString) SAL_COLD
Deprecated API.
Deprecated API.
Definition: profile.hxx:42
bool writeBool(const rtl::OString &rSection, const rtl::OString &rEntry, bool Value)
Definition: profile.hxx:113
bool readBool(const rtl::OString &rSection, const rtl::OString &rEntry, bool bDefault)
Definition: profile.hxx:83
const sal_Char * getStr() const
Returns a pointer to the characters of this string.
Definition: string.hxx:430
const int Profile_WRITELOCK
Definition: profile.hxx:37
SAL_DLLPUBLIC oslProfile osl_openProfile(rtl_uString *strProfileName, oslProfileOption Options) SAL_COLD
Deprecated API.
SAL_DLLPUBLIC sal_Bool osl_writeProfileIdent(oslProfile Profile, const sal_Char *pszSection, const sal_Char *pszEntry, sal_uInt32 FirstId, const sal_Char *Strings[], sal_uInt32 Value) SAL_COLD
Deprecated API.
void * oslProfile
Definition: profile.h:42
~Profile()
Close the opened profile an flush all data to the disk.
Definition: profile.hxx:59
SAL_DLLPUBLIC sal_Bool osl_closeProfile(oslProfile Profile) SAL_COLD
Deprecated API.
std::list< rtl::OString > getSectionEntries(const rtl::OString &rSection)
Get all entries belonging to the specified section.
Definition: profile.hxx:152
SAL_DLLPUBLIC sal_uInt32 osl_readProfileIdent(oslProfile Profile, const sal_Char *pszSection, const sal_Char *pszEntry, sal_uInt32 FirstId, const sal_Char *Strings[], sal_uInt32 Default) SAL_COLD
Deprecated API.
Profile(const rtl::OUString &strProfileName, oslProfileOption Options=Profile_DEFAULT)
Open or create a configuration profile.
Definition: profile.hxx:49
#define osl_Profile_WRITELOCK
Definition: profile.h:38
rtl::OString readString(const rtl::OString &rSection, const rtl::OString &rEntry, const rtl::OString &rDefault)
Definition: profile.hxx:70
oslProfileOption ProfileOption
Definition: profile.hxx:32
#define osl_Profile_READLOCK
Definition: profile.h:37
bool flush()
Definition: profile.hxx:65
SAL_DLLPUBLIC sal_Bool osl_readProfileBool(oslProfile Profile, const sal_Char *pszSection, const sal_Char *pszEntry, sal_Bool Default) SAL_COLD
Deprecated API.
SAL_DLLPUBLIC sal_Bool osl_removeProfileEntry(oslProfile Profile, const sal_Char *pszSection, const sal_Char *pszEntry) SAL_COLD
Deprecated API.
SAL_DLLPUBLIC sal_uInt32 osl_getProfileSectionEntries(oslProfile Profile, const sal_Char *pszSection, sal_Char *pszBuffer, sal_uInt32 MaxLen) SAL_COLD
Deprecated API.
This String class provides base functionality for C++ like Unicode character array handling...
Definition: ustring.hxx:106
SAL_DLLPUBLIC sal_uInt32 osl_getProfileSections(oslProfile Profile, sal_Char *pszBuffer, sal_uInt32 MaxLen) SAL_COLD
Deprecated API.
const int Profile_READLOCK
Definition: profile.hxx:36
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:118
const int Profile_DEFAULT
Definition: profile.hxx:34
SAL_DLLPUBLIC sal_Bool osl_readProfileString(oslProfile Profile, const sal_Char *pszSection, const sal_Char *pszEntry, sal_Char *pszString, sal_uInt32 MaxLen, const sal_Char *pszDefault) SAL_COLD
Deprecated API.
#define osl_Profile_SYSTEM
Definition: profile.h:36
This String class provide base functionality for C++ like 8-Bit character array handling.
Definition: string.hxx:90