OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // http://code.google.com/p/protobuf/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
11 // * Redistributions in binary form must reproduce the above | 11 // * Redistributions in binary form must reproduce the above |
12 // copyright notice, this list of conditions and the following disclaimer | 12 // copyright notice, this list of conditions and the following disclaimer |
13 // in the documentation and/or other materials provided with the | 13 // in the documentation and/or other materials provided with the |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 | 75 |
76 int error = ferror(file); | 76 int error = ferror(file); |
77 if (fclose(file) != 0) return false; | 77 if (fclose(file) != 0) return false; |
78 return error == 0; | 78 return error == 0; |
79 } | 79 } |
80 | 80 |
81 void File::ReadFileToStringOrDie(const string& name, string* output) { | 81 void File::ReadFileToStringOrDie(const string& name, string* output) { |
82 GOOGLE_CHECK(ReadFileToString(name, output)) << "Could not read: " << name; | 82 GOOGLE_CHECK(ReadFileToString(name, output)) << "Could not read: " << name; |
83 } | 83 } |
84 | 84 |
85 bool File::WriteStringToFile(const string& contents, const string& name) { | |
86 FILE* file = fopen(name.c_str(), "wb"); | |
87 if (file == NULL) { | |
88 GOOGLE_LOG(ERROR) << "fopen(" << name << ", \"wb\"): " << strerror(errno); | |
89 return false; | |
90 } | |
91 | |
92 if (fwrite(contents.data(), 1, contents.size(), file) != contents.size()) { | |
93 GOOGLE_LOG(ERROR) << "fwrite(" << name << "): " << strerror(errno); | |
94 return false; | |
95 } | |
96 | |
97 if (fclose(file) != 0) { | |
98 return false; | |
99 } | |
100 return true; | |
101 } | |
102 | |
103 void File::WriteStringToFileOrDie(const string& contents, const string& name) { | 85 void File::WriteStringToFileOrDie(const string& contents, const string& name) { |
104 FILE* file = fopen(name.c_str(), "wb"); | 86 FILE* file = fopen(name.c_str(), "wb"); |
105 GOOGLE_CHECK(file != NULL) | 87 GOOGLE_CHECK(file != NULL) |
106 << "fopen(" << name << ", \"wb\"): " << strerror(errno); | 88 << "fopen(" << name << ", \"wb\"): " << strerror(errno); |
107 GOOGLE_CHECK_EQ(fwrite(contents.data(), 1, contents.size(), file), | 89 GOOGLE_CHECK_EQ(fwrite(contents.data(), 1, contents.size(), file), |
108 contents.size()) | 90 contents.size()) |
109 << "fwrite(" << name << "): " << strerror(errno); | 91 << "fwrite(" << name << "): " << strerror(errno); |
110 GOOGLE_CHECK(fclose(file) == 0) | 92 GOOGLE_CHECK(fclose(file) == 0) |
111 << "fclose(" << name << "): " << strerror(errno); | 93 << "fclose(" << name << "): " << strerror(errno); |
112 } | 94 } |
(...skipping 13 matching lines...) Expand all Loading... |
126 // No parent given. | 108 // No parent given. |
127 return false; | 109 return false; |
128 } | 110 } |
129 | 111 |
130 return RecursivelyCreateDir(path.substr(0, slashpos), mode) && | 112 return RecursivelyCreateDir(path.substr(0, slashpos), mode) && |
131 CreateDir(path, mode); | 113 CreateDir(path, mode); |
132 } | 114 } |
133 | 115 |
134 void File::DeleteRecursively(const string& name, | 116 void File::DeleteRecursively(const string& name, |
135 void* dummy1, void* dummy2) { | 117 void* dummy1, void* dummy2) { |
136 if (name.empty()) return; | |
137 | |
138 // We don't care too much about error checking here since this is only used | 118 // We don't care too much about error checking here since this is only used |
139 // in tests to delete temporary directories that are under /tmp anyway. | 119 // in tests to delete temporary directories that are under /tmp anyway. |
140 | 120 |
141 #ifdef _MSC_VER | 121 #ifdef _MSC_VER |
142 // This interface is so weird. | 122 // This interface is so weird. |
143 WIN32_FIND_DATA find_data; | 123 WIN32_FIND_DATA find_data; |
144 HANDLE find_handle = FindFirstFile((name + "/*").c_str(), &find_data); | 124 HANDLE find_handle = FindFirstFile((name + "/*").c_str(), &find_data); |
145 if (find_handle == INVALID_HANDLE_VALUE) { | 125 if (find_handle == INVALID_HANDLE_VALUE) { |
146 // Just delete it, whatever it is. | 126 // Just delete it, whatever it is. |
147 DeleteFile(name.c_str()); | 127 DeleteFile(name.c_str()); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 | 165 |
186 closedir(dir); | 166 closedir(dir); |
187 rmdir(name.c_str()); | 167 rmdir(name.c_str()); |
188 | 168 |
189 } else if (S_ISREG(stats.st_mode)) { | 169 } else if (S_ISREG(stats.st_mode)) { |
190 remove(name.c_str()); | 170 remove(name.c_str()); |
191 } | 171 } |
192 #endif | 172 #endif |
193 } | 173 } |
194 | 174 |
195 bool File::ChangeWorkingDirectory(const string& new_working_directory) { | |
196 return chdir(new_working_directory.c_str()) == 0; | |
197 } | |
198 | |
199 } // namespace protobuf | 175 } // namespace protobuf |
200 } // namespace google | 176 } // namespace google |
OLD | NEW |