UDK 3.2.7 C/C++ API Reference
osl/profile.hxx
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /*************************************************************************
00003  *
00004  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
00005  *
00006  * Copyright 2000, 2010 Oracle and/or its affiliates.
00007  *
00008  * OpenOffice.org - a multi-platform office productivity suite
00009  *
00010  * This file is part of OpenOffice.org.
00011  *
00012  * OpenOffice.org is free software: you can redistribute it and/or modify
00013  * it under the terms of the GNU Lesser General Public License version 3
00014  * only, as published by the Free Software Foundation.
00015  *
00016  * OpenOffice.org is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU Lesser General Public License version 3 for more details
00020  * (a copy is included in the LICENSE file that accompanied this code).
00021  *
00022  * You should have received a copy of the GNU Lesser General Public License
00023  * version 3 along with OpenOffice.org.  If not, see
00024  * <http://www.openoffice.org/license.html>
00025  * for a copy of the LGPLv3 License.
00026  *
00027  ************************************************************************/
00028 
00029 #ifndef _OSL_PROFILE_HXX_
00030 #define _OSL_PROFILE_HXX_
00031 
00032 #include "profile.h"
00033 #include <rtl/ustring.hxx>
00034 #include <string.h>
00035 #include <list>
00036 
00037 namespace osl {
00038 
00039     typedef oslProfileOption ProfileOption;
00040 
00041     const int Profile_DEFAULT   = osl_Profile_DEFAULT;
00042     const int Profile_SYSTEM    = osl_Profile_SYSTEM;    /* use system depended functinality */
00043     const int Profile_READLOCK  = osl_Profile_READLOCK;  /* lock file for reading            */
00044     const int Profile_WRITELOCK = osl_Profile_WRITELOCK; /* lock file for writing            */
00045 
00049     class Profile {
00050         oslProfile profile;
00051 
00052     public:
00056         Profile(const rtl::OUString strProfileName, oslProfileOption Options = Profile_DEFAULT )
00057         {
00058             profile = osl_openProfile(strProfileName.pData, Options);
00059             if( ! profile )
00060                 throw std::exception();
00061         }
00062 
00063 
00066         ~Profile()
00067         {
00068             osl_closeProfile(profile);
00069         }
00070 
00071 
00072         sal_Bool flush()
00073         {
00074             return osl_flushProfile(profile);
00075         }
00076 
00077         rtl::OString readString( const rtl::OString& rSection, const rtl::OString& rEntry,
00078                                  const rtl::OString& rDefault)
00079         {
00080             sal_Char aBuf[1024];
00081             return osl_readProfileString( profile,
00082                                           rSection.getStr(),
00083                                           rEntry.getStr(),
00084                                           aBuf,
00085                                           sizeof( aBuf ),
00086                                           rDefault.getStr() ) ? rtl::OString( aBuf ) : rtl::OString();
00087 
00088         }
00089 
00090         sal_Bool readBool( const rtl::OString& rSection, const rtl::OString& rEntry, sal_Bool bDefault )
00091         {
00092             return osl_readProfileBool( profile, rSection.getStr(), rEntry.getStr(), bDefault );
00093         }
00094 
00095         sal_uInt32 readIdent(const rtl::OString& rSection, const rtl::OString& rEntry,
00096                              sal_uInt32 nFirstId, const std::list< rtl::OString >& rStrings,
00097                              sal_uInt32 nDefault)
00098         {
00099             int nItems = rStrings.size();
00100             const sal_Char** pStrings = new const sal_Char*[ nItems+1 ];
00101             std::list< rtl::OString >::const_iterator it = rStrings.begin();
00102             nItems = 0;
00103             while( it != rStrings.end() )
00104             {
00105                 pStrings[ nItems++ ] = it->getStr();
00106                 ++it;
00107             }
00108             pStrings[ nItems ] = NULL;
00109             sal_uInt32 nRet = osl_readProfileIdent(profile, rSection.getStr(), rEntry.getStr(), nFirstId, pStrings, nDefault);
00110             delete pStrings;
00111             return nRet;
00112         }
00113 
00114         sal_Bool writeString(const rtl::OString& rSection, const rtl::OString& rEntry,
00115                              const rtl::OString& rString)
00116         {
00117             return osl_writeProfileString(profile, rSection.getStr(), rEntry.getStr(), rString.getStr());
00118         }
00119 
00120         sal_Bool writeBool(const rtl::OString& rSection, const rtl::OString& rEntry, sal_Bool Value)
00121         {
00122             return osl_writeProfileBool(profile, rSection.getStr(), rEntry.getStr(), Value);
00123         }
00124 
00125         sal_Bool writeIdent(const rtl::OString& rSection, const rtl::OString& rEntry,
00126                             sal_uInt32 nFirstId, const std::list< rtl::OString >& rStrings,
00127                             sal_uInt32 nValue)
00128         {
00129             int nItems = rStrings.size();
00130             const sal_Char** pStrings = new const sal_Char*[ nItems+1 ];
00131             std::list< rtl::OString >::const_iterator it = rStrings.begin();
00132             nItems = 0;
00133             while( it != rStrings.end() )
00134             {
00135                 pStrings[ nItems++ ] = it->getStr();
00136                 ++it;
00137             }
00138             pStrings[ nItems ] = NULL;
00139             sal_Bool bRet =
00140                 osl_writeProfileIdent(profile, rSection.getStr(), rEntry.getStr(), nFirstId, pStrings, nValue );
00141             delete pStrings;
00142             return bRet;
00143         }
00144 
00150         sal_Bool removeEntry(const rtl::OString& rSection, const rtl::OString& rEntry)
00151         {
00152             return osl_removeProfileEntry(profile, rSection.getStr(), rEntry.getStr());
00153         }
00154 
00159         std::list< rtl::OString > getSectionEntries(const rtl::OString& rSection )
00160         {
00161             std::list< rtl::OString > aEntries;
00162 
00163             // count buffer size necessary
00164             int n = osl_getProfileSectionEntries( profile, rSection.getStr(), NULL, 0 );
00165             if( n > 1 )
00166             {
00167                 sal_Char* pBuf = new sal_Char[ n+1 ];
00168                 osl_getProfileSectionEntries( profile, rSection.getStr(), pBuf, n+1 );
00169                 int nLen;
00170                 for( n = 0; ( nLen = strlen( pBuf+n ) ); n += nLen+1 )
00171                     aEntries.push_back( rtl::OString( pBuf+n ) );
00172                 delete pBuf;
00173             }
00174 
00175             return aEntries;
00176         }
00177 
00181         std::list< rtl::OString > getSections()
00182         {
00183             std::list< rtl::OString > aSections;
00184 
00185             // count buffer size necessary
00186             int n = osl_getProfileSections( profile, NULL, 0 );
00187             if( n > 1 )
00188             {
00189                 sal_Char* pBuf = new sal_Char[ n+1 ];
00190                 osl_getProfileSections( profile, pBuf, n+1 );
00191                 int nLen;
00192                 for( n = 0; ( nLen = strlen( pBuf+n ) ); n += nLen+1 )
00193                     aSections.push_back( rtl::OString( pBuf+n ) );
00194                 delete pBuf;
00195             }
00196 
00197             return aSections;
00198         }
00199     };
00200 }
00201 
00202 #endif  /* _OSL_PROFILE_HXX_ */
00203 
00204 
00205 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines