1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
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"
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"
50 inline rtl::OUString
pathnameToAbsoluteUrl(rtl::OUString
const & pathname
) {
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
);
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
);
72 inline rtl::OString
pathnameToken(char const * pathname
, char const * root
) {
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
);
86 std::cerr
<< "Error: No project root argument\n";
87 std::exit(EXIT_FAILURE
);
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('/', '\\');
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
);
125 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */