/src/libreoffice/ucb/source/ucp/inc/urihelper.hxx
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 | | #pragma once |
21 | | |
22 | | #include <rtl/ustring.hxx> |
23 | | #include <rtl/ustrbuf.hxx> |
24 | | #include <rtl/uri.hxx> |
25 | | |
26 | | |
27 | | namespace ucb_impl::urihelper { |
28 | | |
29 | | inline OUString encodeSegment( const OUString & rSegment ) |
30 | 0 | { |
31 | 0 | return rtl::Uri::encode( rSegment, |
32 | 0 | rtl_UriCharClassPchar, |
33 | 0 | rtl_UriEncodeIgnoreEscapes, |
34 | 0 | RTL_TEXTENCODING_UTF8 ); |
35 | 0 | } |
36 | | |
37 | | inline OUString decodeSegment( const OUString& rSegment ) |
38 | 11.3k | { |
39 | 11.3k | return rtl::Uri::decode( rSegment, |
40 | 11.3k | rtl_UriDecodeWithCharset, |
41 | 11.3k | RTL_TEXTENCODING_UTF8 ); |
42 | 11.3k | } |
43 | | |
44 | | inline OUString encodeURI( const OUString & rURI ) |
45 | 0 | { |
46 | 0 | OUString aFragment; |
47 | 0 | OUString aParams; |
48 | 0 | OUString aURI; |
49 | |
|
50 | 0 | sal_Int32 nFragment = rURI.lastIndexOf( u'#' ); |
51 | 0 | if ( nFragment != -1 ) |
52 | 0 | aFragment = rURI.copy( nFragment + 1 ); |
53 | |
|
54 | 0 | sal_Int32 nParams = ( nFragment == -1 ) |
55 | 0 | ? rURI.lastIndexOf( u'?' ) |
56 | 0 | : rURI.lastIndexOf( u'?', nFragment ); |
57 | 0 | if ( nParams != -1 ) |
58 | 0 | aParams = ( nFragment == -1 ) |
59 | 0 | ? rURI.copy( nParams + 1 ) |
60 | 0 | : rURI.copy( nParams + 1, nFragment - nParams - 1 ); |
61 | |
|
62 | 0 | aURI = ( nParams != -1 ) |
63 | 0 | ? rURI.copy( 0, nParams ) |
64 | 0 | : ( nFragment != -1 ) |
65 | 0 | ? rURI.copy( 0, nFragment ) |
66 | 0 | : rURI; |
67 | |
|
68 | 0 | if ( aFragment.getLength() > 1 ) |
69 | 0 | aFragment = |
70 | 0 | rtl::Uri::encode( aFragment, |
71 | 0 | rtl_UriCharClassUric, |
72 | 0 | rtl_UriEncodeKeepEscapes, /* #i81690# */ |
73 | 0 | RTL_TEXTENCODING_UTF8 ); |
74 | |
|
75 | 0 | if ( aParams.getLength() > 1 ) |
76 | 0 | aParams = |
77 | 0 | rtl::Uri::encode( aParams, |
78 | 0 | rtl_UriCharClassUric, |
79 | 0 | rtl_UriEncodeKeepEscapes, /* #i81690# */ |
80 | 0 | RTL_TEXTENCODING_UTF8 ); |
81 | |
|
82 | 0 | OUStringBuffer aResult(256); |
83 | 0 | sal_Int32 nIndex = 0; |
84 | 0 | do |
85 | 0 | { |
86 | 0 | aResult.append( |
87 | 0 | rtl::Uri::encode( aURI.getToken( 0, '/', nIndex ), |
88 | 0 | rtl_UriCharClassPchar, |
89 | 0 | rtl_UriEncodeKeepEscapes, /* #i81690# */ |
90 | 0 | RTL_TEXTENCODING_UTF8 ) ); |
91 | 0 | if ( nIndex >= 0 ) |
92 | 0 | aResult.append( u'/' ); |
93 | 0 | } |
94 | 0 | while ( nIndex >= 0 ); |
95 | |
|
96 | 0 | if ( !aParams.isEmpty() ) |
97 | 0 | aResult.append( u"?" + aParams ); |
98 | |
|
99 | 0 | if ( !aFragment.isEmpty() ) |
100 | 0 | aResult.append( u"#" + aFragment ); |
101 | |
|
102 | 0 | return aResult.makeStringAndClear(); |
103 | 0 | } |
104 | | |
105 | | } // namespace |
106 | | |
107 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |