OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #ifndef BIN_FILE_H_ | 5 #ifndef BIN_FILE_H_ |
6 #define BIN_FILE_H_ | 6 #define BIN_FILE_H_ |
7 | 7 |
8 #if defined(_WIN32) | 8 #if defined(_WIN32) |
9 typedef signed __int64 int64_t; | 9 typedef signed __int64 int64_t; |
10 typedef unsigned __int8 uint8_t; | 10 typedef unsigned __int8 uint8_t; |
11 #else | 11 #else |
12 #include <stdint.h> | 12 #include <stdint.h> |
13 #endif | 13 #endif |
14 | 14 |
15 #include <stdlib.h> | 15 #include <stdlib.h> |
16 #include <string.h> | 16 #include <string.h> |
17 #include <stdio.h> | 17 #include <stdio.h> |
18 #include <sys/types.h> | 18 #include <sys/types.h> |
19 | 19 |
20 // Forward declaration. | 20 // Forward declaration. |
21 class FileHandle; | 21 class FileHandle; |
22 | 22 |
23 class File { | 23 class File { |
24 public: | 24 public: |
| 25 enum FileOpenMode { |
| 26 kRead = 0, |
| 27 kWrite = 1, |
| 28 kTruncate = 1 << 2, |
| 29 kWriteTruncate = kWrite | kTruncate |
| 30 }; |
| 31 |
25 ~File(); | 32 ~File(); |
26 | 33 |
27 // Read/Write attempt to transfer num_bytes to/from buffer. It returns | 34 // Read/Write attempt to transfer num_bytes to/from buffer. It returns |
28 // the number of bytes read/written. | 35 // the number of bytes read/written. |
29 int64_t Read(void* buffer, int64_t num_bytes); | 36 int64_t Read(void* buffer, int64_t num_bytes); |
30 int64_t Write(const void* buffer, int64_t num_bytes); | 37 int64_t Write(const void* buffer, int64_t num_bytes); |
31 | 38 |
32 // ReadFully and WriteFully do attempt to transfer num_bytes to/from | 39 // ReadFully and WriteFully do attempt to transfer num_bytes to/from |
33 // the buffer. In the event of short accesses they will loop internally until | 40 // the buffer. In the event of short accesses they will loop internally until |
34 // the whole buffer has been transferred or an error occurs. If an error | 41 // the whole buffer has been transferred or an error occurs. If an error |
(...skipping 16 matching lines...) Expand all Loading... |
51 bool SetPosition(int64_t position); | 58 bool SetPosition(int64_t position); |
52 | 59 |
53 // Truncate (or extend) the file to the given length in bytes. | 60 // Truncate (or extend) the file to the given length in bytes. |
54 bool Truncate(int64_t length); | 61 bool Truncate(int64_t length); |
55 | 62 |
56 // Flush contents of file. | 63 // Flush contents of file. |
57 void Flush(); | 64 void Flush(); |
58 | 65 |
59 const char* name() const { return name_; } | 66 const char* name() const { return name_; } |
60 | 67 |
61 static File* Open(const char* name, bool writable); | 68 // Open the file with the given name. The file is always opened for |
| 69 // reading. If mode contains kWrite the file is opened for both |
| 70 // reading and writing. If mode contains kWrite and the file does |
| 71 // not exist the file is created. The file is truncated to length 0 if |
| 72 // mode contains kTruncate. |
| 73 static File* Open(const char* name, FileOpenMode mode); |
| 74 |
62 static bool Exists(const char* name); | 75 static bool Exists(const char* name); |
63 static bool Create(const char* name); | 76 static bool Create(const char* name); |
64 static bool Delete(const char* name); | 77 static bool Delete(const char* name); |
65 static bool IsAbsolutePath(const char* pathname); | 78 static bool IsAbsolutePath(const char* pathname); |
66 static char* GetCanonicalPath(const char* name); | 79 static char* GetCanonicalPath(const char* name); |
67 static const char* PathSeparator(); | 80 static const char* PathSeparator(); |
68 static const char* StringEscapedPathSeparator(); | 81 static const char* StringEscapedPathSeparator(); |
69 | 82 |
70 private: | 83 private: |
71 File(const char* name, FileHandle* handle) : name_(name), handle_(handle) { } | 84 File(const char* name, FileHandle* handle) : name_(name), handle_(handle) { } |
72 void Close(); | 85 void Close(); |
73 bool IsClosed(); | 86 bool IsClosed(); |
74 | 87 |
75 static const int kClosedFd = -1; | 88 static const int kClosedFd = -1; |
76 | 89 |
77 const char* name_; | 90 const char* name_; |
78 // FileHandle is an OS specific class which stores data about the file. | 91 // FileHandle is an OS specific class which stores data about the file. |
79 FileHandle* handle_; // OS specific handle for the file. | 92 FileHandle* handle_; // OS specific handle for the file. |
80 | 93 |
81 // DISALLOW_COPY_AND_ASSIGN(File). | 94 // DISALLOW_COPY_AND_ASSIGN(File). |
82 File(const File&); | 95 File(const File&); |
83 void operator=(const File&); | 96 void operator=(const File&); |
84 }; | 97 }; |
85 | 98 |
86 #endif // BIN_FILE_H_ | 99 #endif // BIN_FILE_H_ |
OLD | NEW |