Implemented a function to read a file in full
Uses std::optional in case file doesn't exist.
This commit is contained in:
23
asm/main.cpp
23
asm/main.cpp
@@ -10,13 +10,36 @@
|
|||||||
* Description: Entrypoint for assembly program
|
* Description: Entrypoint for assembly program
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <optional>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include <lib/inst.h>
|
#include <lib/inst.h>
|
||||||
|
|
||||||
#include "./lexer.hpp"
|
#include "./lexer.hpp"
|
||||||
|
|
||||||
|
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