1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: out_position.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 #include <toolkit/out_position.hxx>
35 // NOT FULLY DEFINED SERVICES
47 const int C_nAssumedMaxLinkLength
= 500;
54 move_ToParent( Node
* & io_node
,
57 for ( intt n
= 0; n
< i_levels
; ++n
)
59 csv_assert(io_node
!= 0);
60 io_node
= io_node
->Parent();
66 } // namepace anonymous
72 pDirectory(&Node::Null_())
77 Position::Position( Node
& i_directory
,
78 const String
& i_file
)
80 pDirectory(&i_directory
)
84 Position::Position( const Position
& i_directory
,
85 const String
& i_sDifferentFile
)
86 : sFile(i_sDifferentFile
),
87 pDirectory(i_directory
.pDirectory
)
98 Position::operator=( Node
& i_node
)
100 pDirectory
= &i_node
;
106 Position::operator+=( const String
& i_nodeName
)
108 csv_assert(pDirectory
!= 0);
110 pDirectory
= &pDirectory
->Provide_Child(i_nodeName
);
117 Position::operator-=( intt i_levels
)
119 csv_assert(pDirectory
!= 0);
121 for ( intt i
= i_levels
; i
> 0; --i
)
123 pDirectory
= pDirectory
->Parent();
126 pDirectory
= &Node::Null_();
136 Position::LinkToRoot( const String
& ) const
138 StreamLock
sl(C_nAssumedMaxLinkLength
);
139 return sl() << get_UpLink(Depth()) << c_str
;
143 Position::Get_LinkTo( StreamStr
& o_result
,
144 const Position
& i_destination
,
145 const String
& i_localLabel
) const
147 Node
* p1
= pDirectory
;
148 Node
* p2
= i_destination
.pDirectory
;
150 intt diff
= Depth() - i_destination
.Depth();
151 intt pathLength1
= 0;
152 intt pathLength2
= 0;
157 move_ToParent(p1
,pathLength1
);
162 move_ToParent(p2
,pathLength2
);
173 o_result
<< get_UpLink(pathLength1
);
174 i_destination
.pDirectory
->Get_Path(o_result
, pathLength2
);
175 o_result
<< i_destination
.sFile
;
176 if (i_localLabel
.length())
177 o_result
<< "#" << i_localLabel
;
181 Position::Get_LinkToRoot( StreamStr
& o_result
,
182 const String
& ) const
184 o_result
<< get_UpLink(Depth());
188 Position::Set( Node
& i_node
,
189 const String
& i_file
)
192 pDirectory
= &i_node
;
199 get_UpLink(uintt i_depth
)
204 C_sUpLinkArray
[3*C_nMaxDepth
+1] =
205 "../../../../../../../../../../"
206 "../../../../../../../../../../"
207 "../../../../../../../../../../";
209 C_sUpLink
= &C_sUpLinkArray
[0];
211 if ( i_depth
<= C_nMaxDepth
)
213 return C_sUpLink
+ 3*(C_nMaxDepth
- i_depth
);
217 static std::vector
<char>
219 uintt nNeededSize
= i_depth
* 3 + 1;
221 if (aRet
.size() < nNeededSize
)
223 aRet
.resize(nNeededSize
);
224 char * pEnd
= &aRet
[nNeededSize
-1];
227 for ( char * pFill
= &(*aRet
.begin());
231 memcpy(pFill
, C_sUpLink
, 3);
235 return &aRet
[aRet
.size() - 1 - 3*i_depth
];
242 } // namespace output