1
1
Fork 0
mirror of https://github.com/azur1s/bobbylisp.git synced 2024-10-16 02:37:40 -05:00
bobbylisp/lib/io.hpp

53 lines
1.1 KiB
C++
Raw Normal View History

2022-03-12 17:26:59 -06:00
#pragma once
#include <iostream>
2022-03-12 18:17:12 -06:00
#include <fstream>
#include <string>
2022-03-12 17:26:59 -06:00
template<typename T>
/**
2022-03-13 01:22:53 -06:00
* Read the value from stdin and return it.
2022-03-12 17:26:59 -06:00
*/
T hazure_read() {
T x;
std::cin >> x;
return x;
}
template<typename T>
/**
2022-03-13 01:22:53 -06:00
* Prints the value of the variable to the stdout.
*
2022-03-12 17:26:59 -06:00
* @param value The value to print.
*/
void hazure_write(T x) {
std::cout << x;
2022-03-12 18:17:12 -06:00
}
/*
2022-03-13 01:22:53 -06:00
* Read the value from the file and return it.
2022-03-12 18:17:12 -06:00
*
* @param file_name The name of the file to read from.
* @return std::string The value read from the file.
*/
std::string hazure_read_file(std::string filename) {
std::ifstream file(filename);
std::string content((std::istreambuf_iterator<char>(file)),
(std::istreambuf_iterator<char>()));
return content;
}
/*
2022-03-13 01:22:53 -06:00
* Write string to file.
2022-03-12 18:17:12 -06:00
*
* @param filename The file name to write to.
* @param content The content to write.
*/
void hazure_write_file(std::string filename, std::string content) {
std::ofstream file(filename);
if (file.is_open()) {
file << content;
file.close();
} else {
std::cerr << "Unable to open " << filename << std::endl;
}
2022-03-12 17:26:59 -06:00
}