1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 #include <precomp.h> 25 #include <cosv/ploc.hxx> 26 27 // NOT FULLY DECLARED SERVICES 28 #include <ctype.h> 29 #include <cosv/bstream.hxx> 30 #include <cosv/csv_ostream.hxx> 31 32 33 namespace csv 34 { 35 namespace ploc 36 { 37 38 39 class UnixRootDir : public Root 40 { 41 public: 42 UnixRootDir(); 43 44 virtual void Get( 45 ostream & o_rPath ) const; 46 virtual void Get( 47 bostream & o_rPath ) const; 48 virtual DYN Root * CreateCopy() const; 49 virtual const char * 50 OwnDelimiter() const; 51 }; 52 53 class WorkingDir : public Root 54 { 55 public: 56 WorkingDir( 57 const char * i_sDelimiter = Delimiter() ); 58 59 virtual void Get( 60 ostream & o_rPath ) const; 61 virtual void Get( 62 bostream & o_rPath ) const; 63 virtual DYN Root * CreateCopy() const; 64 virtual const char * 65 OwnDelimiter() const; 66 private: 67 String sOwnDelimiter; 68 }; 69 70 class WinRootDir : public Root 71 { 72 public: 73 WinRootDir(); 74 75 virtual void Get( 76 ostream & o_rPath ) const; 77 virtual void Get( 78 bostream & o_rPath ) const; 79 virtual DYN Root * CreateCopy() const; 80 virtual const char * 81 OwnDelimiter() const; 82 }; 83 84 class WinDrive : public Root 85 { 86 public: 87 WinDrive( 88 char i_cDrive ); 89 virtual void Get( 90 ostream & o_rPath ) const; 91 virtual void Get( 92 bostream & o_rPath ) const; 93 virtual DYN Root * CreateCopy() const; 94 virtual const char * 95 OwnDelimiter() const; 96 private: 97 char cDrive; 98 }; 99 100 class WinDriveRootDir : public Root 101 { 102 public: 103 WinDriveRootDir( 104 const char * i_sPath ); 105 WinDriveRootDir( 106 char i_cDrive ); 107 108 virtual void Get( 109 ostream & o_rPath ) const; 110 virtual void Get( 111 bostream & o_rPath ) const; 112 virtual DYN Root * CreateCopy() const; 113 virtual const char * 114 OwnDelimiter() const; 115 private: 116 char cDrive; 117 }; 118 119 class UNCRoot : public Root 120 { 121 public: 122 UNCRoot( 123 const char * i_sPath ); 124 UNCRoot( 125 const String & i_sComputer, 126 const String & i_sEntryPt ); 127 128 virtual void Get( 129 ostream & o_rPath ) const; 130 virtual void Get( 131 bostream & o_rPath ) const; 132 virtual DYN Root * CreateCopy() const; 133 virtual const char * 134 OwnDelimiter() const; 135 private: 136 String sComputer; 137 String sEntryPt; 138 }; 139 140 class InvalidRoot : public Root 141 { 142 public: 143 virtual void Get( 144 ostream & o_rPath ) const; 145 virtual void Get( 146 bostream & o_rPath ) const; 147 virtual DYN Root * CreateCopy() const; 148 virtual const char * 149 OwnDelimiter() const; 150 }; 151 152 153 DYN Root * 154 Create_WindowsRoot( const char * & o_sPathAfterRoot, 155 const char * i_sPath ) 156 { 157 if (i_sPath[0] == '\\') 158 { 159 if (i_sPath[1] == '\\') 160 { // UNC path name 161 o_sPathAfterRoot = strchr(i_sPath+2,'\\'); 162 if (o_sPathAfterRoot != 0) 163 { 164 o_sPathAfterRoot = strchr(o_sPathAfterRoot+1,'\\'); 165 if (o_sPathAfterRoot != 0) 166 ++o_sPathAfterRoot; 167 return new UNCRoot(i_sPath); 168 } 169 return new InvalidRoot; // Incomplete UNC root. 170 } 171 else 172 { 173 o_sPathAfterRoot = i_sPath+1; 174 return new WinRootDir; 175 } 176 } 177 else if (i_sPath[1] == ':') 178 { 179 if ( i_sPath[2] == '\\') 180 { 181 o_sPathAfterRoot = i_sPath + 3; 182 return new WinDriveRootDir(i_sPath); 183 } 184 else 185 { 186 o_sPathAfterRoot = i_sPath + 2; 187 return new WinDrive(*i_sPath); 188 } 189 } 190 else 191 { 192 o_sPathAfterRoot = i_sPath; 193 return new WorkingDir("\\"); 194 } 195 } 196 197 DYN Root * 198 Create_UnixRoot( const char * & o_sPathAfterRoot, 199 const char * i_sPath ) 200 { 201 if (*i_sPath == '/') 202 { 203 o_sPathAfterRoot = i_sPath + 1; 204 return new UnixRootDir; 205 } 206 else // 207 { 208 o_sPathAfterRoot = i_sPath; 209 return new WorkingDir("/"); 210 } // endif 211 } 212 213 214 //********************** Root ****************************// 215 216 Root::~Root() 217 { 218 219 } 220 221 DYN Root * 222 Root::Create_( const char * & o_sPathAfterRoot, 223 const char * i_sPath, 224 const char * i_sDelimiter ) 225 { 226 if (i_sPath[0] == '.') 227 { 228 switch ( i_sPath[1] ) 229 { 230 case '\0': o_sPathAfterRoot = i_sPath + 1; 231 break; 232 case '\\': o_sPathAfterRoot = i_sPath + 2; 233 break; 234 case '/': o_sPathAfterRoot = i_sPath + 2; 235 break; 236 case '.': o_sPathAfterRoot = i_sPath; 237 break; 238 default: 239 o_sPathAfterRoot = 0; 240 return new InvalidRoot; 241 } // end switch (i_sPath[1]) 242 243 return new WorkingDir; 244 } // end if (i_sPath[0] == '.') 245 246 switch (*i_sDelimiter) 247 { 248 case '\\': return Create_WindowsRoot(o_sPathAfterRoot, i_sPath); 249 case '/': return Create_UnixRoot(o_sPathAfterRoot, i_sPath); 250 } 251 252 o_sPathAfterRoot = 0; 253 return new InvalidRoot; 254 } 255 256 257 258 //********************** UnixRootDir ****************************// 259 260 261 UnixRootDir::UnixRootDir() 262 { 263 } 264 265 void 266 UnixRootDir::Get( ostream & o_rPath ) const 267 { 268 o_rPath << '/'; 269 } 270 271 void 272 UnixRootDir::Get( bostream & o_rPath ) const 273 { 274 o_rPath.write( "/", 1 ); 275 } 276 277 DYN Root * 278 UnixRootDir::CreateCopy() const 279 { 280 return new UnixRootDir; 281 } 282 283 const char * 284 UnixRootDir::OwnDelimiter() const 285 { 286 return "/"; 287 } 288 289 290 //********************** WorkingDir ****************************// 291 292 WorkingDir::WorkingDir( const char * i_sDelimiter ) 293 : sOwnDelimiter(i_sDelimiter) 294 { 295 } 296 297 void 298 WorkingDir::Get( ostream & o_rPath ) const 299 { 300 o_rPath << '.' << sOwnDelimiter; 301 } 302 303 void 304 WorkingDir::Get( bostream & o_rPath ) const 305 { 306 o_rPath.write( ".", 1 ); 307 o_rPath.write( sOwnDelimiter ); 308 } 309 310 DYN Root * 311 WorkingDir::CreateCopy() const 312 { 313 return new WorkingDir(sOwnDelimiter); 314 } 315 316 const char * 317 WorkingDir::OwnDelimiter() const 318 { 319 return sOwnDelimiter; 320 } 321 322 323 //********************** WinRootDir ****************************// 324 325 WinRootDir::WinRootDir() 326 { 327 } 328 329 void 330 WinRootDir::Get( ostream & o_rPath ) const 331 { 332 o_rPath << '\\'; 333 } 334 335 void 336 WinRootDir::Get( bostream & o_rPath ) const 337 { 338 o_rPath.write( "\\", 1 ); 339 } 340 341 DYN Root * 342 WinRootDir::CreateCopy() const 343 { 344 return new WinRootDir; 345 } 346 347 const char * 348 WinRootDir::OwnDelimiter() const 349 { 350 return "\\"; 351 } 352 353 354 //********************** WinDrive ****************************// 355 356 WinDrive::WinDrive( char i_cDrive ) 357 : cDrive(static_cast< char >(toupper(i_cDrive))) 358 { 359 } 360 361 void 362 WinDrive::Get( ostream & o_rPath ) const 363 { 364 o_rPath << cDrive << ':'; 365 } 366 367 void 368 WinDrive::Get( bostream & o_rPath ) const 369 { 370 static char buf_[3] = " :"; 371 buf_[0] = cDrive; 372 o_rPath.write( &buf_[0], 2 ); 373 } 374 375 DYN Root * 376 WinDrive::CreateCopy() const 377 { 378 return new WinDrive(cDrive); 379 } 380 381 const char * 382 WinDrive::OwnDelimiter() const 383 { 384 return "\\"; 385 } 386 387 388 //********************** WinDriveRootDir ****************************// 389 390 WinDriveRootDir::WinDriveRootDir( const char * i_sPath ) 391 : cDrive(static_cast< char >(toupper(*i_sPath))) 392 { 393 if ( 'A' > cDrive OR 'Z' < cDrive ) 394 cDrive = 0; 395 } 396 397 WinDriveRootDir::WinDriveRootDir( char i_cDrive ) 398 : cDrive(i_cDrive) 399 { 400 } 401 402 void 403 WinDriveRootDir::Get( ostream & o_rPath ) const 404 { 405 o_rPath << cDrive << ":\\"; 406 } 407 408 void 409 WinDriveRootDir::Get( bostream & o_rPath ) const 410 { 411 static char buf_[4] = " :\\"; 412 buf_[0] = cDrive; 413 o_rPath.write( &buf_[0], 3 ); 414 } 415 416 DYN Root * 417 WinDriveRootDir::CreateCopy() const 418 { 419 return new WinDriveRootDir(cDrive); 420 } 421 422 const char * 423 WinDriveRootDir::OwnDelimiter() const 424 { 425 return "\\"; 426 } 427 428 429 //********************** UNCRoot ****************************// 430 431 UNCRoot::UNCRoot( const char * i_sPath ) 432 // : // sComputer, 433 // sEntryPt 434 { 435 const char * pRestPath = i_sPath + 2; 436 const char * pDirEnd = strchr(pRestPath, '\\'); 437 csv_assert(pDirEnd != 0); 438 439 sComputer = String(pRestPath, pDirEnd - pRestPath); 440 pRestPath = pDirEnd+1; 441 pDirEnd = strchr(pRestPath, '\\'); 442 443 if ( pDirEnd != 0 ) 444 { 445 sEntryPt = String(pRestPath, pDirEnd - pRestPath); 446 } 447 else 448 { 449 sEntryPt = pRestPath; 450 } 451 } 452 453 UNCRoot::UNCRoot( const String & i_sComputer, 454 const String & i_sEntryPt ) 455 : sComputer(i_sComputer), 456 sEntryPt(i_sEntryPt) 457 { 458 } 459 460 void 461 UNCRoot::Get( ostream & o_rPath ) const 462 { 463 o_rPath << "\\\\" << sComputer << '\\' << sEntryPt << "\\"; 464 } 465 466 void 467 UNCRoot::Get( bostream & o_rPath ) const 468 { 469 o_rPath.write( "\\\\", 2 ); 470 o_rPath.write( sComputer ); 471 o_rPath.write( "\\", 1 ); 472 o_rPath.write( sEntryPt ); 473 o_rPath.write( "\\", 1 ); 474 } 475 476 DYN Root * 477 UNCRoot::CreateCopy() const 478 { 479 return new UNCRoot(sComputer,sEntryPt); 480 } 481 482 const char * 483 UNCRoot::OwnDelimiter() const 484 { 485 return "\\"; 486 } 487 488 489 490 //********************** InvalidRoot ****************************// 491 492 void 493 InvalidRoot::Get( ostream & ) const 494 { 495 } 496 497 void 498 InvalidRoot::Get( bostream & ) const 499 { 500 } 501 502 DYN Root * 503 InvalidRoot::CreateCopy() const 504 { 505 return new InvalidRoot; 506 } 507 508 const char * 509 InvalidRoot::OwnDelimiter() const 510 { 511 return 0; 512 } 513 514 515 516 517 } // namespace ploc 518 } // namespace csv 519 520 521 522