ladivic
Loading...
Searching...
No Matches
ldvc_io.hpp
Go to the documentation of this file.
1/*
2 * This file is part of the ladivic library.
3 * Copyright (c) 2024 Nathanne Isip
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24/**
25 *
26 * @file ldvc_io.hpp
27 * @brief Provides utilities for file input/output operations in C++.
28 *
29 * This header file defines functions for performing file input/output operations in C++,
30 * including writing data to a file, reading data from a file, checking file existence,
31 * and creating folders.
32 *
33 * @author Nathanne Isip
34 *
35 */
36
37#ifndef LDVC_IO_HPP
38#define LDVC_IO_HPP
39
40#include <fstream>
41#include <string>
42#include <stdexcept>
43#include <sys/stat.h>
44#include <ldvc_type.hpp>
45
46/**
47 *
48 * @brief Writes data to a file.
49 *
50 * This function writes the specified data to a file with the
51 * given filename. It opens the file in binary mode for writing
52 * and writes the binary representation of the data to the file.
53 *
54 * @tparam T The type of data to be written to the file.
55 *
56 * @param filename The filename of the file to write to.
57 * @param data The data to be written to the file.
58 *
59 * @throw std::runtime_error Thrown if the file cannot be opened for writing.
60 *
61 */
62template <typename T>
63void ldvc_write_file(const string& filename, const T& data)
64{
65 std::ofstream out_file(filename, std::ios::binary);
66 if(!out_file.is_open())
67 throw std::runtime_error("Failed to open file for writing: " + filename);
68
69 out_file.write(reinterpret_cast<const rune*>(&data), sizeof(T));
70 out_file.close();
71}
72
73/**
74 *
75 * @brief Reads data from a file.
76 *
77 * This function reads data from a file with the given filename and returns it.
78 * It opens the file in binary mode for reading and reads the binary representation
79 * of the data from the file.
80 *
81 * @tparam T The type of data to be read from the file.
82 *
83 * @param filename The filename of the file to read from.
84 *
85 * @return T The data read from the file.
86 *
87 * @throw std::runtime_error Thrown if the file cannot be opened for reading.
88 *
89 */
90template <typename T>
91T ldvc_read_file(const string& filename)
92{
93 std::ifstream in_file(filename, std::ios::binary);
94 if(!in_file.is_open())
95 throw std::runtime_error("Failed to open file for reading: " + filename);
96
97 T data;
98 in_file.read(reinterpret_cast<rune*>(&data), sizeof(T));
99 in_file.close();
100
101 return data;
102}
103
104/**
105 *
106 * @brief Checks if a file exists.
107 *
108 * This function checks if a file exists at the specified path.
109 *
110 * @param filename The filename or path of the file to check.
111 *
112 * @return bool True if the file exists, false otherwise.
113 *
114 */
115bool ldvc_file_exists(const string& folder_path);
116
117/**
118 *
119 * @brief Creates a folder with the specified permissions.
120 *
121 * This function creates a folder at the specified path with the given permissions.
122 *
123 * @param folder_path The path of the folder to create.
124 * @param mode The permissions for the folder.
125 *
126 * @return bool True if the folder was created successfully, false otherwise.
127 *
128 */
129bool ldvc_create_folder(const string& folder_path, u16 mode);
130
131/**
132 *
133 * @brief Deletes a file specified by its path.
134 *
135 * This function deletes the file located at the specified file path.
136 *
137 * @param file_path The path to the file to be deleted.
138 *
139 * @return true if the file is successfully deleted, false otherwise.
140 *
141 */
142bool ldvc_delete_file(string file_path);
143
144/**
145 *
146 * @brief Deletes a folder specified by its path.
147 *
148 * This function deletes the folder located at the specified folder path.
149 *
150 * @param folder_path The path to the folder to be deleted.
151 *
152 * @return true if the folder is successfully deleted, false otherwise.
153 *
154 */
155bool ldvc_delete_folder(string folder_path);
156
157#endif
void ldvc_write_file(const string &filename, const T &data)
Writes data to a file.
Definition ldvc_io.hpp:63
T ldvc_read_file(const string &filename)
Reads data from a file.
Definition ldvc_io.hpp:91
bool ldvc_delete_file(string file_path)
Deletes a file specified by its path.
bool ldvc_file_exists(const string &folder_path)
Checks if a file exists.
bool ldvc_create_folder(const string &folder_path, u16 mode)
Creates a folder with the specified permissions.
bool ldvc_delete_folder(string folder_path)
Deletes a folder specified by its path.
Defines common types used throughout the library.
char rune
Alias for a Unicode character.
Definition ldvc_type.hpp:45