update credits
[LibreOffice.git] / connectivity / workben / iniParser / main.cxx
blob05dcfa1f1aa5804a42cb604685f9fb5a1c1fa45d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <rtl/ustring.hxx>
21 #include <stdio.h>
22 #include <com/sun/star/io/IOException.hpp>
23 #include <osl/process.h>
25 #include <map>
26 #include <list>
29 struct ini_NameValue
31 OUString sName;
32 OUString sValue;
34 inline ini_NameValue() SAL_THROW(())
36 inline ini_NameValue(
37 OUString const & name, OUString const & value ) SAL_THROW(())
38 : sName( name ),
39 sValue( value )
43 typedef std::list<
44 ini_NameValue
45 > NameValueList;
47 struct ini_Section
49 OUString sName;
50 NameValueList lList;
52 typedef std::map<OUString,
53 ini_Section
54 >IniSectionMap;
57 class IniParser
59 IniSectionMap mAllSection;
60 public:
61 IniSectionMap * getAllSection(){return &mAllSection;};
62 ini_Section * getSection(OUString const & secName)
64 if (mAllSection.find(secName) != mAllSection.end())
65 return &mAllSection[secName];
66 return NULL;
68 IniParser(OUString const & rIniName) throw(com::sun::star::io::IOException )
70 OUString curDirPth;
71 OUString iniUrl;
72 osl_getProcessWorkingDir( &curDirPth.pData );
73 if (osl_getAbsoluteFileURL( curDirPth.pData, rIniName.pData, &iniUrl.pData ))
74 throw ::com::sun::star::io::IOException();
77 #if OSL_DEBUG_LEVEL > 1
78 OString sFile = OUStringToOString(iniUrl, RTL_TEXTENCODING_ASCII_US);
79 OSL_TRACE(__FILE__" -- parser() - %s\n", sFile.getStr());
80 #endif
81 oslFileHandle handle=NULL;
82 if (iniUrl.getLength() &&
83 osl_File_E_None == osl_openFile(iniUrl.pData, &handle, osl_File_OpenFlag_Read))
85 rtl::ByteSequence seq;
86 sal_uInt64 nSize = 0;
88 osl_getFileSize(handle, &nSize);
89 OUString sectionName( "no name section" );
90 while (true)
92 sal_uInt64 nPos;
93 if (osl_File_E_None != osl_getFilePos(handle, &nPos) || nPos >= nSize)
94 break;
95 if (osl_File_E_None != osl_readLine(handle , (sal_Sequence **) &seq))
96 break;
97 OString line( (const sal_Char *) seq.getConstArray(), seq.getLength() );
98 sal_Int32 nIndex = line.indexOf('=');
99 if (nIndex >= 1)
101 ini_Section *aSection = &mAllSection[sectionName];
102 struct ini_NameValue nameValue;
103 nameValue.sName = OStringToOUString(
104 line.copy(0,nIndex).trim(), RTL_TEXTENCODING_ASCII_US );
105 nameValue.sValue = OStringToOUString(
106 line.copy(nIndex+1).trim(), RTL_TEXTENCODING_UTF8 );
108 aSection->lList.push_back(nameValue);
111 else
113 sal_Int32 nIndexStart = line.indexOf('[');
114 sal_Int32 nIndexEnd = line.indexOf(']');
115 if ( nIndexEnd > nIndexStart && nIndexStart >=0)
117 sectionName = OStringToOUString(
118 line.copy(nIndexStart + 1,nIndexEnd - nIndexStart -1).trim(), RTL_TEXTENCODING_ASCII_US );
119 if (!sectionName.getLength())
120 sectionName = OUString("no name section");
122 ini_Section *aSection = &mAllSection[sectionName];
123 aSection->sName = sectionName;
127 osl_closeFile(handle);
129 #if OSL_DEBUG_LEVEL > 1
130 else
132 OString file_tmp = OUStringToOString(iniUrl, RTL_TEXTENCODING_ASCII_US);
133 OSL_TRACE( __FILE__" -- couldn't open file: %s", file_tmp.getStr() );
134 throw ::com::sun::star::io::IOException();
136 #endif
138 #if OSL_DEBUG_LEVEL > 1
139 void Dump()
141 IniSectionMap::iterator iBegin = mAllSection.begin();
142 IniSectionMap::iterator iEnd = mAllSection.end();
143 for(;iBegin != iEnd;iBegin++)
145 ini_Section *aSection = &(*iBegin).second;
146 OString sec_name_tmp = OUStringToOString(aSection->sName, RTL_TEXTENCODING_ASCII_US);
147 for(NameValueList::iterator itor=aSection->lList.begin();
148 itor != aSection->lList.end();
149 itor++)
151 struct ini_NameValue * aValue = &(*itor);
152 OString name_tmp = OUStringToOString(aValue->sName, RTL_TEXTENCODING_ASCII_US);
153 OString value_tmp = OUStringToOString(aValue->sValue, RTL_TEXTENCODING_UTF8);
154 OSL_TRACE(
155 " section=%s name=%s value=%s\n",
156 sec_name_tmp.getStr(),
157 name_tmp.getStr(),
158 value_tmp.getStr() );
164 #endif
168 #if (defined UNX)
169 int main( int argc, char * argv[] )
170 #else
171 int _cdecl main( int argc, char * argv[] )
172 #endif
176 IniParser parser(OUString("test.ini"));
177 parser.Dump();
178 return 0;
181 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */