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