Update ooo320-m1
[ooovba.git] / autodoc / source / display / toolkit / out_position.cxx
blob8ee7de391724025dd1724404a8be3681b71d9755
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: out_position.cxx,v $
10 * $Revision: 1.9 $
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 ************************************************************************/
31 #include <precomp.h>
32 #include <toolkit/out_position.hxx>
35 // NOT FULLY DEFINED SERVICES
39 namespace output
44 namespace
47 const int C_nAssumedMaxLinkLength = 500;
49 void move_ToParent(
50 Node * & io_node,
51 intt i_levels = 1 );
53 void
54 move_ToParent( Node * & io_node,
55 intt i_levels )
57 for ( intt n = 0; n < i_levels; ++n )
59 csv_assert(io_node != 0);
60 io_node = io_node->Parent();
66 } // namepace anonymous
70 Position::Position()
71 : sFile(),
72 pDirectory(&Node::Null_())
77 Position::Position( Node & i_directory,
78 const String & i_file )
79 : sFile(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)
92 Position::~Position()
97 Position &
98 Position::operator=( Node & i_node )
100 pDirectory = &i_node;
101 sFile.clear();
102 return *this;
105 Position &
106 Position::operator+=( const String & i_nodeName )
108 csv_assert(pDirectory != 0);
110 pDirectory = &pDirectory->Provide_Child(i_nodeName);
111 sFile.clear();
113 return *this;
116 Position &
117 Position::operator-=( intt i_levels )
119 csv_assert(pDirectory != 0);
121 for ( intt i = i_levels; i > 0; --i )
123 pDirectory = pDirectory->Parent();
124 if (pDirectory == 0)
126 pDirectory = &Node::Null_();
127 i = 0;
130 sFile.clear();
132 return *this;
135 String
136 Position::LinkToRoot( const String & ) const
138 StreamLock sl(C_nAssumedMaxLinkLength);
139 return sl() << get_UpLink(Depth()) << c_str;
142 void
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;
154 if ( diff > 0 )
156 pathLength1 = diff;
157 move_ToParent(p1,pathLength1);
159 else if ( diff < 0 )
161 pathLength2 = -diff;
162 move_ToParent(p2,pathLength2);
165 while ( p1 != p2 )
167 move_ToParent(p1);
168 move_ToParent(p2);
169 ++pathLength1;
170 ++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;
180 void
181 Position::Get_LinkToRoot( StreamStr & o_result,
182 const String & ) const
184 o_result << get_UpLink(Depth());
187 void
188 Position::Set( Node & i_node,
189 const String & i_file )
191 sFile = i_file;
192 pDirectory = &i_node;
198 const char *
199 get_UpLink(uintt i_depth)
201 static const uintt
202 C_nMaxDepth = 30;
203 static const char
204 C_sUpLinkArray[3*C_nMaxDepth+1] =
205 "../../../../../../../../../../"
206 "../../../../../../../../../../"
207 "../../../../../../../../../../";
208 static const char *
209 C_sUpLink = &C_sUpLinkArray[0];
211 if ( i_depth <= C_nMaxDepth )
213 return C_sUpLink + 3*(C_nMaxDepth - i_depth);
215 else
216 { // not THREAD fast
217 static std::vector<char>
218 aRet;
219 uintt nNeededSize = i_depth * 3 + 1;
221 if (aRet.size() < nNeededSize)
223 aRet.resize(nNeededSize);
224 char * pEnd = &aRet[nNeededSize-1];
225 *pEnd = '\0';
227 for ( char * pFill = &(*aRet.begin());
228 pFill != pEnd;
229 pFill += 3 )
231 memcpy(pFill, C_sUpLink, 3);
233 } // end if
235 return &aRet[aRet.size() - 1 - 3*i_depth];
242 } // namespace output