aboutsummaryrefslogtreecommitdiff
path: root/CST 126/Homework_2/helpers.hpp
blob: d2edfd6f7b32626d2d28648bff39adb7702f823f (plain) (blame)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#ifndef HELPERS_HPP
#define HELPERS_HPP


#include <fstream>
#include <iostream>

// Read text from a file
inline char* ReadTextFromFile(const char* fileName, char* buffer)
{
    try
    {
        std::ifstream file(fileName);
        if (!file.is_open())
        {
            std::cerr << "Could not open file for text input: " << fileName << std::endl;
            return buffer;
        }

        file.seekg(0, std::ios::end);
        std::streamsize size = file.tellg();
        file.seekg(0, std::ios::beg);

        delete[] buffer;
        buffer = nullptr;

        buffer = new char[size + 1];
        file.read(buffer, size);
        buffer[size] = '\0'; // Null-terminate the string

        file.close();
        return buffer;
    }
    catch (const std::exception& ex)
    {
        std::cerr << "Exception during text file input: " << fileName << " was not successfully streamed to text. " << ex.what() << std::endl;
        return nullptr;
    }
}

// Read binary data from a file
inline char* ReadFileAsBinary(const char* fileName, char* buffer, size_t& size)
{
    try
    {
        std::ifstream file(fileName, std::ios::binary | std::ios::ate);
        if (!file.is_open()) {
            std::cerr << "Could not open file for binary input: " << fileName << std::endl;
            return nullptr;
        }

        size = file.tellg();
        file.seekg(0, std::ios::beg);

        delete[] buffer;
        buffer = new char[size + 1];

        file.read(buffer, size);
        file.close();

        buffer[size] = '\0';

        return buffer;
    }
    catch (const std::exception& ex)
    {
        std::cerr << "Exception during binary file input: " << fileName << " was not successfully streamed to binary. " << ex.what() << std::endl;
        return nullptr;
    }
}

//file writing things
inline bool WriteTextToFile(const char* fileName, const char* fileContents)
{
    std::ofstream file(fileName);
    if (!file.is_open())
    {
        std::cerr << "Could not open file for text output: " << fileName << std::endl;
        return false;
    }

    file << fileContents;

    file.close();
    return true;
}

// Write binary data to a file
inline bool WriteFileFromBinary(const char* fileName, const char* buffer, size_t size)
{
    try
    {
        std::ofstream file(fileName, std::ios::out | std::ios::binary);
        if (!file.is_open())
        {
            std::cerr << "Could not open file for binary output: " << fileName << std::endl;
            return false;
        }

        file.write(buffer, size);
        file.close();
        return true;
    }
    catch (const std::exception& ex)
    {
        std::cerr << "Exception during binary file output: " << fileName << ". " << ex.what() << std::endl;
        return false;
    }
}

#endif