merged tag ooo/OOO330_m14
[LibreOffice.git] / cosv / source / storage / plocroot.cxx
blob441a241b3a6348815083f2a984dfaf4f587b27cc
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 #include <precomp.h>
29 #include <cosv/ploc.hxx>
31 // NOT FULLY DECLARED SERVICES
32 #include <ctype.h>
33 #include <cosv/bstream.hxx>
34 #include <cosv/csv_ostream.hxx>
37 namespace csv
39 namespace ploc
43 class UnixRootDir : public Root
45 public:
46 UnixRootDir();
48 virtual void Get(
49 ostream & o_rPath ) const;
50 virtual void Get(
51 bostream & o_rPath ) const;
52 virtual DYN Root * CreateCopy() const;
53 virtual const char *
54 OwnDelimiter() const;
57 class WorkingDir : public Root
59 public:
60 WorkingDir(
61 const char * i_sDelimiter = Delimiter() );
63 virtual void Get(
64 ostream & o_rPath ) const;
65 virtual void Get(
66 bostream & o_rPath ) const;
67 virtual DYN Root * CreateCopy() const;
68 virtual const char *
69 OwnDelimiter() const;
70 private:
71 String sOwnDelimiter;
74 class WinRootDir : public Root
76 public:
77 WinRootDir();
79 virtual void Get(
80 ostream & o_rPath ) const;
81 virtual void Get(
82 bostream & o_rPath ) const;
83 virtual DYN Root * CreateCopy() const;
84 virtual const char *
85 OwnDelimiter() const;
88 class WinDrive : public Root
90 public:
91 WinDrive(
92 char i_cDrive );
93 virtual void Get(
94 ostream & o_rPath ) const;
95 virtual void Get(
96 bostream & o_rPath ) const;
97 virtual DYN Root * CreateCopy() const;
98 virtual const char *
99 OwnDelimiter() const;
100 private:
101 char cDrive;
104 class WinDriveRootDir : public Root
106 public:
107 WinDriveRootDir(
108 const char * i_sPath );
109 WinDriveRootDir(
110 char i_cDrive );
112 virtual void Get(
113 ostream & o_rPath ) const;
114 virtual void Get(
115 bostream & o_rPath ) const;
116 virtual DYN Root * CreateCopy() const;
117 virtual const char *
118 OwnDelimiter() const;
119 private:
120 char cDrive;
123 class UNCRoot : public Root
125 public:
126 UNCRoot(
127 const char * i_sPath );
128 UNCRoot(
129 const String & i_sComputer,
130 const String & i_sEntryPt );
132 virtual void Get(
133 ostream & o_rPath ) const;
134 virtual void Get(
135 bostream & o_rPath ) const;
136 virtual DYN Root * CreateCopy() const;
137 virtual const char *
138 OwnDelimiter() const;
139 private:
140 String sComputer;
141 String sEntryPt;
144 class InvalidRoot : public Root
146 public:
147 virtual void Get(
148 ostream & o_rPath ) const;
149 virtual void Get(
150 bostream & o_rPath ) const;
151 virtual DYN Root * CreateCopy() const;
152 virtual const char *
153 OwnDelimiter() const;
157 DYN Root *
158 Create_WindowsRoot( const char * & o_sPathAfterRoot,
159 const char * i_sPath )
161 if (i_sPath[0] == '\\')
163 if (i_sPath[1] == '\\')
164 { // UNC path name
165 o_sPathAfterRoot = strchr(i_sPath+2,'\\');
166 if (o_sPathAfterRoot != 0)
168 o_sPathAfterRoot = strchr(o_sPathAfterRoot+1,'\\');
169 if (o_sPathAfterRoot != 0)
170 ++o_sPathAfterRoot;
171 return new UNCRoot(i_sPath);
173 return new InvalidRoot; // Incomplete UNC root.
175 else
177 o_sPathAfterRoot = i_sPath+1;
178 return new WinRootDir;
181 else if (i_sPath[1] == ':')
183 if ( i_sPath[2] == '\\')
185 o_sPathAfterRoot = i_sPath + 3;
186 return new WinDriveRootDir(i_sPath);
188 else
190 o_sPathAfterRoot = i_sPath + 2;
191 return new WinDrive(*i_sPath);
194 else
196 o_sPathAfterRoot = i_sPath;
197 return new WorkingDir("\\");
201 DYN Root *
202 Create_UnixRoot( const char * & o_sPathAfterRoot,
203 const char * i_sPath )
205 if (*i_sPath == '/')
207 o_sPathAfterRoot = i_sPath + 1;
208 return new UnixRootDir;
210 else //
212 o_sPathAfterRoot = i_sPath;
213 return new WorkingDir("/");
214 } // endif
218 //********************** Root ****************************//
220 Root::~Root()
225 DYN Root *
226 Root::Create_( const char * & o_sPathAfterRoot,
227 const char * i_sPath,
228 const char * i_sDelimiter )
230 if (i_sPath[0] == '.')
232 switch ( i_sPath[1] )
234 case '\0': o_sPathAfterRoot = i_sPath + 1;
235 break;
236 case '\\': o_sPathAfterRoot = i_sPath + 2;
237 break;
238 case '/': o_sPathAfterRoot = i_sPath + 2;
239 break;
240 case '.': o_sPathAfterRoot = i_sPath;
241 break;
242 default:
243 o_sPathAfterRoot = 0;
244 return new InvalidRoot;
245 } // end switch (i_sPath[1])
247 return new WorkingDir;
248 } // end if (i_sPath[0] == '.')
250 switch (*i_sDelimiter)
252 case '\\': return Create_WindowsRoot(o_sPathAfterRoot, i_sPath);
253 case '/': return Create_UnixRoot(o_sPathAfterRoot, i_sPath);
256 o_sPathAfterRoot = 0;
257 return new InvalidRoot;
262 //********************** UnixRootDir ****************************//
265 UnixRootDir::UnixRootDir()
269 void
270 UnixRootDir::Get( ostream & o_rPath ) const
272 o_rPath << '/';
275 void
276 UnixRootDir::Get( bostream & o_rPath ) const
278 o_rPath.write( "/", 1 );
281 DYN Root *
282 UnixRootDir::CreateCopy() const
284 return new UnixRootDir;
287 const char *
288 UnixRootDir::OwnDelimiter() const
290 return "/";
294 //********************** WorkingDir ****************************//
296 WorkingDir::WorkingDir( const char * i_sDelimiter )
297 : sOwnDelimiter(i_sDelimiter)
301 void
302 WorkingDir::Get( ostream & o_rPath ) const
304 o_rPath << '.' << sOwnDelimiter;
307 void
308 WorkingDir::Get( bostream & o_rPath ) const
310 o_rPath.write( ".", 1 );
311 o_rPath.write( sOwnDelimiter );
314 DYN Root *
315 WorkingDir::CreateCopy() const
317 return new WorkingDir(sOwnDelimiter);
320 const char *
321 WorkingDir::OwnDelimiter() const
323 return sOwnDelimiter;
327 //********************** WinRootDir ****************************//
329 WinRootDir::WinRootDir()
333 void
334 WinRootDir::Get( ostream & o_rPath ) const
336 o_rPath << '\\';
339 void
340 WinRootDir::Get( bostream & o_rPath ) const
342 o_rPath.write( "\\", 1 );
345 DYN Root *
346 WinRootDir::CreateCopy() const
348 return new WinRootDir;
351 const char *
352 WinRootDir::OwnDelimiter() const
354 return "\\";
358 //********************** WinDrive ****************************//
360 WinDrive::WinDrive( char i_cDrive )
361 : cDrive(static_cast< char >(toupper(i_cDrive)))
365 void
366 WinDrive::Get( ostream & o_rPath ) const
368 o_rPath << cDrive << ':';
371 void
372 WinDrive::Get( bostream & o_rPath ) const
374 static char buf_[3] = " :";
375 buf_[0] = cDrive;
376 o_rPath.write( &buf_[0], 2 );
379 DYN Root *
380 WinDrive::CreateCopy() const
382 return new WinDrive(cDrive);
385 const char *
386 WinDrive::OwnDelimiter() const
388 return "\\";
392 //********************** WinDriveRootDir ****************************//
394 WinDriveRootDir::WinDriveRootDir( const char * i_sPath )
395 : cDrive(static_cast< char >(toupper(*i_sPath)))
397 if ( 'A' > cDrive OR 'Z' < cDrive )
398 cDrive = 0;
401 WinDriveRootDir::WinDriveRootDir( char i_cDrive )
402 : cDrive(i_cDrive)
406 void
407 WinDriveRootDir::Get( ostream & o_rPath ) const
409 o_rPath << cDrive << ":\\";
412 void
413 WinDriveRootDir::Get( bostream & o_rPath ) const
415 static char buf_[4] = " :\\";
416 buf_[0] = cDrive;
417 o_rPath.write( &buf_[0], 3 );
420 DYN Root *
421 WinDriveRootDir::CreateCopy() const
423 return new WinDriveRootDir(cDrive);
426 const char *
427 WinDriveRootDir::OwnDelimiter() const
429 return "\\";
433 //********************** UNCRoot ****************************//
435 UNCRoot::UNCRoot( const char * i_sPath )
436 // : // sComputer,
437 // sEntryPt
439 const char * pRestPath = i_sPath + 2;
440 const char * pDirEnd = strchr(pRestPath, '\\');
441 csv_assert(pDirEnd != 0);
443 sComputer = String(pRestPath, pDirEnd - pRestPath);
444 pRestPath = pDirEnd+1;
445 pDirEnd = strchr(pRestPath, '\\');
447 if ( pDirEnd != 0 )
449 sEntryPt = String(pRestPath, pDirEnd - pRestPath);
451 else
453 sEntryPt = pRestPath;
457 UNCRoot::UNCRoot( const String & i_sComputer,
458 const String & i_sEntryPt )
459 : sComputer(i_sComputer),
460 sEntryPt(i_sEntryPt)
464 void
465 UNCRoot::Get( ostream & o_rPath ) const
467 o_rPath << "\\\\" << sComputer << '\\' << sEntryPt << "\\";
470 void
471 UNCRoot::Get( bostream & o_rPath ) const
473 o_rPath.write( "\\\\", 2 );
474 o_rPath.write( sComputer );
475 o_rPath.write( "\\", 1 );
476 o_rPath.write( sEntryPt );
477 o_rPath.write( "\\", 1 );
480 DYN Root *
481 UNCRoot::CreateCopy() const
483 return new UNCRoot(sComputer,sEntryPt);
486 const char *
487 UNCRoot::OwnDelimiter() const
489 return "\\";
494 //********************** InvalidRoot ****************************//
496 void
497 InvalidRoot::Get( ostream & ) const
501 void
502 InvalidRoot::Get( bostream & ) const
506 DYN Root *
507 InvalidRoot::CreateCopy() const
509 return new InvalidRoot;
512 const char *
513 InvalidRoot::OwnDelimiter() const
515 return 0;
521 } // namespace ploc
522 } // namespace csv