File size: 1,671 Bytes
71e354e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**************************************************************************************************
 *
 * Copyright (c) 2019-2026 Axera Semiconductor (Ningbo) Co., Ltd. All Rights Reserved.
 *
 * This source file is the property of Axera Semiconductor (Ningbo) Co., Ltd. and
 * may not be copied or distributed in any isomorphic form without the prior
 * written consent of Axera Semiconductor (Ningbo) Co., Ltd.
 *
 **************************************************************************************************/
#include "utils/memory_utils.hpp"

namespace utils {

bool file_exist(const std::string &path)
{
    auto flag = false;

    std::fstream fs(path, std::ios::in | std::ios::binary);
    flag = fs.is_open();
    fs.close();

    return flag;
}

bool read_file(const std::string &path, std::vector<char> &data)
{

    std::fstream fs(path, std::ios::in | std::ios::binary);

    if (!fs.is_open())
    {
        return false;
    }

    // get file size
    fs.seekg(0, std::ios::end);
    size_t file_size = fs.tellg();
    fs.seekg(0, std::ios::beg);

    if (file_size == 0)
    {
        return false;
    }

    data.resize(file_size);
    fs.read(data.data(), file_size);
    // data.insert(data.end(), std::istreambuf_iterator<char>(fs), std::istreambuf_iterator<char>());

    fs.close();

    return true;
}

bool read_file(const std::string &path, char **data, size_t *len)
{
    FILE *fp = fopen(path.c_str(), "rb");

    if (!fp)
    {
        return false;
    }

    fseek(fp, 0, SEEK_END);

    *len = ftell(fp);

    fseek(fp, 0, SEEK_SET);

    *data = new char[*len];

    fread(*data, *len, 1, fp);

    fclose(fp);

    return true;
}

}