root/trunk/whisperlib/common/io/file/file.h

Revision 7, 5.4 kB (checked in by whispercastorg, 2 years ago)

version 0.2.0

Line 
1 // Copyright (c) 2009, Whispersoft s.r.l.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Whispersoft s.r.l. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Cosmin Tudorache
31
32 #ifndef __COMMON_IO_FILE_FILE_H__
33 #define __COMMON_IO_FILE_FILE_H__
34
35 #include <unistd.h>
36 #include <string.h>
37
38 #include <string>
39
40 #include <whisperlib/common/base/types.h>
41
42 namespace io {
43
44 class File {
45  public:
46   //////////////////////////////////////////////////////////////////////
47
48   enum Access {
49     GENERIC_READ,
50     GENERIC_WRITE,
51     GENERIC_READ_WRITE
52   };
53   static const char* AccessName(Access);
54
55   //////////////////////////////////////////////////////////////////////
56
57   enum CreationDisposition {
58     // Creates a new file, always.
59     // If a file exists, the function overwrites the file, clears the existing
60     // attributes.
61     CREATE_ALWAYS,
62     // Creates a new file. The function fails if a specified file exists.
63     CREATE_NEW,
64     // Opens a file, always.
65     // If a file does not exist, the function creates a file as if
66     // creation disposition is CREATE_NEW.
67     OPEN_ALWAYS,
68     // Opens a file.
69     // The function fails if the file does not exist.
70     // For more information, see the Remarks section of this topic.
71     OPEN_EXISTING,
72     // Opens a file and truncates it so that its size is zero (0) bytes.
73     // The function fails if the file does not exist.
74     // The calling process must open the file with the GENERIC_WRITE
75     // access right.
76     TRUNCATE_EXISTING
77   };
78   static const char* CreationDispositionName(CreationDisposition);
79
80   //////////////////////////////////////////////////////////////////////
81
82   enum MoveMethod {
83     FILE_SET = SEEK_SET,
84     FILE_CUR = SEEK_CUR,
85     FILE_END = SEEK_END
86   };
87   static const char* MoveMethodName(MoveMethod);
88
89   //////////////////////////////////////////////////////////////////////
90
91  public:
92   File();
93   virtual ~File();
94
95   // Convenience function for creating / opening and truncting a file
96   static File* CreateFileOrDie(const char* filename);
97   static File* TryCreateFile(const char* filename);
98
99   // Convenience function for opening a file for reading
100   static File* OpenFileOrDie(const char* filename);
101   static File* TryOpenFile(const char* filename);
102
103   bool Open(const char* filename, Access access, CreationDisposition cd);
104   void Close();
105
106   bool is_open() const {
107     return fd_ != INVALID_FD_VALUE;
108   }
109
110   const string& filename() const {
111     return filename_;
112   }
113
114   // Returns current file size. The file must be opened.
115   // (Uses local cached variable: size_)
116   int64 Size() const;
117
118   //  Get file pointer position relative to file begin.
119   //  (Uses local cached variable: position_)
120   int64 Position() const;
121
122   // Set file pointer position to the given absolute offset (relative
123   // to file begin)
124   //  Returns the new position
125   int64 SetPosition(int64 distance, MoveMethod move_method = FILE_SET);
126
127   // Reads "len" bytes of data from current file pointer position to
128   // the given "buf".
129   //  [IN/OUT] buf: buffer that receives the read data.
130   //  [IN]  len: the number of bytes to read.
131   // Returns the number of bytes read. Negative on error.
132   int32 Read(void* buf, int32 len);
133
134   // Writes "len" bytes of data from "buf" to file at current file
135   // pointer position.
136   //   buf: buffer that contains the data to be written.
137   //   len: number of bytes to write.
138   // Returns the number of bytes written. Negative on error
139   int32 Write(const void* buf, int32 len);
140
141   // Forces a disk flush
142   void Flush();
143
144  protected:
145   int fd() const {
146     return fd_;
147   }
148
149   string filename_;   // name of the opened file
150   int fd_;            // file descriptor of the opened file
151
152   int64 size_;        // file size
153   int64 position_;    // current file pointer position
154
155   //  Retrieve file size from system and set value in size_
156   // returns: success status
157   int64 UpdateSize();
158
159   // Retrieve file pointer from system and set value in position_
160   // returns: success status
161   int64 UpdatePosition();
162
163   DISALLOW_EVIL_CONSTRUCTORS(File);
164 };
165 }
166
167 #endif  // __COMMON_IO_FILE_FILE_H__
Note: See TracBrowser for help on using the browser.