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