Moved read_file to a general base library
This commit is contained in:
2
Makefile
2
Makefile
@@ -35,7 +35,7 @@ VM_OUT=$(DIST)/ovm.out
|
|||||||
## ASSEMBLY setup
|
## ASSEMBLY setup
|
||||||
ASM_DIST=$(DIST)/asm
|
ASM_DIST=$(DIST)/asm
|
||||||
ASM_SRC=asm
|
ASM_SRC=asm
|
||||||
ASM_CODE:=$(addprefix $(ASM_SRC)/, lexer.cpp, preprocesser.cpp)
|
ASM_CODE:=$(addprefix $(ASM_SRC)/, base.cpp lexer.cpp preprocesser.cpp)
|
||||||
ASM_OBJECTS:=$(ASM_CODE:$(ASM_SRC)/%.cpp=$(ASM_DIST)/%.o)
|
ASM_OBJECTS:=$(ASM_CODE:$(ASM_SRC)/%.cpp=$(ASM_DIST)/%.o)
|
||||||
ASM_DEPS:=$(ASM_OBJECTS:%.o=%.d) $(ASM_DIST)/main.d
|
ASM_DEPS:=$(ASM_OBJECTS:%.o=%.d) $(ASM_DIST)/main.d
|
||||||
ASM_CFLAGS=$(CPPFLAGS)
|
ASM_CFLAGS=$(CPPFLAGS)
|
||||||
|
|||||||
32
asm/base.cpp
Normal file
32
asm/base.cpp
Normal file
@@ -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 <cstdio>
|
||||||
|
|
||||||
|
std::optional<std::string> 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;
|
||||||
|
}
|
||||||
20
asm/base.hpp
Normal file
20
asm/base.hpp
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
/* 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: Base library
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BASE_HPP
|
||||||
|
#define BASE_HPP
|
||||||
|
|
||||||
|
#include <optional>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
std::optional<std::string> read_file(const char *);
|
||||||
|
|
||||||
|
#endif
|
||||||
19
asm/main.cpp
19
asm/main.cpp
@@ -20,28 +20,11 @@
|
|||||||
|
|
||||||
#include <lib/inst.h>
|
#include <lib/inst.h>
|
||||||
|
|
||||||
|
#include "./base.hpp"
|
||||||
#include "./lexer.hpp"
|
#include "./lexer.hpp"
|
||||||
|
|
||||||
using std::pair, std::string, std::string_view, std::vector;
|
using std::pair, std::string, std::string_view, std::vector;
|
||||||
|
|
||||||
std::optional<string> read_file(const char *filename)
|
|
||||||
{
|
|
||||||
FILE *fp = fopen(filename, "rb");
|
|
||||||
if (fp)
|
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
void usage(const char *program_name, FILE *fp)
|
void usage(const char *program_name, FILE *fp)
|
||||||
{
|
{
|
||||||
fprintf(fp,
|
fprintf(fp,
|
||||||
|
|||||||
Reference in New Issue
Block a user