xref: /trunk/main/connectivity/workben/iniParser/main.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
1*9b5730f6SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*9b5730f6SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*9b5730f6SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*9b5730f6SAndrew Rist  * distributed with this work for additional information
6*9b5730f6SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*9b5730f6SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*9b5730f6SAndrew Rist  * "License"); you may not use this file except in compliance
9*9b5730f6SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*9b5730f6SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*9b5730f6SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*9b5730f6SAndrew Rist  * software distributed under the License is distributed on an
15*9b5730f6SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9b5730f6SAndrew Rist  * KIND, either express or implied.  See the License for the
17*9b5730f6SAndrew Rist  * specific language governing permissions and limitations
18*9b5730f6SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*9b5730f6SAndrew Rist  *************************************************************/
21*9b5730f6SAndrew Rist 
22*9b5730f6SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_connectivity.hxx"
26cdf0e10cSrcweir #include <rtl/ustring.hxx>
27cdf0e10cSrcweir #include <stdio.h>
28cdf0e10cSrcweir #include <com/sun/star/io/IOException.hpp>
29cdf0e10cSrcweir #include <osl/process.h>
30cdf0e10cSrcweir using namespace rtl;
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <map>
33cdf0e10cSrcweir #include <list>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir struct ini_NameValue
36cdf0e10cSrcweir {
37cdf0e10cSrcweir     rtl::OUString sName;
38cdf0e10cSrcweir     rtl::OUString sValue;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir     inline ini_NameValue() SAL_THROW( () )
41cdf0e10cSrcweir         {}
ini_NameValueini_NameValue42cdf0e10cSrcweir     inline ini_NameValue(
43cdf0e10cSrcweir         OUString const & name, OUString const & value ) SAL_THROW( () )
44cdf0e10cSrcweir         : sName( name ),
45cdf0e10cSrcweir           sValue( value )
46cdf0e10cSrcweir         {}
47cdf0e10cSrcweir };
48cdf0e10cSrcweir 
49cdf0e10cSrcweir typedef std::list<
50cdf0e10cSrcweir     ini_NameValue
51cdf0e10cSrcweir > NameValueList;
52cdf0e10cSrcweir 
53cdf0e10cSrcweir struct ini_Section
54cdf0e10cSrcweir {
55cdf0e10cSrcweir     rtl::OUString sName;
56cdf0e10cSrcweir     NameValueList lList;
57cdf0e10cSrcweir };
58cdf0e10cSrcweir typedef std::map<rtl::OUString,
59cdf0e10cSrcweir                 ini_Section
60cdf0e10cSrcweir                 >IniSectionMap;
61cdf0e10cSrcweir 
62cdf0e10cSrcweir 
63cdf0e10cSrcweir class IniParser
64cdf0e10cSrcweir {
65cdf0e10cSrcweir     IniSectionMap mAllSection;
66cdf0e10cSrcweir public:
getAllSection()67cdf0e10cSrcweir     IniSectionMap * getAllSection(){return &mAllSection;};
getSection(OUString const & secName)68cdf0e10cSrcweir     ini_Section *  getSection(OUString const & secName)
69cdf0e10cSrcweir     {
70cdf0e10cSrcweir         if (mAllSection.find(secName) != mAllSection.end())
71cdf0e10cSrcweir             return &mAllSection[secName];
72cdf0e10cSrcweir         return NULL;
73cdf0e10cSrcweir     }
IniParser(OUString const & rIniName)74cdf0e10cSrcweir     IniParser(OUString const & rIniName) throw(com::sun::star::io::IOException )
75cdf0e10cSrcweir     {
76cdf0e10cSrcweir         OUString curDirPth;
77cdf0e10cSrcweir         OUString iniUrl;
78cdf0e10cSrcweir         osl_getProcessWorkingDir( &curDirPth.pData );
79cdf0e10cSrcweir         if (osl_getAbsoluteFileURL( curDirPth.pData,    rIniName.pData, &iniUrl.pData ))
80cdf0e10cSrcweir             throw ::com::sun::star::io::IOException();
81cdf0e10cSrcweir 
82cdf0e10cSrcweir 
83cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
84cdf0e10cSrcweir         OString sFile = OUStringToOString(iniUrl, RTL_TEXTENCODING_ASCII_US);
85cdf0e10cSrcweir         OSL_TRACE(__FILE__" -- parser() - %s\n", sFile.getStr());
86cdf0e10cSrcweir #endif
87cdf0e10cSrcweir         oslFileHandle handle=NULL;
88cdf0e10cSrcweir         if (iniUrl.getLength() &&
89cdf0e10cSrcweir             osl_File_E_None == osl_openFile(iniUrl.pData, &handle, osl_File_OpenFlag_Read))
90cdf0e10cSrcweir         {
91cdf0e10cSrcweir             rtl::ByteSequence seq;
92cdf0e10cSrcweir             sal_uInt64 nSize = 0;
93cdf0e10cSrcweir 
94cdf0e10cSrcweir             osl_getFileSize(handle, &nSize);
95cdf0e10cSrcweir             OUString sectionName = OUString::createFromAscii("no name section");
96cdf0e10cSrcweir             while (true)
97cdf0e10cSrcweir             {
98cdf0e10cSrcweir                 sal_uInt64 nPos;
99cdf0e10cSrcweir                 if (osl_File_E_None != osl_getFilePos(handle, &nPos) || nPos >= nSize)
100cdf0e10cSrcweir                     break;
101cdf0e10cSrcweir                 if (osl_File_E_None != osl_readLine(handle , (sal_Sequence **) &seq))
102cdf0e10cSrcweir                     break;
103cdf0e10cSrcweir                 OString line( (const sal_Char *) seq.getConstArray(), seq.getLength() );
104cdf0e10cSrcweir                 sal_Int32 nIndex = line.indexOf('=');
105cdf0e10cSrcweir                 if (nIndex >= 1)
106cdf0e10cSrcweir                 {
107cdf0e10cSrcweir                     ini_Section *aSection = &mAllSection[sectionName];
108cdf0e10cSrcweir                     struct ini_NameValue nameValue;
109cdf0e10cSrcweir                     nameValue.sName = OStringToOUString(
110cdf0e10cSrcweir                         line.copy(0,nIndex).trim(), RTL_TEXTENCODING_ASCII_US );
111cdf0e10cSrcweir                     nameValue.sValue = OStringToOUString(
112cdf0e10cSrcweir                         line.copy(nIndex+1).trim(), RTL_TEXTENCODING_UTF8 );
113cdf0e10cSrcweir 
114cdf0e10cSrcweir                     aSection->lList.push_back(nameValue);
115cdf0e10cSrcweir 
116cdf0e10cSrcweir                 }
117cdf0e10cSrcweir                 else
118cdf0e10cSrcweir                 {
119cdf0e10cSrcweir                     sal_Int32 nIndexStart = line.indexOf('[');
120cdf0e10cSrcweir                     sal_Int32 nIndexEnd = line.indexOf(']');
121cdf0e10cSrcweir                     if ( nIndexEnd > nIndexStart && nIndexStart >=0)
122cdf0e10cSrcweir                     {
123cdf0e10cSrcweir                         sectionName =  OStringToOUString(
124cdf0e10cSrcweir                             line.copy(nIndexStart + 1,nIndexEnd - nIndexStart -1).trim(), RTL_TEXTENCODING_ASCII_US );
125cdf0e10cSrcweir                         if (!sectionName.getLength())
126cdf0e10cSrcweir                             sectionName = OUString::createFromAscii("no name section");
127cdf0e10cSrcweir 
128cdf0e10cSrcweir                         ini_Section *aSection = &mAllSection[sectionName];
129cdf0e10cSrcweir                         aSection->sName = sectionName;
130cdf0e10cSrcweir                     }
131cdf0e10cSrcweir                 }
132cdf0e10cSrcweir             }
133cdf0e10cSrcweir             osl_closeFile(handle);
134cdf0e10cSrcweir         }
135cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
136cdf0e10cSrcweir         else
137cdf0e10cSrcweir         {
138cdf0e10cSrcweir             OString file_tmp = OUStringToOString(iniUrl, RTL_TEXTENCODING_ASCII_US);
139cdf0e10cSrcweir             OSL_TRACE( __FILE__" -- couldn't open file: %s", file_tmp.getStr() );
140cdf0e10cSrcweir             throw ::com::sun::star::io::IOException();
141cdf0e10cSrcweir         }
142cdf0e10cSrcweir #endif
143cdf0e10cSrcweir     }
144cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
Dump()145cdf0e10cSrcweir     void Dump()
146cdf0e10cSrcweir     {
147cdf0e10cSrcweir         IniSectionMap::iterator iBegin = mAllSection.begin();
148cdf0e10cSrcweir         IniSectionMap::iterator iEnd = mAllSection.end();
149cdf0e10cSrcweir         for(;iBegin != iEnd;iBegin++)
150cdf0e10cSrcweir         {
151cdf0e10cSrcweir             ini_Section *aSection = &(*iBegin).second;
152cdf0e10cSrcweir             OString sec_name_tmp = OUStringToOString(aSection->sName, RTL_TEXTENCODING_ASCII_US);
153cdf0e10cSrcweir             for(NameValueList::iterator itor=aSection->lList.begin();
154cdf0e10cSrcweir                 itor != aSection->lList.end();
155cdf0e10cSrcweir                 itor++)
156cdf0e10cSrcweir             {
157cdf0e10cSrcweir                     struct ini_NameValue * aValue = &(*itor);
158cdf0e10cSrcweir                     OString name_tmp = OUStringToOString(aValue->sName, RTL_TEXTENCODING_ASCII_US);
159cdf0e10cSrcweir                     OString value_tmp = OUStringToOString(aValue->sValue, RTL_TEXTENCODING_UTF8);
160cdf0e10cSrcweir                     OSL_TRACE(
161cdf0e10cSrcweir                         " section=%s name=%s value=%s\n",
162cdf0e10cSrcweir                                         sec_name_tmp.getStr(),
163cdf0e10cSrcweir                                         name_tmp.getStr(),
164cdf0e10cSrcweir                                         value_tmp.getStr() );
165cdf0e10cSrcweir 
166cdf0e10cSrcweir             }
167cdf0e10cSrcweir         }
168cdf0e10cSrcweir 
169cdf0e10cSrcweir     }
170cdf0e10cSrcweir #endif
171cdf0e10cSrcweir 
172cdf0e10cSrcweir };
173cdf0e10cSrcweir 
174cdf0e10cSrcweir #if (defined UNX) || (defined OS2)
main(int argc,char * argv[])175cdf0e10cSrcweir int main( int argc, char * argv[] )
176cdf0e10cSrcweir #else
177cdf0e10cSrcweir int _cdecl main( int argc, char * argv[] )
178cdf0e10cSrcweir #endif
179cdf0e10cSrcweir 
180cdf0e10cSrcweir {
181cdf0e10cSrcweir 
182cdf0e10cSrcweir     IniParser parser(OUString::createFromAscii("test.ini"));
183cdf0e10cSrcweir     parser.Dump();
184cdf0e10cSrcweir     return 0;
185cdf0e10cSrcweir }
186