sw: use SAL_RET_MAYBENULL in Lower()
[LibreOffice.git] / connectivity / workben / iniParser / main.cxx
blobc4746fceec09dbfab6d0be94b20280ba8e504008
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 <sal/log.hxx>
22 #include <com/sun/star/io/IOException.hpp>
23 #include <osl/process.h>
25 #include <map>
26 #include <vector>
29 struct ini_NameValue
31 OUString sName;
32 OUString sValue;
34 inline ini_NameValue()
36 inline ini_NameValue(
37 OUString const & name, OUString const & value )
38 : sName( name ),
39 sValue( value )
43 typedef std::vector<
44 ini_NameValue
45 > NameValueVector;
47 struct ini_Section
49 OUString sName;
50 NameValueVector vVector;
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 explicit IniParser(OUString const & rIniName) throw(css::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 css::io::IOException();
77 oslFileHandle handle=NULL;
78 if (iniUrl.getLength() &&
79 osl_File_E_None == osl_openFile(iniUrl.pData, &handle, osl_File_OpenFlag_Read))
81 rtl::ByteSequence seq;
82 sal_uInt64 nSize = 0;
84 osl_getFileSize(handle, &nSize);
85 OUString sectionName( "no name section" );
86 while (true)
88 sal_uInt64 nPos;
89 if (osl_File_E_None != osl_getFilePos(handle, &nPos) || nPos >= nSize)
90 break;
91 if (osl_File_E_None != osl_readLine(handle , (sal_Sequence **) &seq))
92 break;
93 OString line( (const char *) seq.getConstArray(), seq.getLength() );
94 sal_Int32 nIndex = line.indexOf('=');
95 if (nIndex >= 1)
97 ini_Section *aSection = &mAllSection[sectionName];
98 struct ini_NameValue nameValue;
99 nameValue.sName = OStringToOUString(
100 line.copy(0,nIndex).trim(), RTL_TEXTENCODING_ASCII_US );
101 nameValue.sValue = OStringToOUString(
102 line.copy(nIndex+1).trim(), RTL_TEXTENCODING_UTF8 );
104 aSection->vVector.push_back(nameValue);
107 else
109 sal_Int32 nIndexStart = line.indexOf('[');
110 sal_Int32 nIndexEnd = line.indexOf(']');
111 if ( nIndexEnd > nIndexStart && nIndexStart >=0)
113 sectionName = OStringToOUString(
114 line.copy(nIndexStart + 1,nIndexEnd - nIndexStart -1).trim(), RTL_TEXTENCODING_ASCII_US );
115 if (!sectionName.getLength())
116 sectionName = "no name section";
118 ini_Section *aSection = &mAllSection[sectionName];
119 aSection->sName = sectionName;
123 osl_closeFile(handle);
125 #if OSL_DEBUG_LEVEL > 1
126 else
128 SAL_WARN("connectivity", "couldn't open file: " << iniUrl );
129 throw css::io::IOException();
131 #endif
133 #if OSL_DEBUG_LEVEL > 1
134 void Dump()
136 for(auto& rEntry : mAllSection)
138 ini_Section *aSection = &rEntry.second;
139 for(const auto& rValue : aSection->vVector)
141 SAL_WARN("connectivity",
142 " section=" << aSection->sName << " name=" << rValue.sName << " value=" << rValue.sValue );
148 #endif
152 int main( int argc, char * argv[] )
155 IniParser parser(OUString("test.ini"));
156 parser.Dump();
157 return 0;
160 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */