LibreOffice
LibreOffice 4.3 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 #ifndef INCLUDED_OSL_PROFILE_HXX
21 #define INCLUDED_OSL_PROFILE_HXX
22 
23 #include <osl/profile.h>
24 #include <rtl/ustring.hxx>
25 #include <string.h>
26 #include <list>
27 
28 namespace osl {
29 
31 
33  const int Profile_SYSTEM = osl_Profile_SYSTEM; /* use system depended functinality */
34  const int Profile_READLOCK = osl_Profile_READLOCK; /* lock file for reading */
35  const int Profile_WRITELOCK = osl_Profile_WRITELOCK; /* lock file for writing */
36 
40  class Profile {
41  oslProfile profile;
42 
43  public:
47  Profile(const rtl::OUString & strProfileName, oslProfileOption Options = Profile_DEFAULT )
48  {
49  profile = osl_openProfile(strProfileName.pData, Options);
50  if( ! profile )
51  throw std::exception();
52  }
53 
54 
58  {
59  osl_closeProfile(profile);
60  }
61 
62 
63  bool flush()
64  {
65  return osl_flushProfile(profile);
66  }
67 
68  rtl::OString readString( const rtl::OString& rSection, const rtl::OString& rEntry,
69  const rtl::OString& rDefault)
70  {
71  sal_Char aBuf[1024];
72  return osl_readProfileString( profile,
73  rSection.getStr(),
74  rEntry.getStr(),
75  aBuf,
76  sizeof( aBuf ),
77  rDefault.getStr() ) ? rtl::OString( aBuf ) : rtl::OString();
78 
79  }
80 
81  bool readBool( const rtl::OString& rSection, const rtl::OString& rEntry, bool bDefault )
82  {
83  return osl_readProfileBool( profile, rSection.getStr(), rEntry.getStr(), bDefault );
84  }
85 
86  sal_uInt32 readIdent(const rtl::OString& rSection, const rtl::OString& rEntry,
87  sal_uInt32 nFirstId, const std::list< rtl::OString >& rStrings,
88  sal_uInt32 nDefault)
89  {
90  size_t nItems = rStrings.size();
91  const sal_Char** pStrings = new const sal_Char*[ nItems+1 ];
92  std::list< rtl::OString >::const_iterator it = rStrings.begin();
93  nItems = 0;
94  while( it != rStrings.end() )
95  {
96  pStrings[ nItems++ ] = it->getStr();
97  ++it;
98  }
99  pStrings[ nItems ] = NULL;
100  sal_uInt32 nRet = osl_readProfileIdent(profile, rSection.getStr(), rEntry.getStr(), nFirstId, pStrings, nDefault);
101  delete pStrings;
102  return nRet;
103  }
104 
105  bool writeString(const rtl::OString& rSection, const rtl::OString& rEntry,
106  const rtl::OString& rString)
107  {
108  return osl_writeProfileString(profile, rSection.getStr(), rEntry.getStr(), rString.getStr());
109  }
110 
111  bool writeBool(const rtl::OString& rSection, const rtl::OString& rEntry, bool Value)
112  {
113  return osl_writeProfileBool(profile, rSection.getStr(), rEntry.getStr(), Value);
114  }
115 
116  bool writeIdent(const rtl::OString& rSection, const rtl::OString& rEntry,
117  sal_uInt32 nFirstId, const std::list< rtl::OString >& rStrings,
118  sal_uInt32 nValue)
119  {
120  size_t nItems = rStrings.size();
121  const sal_Char** pStrings = new const sal_Char*[ nItems+1 ];
122  std::list< rtl::OString >::const_iterator it = rStrings.begin();
123  nItems = 0;
124  while( it != rStrings.end() )
125  {
126  pStrings[ nItems++ ] = it->getStr();
127  ++it;
128  }
129  pStrings[ nItems ] = NULL;
130  bool bRet =
131  osl_writeProfileIdent(profile, rSection.getStr(), rEntry.getStr(), nFirstId, pStrings, nValue );
132  delete pStrings;
133  return bRet;
134  }
135 
141  bool removeEntry(const rtl::OString& rSection, const rtl::OString& rEntry)
142  {
143  return osl_removeProfileEntry(profile, rSection.getStr(), rEntry.getStr());
144  }
145 
150  std::list< rtl::OString > getSectionEntries(const rtl::OString& rSection )
151  {
152  std::list< rtl::OString > aEntries;
153 
154  // count buffer size necessary
155  size_t n = osl_getProfileSectionEntries( profile, rSection.getStr(), NULL, 0 );
156  if( n > 1 )
157  {
158  sal_Char* pBuf = new sal_Char[ n+1 ];
159  osl_getProfileSectionEntries( profile, rSection.getStr(), pBuf, n+1 );
160  size_t nLen;
161  for( n = 0; ( nLen = strlen( pBuf+n ) ); n += nLen+1 )
162  aEntries.push_back( rtl::OString( pBuf+n ) );
163  delete pBuf;
164  }
165 
166  return aEntries;
167  }
168 
172  std::list< rtl::OString > getSections()
173  {
174  std::list< rtl::OString > aSections;
175 
176  // count buffer size necessary
177  size_t n = osl_getProfileSections( profile, NULL, 0 );
178  if( n > 1 )
179  {
180  sal_Char* pBuf = new sal_Char[ n+1 ];
181  osl_getProfileSections( profile, pBuf, n+1 );
182  size_t nLen;
183  for( n = 0; ( nLen = strlen( pBuf+n ) ); n += nLen+1 )
184  aSections.push_back( rtl::OString( pBuf+n ) );
185  delete pBuf;
186  }
187 
188  return aSections;
189  }
190  };
191 }
192 
193 #endif // INCLUDED_OSL_PROFILE_HXX
194 
195 
196 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const int Profile_SYSTEM
Definition: profile.hxx:33
SAL_DLLPUBLIC sal_Bool osl_writeProfileBool(oslProfile Profile, const sal_Char *pszSection, const sal_Char *pszEntry, sal_Bool Value)
Deprecated API.
bool removeEntry(const rtl::OString &rSection, const rtl::OString &rEntry)
Remove an entry from a section.
Definition: profile.hxx:141
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:86
const sal_Char * getStr() const SAL_THROW(())
Returns a pointer to the characters of this string.
Definition: string.hxx:353
bool writeBool(const rtl::OString &rSection, const rtl::OString &rEntry, bool Value)
Definition: profile.hxx:111
bool readBool(const rtl::OString &rSection, const rtl::OString &rEntry, bool bDefault)
Definition: profile.hxx:81
bool writeString(const rtl::OString &rSection, const rtl::OString &rEntry, const rtl::OString &rString)
Definition: profile.hxx:105
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)
Deprecated API.
#define osl_Profile_SYSTEM
Definition: profile.h:36
const int Profile_READLOCK
Definition: profile.hxx:34
SAL_DLLPUBLIC sal_Bool osl_removeProfileEntry(oslProfile Profile, const sal_Char *pszSection, const sal_Char *pszEntry)
Deprecated API.
SAL_DLLPUBLIC sal_Bool osl_writeProfileString(oslProfile Profile, const sal_Char *pszSection, const sal_Char *pszEntry, const sal_Char *pszString)
Deprecated API.
const int Profile_DEFAULT
Definition: profile.hxx:32
std::list< rtl::OString > getSectionEntries(const rtl::OString &rSection)
Get all entries belonging to the specified section.
Definition: profile.hxx:150
SAL_DLLPUBLIC sal_Bool osl_flushProfile(oslProfile Profile)
Deprecated API.
Profile(const rtl::OUString &strProfileName, oslProfileOption Options=Profile_DEFAULT)
Open or create a configuration profile.
Definition: profile.hxx:47
rtl::OString readString(const rtl::OString &rSection, const rtl::OString &rEntry, const rtl::OString &rDefault)
Definition: profile.hxx:68
SAL_DLLPUBLIC sal_uInt32 osl_getProfileSections(oslProfile Profile, sal_Char *pszBuffer, sal_uInt32 MaxLen)
Deprecated API.
SAL_DLLPUBLIC sal_Bool osl_closeProfile(oslProfile Profile)
Deprecated API.
char sal_Char
A legacy synonym for char.
Definition: types.h:128
~Profile()
Close the opened profile an flush all data to the disk.
Definition: profile.hxx:57
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)
Deprecated API.
Definition: conditn.hxx:30
This String class provide base functionality for C++ like 8-Bit character array handling.
Definition: string.hxx:89
sal_uInt32 oslProfileOption
Definition: profile.h:33
SAL_DLLPUBLIC sal_uInt32 osl_getProfileSectionEntries(oslProfile Profile, const sal_Char *pszSection, sal_Char *pszBuffer, sal_uInt32 MaxLen)
Deprecated API.
bool flush()
Definition: profile.hxx:63
void * oslProfile
Definition: profile.h:42
SAL_DLLPUBLIC oslProfile osl_openProfile(rtl_uString *strProfileName, oslProfileOption Options)
Deprecated API.
oslProfileOption ProfileOption
Definition: profile.hxx:30
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)
Deprecated API.
SAL_DLLPUBLIC sal_Bool osl_readProfileBool(oslProfile Profile, const sal_Char *pszSection, const sal_Char *pszEntry, sal_Bool Default)
Deprecated API.
#define osl_Profile_WRITELOCK
Definition: profile.h:38
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:116
#define osl_Profile_READLOCK
Definition: profile.h:37
This String class provides base functionality for C++ like Unicode character array handling...
Definition: ustring.hxx:82
const int Profile_WRITELOCK
Definition: profile.hxx:35
#define osl_Profile_DEFAULT
Definition: profile.h:35
std::list< rtl::OString > getSections()
Get all section entries.
Definition: profile.hxx:172
Deprecated API.
Definition: profile.hxx:40