ladivic
Loading...
Searching...
No Matches
ldvc_io.hpp File Reference

Provides utilities for file input/output operations in C++. More...

#include <fstream>
#include <string>
#include <stdexcept>
#include <sys/stat.h>
#include <ldvc_type.hpp>
Include dependency graph for ldvc_io.hpp:

Go to the source code of this file.

Functions

template<typename T >
void ldvc_write_file (const string &filename, const T &data)
 Writes data to a file.
 
template<typename T >
T ldvc_read_file (const string &filename)
 Reads data from a file.
 
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_file (string file_path)
 Deletes a file specified by its path.
 
bool ldvc_delete_folder (string folder_path)
 Deletes a folder specified by its path.
 

Detailed Description

Provides utilities for file input/output operations in C++.

This header file defines functions for performing file input/output operations in C++, including writing data to a file, reading data from a file, checking file existence, and creating folders.

Author
Nathanne Isip

Definition in file ldvc_io.hpp.

Function Documentation

◆ ldvc_create_folder()

bool ldvc_create_folder ( const string & folder_path,
u16 mode )

Creates a folder with the specified permissions.

This function creates a folder at the specified path with the given permissions.

Parameters
folder_pathThe path of the folder to create.
modeThe permissions for the folder.
Returns
bool True if the folder was created successfully, false otherwise.

◆ ldvc_delete_file()

bool ldvc_delete_file ( string file_path)

Deletes a file specified by its path.

This function deletes the file located at the specified file path.

Parameters
file_pathThe path to the file to be deleted.
Returns
true if the file is successfully deleted, false otherwise.

◆ ldvc_delete_folder()

bool ldvc_delete_folder ( string folder_path)

Deletes a folder specified by its path.

This function deletes the folder located at the specified folder path.

Parameters
folder_pathThe path to the folder to be deleted.
Returns
true if the folder is successfully deleted, false otherwise.

◆ ldvc_file_exists()

bool ldvc_file_exists ( const string & folder_path)

Checks if a file exists.

This function checks if a file exists at the specified path.

Parameters
filenameThe filename or path of the file to check.
Returns
bool True if the file exists, false otherwise.

◆ ldvc_read_file()

template<typename T >
T ldvc_read_file ( const string & filename)

Reads data from a file.

This function reads data from a file with the given filename and returns it. It opens the file in binary mode for reading and reads the binary representation of the data from the file.

Template Parameters
TThe type of data to be read from the file.
Parameters
filenameThe filename of the file to read from.
Returns
T The data read from the file.
Exceptions
std::runtime_errorThrown if the file cannot be opened for reading.

Definition at line 91 of file ldvc_io.hpp.

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}
T * ldvc_malloc(usize size)
Allocates memory for an array of elements.
Definition ldvc_mem.hpp:61
char rune
Alias for a Unicode character.
Definition ldvc_type.hpp:45

◆ ldvc_write_file()

template<typename T >
void ldvc_write_file ( const string & filename,
const T & data )

Writes data to a file.

This function writes the specified data to a file with the given filename. It opens the file in binary mode for writing and writes the binary representation of the data to the file.

Template Parameters
TThe type of data to be written to the file.
Parameters
filenameThe filename of the file to write to.
dataThe data to be written to the file.
Exceptions
std::runtime_errorThrown if the file cannot be opened for writing.

Definition at line 63 of file ldvc_io.hpp.

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}