android: Update app icon to new startcenter icon
[LibreOffice.git] / dbaccess / source / filter / hsqldb / utils.cxx
blobd8addd33623e4817fb873691eddd71be19fe5bdb
2 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /*
4 * This file is part of the LibreOffice project.
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 * This file incorporates work covered by the following license notice:
12 * Licensed to the Apache Software Foundation (ASF) under one or more
13 * contributor license agreements. See the NOTICE file distributed
14 * with this work for additional information regarding copyright
15 * ownership. The ASF licenses this file to you under the Apache
16 * License, Version 2.0 (the "License"); you may not use this file
17 * except in compliance with the License. You may obtain a copy of
18 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <comphelper/string.hxx>
22 #include <comphelper/processfactory.hxx>
23 #include <connectivity/dbexception.hxx>
24 #include <sal/log.hxx>
26 #include "utils.hxx"
28 using namespace dbahsql;
30 namespace
32 int getHexValue(sal_Unicode c)
34 if (c >= '0' && c <= '9')
36 return c - '0';
38 else if (c >= 'A' && c <= 'F')
40 return c - 'A' + 10;
42 else if (c >= 'a' && c <= 'f')
44 return c - 'a' + 10;
46 else
48 return -1;
52 } // unnamed namespace
54 //Convert ascii escaped unicode to utf-8
55 OUString utils::convertToUTF8(std::string_view original)
57 OUString res = OStringToOUString(original, RTL_TEXTENCODING_UTF8);
58 for (sal_Int32 i = 0;;)
60 i = res.indexOf("\\u", i);
61 if (i == -1)
63 break;
65 i += 2;
66 if (res.getLength() - i >= 4)
68 bool escape = true;
69 sal_Unicode c = 0;
70 for (sal_Int32 j = 0; j != 4; ++j)
72 auto const n = getHexValue(res[i + j]);
73 if (n == -1)
75 escape = false;
76 break;
78 c = (c << 4) | n;
80 if (escape)
82 i -= 2;
83 res = res.replaceAt(i, 6, rtl::OUStringChar(c));
84 ++i;
88 return res;
91 OUString utils::getTableNameFromStmt(std::u16string_view sSql)
93 auto stmtComponents = comphelper::string::split(sSql, sal_Unicode(u' '));
94 assert(stmtComponents.size() > 2);
95 auto wordIter = stmtComponents.begin();
97 if (*wordIter == "CREATE" || *wordIter == "ALTER")
98 ++wordIter;
99 if (*wordIter == "CACHED")
100 ++wordIter;
101 if (*wordIter == "TABLE")
102 ++wordIter;
104 // it may contain spaces if it's put into apostrophes.
105 if (wordIter->indexOf("\"") >= 0)
107 size_t nAposBegin = sSql.find('"');
108 size_t nAposEnd = nAposBegin;
109 bool bProperEndAposFound = false;
110 while (!bProperEndAposFound)
112 nAposEnd = sSql.find('"', nAposEnd + 1);
113 if (nAposEnd == std::u16string_view::npos)
115 SAL_WARN("dbaccess", "no matching \"");
116 return OUString();
118 if (sSql[nAposEnd - 1] != u'\\')
119 bProperEndAposFound = true;
121 std::u16string_view result = sSql.substr(nAposBegin, nAposEnd - nAposBegin + 1);
122 return OUString(result);
125 // next word is the table's name
126 // it might stuck together with the column definitions.
127 sal_Int32 nParenPos = wordIter->indexOf("(");
128 if (nParenPos > 0)
129 return wordIter->copy(0, nParenPos);
130 else
131 return *wordIter;
134 void utils::ensureFirebirdTableLength(std::u16string_view sName)
136 if (sName.size() > 30) // Firebird limitation
138 static constexpr OUStringLiteral NAME_TOO_LONG
139 = u"Firebird 3 doesn't support object (table, field) names "
140 "of more than 30 characters; please shorten your object "
141 "names in the original file and try again.";
142 dbtools::throwGenericSQLException(NAME_TOO_LONG,
143 ::comphelper::getProcessComponentContext());
147 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */