]>
Commit | Line | Data |
---|---|---|
1 | // -*- mode: cpp; mode: fold -*- | |
2 | // Description /*{{{*/ | |
3 | // $Id: fileutl.h,v 1.1 1998/07/02 02:58:13 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 | // Header section: pkglib | |
22 | #ifndef PKGLIB_FILEUTL_H | |
23 | #define PKGLIB_FILEUTL_H | |
24 | ||
25 | #include <string> | |
26 | ||
27 | class File | |
28 | { | |
29 | protected: | |
30 | int iFd; | |
31 | ||
32 | enum LocalFlags {AutoClose = (1<<0),Fail = (1<<1),DelOnFail = (1<<2)}; | |
33 | unsigned long Flags; | |
34 | string FileName; | |
35 | ||
36 | public: | |
37 | enum OpenMode {ReadOnly,WriteEmpty,WriteExists}; | |
38 | ||
39 | bool Read(void *To,unsigned long Size); | |
40 | bool Write(void *From,unsigned long Size); | |
41 | bool Seek(unsigned long To); | |
42 | unsigned long Size(); | |
43 | bool Close(); | |
44 | ||
45 | // Simple manipulators | |
46 | inline int Fd() {return iFd;}; | |
47 | inline bool IsOpen() {return iFd >= 0;}; | |
48 | inline bool Failed() {return (Flags & Fail) == Fail;}; | |
49 | inline void EraseOnFailure() {Flags |= DelOnFail;}; | |
50 | inline void OpFail() {Flags |= Fail;}; | |
51 | ||
52 | File(string FileName,OpenMode Mode,unsigned long Perms = 0666); | |
53 | File(int Fd) : iFd(Fd), Flags(AutoClose) {}; | |
54 | File(int Fd,bool) : iFd(Fd), Flags(0) {}; | |
55 | virtual ~File(); | |
56 | }; | |
57 | ||
58 | bool CopyFile(string From,string To); | |
59 | int GetLock(string File,bool Errors = true); | |
60 | bool FileExists(string File); | |
61 | string SafeGetCWD(); | |
62 | ||
63 | #endif |