xref: /aoo41x/main/cosv/source/storage/plocroot.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 #include <precomp.h>
29 #include <cosv/ploc.hxx>
30 
31 // NOT FULLY DECLARED SERVICES
32 #include <ctype.h>
33 #include <cosv/bstream.hxx>
34 #include <cosv/csv_ostream.hxx>
35 
36 
37 namespace csv
38 {
39 namespace ploc
40 {
41 
42 
43 class UnixRootDir : public Root
44 {
45   public:
46                         UnixRootDir();
47 
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;
55 };
56 
57 class WorkingDir : public Root
58 {
59   public:
60                         WorkingDir(
61                             const char *        i_sDelimiter = Delimiter() );
62 
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;
72 };
73 
74 class WinRootDir : public Root
75 {
76   public:
77                         WinRootDir();
78 
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;
86 };
87 
88 class WinDrive : public Root
89 {
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;
102 };
103 
104 class WinDriveRootDir : public Root
105 {
106   public:
107                         WinDriveRootDir(
108                             const char *        i_sPath );
109                         WinDriveRootDir(
110                             char                i_cDrive );
111 
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;
121 };
122 
123 class UNCRoot : public Root
124 {
125   public:
126                         UNCRoot(
127                             const char *        i_sPath );
128                         UNCRoot(
129                             const String  &     i_sComputer,
130                             const String  &     i_sEntryPt );
131 
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;
142 };
143 
144 class InvalidRoot : public Root
145 {
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;
154 };
155 
156 
157 DYN Root *
158 Create_WindowsRoot( const char * &  o_sPathAfterRoot,
159                     const char *    i_sPath )
160 {
161     if (i_sPath[0] == '\\')
162     {
163      	if (i_sPath[1] == '\\')
164         {   // UNC path name
165          	o_sPathAfterRoot = strchr(i_sPath+2,'\\');
166             if (o_sPathAfterRoot != 0)
167             {
168                 o_sPathAfterRoot = strchr(o_sPathAfterRoot+1,'\\');
169                 if (o_sPathAfterRoot != 0)
170                     ++o_sPathAfterRoot;
171                 return new UNCRoot(i_sPath);
172             }
173             return new InvalidRoot;   // Incomplete UNC root.
174         }
175         else
176         {
177             o_sPathAfterRoot = i_sPath+1;
178          	return new WinRootDir;
179         }
180     }
181     else if (i_sPath[1] == ':')
182     {
183         if ( i_sPath[2] == '\\')
184         {
185             o_sPathAfterRoot = i_sPath + 3;
186             return new WinDriveRootDir(i_sPath);
187         }
188         else
189         {
190             o_sPathAfterRoot = i_sPath + 2;
191             return new WinDrive(*i_sPath);
192         }
193     }
194     else
195     {
196         o_sPathAfterRoot = i_sPath;
197         return new WorkingDir("\\");
198     }
199 }
200 
201 DYN Root *
202 Create_UnixRoot( const char * &     o_sPathAfterRoot,
203                  const char *       i_sPath )
204 {
205     if (*i_sPath == '/')
206     {
207         o_sPathAfterRoot = i_sPath + 1;
208         return new UnixRootDir;
209     }
210     else //
211     {
212         o_sPathAfterRoot = i_sPath;
213         return new WorkingDir("/");
214     }  // endif
215 }
216 
217 
218 //**********************    Root    ****************************//
219 
220 Root::~Root()
221 {
222 
223 }
224 
225 DYN Root *
226 Root::Create_( const char * &   o_sPathAfterRoot,
227                const char *     i_sPath,
228                const char *     i_sDelimiter )
229 {
230     if (i_sPath[0] == '.')
231     {
232         switch ( i_sPath[1] )
233         {
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])
246 
247         return new WorkingDir;
248     }   // end if (i_sPath[0] == '.')
249 
250     switch (*i_sDelimiter)
251     {
252      	case '\\':      return Create_WindowsRoot(o_sPathAfterRoot, i_sPath);
253         case '/':       return Create_UnixRoot(o_sPathAfterRoot, i_sPath);
254     }
255 
256     o_sPathAfterRoot = 0;
257     return new InvalidRoot;
258 }
259 
260 
261 
262 //**********************    UnixRootDir    ****************************//
263 
264 
265 UnixRootDir::UnixRootDir()
266 {
267 }
268 
269 void
270 UnixRootDir::Get( ostream      &      o_rPath ) const
271 {
272     o_rPath << '/';
273 }
274 
275 void
276 UnixRootDir::Get( bostream      &     o_rPath ) const
277 {
278     o_rPath.write( "/", 1 );
279 }
280 
281 DYN Root *
282 UnixRootDir::CreateCopy() const
283 {
284  	return new UnixRootDir;
285 }
286 
287 const char *
288 UnixRootDir::OwnDelimiter() const
289 {
290  	return "/";
291 }
292 
293 
294 //**********************    WorkingDir    ****************************//
295 
296 WorkingDir::WorkingDir( const char * i_sDelimiter )
297     :   sOwnDelimiter(i_sDelimiter)
298 {
299 }
300 
301 void
302 WorkingDir::Get( ostream      &      o_rPath ) const
303 {
304     o_rPath << '.' << sOwnDelimiter;
305 }
306 
307 void
308 WorkingDir::Get( bostream      &     o_rPath ) const
309 {
310     o_rPath.write( ".", 1 );
311     o_rPath.write( sOwnDelimiter );
312 }
313 
314 DYN Root *
315 WorkingDir::CreateCopy() const
316 {
317  	return new WorkingDir(sOwnDelimiter);
318 }
319 
320 const char *
321 WorkingDir::OwnDelimiter() const
322 {
323  	return sOwnDelimiter;
324 }
325 
326 
327 //**********************    WinRootDir    ****************************//
328 
329 WinRootDir::WinRootDir()
330 {
331 }
332 
333 void
334 WinRootDir::Get( ostream      &      o_rPath ) const
335 {
336     o_rPath << '\\';
337 }
338 
339 void
340 WinRootDir::Get( bostream      &     o_rPath ) const
341 {
342     o_rPath.write( "\\", 1 );
343 }
344 
345 DYN Root *
346 WinRootDir::CreateCopy() const
347 {
348  	return new WinRootDir;
349 }
350 
351 const char *
352 WinRootDir::OwnDelimiter() const
353 {
354  	return "\\";
355 }
356 
357 
358 //**********************    WinDrive    ****************************//
359 
360 WinDrive::WinDrive( char i_cDrive )
361     :   cDrive(static_cast< char >(toupper(i_cDrive)))
362 {
363 }
364 
365 void
366 WinDrive::Get( ostream      &      o_rPath ) const
367 {
368     o_rPath << cDrive << ':';
369 }
370 
371 void
372 WinDrive::Get( bostream      &     o_rPath ) const
373 {
374     static char buf_[3] = " :";
375     buf_[0] = cDrive;
376     o_rPath.write( &buf_[0], 2 );
377 }
378 
379 DYN Root *
380 WinDrive::CreateCopy() const
381 {
382  	return new WinDrive(cDrive);
383 }
384 
385 const char *
386 WinDrive::OwnDelimiter() const
387 {
388  	return "\\";
389 }
390 
391 
392 //**********************    WinDriveRootDir    ****************************//
393 
394 WinDriveRootDir::WinDriveRootDir( const char * i_sPath )
395     :   cDrive(static_cast< char >(toupper(*i_sPath)))
396 {
397     if ( 'A' > cDrive OR 'Z' < cDrive )
398         cDrive = 0;
399 }
400 
401 WinDriveRootDir::WinDriveRootDir( char i_cDrive )
402     :   cDrive(i_cDrive)
403 {
404 }
405 
406 void
407 WinDriveRootDir::Get( ostream      &      o_rPath ) const
408 {
409     o_rPath << cDrive << ":\\";
410 }
411 
412 void
413 WinDriveRootDir::Get( bostream      &     o_rPath ) const
414 {
415     static char buf_[4] = " :\\";
416     buf_[0] = cDrive;
417     o_rPath.write( &buf_[0], 3 );
418 }
419 
420 DYN Root *
421 WinDriveRootDir::CreateCopy() const
422 {
423  	return new WinDriveRootDir(cDrive);
424 }
425 
426 const char *
427 WinDriveRootDir::OwnDelimiter() const
428 {
429  	return "\\";
430 }
431 
432 
433 //**********************    UNCRoot    ****************************//
434 
435 UNCRoot::UNCRoot( const char * i_sPath )
436 //  :   // sComputer,
437         // sEntryPt
438 {
439     const char * pRestPath = i_sPath + 2;
440     const char * pDirEnd = strchr(pRestPath, '\\');
441     csv_assert(pDirEnd != 0);
442 
443     sComputer = String(pRestPath, pDirEnd - pRestPath);
444     pRestPath = pDirEnd+1;
445     pDirEnd = strchr(pRestPath, '\\');
446 
447     if ( pDirEnd != 0 )
448     {
449         sEntryPt = String(pRestPath, pDirEnd - pRestPath);
450     }
451     else
452     {
453         sEntryPt = pRestPath;
454     }
455 }
456 
457 UNCRoot::UNCRoot( const String  &     i_sComputer,
458                   const String  &     i_sEntryPt )
459     :   sComputer(i_sComputer),
460         sEntryPt(i_sEntryPt)
461 {
462 }
463 
464 void
465 UNCRoot::Get( ostream      & o_rPath ) const
466 {
467     o_rPath << "\\\\" << sComputer << '\\' << sEntryPt << "\\";
468 }
469 
470 void
471 UNCRoot::Get( bostream      &     o_rPath ) const
472 {
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 );
478 }
479 
480 DYN Root *
481 UNCRoot::CreateCopy() const
482 {
483  	return new UNCRoot(sComputer,sEntryPt);
484 }
485 
486 const char *
487 UNCRoot::OwnDelimiter() const
488 {
489  	return "\\";
490 }
491 
492 
493 
494 //**********************    InvalidRoot    ****************************//
495 
496 void
497 InvalidRoot::Get( ostream      & ) const
498 {
499 }
500 
501 void
502 InvalidRoot::Get( bostream      & ) const
503 {
504 }
505 
506 DYN Root *
507 InvalidRoot::CreateCopy() const
508 {
509  	return new InvalidRoot;
510 }
511 
512 const char *
513 InvalidRoot::OwnDelimiter() const
514 {
515     return 0;
516 }
517 
518 
519 
520 
521 } // namespace ploc
522 } // namespace csv
523 
524 
525 
526