fileutl.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: fileutl.h,v 1.3 1998/07/12 23:58:49 jgg Exp $
  4. /* ######################################################################
  5. File Utilities
  6. CopyFile - Buffered copy of a single file
  7. GetLock - dpkg compatible lock file manipulation (fcntl)
  8. FileExists - Returns true if the file exists
  9. SafeGetCWD - Returns the CWD in a string with overrun protection
  10. The file class is a handy abstraction for various functions+classes
  11. that need to accept filenames.
  12. This source is placed in the Public Domain, do with it what you will
  13. It was originally written by Jason Gunthorpe.
  14. ##################################################################### */
  15. /*}}}*/
  16. // Header section: pkglib
  17. #ifndef PKGLIB_FILEUTL_H
  18. #define PKGLIB_FILEUTL_H
  19. #ifdef __GNUG__
  20. #pragma interface "apt-pkg/fileutl.h"
  21. #endif
  22. #include <string>
  23. class File
  24. {
  25. protected:
  26. int iFd;
  27. enum LocalFlags {AutoClose = (1<<0),Fail = (1<<1),DelOnFail = (1<<2)};
  28. unsigned long Flags;
  29. string FileName;
  30. public:
  31. enum OpenMode {ReadOnly,WriteEmpty,WriteExists};
  32. bool Read(void *To,unsigned long Size);
  33. bool Write(void *From,unsigned long Size);
  34. bool Seek(unsigned long To);
  35. unsigned long Size();
  36. bool Close();
  37. // Simple manipulators
  38. inline int Fd() {return iFd;};
  39. inline bool IsOpen() {return iFd >= 0;};
  40. inline bool Failed() {return (Flags & Fail) == Fail;};
  41. inline void EraseOnFailure() {Flags |= DelOnFail;};
  42. inline void OpFail() {Flags |= Fail;};
  43. File(string FileName,OpenMode Mode,unsigned long Perms = 0666);
  44. File(int Fd) : iFd(Fd), Flags(AutoClose) {};
  45. File(int Fd,bool) : iFd(Fd), Flags(0) {};
  46. virtual ~File();
  47. };
  48. bool CopyFile(string From,string To);
  49. int GetLock(string File,bool Errors = true);
  50. bool FileExists(string File);
  51. string SafeGetCWD();
  52. #endif