update dev300-m58
[ooovba.git] / connectivity / source / drivers / mozab / bootstrap / MNSINIParser.cxx
blobdc422f3cfa0e69c46c41cad1846cb7046f430eb2
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: MNSINIParser.cxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_connectivity.hxx"
33 #include <MNSINIParser.hxx>
34 #include <rtl/byteseq.hxx>
36 ini_Section * IniParser::getSection(OUString const & secName)
38 if (mAllSection.find(secName) != mAllSection.end())
39 return &mAllSection[secName];
40 return NULL;
42 IniParser::IniParser(OUString const & rIniName) throw(com::sun::star::io::IOException )
44 OUString iniUrl;
45 if (osl_File_E_None != osl_getFileURLFromSystemPath(rIniName.pData, &iniUrl.pData))
46 return;
49 #if OSL_DEBUG_LEVEL > 0
50 OString sFile = OUStringToOString(iniUrl, RTL_TEXTENCODING_ASCII_US);
51 OSL_TRACE(__FILE__" -- parser() - %s\n", sFile.getStr());
52 #endif
53 oslFileHandle handle=NULL;
54 oslFileError fileError = osl_File_E_INVAL;
55 try{
56 if (iniUrl.getLength())
57 fileError = osl_openFile(iniUrl.pData, &handle, osl_File_OpenFlag_Read);
59 catch(::com::sun::star::io::IOException e)
61 #if OSL_DEBUG_LEVEL > 0
62 OString file_tmp = OUStringToOString(iniUrl, RTL_TEXTENCODING_ASCII_US);
63 OSL_TRACE( __FILE__" -- couldn't open file: %s", file_tmp.getStr() );
64 #endif
67 if (osl_File_E_None == fileError)
69 rtl::ByteSequence seq;
70 sal_uInt64 nSize = 0;
72 osl_getFileSize(handle, &nSize);
73 OUString sectionName = OUString::createFromAscii("no name section");
74 while (true)
76 sal_uInt64 nPos;
77 if (osl_File_E_None != osl_getFilePos(handle, &nPos) || nPos >= nSize)
78 break;
79 if (osl_File_E_None != osl_readLine(handle , (sal_Sequence **) &seq))
80 break;
81 OString line( (const sal_Char *) seq.getConstArray(), seq.getLength() );
82 sal_Int32 nIndex = line.indexOf('=');
83 if (nIndex >= 1)
85 ini_Section *aSection = &mAllSection[sectionName];
86 struct ini_NameValue nameValue;
87 nameValue.sName = OStringToOUString(
88 line.copy(0,nIndex).trim(), RTL_TEXTENCODING_ASCII_US );
89 nameValue.sValue = OStringToOUString(
90 line.copy(nIndex+1).trim(), RTL_TEXTENCODING_UTF8 );
92 aSection->lList.push_back(nameValue);
95 else
97 sal_Int32 nIndexStart = line.indexOf('[');
98 sal_Int32 nIndexEnd = line.indexOf(']');
99 if ( nIndexEnd > nIndexStart && nIndexStart >=0)
101 sectionName = OStringToOUString(
102 line.copy(nIndexStart + 1,nIndexEnd - nIndexStart -1).trim(), RTL_TEXTENCODING_ASCII_US );
103 if (!sectionName.getLength())
104 sectionName = OUString::createFromAscii("no name section");
106 ini_Section *aSection = &mAllSection[sectionName];
107 aSection->sName = sectionName;
111 osl_closeFile(handle);
113 #if OSL_DEBUG_LEVEL > 0
114 else
116 OString file_tmp = OUStringToOString(iniUrl, RTL_TEXTENCODING_ASCII_US);
117 OSL_TRACE( __FILE__" -- couldn't open file: %s", file_tmp.getStr() );
119 #endif
121 #if OSL_DEBUG_LEVEL > 0
122 void IniParser::Dump()
124 IniSectionMap::iterator iBegin = mAllSection.begin();
125 IniSectionMap::iterator iEnd = mAllSection.end();
126 for(;iBegin != iEnd;iBegin++)
128 ini_Section *aSection = &(*iBegin).second;
129 OString sec_name_tmp = OUStringToOString(aSection->sName, RTL_TEXTENCODING_ASCII_US);
130 for(NameValueList::iterator itor=aSection->lList.begin();
131 itor != aSection->lList.end();
132 itor++)
134 struct ini_NameValue * aValue = &(*itor);
135 OString name_tmp = OUStringToOString(aValue->sName, RTL_TEXTENCODING_ASCII_US);
136 OString value_tmp = OUStringToOString(aValue->sValue, RTL_TEXTENCODING_UTF8);
137 OSL_TRACE(
138 " section=%s name=%s value=%s\n",
139 sec_name_tmp.getStr(),
140 name_tmp.getStr(),
141 value_tmp.getStr() );
147 #endif