Coverage Report

Created: 2026-11-06 11:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/svx/source/gallery2/galini.cxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
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
 */
9
10
/*
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.
14
 */
15
16
#include <sal/config.h>
17
#include <sal/log.hxx>
18
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>
26
27
OUString GalleryFileStorageEntry::ReadStrFromIni(std::string_view aKeyName) const
28
0
{
29
0
    std::unique_ptr<SvStream> pStrm(::utl::UcbStreamHelper::CreateStream(
30
0
        GetStrURL().GetMainURL( INetURLObject::DecodeMechanism::NONE ),
31
0
                                StreamMode::READ ));
32
33
0
    OUString aResult;
34
35
0
    if( pStrm )
36
0
    {
37
0
        const LanguageTag& rLangTag = Application::GetSettings().GetUILanguageTag();
38
0
        const std::vector<OUString> aFallbacks = rLangTag.getFallbackStrings(true);
39
0
        size_t nRank = aFallbacks.size();
40
41
        // Suppose aKeyName is "a"
42
        //         ini has " a [ en ] = Foo
43
        //                   a [ en_US ] = Bar "
44
        //         and aFallbacks is { "en-US", "en" }
45
        // Then we must return "Bar", because its locale has higher fallback rank.
46
47
0
        OStringBuffer aLineBuf;
48
0
        while (pStrm->ReadLine(aLineBuf))
49
0
        {
50
0
            std::string_view aLine(aLineBuf);
51
52
            // comments
53
0
            if (aLine.starts_with("#"))
54
0
                continue;
55
56
0
            size_t n = aLine.find('=');
57
0
            if (n != std::string_view::npos)
58
0
            {
59
0
                std::string_view aKey(o3tl::trim(aLine.substr(0, n)));
60
0
                std::string_view aValue(o3tl::trim(aLine.substr(n + 1)));
61
62
0
                n = aKey.find('[');
63
0
                if (n == std::string_view::npos || n < 1)
64
0
                    continue;
65
66
0
                std::string_view aLocale = o3tl::trim(aKey.substr(n + 1, aKey.find(']', n + 2) - n - 1));
67
0
                aKey = o3tl::trim(aKey.substr(0, n));
68
69
0
                SAL_INFO("svx", "ini file has '" << aKey << "' [ '" << aLocale << "' ] = '" << aValue << "'");
70
71
0
                if (aKey != aKeyName)
72
0
                    continue;
73
74
                // grisly language matching, is this not available somewhere else?
75
0
                OUString aLang
76
0
                    = OStringToOUString(aLocale, RTL_TEXTENCODING_ASCII_US).replace('_', '-');
77
0
                for (n = 0; n < nRank; ++n)
78
0
                {
79
0
                    auto& rFallback = aFallbacks[n];
80
0
                    SAL_INFO( "svx", "compare '" << aLang << "' with '" << rFallback << "' rank " << nRank << " vs. " << n );
81
0
                    if (rFallback == aLang)
82
0
                    {
83
0
                        nRank = n; // try to get the most accurate match
84
0
                        aResult = OStringToOUString(aValue, RTL_TEXTENCODING_UTF8);
85
0
                    }
86
0
                }
87
0
                if (nRank == 0)
88
0
                    break;
89
0
            }
90
0
        }
91
0
    }
92
93
0
    SAL_INFO( "svx", "readStrFromIni returns '" << aResult << "'");
94
0
    return aResult;
95
0
}
96
97
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */