chore: extract io function to file

This commit is contained in:
ktyl 2023-07-27 23:23:51 +01:00 committed by Cat Flynn
parent d845c71385
commit 6f5a70981d
4 changed files with 30 additions and 20 deletions

View File

@ -24,7 +24,7 @@ elseif(APPLE)
message(FATAL_ERROR "macOS is not supported yet.") message(FATAL_ERROR "macOS is not supported yet.")
endif() endif()
add_executable(${PROJECT_NAME} hello.cpp) add_executable(${PROJECT_NAME} hello.cpp io.cpp)
if(WIN32) if(WIN32)
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD

View File

@ -40,29 +40,11 @@
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <iostream> #include <iostream>
#include <fstream>
#include <sstream>
#include <string> #include <string>
#include <chrono> #include <chrono>
std::string readFile(const std::string& filePath) #include "io.hpp"
{
std::ifstream fileStream(filePath, std::ios::in);
if (!fileStream.is_open())
{
std::cerr << "Could not read file " << filePath <<
". File does not exist." << std::endl;
return "";
}
std::stringstream sstr;
sstr << fileStream.rdbuf();
fileStream.close();
return sstr.str();
}
GLuint compileShader(const std::string& shaderPath, GLenum shaderType) GLuint compileShader(const std::string& shaderPath, GLenum shaderType)
{ {

23
io.cpp Normal file
View File

@ -0,0 +1,23 @@
#include "io.hpp"
#include <iostream>
#include <fstream>
#include <sstream>
std::string readFile(const std::string& filePath)
{
std::ifstream fileStream(filePath, std::ios::in);
if (!fileStream.is_open())
{
std::cerr << "Could not read file " << filePath <<
". File does not exist." << std::endl;
return "";
}
std::stringstream sstr;
sstr << fileStream.rdbuf();
fileStream.close();
return sstr.str();
}

5
io.hpp Normal file
View File

@ -0,0 +1,5 @@
#pragma once
#include <string>
std::string readFile(const std::string& filePath);