aboutsummaryrefslogtreecommitdiffstats
path: root/yage/core/iomanager.cpp
blob: 1b2aba023c609df2f733ab6de7bf93d82677c651 (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
/** ---------------------------------------------------------------------------
 * -*- c++ -*-
 * @file: iomanager.cpp
 *
 * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com>
 * MIT License, see LICENSE file for more details.
 * ----------------------------------------------------------------------------
 */

#include <yage/core/iomanager.h>

#include <fstream>
#include <regex>
#include <stdexcept>

namespace yage
{

namespace IoManager
{

bool readFileToBuffer(std::string const &file_path,
                      std::vector<unsigned char> &buffer)
{
    std::ifstream file(file_path, std::ios::binary);
    if (!file.is_open()) {
        throw std::runtime_error("Could not open '" + file_path + "'");
    }

    // seek to the end
    file.seekg(0, std::ios::end);

    // get the file size
    int file_size = file.tellg();
    file.seekg(0, std::ios::beg);

    // reduce file size by header bytes
    file_size -= file.tellg();

    buffer.resize(file_size);
    file.read((char *)&buffer[0], file_size);
    file.close();

    return true;
}

} // namespace IoManager

} // namespace yage