merge the formfield patch from ooo-build
[ooovba.git] / cosv / source / storage / plocroot.cxx
blobee261b9ac768786091e48dce381802c503cdd2fc
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: plocroot.cxx,v $
10 * $Revision: 1.6 $
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 <cosv/ploc.hxx>
34 // NOT FULLY DECLARED SERVICES
35 #include <ctype.h>
36 #include <cosv/bstream.hxx>
37 #include <cosv/csv_ostream.hxx>
40 namespace csv
42 namespace ploc
46 class UnixRootDir : public Root
48 public:
49 UnixRootDir();
51 virtual void Get(
52 ostream & o_rPath ) const;
53 virtual void Get(
54 bostream & o_rPath ) const;
55 virtual DYN Root * CreateCopy() const;
56 virtual const char *
57 OwnDelimiter() const;
60 class WorkingDir : public Root
62 public:
63 WorkingDir(
64 const char * i_sDelimiter = Delimiter() );
66 virtual void Get(
67 ostream & o_rPath ) const;
68 virtual void Get(
69 bostream & o_rPath ) const;
70 virtual DYN Root * CreateCopy() const;
71 virtual const char *
72 OwnDelimiter() const;
73 private:
74 String sOwnDelimiter;
77 class WinRootDir : public Root
79 public:
80 WinRootDir();
82 virtual void Get(
83 ostream & o_rPath ) const;
84 virtual void Get(
85 bostream & o_rPath ) const;
86 virtual DYN Root * CreateCopy() const;
87 virtual const char *
88 OwnDelimiter() const;
91 class WinDrive : public Root
93 public:
94 WinDrive(
95 char i_cDrive );
96 virtual void Get(
97 ostream & o_rPath ) const;
98 virtual void Get(
99 bostream & o_rPath ) const;
100 virtual DYN Root * CreateCopy() const;
101 virtual const char *
102 OwnDelimiter() const;
103 private:
104 char cDrive;
107 class WinDriveRootDir : public Root
109 public:
110 WinDriveRootDir(
111 const char * i_sPath );
112 WinDriveRootDir(
113 char i_cDrive );
115 virtual void Get(
116 ostream & o_rPath ) const;
117 virtual void Get(
118 bostream & o_rPath ) const;
119 virtual DYN Root * CreateCopy() const;
120 virtual const char *
121 OwnDelimiter() const;
122 private:
123 char cDrive;
126 class UNCRoot : public Root
128 public:
129 UNCRoot(
130 const char * i_sPath );
131 UNCRoot(
132 const String & i_sComputer,
133 const String & i_sEntryPt );
135 virtual void Get(
136 ostream & o_rPath ) const;
137 virtual void Get(
138 bostream & o_rPath ) const;
139 virtual DYN Root * CreateCopy() const;
140 virtual const char *
141 OwnDelimiter() const;
142 private:
143 String sComputer;
144 String sEntryPt;
147 class InvalidRoot : public Root
149 public:
150 virtual void Get(
151 ostream & o_rPath ) const;
152 virtual void Get(
153 bostream & o_rPath ) const;
154 virtual DYN Root * CreateCopy() const;
155 virtual const char *
156 OwnDelimiter() const;
160 DYN Root *
161 Create_WindowsRoot( const char * & o_sPathAfterRoot,
162 const char * i_sPath )
164 if (i_sPath[0] == '\\')
166 if (i_sPath[1] == '\\')
167 { // UNC path name
168 o_sPathAfterRoot = strchr(i_sPath+2,'\\');
169 if (o_sPathAfterRoot != 0)
171 o_sPathAfterRoot = strchr(o_sPathAfterRoot+1,'\\');
172 if (o_sPathAfterRoot != 0)
173 ++o_sPathAfterRoot;
174 return new UNCRoot(i_sPath);
176 return new InvalidRoot; // Incomplete UNC root.
178 else
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);
191 else
193 o_sPathAfterRoot = i_sPath + 2;
194 return new WinDrive(*i_sPath);
197 else
199 o_sPathAfterRoot = i_sPath;
200 return new WorkingDir("\\");
204 DYN Root *
205 Create_UnixRoot( const char * & o_sPathAfterRoot,
206 const char * i_sPath )
208 if (*i_sPath == '/')
210 o_sPathAfterRoot = i_sPath + 1;
211 return new UnixRootDir;
213 else //
215 o_sPathAfterRoot = i_sPath;
216 return new WorkingDir("/");
217 } // endif
221 //********************** Root ****************************//
223 Root::~Root()
228 DYN 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;
238 break;
239 case '\\': o_sPathAfterRoot = i_sPath + 2;
240 break;
241 case '/': o_sPathAfterRoot = i_sPath + 2;
242 break;
243 case '.': o_sPathAfterRoot = i_sPath;
244 break;
245 default:
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()
272 void
273 UnixRootDir::Get( ostream & o_rPath ) const
275 o_rPath << '/';
278 void
279 UnixRootDir::Get( bostream & o_rPath ) const
281 o_rPath.write( "/", 1 );
284 DYN Root *
285 UnixRootDir::CreateCopy() const
287 return new UnixRootDir;
290 const char *
291 UnixRootDir::OwnDelimiter() const
293 return "/";
297 //********************** WorkingDir ****************************//
299 WorkingDir::WorkingDir( const char * i_sDelimiter )
300 : sOwnDelimiter(i_sDelimiter)
304 void
305 WorkingDir::Get( ostream & o_rPath ) const
307 o_rPath << '.' << sOwnDelimiter;
310 void
311 WorkingDir::Get( bostream & o_rPath ) const
313 o_rPath.write( ".", 1 );
314 o_rPath.write( sOwnDelimiter );
317 DYN Root *
318 WorkingDir::CreateCopy() const
320 return new WorkingDir(sOwnDelimiter);
323 const char *
324 WorkingDir::OwnDelimiter() const
326 return sOwnDelimiter;
330 //********************** WinRootDir ****************************//
332 WinRootDir::WinRootDir()
336 void
337 WinRootDir::Get( ostream & o_rPath ) const
339 o_rPath << '\\';
342 void
343 WinRootDir::Get( bostream & o_rPath ) const
345 o_rPath.write( "\\", 1 );
348 DYN Root *
349 WinRootDir::CreateCopy() const
351 return new WinRootDir;
354 const char *
355 WinRootDir::OwnDelimiter() const
357 return "\\";
361 //********************** WinDrive ****************************//
363 WinDrive::WinDrive( char i_cDrive )
364 : cDrive(static_cast< char >(toupper(i_cDrive)))
368 void
369 WinDrive::Get( ostream & o_rPath ) const
371 o_rPath << cDrive << ':';
374 void
375 WinDrive::Get( bostream & o_rPath ) const
377 static char buf_[3] = " :";
378 buf_[0] = cDrive;
379 o_rPath.write( &buf_[0], 2 );
382 DYN Root *
383 WinDrive::CreateCopy() const
385 return new WinDrive(cDrive);
388 const char *
389 WinDrive::OwnDelimiter() const
391 return "\\";
395 //********************** WinDriveRootDir ****************************//
397 WinDriveRootDir::WinDriveRootDir( const char * i_sPath )
398 : cDrive(static_cast< char >(toupper(*i_sPath)))
400 if ( 'A' > cDrive OR 'Z' < cDrive )
401 cDrive = 0;
404 WinDriveRootDir::WinDriveRootDir( char i_cDrive )
405 : cDrive(i_cDrive)
409 void
410 WinDriveRootDir::Get( ostream & o_rPath ) const
412 o_rPath << cDrive << ":\\";
415 void
416 WinDriveRootDir::Get( bostream & o_rPath ) const
418 static char buf_[4] = " :\\";
419 buf_[0] = cDrive;
420 o_rPath.write( &buf_[0], 3 );
423 DYN Root *
424 WinDriveRootDir::CreateCopy() const
426 return new WinDriveRootDir(cDrive);
429 const char *
430 WinDriveRootDir::OwnDelimiter() const
432 return "\\";
436 //********************** UNCRoot ****************************//
438 UNCRoot::UNCRoot( const char * i_sPath )
439 // : // sComputer,
440 // sEntryPt
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, '\\');
450 if ( pDirEnd != 0 )
452 sEntryPt = String(pRestPath, pDirEnd - pRestPath);
454 else
456 sEntryPt = pRestPath;
460 UNCRoot::UNCRoot( const String & i_sComputer,
461 const String & i_sEntryPt )
462 : sComputer(i_sComputer),
463 sEntryPt(i_sEntryPt)
467 void
468 UNCRoot::Get( ostream & o_rPath ) const
470 o_rPath << "\\\\" << sComputer << '\\' << sEntryPt << "\\";
473 void
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 );
483 DYN Root *
484 UNCRoot::CreateCopy() const
486 return new UNCRoot(sComputer,sEntryPt);
489 const char *
490 UNCRoot::OwnDelimiter() const
492 return "\\";
497 //********************** InvalidRoot ****************************//
499 void
500 InvalidRoot::Get( ostream & ) const
504 void
505 InvalidRoot::Get( bostream & ) const
509 DYN Root *
510 InvalidRoot::CreateCopy() const
512 return new InvalidRoot;
515 const char *
516 InvalidRoot::OwnDelimiter() const
518 return 0;
524 } // namespace ploc
525 } // namespace csv