Skip to content

Commit

Permalink
Use QFile/QString for C parser files and paths
Browse files Browse the repository at this point in the history
  • Loading branch information
GriffinRichards committed Dec 12, 2024
1 parent 11dd730 commit 6b70aba
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 36 deletions.
5 changes: 2 additions & 3 deletions include/lib/fex/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <cstdint>
#include <string>
#include <vector>
#include <QString>

namespace fex
{
Expand Down Expand Up @@ -89,9 +90,7 @@ namespace fex
Lexer() = default;
~Lexer() = default;

std::vector<Token> LexFile(const std::string &path);
std::vector<Token> LexString(const std::string &data);
void LexFileDumpTokens(const std::string &path, const std::string &out);
std::vector<Token> LexFile(const QString &path);

private:
std::vector<Token> Lex();
Expand Down
2 changes: 1 addition & 1 deletion include/lib/fex/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace fex
std::vector<Array> ParseTopLevelArrays(std::vector<Token> tokens);
std::map<std::string, ArrayValue> ParseTopLevelObjects(std::vector<Token> tokens);

std::map<std::string, int> ReadDefines(const std::string &filename, std::vector<std::string> matching);
std::map<std::string, int> ReadDefines(const QString &filename, std::vector<std::string> matching);

private:
int EvaluateExpression(std::vector<Token> tokens);
Expand Down
2 changes: 1 addition & 1 deletion src/core/parseutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ bool ParseUtil::gameStringToBool(QString gameString, bool * ok) {
QMap<QString, QHash<QString, QString>> ParseUtil::readCStructs(const QString &filename, const QString &label, const QHash<int, QString> memberMap) {
QString filePath = this->root + "/" + filename;
auto cParser = fex::Parser();
auto tokens = fex::Lexer().LexFile(filePath.toStdString());
auto tokens = fex::Lexer().LexFile(filePath);
auto structs = cParser.ParseTopLevelObjects(tokens);
QMap<QString, QHash<QString, QString>> structMaps;
for (auto it = structs.begin(); it != structs.end(); it++) {
Expand Down
39 changes: 9 additions & 30 deletions src/lib/fex/lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <fstream>
#include <iostream>
#include <sstream>
#include <QFile>

namespace fex
{
Expand Down Expand Up @@ -155,48 +156,26 @@ namespace fex
return Token(Token::Type::kDefine, filename_, line_number_);
}

std::vector<Token> Lexer::LexString(const std::string &data)
std::vector<Token> Lexer::LexFile(const QString &path)
{
filename_ = "string literal";
filename_ = path.toStdString();
line_number_ = 1;
index_ = 0;
data_ = data;

return Lex();
}

std::vector<Token> Lexer::LexFile(const std::string &path)
{
filename_ = path;
line_number_ = 1;
// Note: Using QFile instead of ifstream to handle encoding differences between platforms
// (specifically to handle accented characters on Windows)
QFile file(path);
file.open(QIODevice::ReadOnly);

std::ifstream file;
file.open(path);

std::stringstream stream;
stream << file.rdbuf();
const QByteArray data = file.readAll();

index_ = 0;
data_ = stream.str();
data_ = data.toStdString();

file.close();

return Lex();
}

void Lexer::LexFileDumpTokens(const std::string &path, const std::string &out)
{
std::ofstream file;
file.open(out);

for (Token token : LexFile(path))
{
file << token.ToString() << std::endl;
}

file.close();
}

std::vector<Token> Lexer::Lex()
{
std::vector<Token> tokens;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/fex/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ namespace fex
return DefineStatement(identifer, value);
}

std::map<std::string, int> Parser::ReadDefines(const std::string &filename, std::vector<std::string> matching)
std::map<std::string, int> Parser::ReadDefines(const QString &filename, std::vector<std::string> matching)
{
std::map<std::string, int> out;

Expand Down

0 comments on commit 6b70aba

Please sign in to comment.