PTLib  Version 2.10.11
file.h
Go to the documentation of this file.
1 /*
2  * file.h
3  *
4  * Operating System file I/O channel class.
5  *
6  * Portable Windows Library
7  *
8  * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23  *
24  * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
25  * All Rights Reserved.
26  *
27  * Contributor(s): ______________________________________.
28  *
29  * $Revision: 24177 $
30  * $Author: rjongbloed $
31  * $Date: 2010-04-05 06:52:04 -0500 (Mon, 05 Apr 2010) $
32  */
33 
34 
35 #ifndef PTLIB_FILE_H
36 #define PTLIB_FILE_H
37 
38 #ifdef P_USE_PRAGMA
39 #pragma interface
40 #endif
41 
42 #ifndef _WIN32
43 #include <sys/stat.h>
44 #endif
45 
46 
47 
49 // Binary Files
50 
60 class PFile : public PChannel
61 {
62  PCLASSINFO(PFile, PChannel);
63 
64  public:
71  PFile();
72 
77  enum OpenMode {
81  };
82 
93  enum OpenOptions {
95  ModeDefault = -1,
97  MustExist = 0,
99  Create = 1,
101  Truncate = 2,
103  Exclusive = 4,
110  };
111 
120  PFile(
121  OpenMode mode,
122  int opts = ModeDefault
123  );
124 
131  PFile(
132  const PFilePath & name,
133  OpenMode mode = ReadWrite,
134  int opts = ModeDefault
135  );
136 
138  ~PFile();
140 
141 
151  const PObject & obj
152  ) const;
154 
155 
164  virtual PString GetName() const;
165 
177  virtual PBoolean Read(
178  void * buf,
179  PINDEX len
180  );
181 
191  virtual PBoolean Write(
192  const void * buf,
193  PINDEX len
194  );
195 
199  virtual PBoolean Close();
201 
202 
212  static PBoolean Exists(
213  const PFilePath & name
214  );
215 
223  PBoolean Exists() const;
224 
234  static PBoolean Access(
235  const PFilePath & name,
236  OpenMode mode
237  );
238 
250  OpenMode mode
251  );
252 
265  static PBoolean Remove(
266  const PFilePath & name, // Name of file to delete.
267  PBoolean force = false // Force deletion even if file is protected.
268  );
269  static PBoolean Remove(
270  const PString & name, // Name of file to delete.
271  PBoolean force = false // Force deletion even if file is protected.
272  );
273 
287  PBoolean force = false // Force deletion even if file is protected.
288  );
289 
305  static PBoolean Rename(
306  const PFilePath & oldname,
307  const PString & newname,
308  PBoolean force = false
310  );
311 
329  const PString & newname,
330  PBoolean force = false
332  );
333 
339  static PBoolean Copy(
340  const PFilePath & oldname,
341  const PFilePath & newname,
342  PBoolean force = false
344  );
345 
351  PBoolean Copy(
352  const PFilePath & newname,
353  PBoolean force = false
355  );
356 
366  static PBoolean Move(
367  const PFilePath & oldname,
368  const PFilePath & newname,
369  PBoolean force = false
371  );
372 
382  PBoolean Move(
383  const PFilePath & newname,
384  PBoolean force = false
386  );
388 
397  const PFilePath & GetFilePath() const;
398 
402  void SetFilePath(
403  const PString & path
404  );
405 
406 
418  virtual PBoolean Open(
419  OpenMode mode = ReadWrite, // Mode in which to open the file.
420  int opts = ModeDefault // Options for open operation.
421  );
422 
433  virtual PBoolean Open(
434  const PFilePath & name, // Name of file to open.
435  OpenMode mode = ReadWrite, // Mode in which to open the file.
436  int opts = ModeDefault // <code>OpenOptions</code> enum# for open operation.
437  );
438 
444  virtual off_t GetLength() const;
445 
452  virtual PBoolean SetLength(
453  off_t len // New length of file.
454  );
455 
459  Start = SEEK_SET,
461  Current = SEEK_CUR,
463  End = SEEK_END
464  };
465 
476  virtual PBoolean SetPosition(
477  off_t pos,
478  FilePositionOrigin origin = Start
479  );
480 
487  virtual off_t GetPosition() const;
488 
495  PBoolean IsEndOfFile() const;
496 
502  static PBoolean GetInfo(
503  const PFilePath & name, // Name of file to get the information on.
504  PFileInfo & info
505  // <code>PFileInfo</code> structure to receive the information.
506  );
507 
514  PFileInfo & info
515  // <code>PFileInfo</code> structure to receive the information.
516  );
517 
523  static PBoolean SetPermissions(
524  const PFilePath & name, // Name of file to change the permission of.
525  int permissions // New permissions mask for the file.
526  );
533  int permissions // New permissions mask for the file.
534  );
536 
537  protected:
538  // Member variables
539 
542 
543 
544 // Include platform dependent part of class
545 #ifdef _WIN32
546 #include "msos/ptlib/file.h"
547 #else
548 #include "unix/ptlib/file.h"
549 #endif
550 };
551 
552 
553 #endif // PTLIB_FILE_H
554 
555 
556 // End Of File ///////////////////////////////////////////////////////////////
virtual PBoolean Close()
Close the file channel.
virtual PBoolean Read(void *buf, PINDEX len)
Low level read from the file channel.
File open fails if file already exists.
Definition: file.h:103
This class represents a disk file.
Definition: file.h:60
PBoolean IsEndOfFile() const
Determine if the current file position is at the end of the file.
virtual PBoolean Open(OpenMode mode=ReadWrite, int opts=ModeDefault)
Open the current file in the specified mode and with the specified options.
File may not be read by another process.
Definition: file.h:107
This class describes a full description for a file on the particular platform.
Definition: filepath.h:65
File may not be written by another process.
Definition: file.h:109
File can be both read and written.
Definition: file.h:80
Comparison
Result of the comparison operation performed by the Compare() function.
Definition: object.h:1184
const PFilePath & GetFilePath() const
Get the full path name of the file.
virtual PBoolean SetPosition(off_t pos, FilePositionOrigin origin=Start)
Set the current active position in the file for the next read or write operation. ...
File can be read but not written.
Definition: file.h:78
static PBoolean Access(const PFilePath &name, OpenMode mode)
Check for file access modes.
PBoolean removeOnClose
File is to be removed when closed.
Definition: file.h:541
BOOL PBoolean
Definition: object.h:102
static PBoolean Remove(const PFilePath &name, PBoolean force=false)
Delete the specified file.
static PBoolean Copy(const PFilePath &oldname, const PFilePath &newname, PBoolean force=false)
Make a copy of the specified file.
Set position relative to current file position.
Definition: file.h:461
virtual off_t GetPosition() const
Get the current active position in the file for the next read or write operation. ...
virtual PBoolean Write(const void *buf, PINDEX len)
Low level write to the file channel.
Abstract class defining I/O channel semantics.
Definition: channel.h:107
FilePositionOrigin
Options for the origin in setting the file position.
Definition: file.h:457
static PBoolean Move(const PFilePath &oldname, const PFilePath &newname, PBoolean force=false)
Move the specified file.
File open fails if file does not exist.
Definition: file.h:97
Comparison Compare(const PObject &obj) const
Determine the relative rank of the two objects.
File can be written but not read.
Definition: file.h:79
PFile()
Create a file object but do not open it.
The character string class.
Definition: pstring.h:108
void SetFilePath(const PString &path)
Set the full path name of the file.
File options depend on the OpenMode parameter.
Definition: file.h:95
static PBoolean GetInfo(const PFilePath &name, PFileInfo &info)
Get information (eg protection, timestamps) on the specified file.
Set position relative to end of file.
Definition: file.h:463
PFilePath path
The fully qualified path name for the file.
Definition: file.h:540
virtual off_t GetLength() const
Get the current size of the file.
static PBoolean Rename(const PFilePath &oldname, const PString &newname, PBoolean force=false)
Change the specified files name.
File is created if it does not exist.
Definition: file.h:99
~PFile()
Close the file on destruction.
Set position relative to start of file.
Definition: file.h:459
virtual PBoolean SetLength(off_t len)
Set the size of the file, padding with 0 bytes if it would require expanding the file, or truncating it if being made shorter.
OpenOptions
When a file is opened, a number of options may be associated with the open file.
Definition: file.h:93
PBoolean Exists() const
Check for file existance.
Class containing the system information on a file path.
Definition: pdirect.h:63
static PBoolean SetPermissions(const PFilePath &name, int permissions)
Set permissions on the specified file.
File is temporary and is to be deleted when closed.
Definition: file.h:105
Ultimate parent class for all objects in the class library.
Definition: object.h:1118
virtual PString GetName() const
Get the platform and I/O channel type name of the channel.
OpenMode
When a file is opened, it may restrict the access available to operations on the object instance...
Definition: file.h:77
File is set to zero length if it already exists.
Definition: file.h:101