My Project
UDK 3.2.7 C/C++ API Reference
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
file.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * Copyright 2000, 2010 Oracle and/or its affiliates.
7  *
8  * OpenOffice.org - a multi-platform office productivity suite
9  *
10  * This file is part of OpenOffice.org.
11  *
12  * OpenOffice.org is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License version 3
14  * only, as published by the Free Software Foundation.
15  *
16  * OpenOffice.org is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License version 3 for more details
20  * (a copy is included in the LICENSE file that accompanied this code).
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * version 3 along with OpenOffice.org. If not, see
24  * <http://www.openoffice.org/license.html>
25  * for a copy of the LGPLv3 License.
26  *
27  ************************************************************************/
28 
29 #ifndef _OSL_FILE_HXX_
30 #define _OSL_FILE_HXX_
31 
32 #include "sal/config.h"
33 
34 #include <cassert>
35 
36 #include <osl/time.h>
37 #include <rtl/memory.h>
38 #include <rtl/ustring.hxx>
39 
40 #include <osl/file.h>
41 #include <rtl/byteseq.hxx>
42 
43 #include <stdio.h>
44 
45 namespace osl
46 {
47 
48 
49 // -----------------------------------------------------------------------------
57 class FileBase
58 {
59 public:
60 
61  enum RC {
107  E_invalidError = osl_File_E_invalidError, /* unmapped error: always last entry in enum! */
110  };
111 
112 
113 public:
114 
136  static inline RC getCanonicalName( const ::rtl::OUString& ustrRequestedURL, ::rtl::OUString& ustrValidURL )
137  {
138  return (RC) osl_getCanonicalName( ustrRequestedURL.pData, &ustrValidURL.pData );
139  }
140 
176  static inline RC getAbsoluteFileURL( const ::rtl::OUString& ustrBaseDirectoryURL, const ::rtl::OUString& ustrRelativeFileURL, ::rtl::OUString& ustrAbsoluteFileURL )
177  {
178  return (RC) osl_getAbsoluteFileURL( ustrBaseDirectoryURL.pData, ustrRelativeFileURL.pData, &ustrAbsoluteFileURL.pData );
179  }
180 
196  static inline RC getSystemPathFromFileURL( const ::rtl::OUString& ustrFileURL, ::rtl::OUString& ustrSystemPath )
197  {
198  return (RC) osl_getSystemPathFromFileURL( ustrFileURL.pData, &ustrSystemPath.pData );
199  }
200 
216  static inline RC getFileURLFromSystemPath( const ::rtl::OUString& ustrSystemPath, ::rtl::OUString& ustrFileURL )
217  {
218  return (RC) osl_getFileURLFromSystemPath( ustrSystemPath.pData, &ustrFileURL.pData );
219  }
220 
249  static inline RC searchFileURL( const ::rtl::OUString& ustrFileName, const ::rtl::OUString& ustrSearchPath, ::rtl::OUString& ustrFileURL )
250  {
251  return (RC) osl_searchFileURL( ustrFileName.pData, ustrSearchPath.pData, &ustrFileURL.pData );
252  }
253 
264  static inline RC getTempDirURL( ::rtl::OUString& ustrTempDirURL )
265  {
266  return (RC) osl_getTempDirURL( &ustrTempDirURL.pData );
267  }
268 
318  static inline RC createTempFile(
319  ::rtl::OUString* pustrDirectoryURL,
320  oslFileHandle* pHandle,
321  ::rtl::OUString* pustrTempFileURL)
322  {
323  rtl_uString* pustr_dir_url = pustrDirectoryURL ? pustrDirectoryURL->pData : 0;
324  rtl_uString** ppustr_tmp_file_url = pustrTempFileURL ? &pustrTempFileURL->pData : 0;
325 
326  return (RC) osl_createTempFile(pustr_dir_url, pHandle, ppustr_tmp_file_url);
327  }
328 };
329 
330 
331 // -----------------------------------------------------------------------------
337 class VolumeDevice : public FileBase
338 {
339  oslVolumeDeviceHandle _aHandle;
340 
341 public:
342 
346  VolumeDevice() : _aHandle( NULL )
347  {
348  }
349 
356  VolumeDevice( const VolumeDevice & rDevice )
357  {
358  _aHandle = rDevice._aHandle;
359  if ( _aHandle )
360  osl_acquireVolumeDeviceHandle( _aHandle );
361  }
362 
367  {
368  if ( _aHandle )
369  osl_releaseVolumeDeviceHandle( _aHandle );
370  }
371 
378  inline VolumeDevice & operator =( const VolumeDevice & rDevice )
379  {
380  oslVolumeDeviceHandle newHandle = rDevice._aHandle;
381 
382  if ( newHandle )
383  osl_acquireVolumeDeviceHandle( newHandle );
384 
385  if ( _aHandle )
386  osl_releaseVolumeDeviceHandle( _aHandle );
387 
388  _aHandle = newHandle;
389 
390  return *this;
391  }
392 
399  {
400  rtl::OUString aPath;
401  osl_getVolumeDeviceMountPath( _aHandle, &aPath.pData );
402  return aPath;
403  }
404 
405  friend class VolumeInfo;
406 };
407 
408 // -----------------------------------------------------------------------------
409 
410 class Directory;
411 
421 {
422  oslVolumeInfo _aInfo;
423  sal_uInt32 _nMask;
424  VolumeDevice _aDevice;
425 
430 
434  VolumeInfo& operator = ( VolumeInfo& );
435 
436 public:
437 
444  VolumeInfo( sal_uInt32 nMask ): _nMask( nMask )
445  {
446  _aInfo.uStructSize = sizeof( oslVolumeInfo );
447  rtl_fillMemory( &_aInfo.uValidFields, sizeof( oslVolumeInfo ) - sizeof( sal_uInt32 ), 0 );
448  _aInfo.pDeviceHandle = &_aDevice._aHandle;
449  }
450 
455  {
456  if( _aInfo.ustrFileSystemName )
458  }
459 
468  inline sal_Bool isValid( sal_uInt32 nMask ) const
469  {
470  return ( nMask & _aInfo.uValidFields ) == nMask;
471  }
472 
479  inline sal_Bool getRemoteFlag() const
480  {
481  return 0 != (_aInfo.uAttributes & osl_Volume_Attribute_Remote);
482  }
483 
491  {
492  return 0 != (_aInfo.uAttributes & osl_Volume_Attribute_Removeable);
493  }
494 
502  {
503  return 0 != (_aInfo.uAttributes & osl_Volume_Attribute_CompactDisc);
504  }
505 
513  {
514  return 0 != (_aInfo.uAttributes & osl_Volume_Attribute_FloppyDisk);
515  }
516 
523  inline sal_Bool getFixedDiskFlag() const
524  {
525  return 0 != (_aInfo.uAttributes & osl_Volume_Attribute_FixedDisk);
526  }
527 
534  inline sal_Bool getRAMDiskFlag() const
535  {
536  return 0 != (_aInfo.uAttributes & osl_Volume_Attribute_RAMDisk);
537  }
538 
546  inline sal_uInt64 getTotalSpace() const
547  {
548  return _aInfo.uTotalSpace;
549  }
550 
558  inline sal_uInt64 getFreeSpace() const
559  {
560  return _aInfo.uFreeSpace;
561  }
562 
570  inline sal_uInt64 getUsedSpace() const
571  {
572  return _aInfo.uUsedSpace;
573  }
574 
582  inline sal_uInt32 getMaxNameLength() const
583  {
584  return _aInfo.uMaxNameLength;
585  }
586 
594  inline sal_uInt32 getMaxPathLength() const
595  {
596  return _aInfo.uMaxPathLength;
597  }
598 
606  inline ::rtl::OUString getFileSystemName() const
607  {
609  }
610 
611 
620  {
621  return _aDevice;
622  }
623 
631  {
633  }
634 
643  {
645  }
646 
647  friend class Directory;
648 };
649 
650 // -----------------------------------------------------------------------------
651 class DirectoryItem;
652 
659 {
660  oslFileStatus _aStatus;
661  sal_uInt32 _nMask;
662 
667 
671  FileStatus& operator = ( FileStatus& );
672 
673 public:
674 
675  enum Type {
684  };
685 
692  FileStatus( sal_uInt32 nMask ): _nMask( nMask )
693  {
694  _aStatus.uStructSize = sizeof( oslFileStatus );
695  rtl_fillMemory( &_aStatus.uValidFields, sizeof( oslFileStatus ) - sizeof( sal_uInt32 ), 0 );
696  }
697 
702  {
703  if ( _aStatus.ustrFileURL )
704  rtl_uString_release( _aStatus.ustrFileURL );
705  if ( _aStatus.ustrLinkTargetURL )
707  if ( _aStatus.ustrFileName )
708  rtl_uString_release( _aStatus.ustrFileName );
709  }
710 
720  inline sal_Bool isValid( sal_uInt32 nMask ) const
721  {
722  return ( nMask & _aStatus.uValidFields ) == nMask;
723  }
724 
730  inline Type getFileType() const
731  {
733  return static_cast< Type >(_aStatus.eType);
734  }
735 
745  inline sal_Bool isDirectory() const
746  {
747  return ( getFileType() == Directory || getFileType() == Volume );
748  }
749 
760  inline sal_Bool isRegular() const
761  {
762  return ( getFileType() == Regular );
763  }
764 
773  inline sal_Bool isLink() const
774  {
775  return ( getFileType() == Link );
776  }
777 
784  inline sal_uInt64 getAttributes() const
785  {
787  return _aStatus.uAttributes;
788  }
789 
796  inline TimeValue getCreationTime() const
797  {
799  return _aStatus.aCreationTime;
800  }
801 
808  inline TimeValue getAccessTime() const
809  {
811  return _aStatus.aAccessTime;
812  }
813 
820  inline TimeValue getModifyTime() const
821  {
823  return _aStatus.aModifyTime;
824  }
825 
832  inline sal_uInt64 getFileSize() const
833  {
835  return _aStatus.uFileSize;
836  }
837 
844  inline ::rtl::OUString getFileName() const
845  {
847  return rtl::OUString(_aStatus.ustrFileName);
848  }
849 
850 
857  inline ::rtl::OUString getFileURL() const
858  {
860  return rtl::OUString(_aStatus.ustrFileURL);
861  }
862 
869  inline ::rtl::OUString getLinkTargetURL() const
870  {
872  return rtl::OUString(_aStatus.ustrLinkTargetURL);
873  }
874 
875  friend class DirectoryItem;
876 };
877 
878 
879 // -----------------------------------------------------------------------------
886 class File: public FileBase
887 {
888  oslFileHandle _pData;
889  ::rtl::OUString _aPath;
890 
894  File( File& );
895 
899  File& operator = ( File& );
900 
901 public:
902 
909  File( const ::rtl::OUString& ustrFileURL ): _pData( 0 ), _aPath( ustrFileURL ) {}
910 
914  inline ~File()
915  {
916  close();
917  }
918 
962  inline RC open( sal_uInt32 uFlags )
963  {
964  return (RC) osl_openFile( _aPath.pData, &_pData, uFlags );
965  }
966 
981  inline RC close()
982  {
984 
985  if( _pData )
986  {
987  Error=osl_closeFile( _pData );
988  _pData = NULL;
989  }
990 
991  return (RC) Error;
992  }
993 
1011  inline RC setPos( sal_uInt32 uHow, sal_Int64 uPos ) SAL_WARN_UNUSED_RESULT
1012  {
1013  return (RC) osl_setFilePos( _pData, uHow, uPos );
1014  }
1015 
1032  inline RC getPos( sal_uInt64& uPos )
1033  {
1034  return (RC) osl_getFilePos( _pData, &uPos );
1035  }
1036 
1059  inline RC isEndOfFile( sal_Bool *pIsEOF )
1060  {
1061  return (RC) osl_isEndOfFile( _pData, pIsEOF );
1062  }
1063 
1082  inline RC setSize( sal_uInt64 uSize )
1083  {
1084  return (RC) osl_setFileSize( _pData, uSize );
1085  }
1086 
1107  inline RC getSize( sal_uInt64 &rSize )
1108  {
1109  return (RC) osl_getFileSize( _pData, &rSize );
1110  }
1111 
1144  inline RC read( void *pBuffer, sal_uInt64 uBytesRequested, sal_uInt64& rBytesRead )
1145  {
1146  return (RC) osl_readFile( _pData, pBuffer, uBytesRequested, &rBytesRead );
1147  }
1148 
1183  inline RC write(const void *pBuffer, sal_uInt64 uBytesToWrite, sal_uInt64& rBytesWritten)
1184  {
1185  return (RC) osl_writeFile( _pData, pBuffer, uBytesToWrite, &rBytesWritten );
1186  }
1187 
1188 
1213  inline RC readLine( ::rtl::ByteSequence& aSeq )
1214  {
1215  return (RC) osl_readLine( _pData, reinterpret_cast<sal_Sequence**>(&aSeq) );
1216  }
1217 
1249  inline RC sync() const
1250  {
1251  OSL_PRECOND(_pData, "File::sync(): File not open");
1252  return (RC)osl_syncFile(_pData);
1253  }
1254 
1281  inline static RC copy( const ::rtl::OUString& ustrSourceFileURL, const ::rtl::OUString& ustrDestFileURL )
1282  {
1283  return (RC) osl_copyFile( ustrSourceFileURL.pData, ustrDestFileURL.pData );
1284  }
1285 
1310  inline static RC move( const ::rtl::OUString& ustrSourceFileURL, const ::rtl::OUString& ustrDestFileURL )
1311  {
1312  return (RC) osl_moveFile( ustrSourceFileURL.pData, ustrDestFileURL.pData );
1313  }
1314 
1343  inline static RC remove( const ::rtl::OUString& ustrFileURL )
1344  {
1345  return (RC) osl_removeFile( ustrFileURL.pData );
1346  }
1347 
1363  inline static RC setAttributes( const ::rtl::OUString& ustrFileURL, sal_uInt64 uAttributes )
1364  {
1365  return (RC) osl_setFileAttributes( ustrFileURL.pData, uAttributes );
1366  }
1367 
1390  inline static RC setTime(
1391  const ::rtl::OUString& ustrFileURL,
1392  const TimeValue& rCreationTime,
1393  const TimeValue& rLastAccessTime,
1394  const TimeValue& rLastWriteTime )
1395  {
1396  return (RC) osl_setFileTime(
1397  ustrFileURL.pData,
1398  &rCreationTime,
1399  &rLastAccessTime,
1400  &rLastWriteTime );
1401  }
1402 
1403  friend class DirectoryItem;
1404 };
1405 
1406 // -----------------------------------------------------------------------------
1413 {
1414  oslDirectoryItem _pData;
1415 
1416 public:
1417 
1421  DirectoryItem(): _pData( NULL )
1422  {
1423  }
1424 
1428  DirectoryItem( const DirectoryItem& rItem ): _pData( rItem._pData)
1429  {
1430  if( _pData )
1431  osl_acquireDirectoryItem( _pData );
1432  }
1433 
1438  {
1439  if( _pData )
1440  osl_releaseDirectoryItem( _pData );
1441  }
1442 
1447  {
1448  if (&rItem != this)
1449  {
1450  if( _pData )
1451  osl_releaseDirectoryItem( _pData );
1452 
1453  _pData = rItem._pData;
1454 
1455  if( _pData )
1456  osl_acquireDirectoryItem( _pData );
1457  }
1458  return *this;
1459  }
1460 
1467  inline sal_Bool is()
1468  {
1469  return _pData != NULL;
1470  }
1471 
1506  static inline RC get( const ::rtl::OUString& ustrFileURL, DirectoryItem& rItem )
1507  {
1508  if( rItem._pData)
1509  {
1510  osl_releaseDirectoryItem( rItem._pData );
1511  rItem._pData = NULL;
1512  }
1513 
1514  return (RC) osl_getDirectoryItem( ustrFileURL.pData, &rItem._pData );
1515  }
1516 
1549  inline RC getFileStatus( FileStatus& rStatus )
1550  {
1551  return (RC) osl_getFileStatus( _pData, &rStatus._aStatus, rStatus._nMask );
1552  }
1553 
1570  inline sal_Bool isIdenticalTo( const DirectoryItem &pOther )
1571  {
1572  return osl_identicalDirectoryItem( _pData, pOther._pData );
1573  }
1574 
1575  friend class Directory;
1576 };
1577 
1578 //###########################################
1579 
1591 {
1592 public:
1594 
1604  virtual void DirectoryCreated(const rtl::OUString& aDirectoryUrl) = 0;
1605 };
1606 
1607 //###########################################
1608 // This just an internal helper function for
1609 // private use.
1610 extern "C" inline void SAL_CALL onDirectoryCreated(void* pData, rtl_uString* aDirectoryUrl)
1611 {
1612  (static_cast<DirectoryCreationObserver*>(pData))->DirectoryCreated(aDirectoryUrl);
1613 }
1614 
1621 class Directory: public FileBase
1622 {
1623  oslDirectory _pData;
1624  ::rtl::OUString _aPath;
1625 
1629  Directory( Directory& );
1630 
1634  Directory& operator = ( Directory& );
1635 
1636 public:
1637 
1645  Directory( const ::rtl::OUString& strPath ): _pData( 0 ), _aPath( strPath )
1646  {
1647  }
1648 
1653  {
1654  close();
1655  }
1656 
1675  inline RC open()
1676  {
1677  return (RC) osl_openDirectory( _aPath.pData, &_pData );
1678  }
1679 
1691  inline sal_Bool isOpen() { return _pData != NULL; }
1692 
1705  inline RC close()
1706  {
1707  oslFileError Error = osl_File_E_BADF;
1708 
1709  if( _pData )
1710  {
1711  Error=osl_closeDirectory( _pData );
1712  _pData = NULL;
1713  }
1714 
1715  return (RC) Error;
1716  }
1717 
1718 
1736  inline RC reset()
1737  {
1738  close();
1739  return open();
1740  }
1741 
1765  inline RC getNextItem( DirectoryItem& rItem, sal_uInt32 nHint = 0 )
1766  {
1767  if( rItem._pData )
1768  {
1769  osl_releaseDirectoryItem( rItem._pData );
1770  rItem._pData = 0;
1771  }
1772  return ( RC) osl_getNextDirectoryItem( _pData, &rItem._pData, nHint );
1773  }
1774 
1775 
1807  inline static RC getVolumeInfo( const ::rtl::OUString& ustrDirectoryURL, VolumeInfo& rInfo )
1808  {
1809  return (RC) osl_getVolumeInformation( ustrDirectoryURL.pData, &rInfo._aInfo, rInfo._nMask );
1810  }
1811 
1839  inline static RC create( const ::rtl::OUString& ustrDirectoryURL )
1840  {
1841  return (RC) osl_createDirectory( ustrDirectoryURL.pData );
1842  }
1843 
1872  inline static RC remove( const ::rtl::OUString& ustrDirectoryURL )
1873  {
1874  return (RC) osl_removeDirectory( ustrDirectoryURL.pData );
1875  }
1876 
1929  static RC createPath(
1930  const ::rtl::OUString& aDirectoryUrl,
1931  DirectoryCreationObserver* aDirectoryCreationObserver = NULL)
1932  {
1933  return (RC)osl_createDirectoryPath(
1934  aDirectoryUrl.pData,
1935  (aDirectoryCreationObserver) ? onDirectoryCreated : NULL,
1936  aDirectoryCreationObserver);
1937  }
1938 };
1939 
1940 } /* namespace osl */
1941 
1942 #endif /* _OSL_FILE_HXX_ */
1943 
1944 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */