File size: 1,392 Bytes
f8c5b0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#pragma once

// OpenCL SDK includes
#include <CL/Utils/Utils.hpp>

// STL includes
#include <fstream>
#include <string>

namespace cl {
namespace util {
    // Scott Meyers, Effective STL, Addison-Wesley Professional, 2001, Item 29
    // with error handling
    UTILSCPP_EXPORT
    std::string read_text_file(const char* const filename, cl_int* const error)
    {
        std::ifstream in(filename);
        if (in.good())
        {
            try
            {
                std::string red((std::istreambuf_iterator<char>(in)),
                                std::istreambuf_iterator<char>());
                if (in.good() && in.eof())
                {
                    if (error != nullptr) *error = CL_SUCCESS;
                    return red;
                }
                else
                {
                    detail::errHandler(CL_UTIL_FILE_OPERATION_ERROR, error,
                                       "File read error!");
                    return std::string();
                }
            } catch (std::bad_alloc& ex)
            {
                detail::errHandler(CL_OUT_OF_RESOURCES, error,
                                   "Bad allocation!");
                return std::string();
            }
        }
        else
        {
            detail::errHandler(CL_INVALID_VALUE, error, "No file!");
            return std::string();
        }
    }
}
}