Coverage Report

Created: 2025-12-08 09:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sc/source/ui/dbgui/imoptdlg.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
 * This file incorporates work covered by the following license notice:
10
 *
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 .
18
 */
19
20
#include <imoptdlg.hxx>
21
#include <asciiopt.hxx>
22
#include <comphelper/string.hxx>
23
#include <unotools/charclass.hxx>
24
#include <osl/thread.h>
25
#include <o3tl/string_view.hxx>
26
#include <global.hxx>
27
28
const char pStrFix[] = "FIX";
29
30
//  The option string can no longer contain a semicolon (because of pick list),
31
//  therefore, starting with version 336 comma instead
32
33
ScImportOptions::ScImportOptions( std::u16string_view rStr )
34
0
{
35
    // Use the same string format as ScAsciiOptions,
36
    // because the import options string is passed here when a CSV file is loaded and saved again.
37
    // The old format is still supported because it might be used in macros.
38
39
0
    bFixedWidth = false;
40
0
    nFieldSepCode = 0;
41
0
    nTextSepCode = 0;
42
0
    eCharSet = RTL_TEXTENCODING_DONTKNOW;
43
0
    bSaveAsShown = true;    // "true" if not in string (after CSV import)
44
0
    bQuoteAllText = false;
45
0
    bSaveNumberAsSuch = true;
46
0
    bSaveFormulas = false;
47
0
    bRemoveSpace = false;
48
0
    nSheetToExport = 0;
49
0
    bEvaluateFormulas = true;   // true if not present at all, for compatibility
50
0
    bIncludeBOM = false;
51
0
    sal_Int32 nTokenCount = comphelper::string::getTokenCount(rStr, ',');
52
0
    if ( nTokenCount < 3 )
53
0
        return;
54
55
0
    sal_Int32 nIdx{ 0 };
56
    // first 3 tokens: common
57
0
    OUString aToken( o3tl::getToken(rStr, 0, ',', nIdx ) );
58
0
    if( aToken.equalsIgnoreAsciiCase( pStrFix ) )
59
0
        bFixedWidth = true;
60
0
    else
61
0
        nFieldSepCode = ScAsciiOptions::GetWeightedFieldSep( aToken, true);
62
0
    nTextSepCode  = static_cast<sal_Unicode>(o3tl::toInt32(o3tl::getToken(rStr, 0, ',', nIdx)));
63
0
    aStrFont      = o3tl::getToken(rStr, 0, ',', nIdx);
64
0
    eCharSet      = ScGlobal::GetCharsetValue(aStrFont);
65
66
0
    if ( nTokenCount == 4 )
67
0
    {
68
        // compatibility with old options string: "Save as shown" as 4th token, numeric
69
0
        bSaveAsShown = o3tl::toInt32(o3tl::getToken(rStr, 0, ',', nIdx)) != 0;
70
0
        bQuoteAllText = true;   // use old default then
71
0
    }
72
0
    else
73
0
    {
74
        // look at the same positions as in ScAsciiOptions
75
0
        if ( nTokenCount >= 7 )
76
0
            bQuoteAllText = o3tl::getToken(rStr, 3, ',', nIdx) == u"true";  // 7th token
77
0
        if ( nTokenCount >= 8 )
78
0
            bSaveNumberAsSuch = o3tl::getToken(rStr, 0, ',', nIdx) == u"true";
79
0
        if ( nTokenCount >= 9 )
80
0
            bSaveAsShown = o3tl::getToken(rStr, 0, ',', nIdx) == u"true";
81
0
        if ( nTokenCount >= 10 )
82
0
            bSaveFormulas = o3tl::getToken(rStr, 0, ',', nIdx) == u"true";
83
0
        if ( nTokenCount >= 11 )
84
0
            bRemoveSpace = o3tl::getToken(rStr, 0, ',', nIdx) == u"true";
85
0
        if ( nTokenCount >= 12 )
86
0
        {
87
0
            const OUString aTok(o3tl::getToken(rStr,0, ',', nIdx));
88
0
            if (aTok == "-1")
89
0
                nSheetToExport = -1;    // all
90
0
            else if (aTok.isEmpty() || CharClass::isAsciiNumeric(aTok))
91
0
                nSheetToExport = aTok.toInt32();
92
0
            else
93
0
                nSheetToExport = -23;   // invalid, force error
94
0
        }
95
0
        if ( nTokenCount >= 13 )
96
            // If present, defaults to "false".
97
0
            bEvaluateFormulas = o3tl::getToken(rStr, 0, ',', nIdx) == u"true";
98
0
        if (nTokenCount >= 14)
99
0
            bIncludeBOM = o3tl::getToken(rStr, 0, ',', nIdx) == u"true";
100
0
    }
101
0
}
102
103
OUString ScImportOptions::BuildString() const
104
0
{
105
0
    OUString aResult;
106
107
0
    if( bFixedWidth )
108
0
        aResult += pStrFix;
109
0
    else
110
0
        aResult += OUString::number(nFieldSepCode);
111
0
    aResult += "," + OUString::number(nTextSepCode) + "," + aStrFont +
112
                                                 // use the same string format as ScAsciiOptions:
113
0
            ",1,,0," +                           // first row, no column info, default language
114
0
            OUString::boolean( bQuoteAllText ) + // same as "quoted field as text" in ScAsciiOptions
115
0
            "," +
116
0
            OUString::boolean( bSaveNumberAsSuch ) + // "save number as such": not in ScAsciiOptions
117
0
            "," +
118
0
            OUString::boolean( bSaveAsShown ) +  // "save as shown": not in ScAsciiOptions
119
0
            "," +
120
0
            OUString::boolean( bSaveFormulas ) +  // "save formulas": not in ScAsciiOptions
121
0
            "," +
122
0
            OUString::boolean( bRemoveSpace ) +  // same as "Remove space" in ScAsciiOptions
123
0
            "," +
124
0
            OUString::number(nSheetToExport) +  // Only available for command line --convert-to
125
0
            "," +
126
0
            OUString::boolean( bEvaluateFormulas ) +  // same as "Evaluate formulas" in ScAsciiOptions
127
0
            "," +
128
0
            OUString::boolean(bIncludeBOM) ;  // same as "Include BOM" in ScAsciiOptions
129
130
0
    return aResult;
131
0
}
132
133
void ScImportOptions::SetTextEncoding( rtl_TextEncoding nEnc )
134
0
{
135
0
    eCharSet = (nEnc == RTL_TEXTENCODING_DONTKNOW ?
136
0
        osl_getThreadTextEncoding() : nEnc);
137
0
    aStrFont = ScGlobal::GetCharsetString( nEnc );
138
0
}
139
140
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */