1 #include "xmpmeta/file.h"
2
3 #include <cstdio>
4 #include "android-base/logging.h"
5
6 namespace dynamic_depth {
7 namespace xmpmeta {
8
9 using std::string;
10
WriteStringToFileOrDie(const string & data,const string & filename)11 void WriteStringToFileOrDie(const string& data, const string& filename) {
12 FILE* file_descriptor = fopen(filename.c_str(), "wb");
13 if (!file_descriptor) {
14 LOG(FATAL) << "Couldn't write to file: " << filename;
15 }
16 fwrite(data.c_str(), 1, data.size(), file_descriptor);
17 fclose(file_descriptor);
18 }
19
ReadFileToStringOrDie(const string & filename,string * data)20 void ReadFileToStringOrDie(const string& filename, string* data) {
21 FILE* file_descriptor = fopen(filename.c_str(), "r");
22
23 if (!file_descriptor) {
24 LOG(FATAL) << "Couldn't read file: " << filename;
25 }
26
27 // Resize the input buffer appropriately.
28 fseek(file_descriptor, 0L, SEEK_END);
29 int num_bytes = ftell(file_descriptor);
30 data->resize(num_bytes);
31
32 // Read the data.
33 fseek(file_descriptor, 0L, SEEK_SET);
34 int num_read =
35 fread(&((*data)[0]), sizeof((*data)[0]), num_bytes, file_descriptor);
36 if (num_read != num_bytes) {
37 LOG(FATAL) << "Couldn't read all of " << filename
38 << "expected bytes: " << num_bytes * sizeof((*data)[0])
39 << "actual bytes: " << num_read;
40 }
41 fclose(file_descriptor);
42 }
43
JoinPath(const string & dirname,const string & basename)44 string JoinPath(const string& dirname, const string& basename) {
45 #ifdef _WIN32
46 static const char separator = '\\';
47 #else
48 static const char separator = '/';
49 #endif // _WIN32
50
51 if ((!basename.empty() && basename[0] == separator) || dirname.empty()) {
52 return basename;
53 } else if (dirname[dirname.size() - 1] == separator) {
54 return dirname + basename;
55 } else {
56 return dirname + string(&separator, 1) + basename;
57 }
58 }
59
60 } // namespace xmpmeta
61 } // namespace dynamic_depth
62