summaryrefslogtreecommitdiffstats
path: root/src/utils/file_utils.cc
blob: d61a2cd6f0850b45041bda324e240db41a80e663 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "image_io/utils/file_utils.h"

#include <sys/stat.h>
#import <fstream>
#import <iostream>
#import <memory>

#include "image_io/base/data_range.h"
#include "image_io/base/message_handler.h"

namespace photos_editing_formats {
namespace image_io {

using std::fstream;
using std::istream;
using std::ostream;
using std::unique_ptr;

bool GetFileSize(const std::string& file_name, size_t* size) {
  struct stat stat_buf;
  if (stat(file_name.c_str(), &stat_buf)) {
    return false;
  } else {
    if (size) {
      *size = stat_buf.st_size;
    }
    return true;
  }
}

unique_ptr<ostream> OpenOutputFile(const std::string& file_name,
                                   ReportErrorPolicy report_error_policy) {
  auto* file_stream = new fstream(file_name, std::ios::out | std::ios::binary);
  if (file_stream && !file_stream->is_open()) {
    delete file_stream;
    file_stream = nullptr;
    if (report_error_policy == ReportErrorPolicy::kReportError) {
      MessageHandler::Get()->ReportMessage(Message::kStdLibError, file_name);
    }
  }
  return unique_ptr<ostream>(file_stream);
}

unique_ptr<istream> OpenInputFile(const std::string& file_name,
                                  ReportErrorPolicy report_error_policy) {
  auto* file_stream = new fstream(file_name, std::ios::in | std::ios::binary);
  if (file_stream && !file_stream->is_open()) {
    delete file_stream;
    file_stream = nullptr;
    if (report_error_policy == ReportErrorPolicy::kReportError) {
      MessageHandler::Get()->ReportMessage(Message::kStdLibError, file_name);
    }
  }
  return unique_ptr<istream>(file_stream);
}

std::shared_ptr<DataSegment> ReadEntireFile(
    const std::string& file_name, ReportErrorPolicy report_error_policy) {
  size_t buffer_size = 0;
  std::shared_ptr<DataSegment> shared_data_segment;
  if (GetFileSize(file_name, &buffer_size)) {
    unique_ptr<istream> shared_istream =
        OpenInputFile(file_name, ReportErrorPolicy::kDontReportError);
    if (shared_istream) {
      Byte* buffer = new Byte[buffer_size];
      if (buffer) {
        shared_data_segment =
            DataSegment::Create(DataRange(0, buffer_size), buffer);
        shared_istream->read(reinterpret_cast<char*>(buffer), buffer_size);
        size_t bytes_read = shared_istream->tellg();
        if (bytes_read != buffer_size) {
          shared_data_segment.reset();
        }
      }
    }
  }
  if (!shared_data_segment &&
      report_error_policy == ReportErrorPolicy::kReportError) {
    MessageHandler::Get()->ReportMessage(Message::kStdLibError, file_name);
  }
  return shared_data_segment;
}

}  // namespace image_io
}  // namespace photos_editing_formats