1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 // MARKER(update_precomp.py): autogen include statement, do not remove 29 #include "precompiled_connectivity.hxx" 30 #include <rtl/ustring.hxx> 31 #include <stdio.h> 32 #include <com/sun/star/io/IOException.hpp> 33 #include <osl/process.h> 34 using namespace rtl; 35 36 #include <map> 37 #include <list> 38 39 struct ini_NameValue 40 { 41 rtl::OUString sName; 42 rtl::OUString sValue; 43 44 inline ini_NameValue() SAL_THROW( () ) 45 {} 46 inline ini_NameValue( 47 OUString const & name, OUString const & value ) SAL_THROW( () ) 48 : sName( name ), 49 sValue( value ) 50 {} 51 }; 52 53 typedef std::list< 54 ini_NameValue 55 > NameValueList; 56 57 struct ini_Section 58 { 59 rtl::OUString sName; 60 NameValueList lList; 61 }; 62 typedef std::map<rtl::OUString, 63 ini_Section 64 >IniSectionMap; 65 66 67 class IniParser 68 { 69 IniSectionMap mAllSection; 70 public: 71 IniSectionMap * getAllSection(){return &mAllSection;}; 72 ini_Section * getSection(OUString const & secName) 73 { 74 if (mAllSection.find(secName) != mAllSection.end()) 75 return &mAllSection[secName]; 76 return NULL; 77 } 78 IniParser(OUString const & rIniName) throw(com::sun::star::io::IOException ) 79 { 80 OUString curDirPth; 81 OUString iniUrl; 82 osl_getProcessWorkingDir( &curDirPth.pData ); 83 if (osl_getAbsoluteFileURL( curDirPth.pData, rIniName.pData, &iniUrl.pData )) 84 throw ::com::sun::star::io::IOException(); 85 86 87 #if OSL_DEBUG_LEVEL > 1 88 OString sFile = OUStringToOString(iniUrl, RTL_TEXTENCODING_ASCII_US); 89 OSL_TRACE(__FILE__" -- parser() - %s\n", sFile.getStr()); 90 #endif 91 oslFileHandle handle=NULL; 92 if (iniUrl.getLength() && 93 osl_File_E_None == osl_openFile(iniUrl.pData, &handle, osl_File_OpenFlag_Read)) 94 { 95 rtl::ByteSequence seq; 96 sal_uInt64 nSize = 0; 97 98 osl_getFileSize(handle, &nSize); 99 OUString sectionName = OUString::createFromAscii("no name section"); 100 while (true) 101 { 102 sal_uInt64 nPos; 103 if (osl_File_E_None != osl_getFilePos(handle, &nPos) || nPos >= nSize) 104 break; 105 if (osl_File_E_None != osl_readLine(handle , (sal_Sequence **) &seq)) 106 break; 107 OString line( (const sal_Char *) seq.getConstArray(), seq.getLength() ); 108 sal_Int32 nIndex = line.indexOf('='); 109 if (nIndex >= 1) 110 { 111 ini_Section *aSection = &mAllSection[sectionName]; 112 struct ini_NameValue nameValue; 113 nameValue.sName = OStringToOUString( 114 line.copy(0,nIndex).trim(), RTL_TEXTENCODING_ASCII_US ); 115 nameValue.sValue = OStringToOUString( 116 line.copy(nIndex+1).trim(), RTL_TEXTENCODING_UTF8 ); 117 118 aSection->lList.push_back(nameValue); 119 120 } 121 else 122 { 123 sal_Int32 nIndexStart = line.indexOf('['); 124 sal_Int32 nIndexEnd = line.indexOf(']'); 125 if ( nIndexEnd > nIndexStart && nIndexStart >=0) 126 { 127 sectionName = OStringToOUString( 128 line.copy(nIndexStart + 1,nIndexEnd - nIndexStart -1).trim(), RTL_TEXTENCODING_ASCII_US ); 129 if (!sectionName.getLength()) 130 sectionName = OUString::createFromAscii("no name section"); 131 132 ini_Section *aSection = &mAllSection[sectionName]; 133 aSection->sName = sectionName; 134 } 135 } 136 } 137 osl_closeFile(handle); 138 } 139 #if OSL_DEBUG_LEVEL > 1 140 else 141 { 142 OString file_tmp = OUStringToOString(iniUrl, RTL_TEXTENCODING_ASCII_US); 143 OSL_TRACE( __FILE__" -- couldn't open file: %s", file_tmp.getStr() ); 144 throw ::com::sun::star::io::IOException(); 145 } 146 #endif 147 } 148 #if OSL_DEBUG_LEVEL > 1 149 void Dump() 150 { 151 IniSectionMap::iterator iBegin = mAllSection.begin(); 152 IniSectionMap::iterator iEnd = mAllSection.end(); 153 for(;iBegin != iEnd;iBegin++) 154 { 155 ini_Section *aSection = &(*iBegin).second; 156 OString sec_name_tmp = OUStringToOString(aSection->sName, RTL_TEXTENCODING_ASCII_US); 157 for(NameValueList::iterator itor=aSection->lList.begin(); 158 itor != aSection->lList.end(); 159 itor++) 160 { 161 struct ini_NameValue * aValue = &(*itor); 162 OString name_tmp = OUStringToOString(aValue->sName, RTL_TEXTENCODING_ASCII_US); 163 OString value_tmp = OUStringToOString(aValue->sValue, RTL_TEXTENCODING_UTF8); 164 OSL_TRACE( 165 " section=%s name=%s value=%s\n", 166 sec_name_tmp.getStr(), 167 name_tmp.getStr(), 168 value_tmp.getStr() ); 169 170 } 171 } 172 173 } 174 #endif 175 176 }; 177 178 #if (defined UNX) || (defined OS2) 179 int main( int argc, char * argv[] ) 180 #else 181 int _cdecl main( int argc, char * argv[] ) 182 #endif 183 184 { 185 186 IniParser parser(OUString::createFromAscii("test.ini")); 187 parser.Dump(); 188 return 0; 189 } 190