Bump for 3.6-28
[LibreOffice.git] / l10ntools / source / common.hxx
blob0129c6a7c34eedd205ac968960c824e0b238e1a9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * Version: MPL 1.1 / GPLv3+ / LGPLv3+
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License or as specified alternatively below. You may obtain a copy of
8 * the License at http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * Major Contributor(s):
16 * [ Copyright (C) 2012 Red Hat, Inc., Stephan Bergmann <sbergman@redhat.com>
17 * (initial developer) ]
19 * All Rights Reserved.
21 * For minor contributions see the git repository.
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
25 * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
26 * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
27 * instead of those above.
30 #ifndef INCLUDED_L10NTOOLS_SOURCE_COMMON_HXX
31 #define INCLUDED_L10NTOOLS_SOURCE_COMMON_HXX
33 #include "sal/config.h"
35 #include <cstdlib>
36 #include <iostream>
38 #include "osl/file.hxx"
39 #include "osl/process.h"
40 #include "osl/thread.h"
41 #include "rtl/string.h"
42 #include "rtl/string.hxx"
43 #include "rtl/textcvt.h"
44 #include "rtl/uri.hxx"
45 #include "rtl/ustring.h"
46 #include "rtl/ustring.hxx"
48 namespace common {
50 inline rtl::OUString pathnameToAbsoluteUrl(rtl::OUString const & pathname) {
51 rtl::OUString url;
52 if (osl::FileBase::getFileURLFromSystemPath(pathname, url)
53 != osl::FileBase::E_None)
55 std::cerr << "Error: Cannot convert input pathname to URL\n";
56 std::exit(EXIT_FAILURE);
58 rtl::OUString cwd;
59 if (osl_getProcessWorkingDir(&cwd.pData) != osl_Process_E_None) {
60 std::cerr << "Error: Cannot determine cwd\n";
61 std::exit(EXIT_FAILURE);
63 if (osl::FileBase::getAbsoluteFileURL(cwd, url, url)
64 != osl::FileBase::E_None)
66 std::cerr << "Error: Cannot convert input URL to absolute URL\n";
67 std::exit(EXIT_FAILURE);
69 return url;
72 inline rtl::OString pathnameToken(char const * pathname, char const * root) {
73 rtl::OUString full;
74 if (!rtl_convertStringToUString(
75 &full.pData, pathname, rtl_str_getLength(pathname),
76 osl_getThreadTextEncoding(),
77 (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR
78 | RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR
79 | RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR)))
81 std::cerr << "Error: Cannot convert input pathname to UTF-16\n";
82 std::exit(EXIT_FAILURE);
84 full = pathnameToAbsoluteUrl(full);
85 if (root == 0) {
86 std::cerr << "Error: No project root argument\n";
87 std::exit(EXIT_FAILURE);
89 rtl::OUString base;
90 if (!rtl_convertStringToUString(
91 &base.pData, root, rtl_str_getLength(root),
92 osl_getThreadTextEncoding(),
93 (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR
94 | RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR
95 | RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR)))
97 std::cerr << "Error: Cannot convert project root to UTF-16\n";
98 std::exit(EXIT_FAILURE);
100 base = rtl::Uri::convertRelToAbs(full, base);
101 if (full.getLength() <= base.getLength() || base.isEmpty()
102 || base[base.getLength() - 1] != '/'
103 || full[base.getLength() - 1] != '/')
105 std::cerr << "Error: Cannot extract suffix from input pathname\n";
106 std::exit(EXIT_FAILURE);
108 full = full.copy(base.getLength()).replace('/', '\\');
109 rtl::OString suffix;
110 if (!full.convertToString(
111 &suffix, osl_getThreadTextEncoding(),
112 (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
113 | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR)))
115 std::cerr << "Error: Cannot convert suffix from UTF-16\n";
116 std::exit(EXIT_FAILURE);
118 return suffix;
123 #endif
125 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */