xref: /AOO41X/main/extensions/source/mozbootstrap/MNSINIParser.cxx (revision 9874e4f6d986ac5869ea45d8ed5f7bf59eeaff2e)
1*9874e4f6SJuergen Schmidt /**************************************************************
2*9874e4f6SJuergen Schmidt  *
3*9874e4f6SJuergen Schmidt  * Licensed to the Apache Software Foundation (ASF) under one
4*9874e4f6SJuergen Schmidt  * or more contributor license agreements.  See the NOTICE file
5*9874e4f6SJuergen Schmidt  * distributed with this work for additional information
6*9874e4f6SJuergen Schmidt  * regarding copyright ownership.  The ASF licenses this file
7*9874e4f6SJuergen Schmidt  * to you under the Apache License, Version 2.0 (the
8*9874e4f6SJuergen Schmidt  * "License"); you may not use this file except in compliance
9*9874e4f6SJuergen Schmidt  * with the License.  You may obtain a copy of the License at
10*9874e4f6SJuergen Schmidt  *
11*9874e4f6SJuergen Schmidt  *   http://www.apache.org/licenses/LICENSE-2.0
12*9874e4f6SJuergen Schmidt  *
13*9874e4f6SJuergen Schmidt  * Unless required by applicable law or agreed to in writing,
14*9874e4f6SJuergen Schmidt  * software distributed under the License is distributed on an
15*9874e4f6SJuergen Schmidt  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9874e4f6SJuergen Schmidt  * KIND, either express or implied.  See the License for the
17*9874e4f6SJuergen Schmidt  * specific language governing permissions and limitations
18*9874e4f6SJuergen Schmidt  * under the License.
19*9874e4f6SJuergen Schmidt  *
20*9874e4f6SJuergen Schmidt  *************************************************************/
21*9874e4f6SJuergen Schmidt 
22*9874e4f6SJuergen Schmidt // MARKER(update_precomp.py): autogen include statement, do not remove
23*9874e4f6SJuergen Schmidt #include "precompiled_extensions.hxx"
24*9874e4f6SJuergen Schmidt #include <MNSINIParser.hxx>
25*9874e4f6SJuergen Schmidt #include <rtl/byteseq.hxx>
26*9874e4f6SJuergen Schmidt 
IniParser(OUString const & rIniName)27*9874e4f6SJuergen Schmidt IniParser::IniParser(OUString const & rIniName) throw(com::sun::star::io::IOException )
28*9874e4f6SJuergen Schmidt {
29*9874e4f6SJuergen Schmidt     OUString iniUrl;
30*9874e4f6SJuergen Schmidt     if (osl_File_E_None != osl_getFileURLFromSystemPath(rIniName.pData, &iniUrl.pData))
31*9874e4f6SJuergen Schmidt         return;
32*9874e4f6SJuergen Schmidt 
33*9874e4f6SJuergen Schmidt 
34*9874e4f6SJuergen Schmidt #if OSL_DEBUG_LEVEL > 0
35*9874e4f6SJuergen Schmidt     OString sFile = OUStringToOString(iniUrl, RTL_TEXTENCODING_ASCII_US);
36*9874e4f6SJuergen Schmidt     OSL_TRACE(__FILE__" -- parser() - %s\n", sFile.getStr());
37*9874e4f6SJuergen Schmidt #endif
38*9874e4f6SJuergen Schmidt     oslFileHandle handle=NULL;
39*9874e4f6SJuergen Schmidt     oslFileError fileError = osl_File_E_INVAL;
40*9874e4f6SJuergen Schmidt     try{
41*9874e4f6SJuergen Schmidt         if (iniUrl.getLength())
42*9874e4f6SJuergen Schmidt             fileError = osl_openFile(iniUrl.pData, &handle, osl_File_OpenFlag_Read);
43*9874e4f6SJuergen Schmidt     }
44*9874e4f6SJuergen Schmidt     catch(::com::sun::star::io::IOException e)
45*9874e4f6SJuergen Schmidt     {
46*9874e4f6SJuergen Schmidt #if OSL_DEBUG_LEVEL > 0
47*9874e4f6SJuergen Schmidt         OString file_tmp = OUStringToOString(iniUrl, RTL_TEXTENCODING_ASCII_US);
48*9874e4f6SJuergen Schmidt         OSL_TRACE( __FILE__" -- couldn't open file: %s", file_tmp.getStr() );
49*9874e4f6SJuergen Schmidt #endif
50*9874e4f6SJuergen Schmidt     }
51*9874e4f6SJuergen Schmidt 
52*9874e4f6SJuergen Schmidt     if (osl_File_E_None == fileError)
53*9874e4f6SJuergen Schmidt     {
54*9874e4f6SJuergen Schmidt         rtl::ByteSequence seq;
55*9874e4f6SJuergen Schmidt         sal_uInt64 nSize = 0;
56*9874e4f6SJuergen Schmidt 
57*9874e4f6SJuergen Schmidt         osl_getFileSize(handle, &nSize);
58*9874e4f6SJuergen Schmidt         OUString sectionName = OUString::createFromAscii("no name section");
59*9874e4f6SJuergen Schmidt         while (true)
60*9874e4f6SJuergen Schmidt         {
61*9874e4f6SJuergen Schmidt             sal_uInt64 nPos;
62*9874e4f6SJuergen Schmidt             if (osl_File_E_None != osl_getFilePos(handle, &nPos) || nPos >= nSize)
63*9874e4f6SJuergen Schmidt                 break;
64*9874e4f6SJuergen Schmidt             if (osl_File_E_None != osl_readLine(handle , (sal_Sequence **) &seq))
65*9874e4f6SJuergen Schmidt                 break;
66*9874e4f6SJuergen Schmidt             OString line( (const sal_Char *) seq.getConstArray(), seq.getLength() );
67*9874e4f6SJuergen Schmidt             sal_Int32 nIndex = line.indexOf('=');
68*9874e4f6SJuergen Schmidt             if (nIndex >= 1)
69*9874e4f6SJuergen Schmidt             {
70*9874e4f6SJuergen Schmidt                 ini_Section *aSection = &mAllSection[sectionName];
71*9874e4f6SJuergen Schmidt                 struct ini_NameValue nameValue;
72*9874e4f6SJuergen Schmidt                 nameValue.sName = OStringToOUString(
73*9874e4f6SJuergen Schmidt                     line.copy(0,nIndex).trim(), RTL_TEXTENCODING_ASCII_US );
74*9874e4f6SJuergen Schmidt                 nameValue.sValue = OStringToOUString(
75*9874e4f6SJuergen Schmidt                     line.copy(nIndex+1).trim(), RTL_TEXTENCODING_UTF8 );
76*9874e4f6SJuergen Schmidt 
77*9874e4f6SJuergen Schmidt                 aSection->lList.push_back(nameValue);
78*9874e4f6SJuergen Schmidt 
79*9874e4f6SJuergen Schmidt             }
80*9874e4f6SJuergen Schmidt             else
81*9874e4f6SJuergen Schmidt             {
82*9874e4f6SJuergen Schmidt                 sal_Int32 nIndexStart = line.indexOf('[');
83*9874e4f6SJuergen Schmidt                 sal_Int32 nIndexEnd = line.indexOf(']');
84*9874e4f6SJuergen Schmidt                 if ( nIndexEnd > nIndexStart && nIndexStart >=0)
85*9874e4f6SJuergen Schmidt                 {
86*9874e4f6SJuergen Schmidt                     sectionName =  OStringToOUString(
87*9874e4f6SJuergen Schmidt                         line.copy(nIndexStart + 1,nIndexEnd - nIndexStart -1).trim(), RTL_TEXTENCODING_ASCII_US );
88*9874e4f6SJuergen Schmidt                     if (!sectionName.getLength())
89*9874e4f6SJuergen Schmidt                         sectionName = OUString::createFromAscii("no name section");
90*9874e4f6SJuergen Schmidt 
91*9874e4f6SJuergen Schmidt                     ini_Section *aSection = &mAllSection[sectionName];
92*9874e4f6SJuergen Schmidt                     aSection->sName = sectionName;
93*9874e4f6SJuergen Schmidt                 }
94*9874e4f6SJuergen Schmidt             }
95*9874e4f6SJuergen Schmidt         }
96*9874e4f6SJuergen Schmidt         osl_closeFile(handle);
97*9874e4f6SJuergen Schmidt     }
98*9874e4f6SJuergen Schmidt #if OSL_DEBUG_LEVEL > 0
99*9874e4f6SJuergen Schmidt     else
100*9874e4f6SJuergen Schmidt     {
101*9874e4f6SJuergen Schmidt         OString file_tmp = OUStringToOString(iniUrl, RTL_TEXTENCODING_ASCII_US);
102*9874e4f6SJuergen Schmidt         OSL_TRACE( __FILE__" -- couldn't open file: %s", file_tmp.getStr() );
103*9874e4f6SJuergen Schmidt     }
104*9874e4f6SJuergen Schmidt #endif
105*9874e4f6SJuergen Schmidt }
106*9874e4f6SJuergen Schmidt #if OSL_DEBUG_LEVEL > 0
Dump()107*9874e4f6SJuergen Schmidt void IniParser::Dump()
108*9874e4f6SJuergen Schmidt {
109*9874e4f6SJuergen Schmidt     IniSectionMap::iterator iBegin = mAllSection.begin();
110*9874e4f6SJuergen Schmidt     IniSectionMap::iterator iEnd = mAllSection.end();
111*9874e4f6SJuergen Schmidt     for(;iBegin != iEnd;iBegin++)
112*9874e4f6SJuergen Schmidt     {
113*9874e4f6SJuergen Schmidt         ini_Section *aSection = &(*iBegin).second;
114*9874e4f6SJuergen Schmidt         OString sec_name_tmp = OUStringToOString(aSection->sName, RTL_TEXTENCODING_ASCII_US);
115*9874e4f6SJuergen Schmidt         for(NameValueList::iterator itor=aSection->lList.begin();
116*9874e4f6SJuergen Schmidt             itor != aSection->lList.end();
117*9874e4f6SJuergen Schmidt             itor++)
118*9874e4f6SJuergen Schmidt         {
119*9874e4f6SJuergen Schmidt                 struct ini_NameValue * aValue = &(*itor);
120*9874e4f6SJuergen Schmidt                 OString name_tmp = OUStringToOString(aValue->sName, RTL_TEXTENCODING_ASCII_US);
121*9874e4f6SJuergen Schmidt                 OString value_tmp = OUStringToOString(aValue->sValue, RTL_TEXTENCODING_UTF8);
122*9874e4f6SJuergen Schmidt                 OSL_TRACE(
123*9874e4f6SJuergen Schmidt                     " section=%s name=%s value=%s\n",
124*9874e4f6SJuergen Schmidt                                     sec_name_tmp.getStr(),
125*9874e4f6SJuergen Schmidt                                     name_tmp.getStr(),
126*9874e4f6SJuergen Schmidt                                     value_tmp.getStr() );
127*9874e4f6SJuergen Schmidt 
128*9874e4f6SJuergen Schmidt         }
129*9874e4f6SJuergen Schmidt     }
130*9874e4f6SJuergen Schmidt 
131*9874e4f6SJuergen Schmidt }
132*9874e4f6SJuergen Schmidt #endif
133*9874e4f6SJuergen Schmidt 
134