1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
22 #include <com/sun/star/io/IOException.hpp>
23 #include <osl/process.h>
34 inline ini_NameValue() SAL_THROW(())
37 OUString
const & name
, OUString
const & value
) SAL_THROW(())
52 typedef std::map
<OUString
,
59 IniSectionMap mAllSection
;
61 IniSectionMap
* getAllSection(){return &mAllSection
;};
62 ini_Section
* getSection(OUString
const & secName
)
64 if (mAllSection
.find(secName
) != mAllSection
.end())
65 return &mAllSection
[secName
];
68 IniParser(OUString
const & rIniName
) throw(com::sun::star::io::IOException
)
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());
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
;
88 osl_getFileSize(handle
, &nSize
);
89 OUString
sectionName( "no name section" );
93 if (osl_File_E_None
!= osl_getFilePos(handle
, &nPos
) || nPos
>= nSize
)
95 if (osl_File_E_None
!= osl_readLine(handle
, (sal_Sequence
**) &seq
))
97 OString
line( (const sal_Char
*) seq
.getConstArray(), seq
.getLength() );
98 sal_Int32 nIndex
= line
.indexOf('=');
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
);
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
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();
138 #if OSL_DEBUG_LEVEL > 1
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();
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
);
155 " section=%s name=%s value=%s\n",
156 sec_name_tmp
.getStr(),
158 value_tmp
.getStr() );
169 int main( int argc
, char * argv
[] )
171 int _cdecl
main( int argc
, char * argv
[] )
176 IniParser
parser(OUString("test.ini"));
181 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */