1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <toolkit/out_position.hxx>
24 // NOT FULLY DEFINED SERVICES
36 const int C_nAssumedMaxLinkLength
= 500;
43 move_ToParent( Node
* & io_node
,
46 for ( intt n
= 0; n
< i_levels
; ++n
)
48 csv_assert(io_node
!= 0);
49 io_node
= io_node
->Parent();
55 } // namepace anonymous
61 pDirectory(&Node::Null_())
66 Position::Position( Node
& i_directory
,
67 const String
& i_file
)
69 pDirectory(&i_directory
)
73 Position::Position( const Position
& i_directory
,
74 const String
& i_sDifferentFile
)
75 : sFile(i_sDifferentFile
),
76 pDirectory(i_directory
.pDirectory
)
87 Position::operator=( Node
& i_node
)
95 Position::operator+=( const String
& i_nodeName
)
97 csv_assert(pDirectory
!= 0);
99 pDirectory
= &pDirectory
->Provide_Child(i_nodeName
);
106 Position::operator-=( intt i_levels
)
108 csv_assert(pDirectory
!= 0);
110 for ( intt i
= i_levels
; i
> 0; --i
)
112 pDirectory
= pDirectory
->Parent();
115 pDirectory
= &Node::Null_();
125 Position::LinkToRoot() const
127 StreamLock
sl(C_nAssumedMaxLinkLength
);
128 return sl() << get_UpLink(Depth()) << c_str
;
132 Position::Get_LinkTo( StreamStr
& o_result
,
133 const Position
& i_destination
,
134 const String
& i_localLabel
) const
136 Node
* p1
= pDirectory
;
137 Node
* p2
= i_destination
.pDirectory
;
139 intt diff
= Depth() - i_destination
.Depth();
140 intt pathLength1
= 0;
141 intt pathLength2
= 0;
146 move_ToParent(p1
,pathLength1
);
151 move_ToParent(p2
,pathLength2
);
162 o_result
<< get_UpLink(pathLength1
);
163 i_destination
.pDirectory
->Get_Path(o_result
, pathLength2
);
164 o_result
<< i_destination
.sFile
;
165 if (i_localLabel
.length())
166 o_result
<< "#" << i_localLabel
;
170 Position::Get_LinkToRoot( StreamStr
& o_result
) const
172 o_result
<< get_UpLink(Depth());
176 Position::Set( Node
& i_node
,
177 const String
& i_file
)
180 pDirectory
= &i_node
;
187 get_UpLink(uintt i_depth
)
192 C_sUpLinkArray
[3*C_nMaxDepth
+1] =
193 "../../../../../../../../../../"
194 "../../../../../../../../../../"
195 "../../../../../../../../../../";
197 C_sUpLink
= &C_sUpLinkArray
[0];
199 if ( i_depth
<= C_nMaxDepth
)
201 return C_sUpLink
+ 3*(C_nMaxDepth
- i_depth
);
205 static std::vector
<char>
207 uintt nNeededSize
= i_depth
* 3 + 1;
209 if (aRet
.size() < nNeededSize
)
211 aRet
.resize(nNeededSize
);
212 char * pEnd
= &aRet
[nNeededSize
-1];
215 for ( char * pFill
= &(*aRet
.begin());
219 memcpy(pFill
, C_sUpLink
, 3);
223 return &aRet
[aRet
.size() - 1 - 3*i_depth
];
230 } // namespace output
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */