update dev300-m58
[ooovba.git] / connectivity / workben / iniParser / main.cxx
bloba65788eba970d80a1df13f39539ff7cd52289441
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: main.cxx,v $
10 * $Revision: 1.6 $
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 <rtl/ustring.hxx>
34 #include <stdio.h>
35 #include <com/sun/star/io/IOException.hpp>
36 #include <osl/process.h>
37 using namespace rtl;
39 #include <map>
40 #include <list>
42 struct ini_NameValue
44 rtl::OUString sName;
45 rtl::OUString sValue;
47 inline ini_NameValue() SAL_THROW( () )
49 inline ini_NameValue(
50 OUString const & name, OUString const & value ) SAL_THROW( () )
51 : sName( name ),
52 sValue( value )
56 typedef std::list<
57 ini_NameValue
58 > NameValueList;
60 struct ini_Section
62 rtl::OUString sName;
63 NameValueList lList;
65 typedef std::map<rtl::OUString,
66 ini_Section
67 >IniSectionMap;
70 class IniParser
72 IniSectionMap mAllSection;
73 public:
74 IniSectionMap * getAllSection(){return &mAllSection;};
75 ini_Section * getSection(OUString const & secName)
77 if (mAllSection.find(secName) != mAllSection.end())
78 return &mAllSection[secName];
79 return NULL;
81 IniParser(OUString const & rIniName) throw(com::sun::star::io::IOException )
83 OUString curDirPth;
84 OUString iniUrl;
85 osl_getProcessWorkingDir( &curDirPth.pData );
86 if (osl_getAbsoluteFileURL( curDirPth.pData, rIniName.pData, &iniUrl.pData ))
87 throw ::com::sun::star::io::IOException();
90 #if OSL_DEBUG_LEVEL > 1
91 OString sFile = OUStringToOString(iniUrl, RTL_TEXTENCODING_ASCII_US);
92 OSL_TRACE(__FILE__" -- parser() - %s\n", sFile.getStr());
93 #endif
94 oslFileHandle handle=NULL;
95 if (iniUrl.getLength() &&
96 osl_File_E_None == osl_openFile(iniUrl.pData, &handle, osl_File_OpenFlag_Read))
98 rtl::ByteSequence seq;
99 sal_uInt64 nSize = 0;
101 osl_getFileSize(handle, &nSize);
102 OUString sectionName = OUString::createFromAscii("no name section");
103 while (true)
105 sal_uInt64 nPos;
106 if (osl_File_E_None != osl_getFilePos(handle, &nPos) || nPos >= nSize)
107 break;
108 if (osl_File_E_None != osl_readLine(handle , (sal_Sequence **) &seq))
109 break;
110 OString line( (const sal_Char *) seq.getConstArray(), seq.getLength() );
111 sal_Int32 nIndex = line.indexOf('=');
112 if (nIndex >= 1)
114 ini_Section *aSection = &mAllSection[sectionName];
115 struct ini_NameValue nameValue;
116 nameValue.sName = OStringToOUString(
117 line.copy(0,nIndex).trim(), RTL_TEXTENCODING_ASCII_US );
118 nameValue.sValue = OStringToOUString(
119 line.copy(nIndex+1).trim(), RTL_TEXTENCODING_UTF8 );
121 aSection->lList.push_back(nameValue);
124 else
126 sal_Int32 nIndexStart = line.indexOf('[');
127 sal_Int32 nIndexEnd = line.indexOf(']');
128 if ( nIndexEnd > nIndexStart && nIndexStart >=0)
130 sectionName = OStringToOUString(
131 line.copy(nIndexStart + 1,nIndexEnd - nIndexStart -1).trim(), RTL_TEXTENCODING_ASCII_US );
132 if (!sectionName.getLength())
133 sectionName = OUString::createFromAscii("no name section");
135 ini_Section *aSection = &mAllSection[sectionName];
136 aSection->sName = sectionName;
140 osl_closeFile(handle);
142 #if OSL_DEBUG_LEVEL > 1
143 else
145 OString file_tmp = OUStringToOString(iniUrl, RTL_TEXTENCODING_ASCII_US);
146 OSL_TRACE( __FILE__" -- couldn't open file: %s", file_tmp.getStr() );
147 throw ::com::sun::star::io::IOException();
149 #endif
151 #if OSL_DEBUG_LEVEL > 1
152 void Dump()
154 IniSectionMap::iterator iBegin = mAllSection.begin();
155 IniSectionMap::iterator iEnd = mAllSection.end();
156 for(;iBegin != iEnd;iBegin++)
158 ini_Section *aSection = &(*iBegin).second;
159 OString sec_name_tmp = OUStringToOString(aSection->sName, RTL_TEXTENCODING_ASCII_US);
160 for(NameValueList::iterator itor=aSection->lList.begin();
161 itor != aSection->lList.end();
162 itor++)
164 struct ini_NameValue * aValue = &(*itor);
165 OString name_tmp = OUStringToOString(aValue->sName, RTL_TEXTENCODING_ASCII_US);
166 OString value_tmp = OUStringToOString(aValue->sValue, RTL_TEXTENCODING_UTF8);
167 OSL_TRACE(
168 " section=%s name=%s value=%s\n",
169 sec_name_tmp.getStr(),
170 name_tmp.getStr(),
171 value_tmp.getStr() );
177 #endif
181 #if (defined UNX) || (defined OS2)
182 int main( int argc, char * argv[] )
183 #else
184 int _cdecl main( int argc, char * argv[] )
185 #endif
189 IniParser parser(OUString::createFromAscii("test.ini"));
190 parser.Dump();
191 return 0;