LibreOffice Module ucb (master)  1
urihelper.hxx
Go to the documentation of this file.
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 
28 
29  inline OUString encodeSegment( const OUString & rSegment )
30  {
31  return rtl::Uri::encode( rSegment,
32  rtl_UriCharClassPchar,
33  rtl_UriEncodeIgnoreEscapes,
34  RTL_TEXTENCODING_UTF8 );
35  }
36 
37  inline OUString decodeSegment( const OUString& rSegment )
38  {
39  return rtl::Uri::decode( rSegment,
40  rtl_UriDecodeWithCharset,
41  RTL_TEXTENCODING_UTF8 );
42  }
43 
44  inline OUString encodeURI( const OUString & rURI )
45  {
46  OUString aFragment;
47  OUString aParams;
48  OUString aURI;
49 
50  sal_Int32 nFragment = rURI.lastIndexOf( u'#' );
51  if ( nFragment != -1 )
52  aFragment = rURI.copy( nFragment + 1 );
53 
54  sal_Int32 nParams = ( nFragment == -1 )
55  ? rURI.lastIndexOf( u'?' )
56  : rURI.lastIndexOf( u'?', nFragment );
57  if ( nParams != -1 )
58  aParams = ( nFragment == -1 )
59  ? rURI.copy( nParams + 1 )
60  : rURI.copy( nParams + 1, nFragment - nParams - 1 );
61 
62  aURI = ( nParams != -1 )
63  ? rURI.copy( 0, nParams )
64  : ( nFragment != -1 )
65  ? rURI.copy( 0, nFragment )
66  : rURI;
67 
68  if ( aFragment.getLength() > 1 )
69  aFragment =
70  rtl::Uri::encode( aFragment,
71  rtl_UriCharClassUric,
72  rtl_UriEncodeKeepEscapes, /* #i81690# */
73  RTL_TEXTENCODING_UTF8 );
74 
75  if ( aParams.getLength() > 1 )
76  aParams =
77  rtl::Uri::encode( aParams,
78  rtl_UriCharClassUric,
79  rtl_UriEncodeKeepEscapes, /* #i81690# */
80  RTL_TEXTENCODING_UTF8 );
81 
82  OUStringBuffer aResult(256);
83  sal_Int32 nIndex = 0;
84  do
85  {
86  aResult.append(
87  rtl::Uri::encode( aURI.getToken( 0, '/', nIndex ),
88  rtl_UriCharClassPchar,
89  rtl_UriEncodeKeepEscapes, /* #i81690# */
90  RTL_TEXTENCODING_UTF8 ) );
91  if ( nIndex >= 0 )
92  aResult.append( u'/' );
93  }
94  while ( nIndex >= 0 );
95 
96  if ( !aParams.isEmpty() )
97  {
98  aResult.append( u'?' );
99  aResult.append( aParams );
100  }
101 
102  if ( !aFragment.isEmpty() )
103  {
104  aResult.append( u'#' );
105  aResult.append( aFragment );
106  }
107 
108  return aResult.makeStringAndClear();
109  }
110 
111 } // namespace
112 
113 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 nIndex
OUString encodeURI(const OUString &rURI)
Definition: urihelper.hxx:44
OUString encodeSegment(const OUString &rSegment)
Definition: urihelper.hxx:29
OUString decodeSegment(const OUString &rSegment)
Definition: urihelper.hxx:37
float u