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: plocroot.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 <cosv/ploc.hxx>
34 // NOT FULLY DECLARED SERVICES
36 #include <cosv/bstream.hxx>
37 #include <cosv/csv_ostream.hxx>
46 class UnixRootDir
: public Root
52 ostream
& o_rPath
) const;
54 bostream
& o_rPath
) const;
55 virtual DYN Root
* CreateCopy() const;
60 class WorkingDir
: public Root
64 const char * i_sDelimiter
= Delimiter() );
67 ostream
& o_rPath
) const;
69 bostream
& o_rPath
) const;
70 virtual DYN Root
* CreateCopy() const;
77 class WinRootDir
: public Root
83 ostream
& o_rPath
) const;
85 bostream
& o_rPath
) const;
86 virtual DYN Root
* CreateCopy() const;
91 class WinDrive
: public Root
97 ostream
& o_rPath
) const;
99 bostream
& o_rPath
) const;
100 virtual DYN Root
* CreateCopy() const;
102 OwnDelimiter() const;
107 class WinDriveRootDir
: public Root
111 const char * i_sPath
);
116 ostream
& o_rPath
) const;
118 bostream
& o_rPath
) const;
119 virtual DYN Root
* CreateCopy() const;
121 OwnDelimiter() const;
126 class UNCRoot
: public Root
130 const char * i_sPath
);
132 const String
& i_sComputer
,
133 const String
& i_sEntryPt
);
136 ostream
& o_rPath
) const;
138 bostream
& o_rPath
) const;
139 virtual DYN Root
* CreateCopy() const;
141 OwnDelimiter() const;
147 class InvalidRoot
: public Root
151 ostream
& o_rPath
) const;
153 bostream
& o_rPath
) const;
154 virtual DYN Root
* CreateCopy() const;
156 OwnDelimiter() const;
161 Create_WindowsRoot( const char * & o_sPathAfterRoot
,
162 const char * i_sPath
)
164 if (i_sPath
[0] == '\\')
166 if (i_sPath
[1] == '\\')
168 o_sPathAfterRoot
= strchr(i_sPath
+2,'\\');
169 if (o_sPathAfterRoot
!= 0)
171 o_sPathAfterRoot
= strchr(o_sPathAfterRoot
+1,'\\');
172 if (o_sPathAfterRoot
!= 0)
174 return new UNCRoot(i_sPath
);
176 return new InvalidRoot
; // Incomplete UNC root.
180 o_sPathAfterRoot
= i_sPath
+1;
181 return new WinRootDir
;
184 else if (i_sPath
[1] == ':')
186 if ( i_sPath
[2] == '\\')
188 o_sPathAfterRoot
= i_sPath
+ 3;
189 return new WinDriveRootDir(i_sPath
);
193 o_sPathAfterRoot
= i_sPath
+ 2;
194 return new WinDrive(*i_sPath
);
199 o_sPathAfterRoot
= i_sPath
;
200 return new WorkingDir("\\");
205 Create_UnixRoot( const char * & o_sPathAfterRoot
,
206 const char * i_sPath
)
210 o_sPathAfterRoot
= i_sPath
+ 1;
211 return new UnixRootDir
;
215 o_sPathAfterRoot
= i_sPath
;
216 return new WorkingDir("/");
221 //********************** Root ****************************//
229 Root::Create_( const char * & o_sPathAfterRoot
,
230 const char * i_sPath
,
231 const char * i_sDelimiter
)
233 if (i_sPath
[0] == '.')
235 switch ( i_sPath
[1] )
237 case '\0': o_sPathAfterRoot
= i_sPath
+ 1;
239 case '\\': o_sPathAfterRoot
= i_sPath
+ 2;
241 case '/': o_sPathAfterRoot
= i_sPath
+ 2;
243 case '.': o_sPathAfterRoot
= i_sPath
;
246 o_sPathAfterRoot
= 0;
247 return new InvalidRoot
;
248 } // end switch (i_sPath[1])
250 return new WorkingDir
;
251 } // end if (i_sPath[0] == '.')
253 switch (*i_sDelimiter
)
255 case '\\': return Create_WindowsRoot(o_sPathAfterRoot
, i_sPath
);
256 case '/': return Create_UnixRoot(o_sPathAfterRoot
, i_sPath
);
259 o_sPathAfterRoot
= 0;
260 return new InvalidRoot
;
265 //********************** UnixRootDir ****************************//
268 UnixRootDir::UnixRootDir()
273 UnixRootDir::Get( ostream
& o_rPath
) const
279 UnixRootDir::Get( bostream
& o_rPath
) const
281 o_rPath
.write( "/", 1 );
285 UnixRootDir::CreateCopy() const
287 return new UnixRootDir
;
291 UnixRootDir::OwnDelimiter() const
297 //********************** WorkingDir ****************************//
299 WorkingDir::WorkingDir( const char * i_sDelimiter
)
300 : sOwnDelimiter(i_sDelimiter
)
305 WorkingDir::Get( ostream
& o_rPath
) const
307 o_rPath
<< '.' << sOwnDelimiter
;
311 WorkingDir::Get( bostream
& o_rPath
) const
313 o_rPath
.write( ".", 1 );
314 o_rPath
.write( sOwnDelimiter
);
318 WorkingDir::CreateCopy() const
320 return new WorkingDir(sOwnDelimiter
);
324 WorkingDir::OwnDelimiter() const
326 return sOwnDelimiter
;
330 //********************** WinRootDir ****************************//
332 WinRootDir::WinRootDir()
337 WinRootDir::Get( ostream
& o_rPath
) const
343 WinRootDir::Get( bostream
& o_rPath
) const
345 o_rPath
.write( "\\", 1 );
349 WinRootDir::CreateCopy() const
351 return new WinRootDir
;
355 WinRootDir::OwnDelimiter() const
361 //********************** WinDrive ****************************//
363 WinDrive::WinDrive( char i_cDrive
)
364 : cDrive(static_cast< char >(toupper(i_cDrive
)))
369 WinDrive::Get( ostream
& o_rPath
) const
371 o_rPath
<< cDrive
<< ':';
375 WinDrive::Get( bostream
& o_rPath
) const
377 static char buf_
[3] = " :";
379 o_rPath
.write( &buf_
[0], 2 );
383 WinDrive::CreateCopy() const
385 return new WinDrive(cDrive
);
389 WinDrive::OwnDelimiter() const
395 //********************** WinDriveRootDir ****************************//
397 WinDriveRootDir::WinDriveRootDir( const char * i_sPath
)
398 : cDrive(static_cast< char >(toupper(*i_sPath
)))
400 if ( 'A' > cDrive OR
'Z' < cDrive
)
404 WinDriveRootDir::WinDriveRootDir( char i_cDrive
)
410 WinDriveRootDir::Get( ostream
& o_rPath
) const
412 o_rPath
<< cDrive
<< ":\\";
416 WinDriveRootDir::Get( bostream
& o_rPath
) const
418 static char buf_
[4] = " :\\";
420 o_rPath
.write( &buf_
[0], 3 );
424 WinDriveRootDir::CreateCopy() const
426 return new WinDriveRootDir(cDrive
);
430 WinDriveRootDir::OwnDelimiter() const
436 //********************** UNCRoot ****************************//
438 UNCRoot::UNCRoot( const char * i_sPath
)
442 const char * pRestPath
= i_sPath
+ 2;
443 const char * pDirEnd
= strchr(pRestPath
, '\\');
444 csv_assert(pDirEnd
!= 0);
446 sComputer
= String(pRestPath
, pDirEnd
- pRestPath
);
447 pRestPath
= pDirEnd
+1;
448 pDirEnd
= strchr(pRestPath
, '\\');
452 sEntryPt
= String(pRestPath
, pDirEnd
- pRestPath
);
456 sEntryPt
= pRestPath
;
460 UNCRoot::UNCRoot( const String
& i_sComputer
,
461 const String
& i_sEntryPt
)
462 : sComputer(i_sComputer
),
468 UNCRoot::Get( ostream
& o_rPath
) const
470 o_rPath
<< "\\\\" << sComputer
<< '\\' << sEntryPt
<< "\\";
474 UNCRoot::Get( bostream
& o_rPath
) const
476 o_rPath
.write( "\\\\", 2 );
477 o_rPath
.write( sComputer
);
478 o_rPath
.write( "\\", 1 );
479 o_rPath
.write( sEntryPt
);
480 o_rPath
.write( "\\", 1 );
484 UNCRoot::CreateCopy() const
486 return new UNCRoot(sComputer
,sEntryPt
);
490 UNCRoot::OwnDelimiter() const
497 //********************** InvalidRoot ****************************//
500 InvalidRoot::Get( ostream
& ) const
505 InvalidRoot::Get( bostream
& ) const
510 InvalidRoot::CreateCopy() const
512 return new InvalidRoot
;
516 InvalidRoot::OwnDelimiter() const