Best effort to create directories of SAL_LOG_FILE
[LibreOffice.git] / svx / source / gallery2 / galini.cxx
blobc88a7f4f2f8d9af5335f18959813a4f3033d9b22
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/.
8 */
11 * The world's quickest and lamest .desktop / .ini file parser.
12 * Ideally the .thm file would move to a .desktop file in
13 * future.
16 #include <sal/config.h>
17 #include <sal/log.hxx>
19 #include <unotools/ucbstreamhelper.hxx>
20 #include <galleryfilestorageentry.hxx>
21 #include <i18nlangtag/languagetag.hxx>
22 #include <vcl/svapp.hxx>
23 #include <vcl/settings.hxx>
24 #include <o3tl/string_view.hxx>
25 #include <memory>
27 OUString GalleryFileStorageEntry::ReadStrFromIni(std::u16string_view aKeyName ) const
29 std::unique_ptr<SvStream> pStrm(::utl::UcbStreamHelper::CreateStream(
30 GetStrURL().GetMainURL( INetURLObject::DecodeMechanism::NONE ),
31 StreamMode::READ ));
33 const LanguageTag &rLangTag = Application::GetSettings().GetUILanguageTag();
35 ::std::vector< OUString > aFallbacks = rLangTag.getFallbackStrings( true);
37 OUString aResult;
38 sal_Int32 nRank = 42;
40 if( pStrm )
42 OString aLine;
43 while( pStrm->ReadLine( aLine ) )
45 OUString aKey;
46 OUString aLocale;
47 OUString aValue;
48 sal_Int32 n;
50 // comments
51 if( aLine.startsWith( "#" ) )
52 continue;
54 // a[en_US] = Bob
55 if( ( n = aLine.indexOf( '=' ) ) >= 1)
57 aKey = OStringToOUString(
58 o3tl::trim(aLine.subView( 0, n )), RTL_TEXTENCODING_ASCII_US );
59 aValue = OStringToOUString(
60 o3tl::trim(aLine.subView( n + 1 )), RTL_TEXTENCODING_UTF8 );
62 if( ( n = aKey.indexOf( '[' ) ) >= 1 )
64 aLocale = o3tl::trim(aKey.subView( n + 1 ));
65 aKey = o3tl::trim(aKey.subView( 0, n ));
66 if( (n = aLocale.indexOf( ']' ) ) >= 1 )
67 aLocale = o3tl::trim(aLocale.subView( 0, n ));
70 SAL_INFO("svx", "ini file has '" << aKey << "' [ '" << aLocale << "' ] = '" << aValue << "'");
72 // grisly language matching, is this not available somewhere else?
73 if( aKey == aKeyName )
75 /* FIXME-BCP47: what is this supposed to do? */
76 n = 0;
77 OUString aLang = aLocale.replace('_','-');
78 for( const auto& rFallback : aFallbacks )
80 SAL_INFO( "svx", "compare '" << aLang << "' with '" << rFallback << "' rank " << nRank << " vs. " << n );
81 if( rFallback == aLang && n < nRank ) {
82 nRank = n; // try to get the most accurate match
83 aResult = aValue;
85 ++n;
91 SAL_INFO( "svx", "readStrFromIni returns '" << aResult << "'");
92 return aResult;
95 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */