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
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_connectivity.hxx"
26 #include <rtl/ustring.hxx>
27 #include <stdio.h>
28 #include <com/sun/star/io/IOException.hpp>
29 #include <osl/process.h>
30 using namespace rtl;
31
32 #include <map>
33 #include <list>
34
35 struct ini_NameValue
36 {
37 rtl::OUString sName;
38 rtl::OUString sValue;
39
40 inline ini_NameValue() SAL_THROW( () )
41 {}
ini_NameValueini_NameValue42 inline ini_NameValue(
43 OUString const & name, OUString const & value ) SAL_THROW( () )
44 : sName( name ),
45 sValue( value )
46 {}
47 };
48
49 typedef std::list<
50 ini_NameValue
51 > NameValueList;
52
53 struct ini_Section
54 {
55 rtl::OUString sName;
56 NameValueList lList;
57 };
58 typedef std::map<rtl::OUString,
59 ini_Section
60 >IniSectionMap;
61
62
63 class IniParser
64 {
65 IniSectionMap mAllSection;
66 public:
getAllSection()67 IniSectionMap * getAllSection(){return &mAllSection;};
getSection(OUString const & secName)68 ini_Section * getSection(OUString const & secName)
69 {
70 if (mAllSection.find(secName) != mAllSection.end())
71 return &mAllSection[secName];
72 return NULL;
73 }
IniParser(OUString const & rIniName)74 IniParser(OUString const & rIniName) throw(com::sun::star::io::IOException )
75 {
76 OUString curDirPth;
77 OUString iniUrl;
78 osl_getProcessWorkingDir( &curDirPth.pData );
79 if (osl_getAbsoluteFileURL( curDirPth.pData, rIniName.pData, &iniUrl.pData ))
80 throw ::com::sun::star::io::IOException();
81
82
83 #if OSL_DEBUG_LEVEL > 1
84 OString sFile = OUStringToOString(iniUrl, RTL_TEXTENCODING_ASCII_US);
85 OSL_TRACE(__FILE__" -- parser() - %s\n", sFile.getStr());
86 #endif
87 oslFileHandle handle=NULL;
88 if (iniUrl.getLength() &&
89 osl_File_E_None == osl_openFile(iniUrl.pData, &handle, osl_File_OpenFlag_Read))
90 {
91 rtl::ByteSequence seq;
92 sal_uInt64 nSize = 0;
93
94 osl_getFileSize(handle, &nSize);
95 OUString sectionName = OUString::createFromAscii("no name section");
96 while (true)
97 {
98 sal_uInt64 nPos;
99 if (osl_File_E_None != osl_getFilePos(handle, &nPos) || nPos >= nSize)
100 break;
101 if (osl_File_E_None != osl_readLine(handle , (sal_Sequence **) &seq))
102 break;
103 OString line( (const sal_Char *) seq.getConstArray(), seq.getLength() );
104 sal_Int32 nIndex = line.indexOf('=');
105 if (nIndex >= 1)
106 {
107 ini_Section *aSection = &mAllSection[sectionName];
108 struct ini_NameValue nameValue;
109 nameValue.sName = OStringToOUString(
110 line.copy(0,nIndex).trim(), RTL_TEXTENCODING_ASCII_US );
111 nameValue.sValue = OStringToOUString(
112 line.copy(nIndex+1).trim(), RTL_TEXTENCODING_UTF8 );
113
114 aSection->lList.push_back(nameValue);
115
116 }
117 else
118 {
119 sal_Int32 nIndexStart = line.indexOf('[');
120 sal_Int32 nIndexEnd = line.indexOf(']');
121 if ( nIndexEnd > nIndexStart && nIndexStart >=0)
122 {
123 sectionName = OStringToOUString(
124 line.copy(nIndexStart + 1,nIndexEnd - nIndexStart -1).trim(), RTL_TEXTENCODING_ASCII_US );
125 if (!sectionName.getLength())
126 sectionName = OUString::createFromAscii("no name section");
127
128 ini_Section *aSection = &mAllSection[sectionName];
129 aSection->sName = sectionName;
130 }
131 }
132 }
133 osl_closeFile(handle);
134 }
135 #if OSL_DEBUG_LEVEL > 1
136 else
137 {
138 OString file_tmp = OUStringToOString(iniUrl, RTL_TEXTENCODING_ASCII_US);
139 OSL_TRACE( __FILE__" -- couldn't open file: %s", file_tmp.getStr() );
140 throw ::com::sun::star::io::IOException();
141 }
142 #endif
143 }
144 #if OSL_DEBUG_LEVEL > 1
Dump()145 void Dump()
146 {
147 IniSectionMap::iterator iBegin = mAllSection.begin();
148 IniSectionMap::iterator iEnd = mAllSection.end();
149 for(;iBegin != iEnd;iBegin++)
150 {
151 ini_Section *aSection = &(*iBegin).second;
152 OString sec_name_tmp = OUStringToOString(aSection->sName, RTL_TEXTENCODING_ASCII_US);
153 for(NameValueList::iterator itor=aSection->lList.begin();
154 itor != aSection->lList.end();
155 itor++)
156 {
157 struct ini_NameValue * aValue = &(*itor);
158 OString name_tmp = OUStringToOString(aValue->sName, RTL_TEXTENCODING_ASCII_US);
159 OString value_tmp = OUStringToOString(aValue->sValue, RTL_TEXTENCODING_UTF8);
160 OSL_TRACE(
161 " section=%s name=%s value=%s\n",
162 sec_name_tmp.getStr(),
163 name_tmp.getStr(),
164 value_tmp.getStr() );
165
166 }
167 }
168
169 }
170 #endif
171
172 };
173
174 #if (defined UNX) || (defined OS2)
main(int argc,char * argv[])175 int main( int argc, char * argv[] )
176 #else
177 int _cdecl main( int argc, char * argv[] )
178 #endif
179
180 {
181
182 IniParser parser(OUString::createFromAscii("test.ini"));
183 parser.Dump();
184 return 0;
185 }
186