home *** CD-ROM | disk | FTP | other *** search
/ PC World 2000 February / PCWorld_2000-02_cd.bin / live / usr / include / apt-pkg / fileutl.h < prev    next >
C/C++ Source or Header  |  1999-06-21  |  2KB  |  79 lines

  1. // -*- mode: cpp; mode: fold -*-
  2. // Description                                /*{{{*/
  3. // $Id: fileutl.h,v 1.18 1999/04/20 05:02:09 jgg Exp $
  4. /* ######################################################################
  5.    
  6.    File Utilities
  7.    
  8.    CopyFile - Buffered copy of a single file
  9.    GetLock - dpkg compatible lock file manipulation (fcntl)
  10.    FileExists - Returns true if the file exists
  11.    SafeGetCWD - Returns the CWD in a string with overrun protection 
  12.    
  13.    The file class is a handy abstraction for various functions+classes
  14.    that need to accept filenames.
  15.    
  16.    This source is placed in the Public Domain, do with it what you will
  17.    It was originally written by Jason Gunthorpe.
  18.    
  19.    ##################################################################### */
  20.                                     /*}}}*/
  21. #ifndef PKGLIB_FILEUTL_H
  22. #define PKGLIB_FILEUTL_H
  23.  
  24. #ifdef __GNUG__
  25. #pragma interface "apt-pkg/fileutl.h"
  26. #endif 
  27.  
  28. #include <string>
  29.  
  30. class FileFd
  31. {
  32.    protected:
  33.    int iFd;
  34.  
  35.    enum LocalFlags {AutoClose = (1<<0),Fail = (1<<1),DelOnFail = (1<<2)};
  36.    unsigned long Flags;
  37.    string FileName;
  38.    
  39.    public:
  40.    enum OpenMode {ReadOnly,WriteEmpty,WriteExists,WriteAny};
  41.    
  42.    bool Read(void *To,unsigned long Size);
  43.    bool Write(const void *From,unsigned long Size);
  44.    bool Seek(unsigned long To);
  45.    bool Truncate(unsigned long To);
  46.    unsigned long Tell();
  47.    unsigned long Size();
  48.    bool Close();
  49.  
  50.    // Simple manipulators
  51.    inline int Fd() {return iFd;};
  52.    inline void Fd(int fd) {iFd = fd;};
  53.    inline bool IsOpen() {return iFd >= 0;};
  54.    inline bool Failed() {return (Flags & Fail) == Fail;};
  55.    inline void EraseOnFailure() {Flags |= DelOnFail;};
  56.    inline void OpFail() {Flags |= Fail;};
  57.    inline string &Name() {return FileName;};
  58.    
  59.    FileFd(string FileName,OpenMode Mode,unsigned long Perms = 0666);
  60.    FileFd(int Fd = -1) : iFd(Fd), Flags(AutoClose) {};
  61.    FileFd(int Fd,bool) : iFd(Fd), Flags(0) {};
  62.    virtual ~FileFd();
  63. };
  64.  
  65. bool CopyFile(FileFd &From,FileFd &To);
  66. int GetLock(string File,bool Errors = true);
  67. bool FileExists(string File);
  68. string SafeGetCWD();
  69. void SetCloseExec(int Fd,bool Close);
  70. void SetNonBlock(int Fd,bool Block);
  71. bool WaitFd(int Fd,bool write = false,unsigned long timeout = 0);
  72. int ExecFork();
  73.  
  74. // File string manipulators
  75. string flNotDir(string File);
  76. string flNotFile(string File);
  77.  
  78. #endif
  79.