@@ -44,33 +44,17 @@ class File {
44
44
// Reads "size" bytes to buff from file, buff should be pre-allocated.
45
45
size_t Read (void * buff, size_t size);
46
46
47
- // Reads "size" bytes to buff from file, buff should be pre-allocated.
48
- // If read failed, program will exit.
49
- void ReadOrDie (void * buff, size_t size);
50
-
51
- // Reads a line from file to a string.
52
- // Each line must be no more than max_length bytes.
53
- char * ReadLine (char * output, uint64_t max_length);
54
-
55
47
// Reads the whole file to a string, with a maximum length of 'max_length'.
56
48
// Returns the number of bytes read.
57
49
int64_t ReadToString (std::string* line, uint64_t max_length);
58
50
59
51
// Writes "size" bytes of buff to file, buff should be pre-allocated.
60
52
size_t Write (const void * buff, size_t size);
61
53
62
- // Writes "size" bytes of buff to file, buff should be pre-allocated.
63
- // If write failed, program will exit.
64
- void WriteOrDie (const void * buff, size_t size);
65
-
66
54
// Writes a string to file.
67
55
size_t WriteString (absl::string_view str);
68
56
69
- // Writes a string to file and append a "\n".
70
- bool WriteLine (absl::string_view line);
71
-
72
57
// Closes the file and delete the underlying FILE* descriptor.
73
- bool Close ();
74
58
absl::Status Close (int flags);
75
59
76
60
// Flushes buffer.
@@ -85,12 +69,6 @@ class File {
85
69
// Returns the file name.
86
70
absl::string_view filename () const ;
87
71
88
- // Deletes a file.
89
- static bool Delete (absl::string_view filename);
90
-
91
- // Tests if a file exists.
92
- static bool Exists (absl::string_view filename);
93
-
94
72
bool Open () const ;
95
73
96
74
private:
@@ -108,61 +86,62 @@ inline Options Defaults() { return 0xBABA; }
108
86
109
87
// As of 2016-01, these methods can only be used with flags = file::Defaults().
110
88
89
+ // ---- File API ----
90
+
111
91
// The caller should free the File after closing it by passing *f to delete.
112
92
absl::Status Open (absl::string_view filename, absl::string_view mode, File** f,
113
93
Options options);
114
94
// The caller should free the File after closing it by passing the returned
115
95
// pointer to delete.
116
96
File* OpenOrDie (absl::string_view filename, absl::string_view mode,
117
97
Options options);
98
+
99
+ absl::Status Delete (absl::string_view path, Options options);
100
+ absl::Status Exists (absl::string_view path, Options options);
101
+
102
+ // ---- Content API ----
103
+
104
+ absl::StatusOr<std::string> GetContents (absl::string_view path,
105
+ Options options);
106
+
107
+ absl::Status GetContents (absl::string_view filename, std::string* output,
108
+ Options options);
109
+
110
+ absl::Status SetContents (absl::string_view filename, absl::string_view contents,
111
+ Options options);
112
+
113
+ absl::Status WriteString (File* file, absl::string_view contents,
114
+ Options options);
115
+
116
+ // ---- Protobuf API ----
117
+
118
118
absl::Status GetTextProto (absl::string_view filename,
119
119
google::protobuf::Message* proto, Options options);
120
+
120
121
template <typename T>
121
122
absl::StatusOr<T> GetTextProto (absl::string_view filename, Options options) {
122
123
T proto;
123
124
RETURN_IF_ERROR (GetTextProto (filename, &proto, options));
124
125
return proto;
125
126
}
127
+
126
128
absl::Status SetTextProto (absl::string_view filename,
127
129
const google::protobuf::Message& proto,
128
130
Options options);
131
+
129
132
absl::Status GetBinaryProto (absl::string_view filename,
130
133
google::protobuf::Message* proto, Options options);
131
134
template <typename T>
135
+
132
136
absl::StatusOr<T> GetBinaryProto (absl::string_view filename, Options options) {
133
137
T proto;
134
138
RETURN_IF_ERROR (GetBinaryProto (filename, &proto, options));
135
139
return proto;
136
140
}
141
+
137
142
absl::Status SetBinaryProto (absl::string_view filename,
138
143
const google::protobuf::Message& proto,
139
144
Options options);
140
- absl::Status SetContents (absl::string_view filename, absl::string_view contents,
141
- Options options);
142
- absl::StatusOr<std::string> GetContents (absl::string_view path,
143
- Options options);
144
- absl::Status GetContents (absl::string_view filename, std::string* output,
145
- Options options);
146
- absl::Status WriteString (File* file, absl::string_view contents,
147
- Options options);
148
-
149
- bool ReadFileToString (absl::string_view file_name, std::string* output);
150
- bool WriteStringToFile (absl::string_view data, absl::string_view file_name);
151
- bool ReadFileToProto (absl::string_view file_name,
152
- google::protobuf::Message* proto);
153
- void ReadFileToProtoOrDie (absl::string_view file_name,
154
- google::protobuf::Message* proto);
155
- bool WriteProtoToASCIIFile (const google::protobuf::Message& proto,
156
- absl::string_view file_name);
157
- void WriteProtoToASCIIFileOrDie (const google::protobuf::Message& proto,
158
- absl::string_view file_name);
159
- bool WriteProtoToFile (const google::protobuf::Message& proto,
160
- absl::string_view file_name);
161
- void WriteProtoToFileOrDie (const google::protobuf::Message& proto,
162
- absl::string_view file_name);
163
-
164
- absl::Status Delete (absl::string_view path, Options options);
165
- absl::Status Exists (absl::string_view path, Options options);
166
145
167
146
} // namespace file
168
147
0 commit comments