From f661438c93806e42cfbbbd39d79c77924a4a5a16 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Mon, 15 Apr 2024 04:53:42 +0630 Subject: Moved read_file to a general base library --- asm/base.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 asm/base.cpp (limited to 'asm/base.cpp') diff --git a/asm/base.cpp b/asm/base.cpp new file mode 100644 index 0000000..d2812f1 --- /dev/null +++ b/asm/base.cpp @@ -0,0 +1,32 @@ +/* Copyright (C) 2024 Aryadev Chavali + + * You may distribute and modify this code under the terms of the GPLv2 + * license. You should have received a copy of the GPLv2 license with + * this file. If not, please write to: aryadev@aryadevchavali.com. + + * Created: 2024-04-14 + * Author: Aryadev Chavali + * Description: + */ + +#include "./base.hpp" + +#include + +std::optional read_file(const char *filename) +{ + FILE *fp = fopen(filename, "rb"); + if (fp) + { + std::string contents; + fseek(fp, 0, SEEK_END); + contents.resize(ftell(fp)); + rewind(fp); + fread(&contents[0], 1, contents.size(), fp); + fclose(fp); + + return contents; + } + else + return std::nullopt; +} -- cgit v1.2.3-13-gbd6f