Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * Version: MPL 1.1 / GPLv3+ / LGPLv3+
4 : *
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/
9 : *
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.
14 : *
15 : * Major Contributor(s):
16 : * [ Copyright (C) 2012 Red Hat, Inc., Stephan Bergmann <sbergman@redhat.com>
17 : * (initial developer) ]
18 : *
19 : * All Rights Reserved.
20 : *
21 : * For minor contributions see the git repository.
22 : *
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.
28 : */
29 :
30 : #ifndef INCLUDED_L10NTOOLS_SOURCE_COMMON_HXX
31 : #define INCLUDED_L10NTOOLS_SOURCE_COMMON_HXX
32 :
33 : #include "sal/config.h"
34 :
35 : #include <cstdlib>
36 : #include <iostream>
37 :
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"
47 :
48 : namespace common {
49 :
50 0 : inline rtl::OUString pathnameToAbsoluteUrl(rtl::OUString const & pathname) {
51 0 : rtl::OUString url;
52 0 : if (osl::FileBase::getFileURLFromSystemPath(pathname, url)
53 : != osl::FileBase::E_None)
54 : {
55 0 : std::cerr << "Error: Cannot convert input pathname to URL\n";
56 0 : std::exit(EXIT_FAILURE);
57 : }
58 0 : rtl::OUString cwd;
59 0 : if (osl_getProcessWorkingDir(&cwd.pData) != osl_Process_E_None) {
60 0 : std::cerr << "Error: Cannot determine cwd\n";
61 0 : std::exit(EXIT_FAILURE);
62 : }
63 0 : if (osl::FileBase::getAbsoluteFileURL(cwd, url, url)
64 : != osl::FileBase::E_None)
65 : {
66 0 : std::cerr << "Error: Cannot convert input URL to absolute URL\n";
67 0 : std::exit(EXIT_FAILURE);
68 : }
69 0 : return url;
70 : }
71 :
72 0 : inline rtl::OString pathnameToken(char const * pathname, char const * root) {
73 0 : rtl::OUString full;
74 0 : if (!rtl_convertStringToUString(
75 : &full.pData, pathname, rtl_str_getLength(pathname),
76 0 : osl_getThreadTextEncoding(),
77 : (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR
78 : | RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR
79 0 : | RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR)))
80 : {
81 0 : std::cerr << "Error: Cannot convert input pathname to UTF-16\n";
82 0 : std::exit(EXIT_FAILURE);
83 : }
84 0 : full = pathnameToAbsoluteUrl(full);
85 0 : if (root == 0) {
86 0 : std::cerr << "Error: No project root argument\n";
87 0 : std::exit(EXIT_FAILURE);
88 : }
89 0 : rtl::OUString base;
90 0 : if (!rtl_convertStringToUString(
91 : &base.pData, root, rtl_str_getLength(root),
92 0 : osl_getThreadTextEncoding(),
93 : (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR
94 : | RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR
95 0 : | RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR)))
96 : {
97 0 : std::cerr << "Error: Cannot convert project root to UTF-16\n";
98 0 : std::exit(EXIT_FAILURE);
99 : }
100 0 : base = rtl::Uri::convertRelToAbs(full, base);
101 0 : if (full.getLength() <= base.getLength() || base.isEmpty()
102 0 : || base[base.getLength() - 1] != '/'
103 0 : || full[base.getLength() - 1] != '/')
104 : {
105 0 : std::cerr << "Error: Cannot extract suffix from input pathname\n";
106 0 : std::exit(EXIT_FAILURE);
107 : }
108 0 : full = full.copy(base.getLength()).replace('/', '\\');
109 0 : rtl::OString suffix;
110 0 : if (!full.convertToString(
111 0 : &suffix, osl_getThreadTextEncoding(),
112 : (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR
113 0 : | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR)))
114 : {
115 0 : std::cerr << "Error: Cannot convert suffix from UTF-16\n";
116 0 : std::exit(EXIT_FAILURE);
117 : }
118 0 : return suffix;
119 : }
120 :
121 : }
122 :
123 : #endif
124 :
125 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|