From de5aa7e5e4e4e0f952cf315fc09d202816cc94d6 Mon Sep 17 00:00:00 2001 From: water111 <48171810+water111@users.noreply.github.com> Date: Thu, 10 Sep 2020 20:03:31 -0400 Subject: [PATCH] Move duplicated utilities to the common util folder and remove `NEXT_DIR` (#29) * move things to the common library and remove next_dir * fix for windows * one last windows fix * last fix for real this time * debug listener test * fix listener threading bug --- {decompiler => common}/util/BinaryReader.h | 0 common/util/CMakeLists.txt | 3 +- common/util/FileUtil.cpp | 71 +++++++++++++++++-- common/util/FileUtil.h | 11 ++- {goalc => common}/util/MatchParam.h | 2 - {decompiler => common}/util/Timer.cpp | 0 {decompiler => common}/util/Timer.h | 0 decompiler/CMakeLists.txt | 4 +- decompiler/Disasm/InstructionMatching.h | 18 +---- decompiler/ObjectFile/ObjectFileDB.cpp | 13 ++-- decompiler/config.cpp | 3 +- decompiler/main.cpp | 5 +- decompiler/util/FileIO.cpp | 35 --------- decompiler/util/FileIO.h | 4 -- game/CMakeLists.txt | 4 +- game/overlord/fake_iso.cpp | 15 +--- gc.sh | 2 - gk.sh | 2 - goalc/CMakeLists.txt | 7 +- goalc/compiler/Compiler.cpp | 4 +- goalc/compiler/Compiler.h | 5 +- goalc/compiler/Util.cpp | 5 +- .../compiler/compilation/CompilerControl.cpp | 8 +-- goalc/goos/CMakeLists.txt | 2 +- goalc/goos/Interpreter.cpp | 4 +- goalc/goos/Interpreter.h | 6 +- goalc/goos/InterpreterEval.cpp | 4 +- goalc/goos/Object.cpp | 4 +- goalc/goos/Reader.cpp | 20 ++---- goalc/goos/Reader.h | 3 +- goalc/goos/TextDB.cpp | 4 +- goalc/listener/Listener.cpp | 9 ++- goalc/util/CMakeLists.txt | 1 - goalc/util/Timer.cpp | 54 -------------- goalc/util/Timer.h | 47 ------------ goalc/util/file_io.cpp | 45 ------------ goalc/util/file_io.h | 14 ---- goalc/util/text_util.cpp | 16 ----- goalc/util/text_util.h | 13 ---- test-cov.sh | 2 - test.sh | 2 - test/CMakeLists.txt | 4 +- test/test_common_util.cpp | 2 +- test/test_reader.cpp | 10 +-- test_code_coverage.sh | 2 - 45 files changed, 138 insertions(+), 351 deletions(-) rename {decompiler => common}/util/BinaryReader.h (100%) rename {goalc => common}/util/MatchParam.h (93%) rename {decompiler => common}/util/Timer.cpp (100%) rename {decompiler => common}/util/Timer.h (100%) delete mode 100644 goalc/util/CMakeLists.txt delete mode 100644 goalc/util/Timer.cpp delete mode 100644 goalc/util/Timer.h delete mode 100644 goalc/util/file_io.cpp delete mode 100644 goalc/util/file_io.h delete mode 100644 goalc/util/text_util.cpp delete mode 100644 goalc/util/text_util.h diff --git a/decompiler/util/BinaryReader.h b/common/util/BinaryReader.h similarity index 100% rename from decompiler/util/BinaryReader.h rename to common/util/BinaryReader.h diff --git a/common/util/CMakeLists.txt b/common/util/CMakeLists.txt index 1e2bef4c7e..23f5a949df 100644 --- a/common/util/CMakeLists.txt +++ b/common/util/CMakeLists.txt @@ -1,3 +1,4 @@ add_library(common_util SHARED - FileUtil.cpp) + FileUtil.cpp + Timer.cpp) diff --git a/common/util/FileUtil.cpp b/common/util/FileUtil.cpp index 5db532eef5..01c07c1997 100644 --- a/common/util/FileUtil.cpp +++ b/common/util/FileUtil.cpp @@ -1,6 +1,9 @@ #include "FileUtil.h" #include #include /* defines FILENAME_MAX */ +#include +#include +#include #ifdef _WIN32 #include @@ -8,7 +11,7 @@ #include #endif -std::string FileUtil::GetProjectPath() { +std::string file_util::get_project_path() { #ifdef _WIN32 char buffer[FILENAME_MAX]; GetModuleFileNameA(NULL, buffer, FILENAME_MAX); @@ -16,20 +19,21 @@ std::string FileUtil::GetProjectPath() { "\\jak-project\\"); // Strip file path down to \jak-project\ directory return std::string(buffer).substr( 0, pos + 12); // + 12 to include "\jak-project" in the returned filepath -#else // do Linux stuff +#else + // do Linux stuff char buffer[FILENAME_MAX]; readlink("/proc/self/exe", buffer, FILENAME_MAX); // /proc/self acts like a "virtual folder" containing information about // the current process - std::string::size_type pos = std::string(buffer).find_last_of( + std::string::size_type pos = std::string(buffer).rfind( "/jak-project/"); // Strip file path down to /jak-project/ directory return std::string(buffer).substr( 0, pos + 12); // + 12 to include "/jak-project" in the returned filepath #endif } -std::string FileUtil::get_file_path(const std::vector& input) { - std::string currentPath = FileUtil::GetProjectPath(); +std::string file_util::get_file_path(const std::vector& input) { + std::string currentPath = file_util::get_project_path(); char dirSeparator; #ifdef _WIN32 @@ -39,9 +43,64 @@ std::string FileUtil::get_file_path(const std::vector& input) { #endif std::string filePath = currentPath; - for (int i = 0; i < input.size(); i++) { + for (int i = 0; i < int(input.size()); i++) { filePath = filePath + dirSeparator + input[i]; } return filePath; } + +void file_util::write_binary_file(const std::string& name, void* data, size_t size) { + FILE* fp = fopen(name.c_str(), "wb"); + if (!fp) { + throw std::runtime_error("couldn't open file " + name); + } + + if (fwrite(data, size, 1, fp) != 1) { + throw std::runtime_error("couldn't write file " + name); + } + + fclose(fp); +} + +void file_util::write_text_file(const std::string& file_name, const std::string& text) { + FILE* fp = fopen(file_name.c_str(), "w"); + if (!fp) { + printf("Failed to fopen %s\n", file_name.c_str()); + throw std::runtime_error("Failed to open file"); + } + fprintf(fp, "%s\n", text.c_str()); + fclose(fp); +} + +std::vector file_util::read_binary_file(const std::string& filename) { + auto fp = fopen(filename.c_str(), "rb"); + if (!fp) + throw std::runtime_error("File " + filename + " cannot be opened"); + fseek(fp, 0, SEEK_END); + auto len = ftell(fp); + rewind(fp); + + std::vector data; + data.resize(len); + + if (fread(data.data(), len, 1, fp) != 1) { + throw std::runtime_error("File " + filename + " cannot be read"); + } + + return data; +} + +std::string file_util::read_text_file(const std::string& path) { + std::ifstream file(path); + if (!file.good()) { + throw std::runtime_error("couldn't open " + path); + } + std::stringstream ss; + ss << file.rdbuf(); + return ss.str(); +} + +bool file_util::is_printable_char(char c) { + return c >= ' ' && c <= '~'; +} \ No newline at end of file diff --git a/common/util/FileUtil.h b/common/util/FileUtil.h index a5ebddcfe8..d2a2b68abc 100644 --- a/common/util/FileUtil.h +++ b/common/util/FileUtil.h @@ -2,7 +2,12 @@ #include #include -namespace FileUtil { -std::string GetProjectPath(); +namespace file_util { +std::string get_project_path(); std::string get_file_path(const std::vector& input); -} // namespace FileUtil +void write_binary_file(const std::string& name, void* data, size_t size); +void write_text_file(const std::string& file_name, const std::string& text); +std::vector read_binary_file(const std::string& filename); +std::string read_text_file(const std::string& path); +bool is_printable_char(char c); +} // namespace file_util diff --git a/goalc/util/MatchParam.h b/common/util/MatchParam.h similarity index 93% rename from goalc/util/MatchParam.h rename to common/util/MatchParam.h index e8a5cc8c12..5c954ea725 100644 --- a/goalc/util/MatchParam.h +++ b/common/util/MatchParam.h @@ -1,7 +1,6 @@ #ifndef JAK1_MATCHPARAM_H #define JAK1_MATCHPARAM_H -namespace util { template struct MatchParam { MatchParam() { is_wildcard = true; } @@ -18,6 +17,5 @@ struct MatchParam { bool operator==(const T& other) const { return is_wildcard || (value == other); } bool operator!=(const T& other) const { return !(*this == other); } }; -} // namespace util #endif // JAK1_MATCHPARAM_H diff --git a/decompiler/util/Timer.cpp b/common/util/Timer.cpp similarity index 100% rename from decompiler/util/Timer.cpp rename to common/util/Timer.cpp diff --git a/decompiler/util/Timer.h b/common/util/Timer.h similarity index 100% rename from decompiler/util/Timer.h rename to common/util/Timer.h diff --git a/decompiler/CMakeLists.txt b/decompiler/CMakeLists.txt index 16aecbbc36..eca54d7d2f 100644 --- a/decompiler/CMakeLists.txt +++ b/decompiler/CMakeLists.txt @@ -12,7 +12,6 @@ add_executable(decompiler util/FileIO.cpp config.cpp util/LispPrint.cpp - util/Timer.cpp Function/BasicBlocks.cpp Disasm/InstructionMatching.cpp TypeSystem/GoalType.cpp @@ -22,4 +21,5 @@ add_executable(decompiler TypeSystem/TypeSpec.cpp Function/CfgVtx.cpp Function/CfgVtx.h) target_link_libraries(decompiler - minilzo) \ No newline at end of file + minilzo + common_util) \ No newline at end of file diff --git a/decompiler/Disasm/InstructionMatching.h b/decompiler/Disasm/InstructionMatching.h index 96098cac92..54375d27a8 100644 --- a/decompiler/Disasm/InstructionMatching.h +++ b/decompiler/Disasm/InstructionMatching.h @@ -2,23 +2,7 @@ #define JAK_DISASSEMBLER_INSTRUCTIONMATCHING_H #include "Instruction.h" - -template -struct MatchParam { - MatchParam() { is_wildcard = true; } - - // intentionally not explicit so you don't have to put MatchParam(blah) everywhere - MatchParam(T x) { - value = x; - is_wildcard = false; - } - - T value; - bool is_wildcard = true; - - bool operator==(const T& other) { return is_wildcard || (value == other); } - bool operator!=(const T& other) { return !(*this == other); } -}; +#include "common/util/MatchParam.h" bool is_no_link_gpr_store(const Instruction& instr, MatchParam size, diff --git a/decompiler/ObjectFile/ObjectFileDB.cpp b/decompiler/ObjectFile/ObjectFileDB.cpp index 92a5c0c8d6..df1fbe711f 100644 --- a/decompiler/ObjectFile/ObjectFileDB.cpp +++ b/decompiler/ObjectFile/ObjectFileDB.cpp @@ -13,9 +13,10 @@ #include "LinkedObjectFileCreation.h" #include "decompiler/config.h" #include "third-party/minilzo/minilzo.h" -#include "decompiler/util/BinaryReader.h" +#include "common/util/BinaryReader.h" #include "decompiler/util/FileIO.h" -#include "decompiler/util/Timer.h" +#include "common/util/Timer.h" +#include "common/util/FileUtil.h" #include "decompiler/Function/BasicBlocks.h" /*! @@ -142,7 +143,7 @@ constexpr int MAX_CHUNK_SIZE = 0x8000; * Load the objects stored in the given DGO into the ObjectFileDB */ void ObjectFileDB::get_objs_from_dgo(const std::string& filename) { - auto dgo_data = read_binary_file(filename); + auto dgo_data = file_util::read_binary_file(filename); stats.total_dgo_bytes += dgo_data.size(); const char jak2_header[] = "oZlB"; @@ -415,7 +416,7 @@ void ObjectFileDB::write_object_file_words(const std::string& output_dir, bool d auto file_text = obj.linked_data.print_words(); auto file_name = combine_path(output_dir, obj.record.to_unique_name() + ".txt"); total_bytes += file_text.size(); - write_text_file(file_name, file_text); + file_util::write_text_file(file_name, file_text); total_files++; } }); @@ -442,7 +443,7 @@ void ObjectFileDB::write_disassembly(const std::string& output_dir, auto file_text = obj.linked_data.print_disassembly(); auto file_name = combine_path(output_dir, obj.record.to_unique_name() + ".func"); total_bytes += file_text.size(); - write_text_file(file_name, file_text); + file_util::write_text_file(file_name, file_text); total_files++; } }); @@ -517,7 +518,7 @@ void ObjectFileDB::find_and_write_scripts(const std::string& output_dir) { }); auto file_name = combine_path(output_dir, "all_scripts.lisp"); - write_text_file(file_name, all_scripts); + file_util::write_text_file(file_name, all_scripts); printf("Found scripts:\n"); printf(" total %.3f ms\n", timer.getMs()); diff --git a/decompiler/config.cpp b/decompiler/config.cpp index f633806745..9f67840882 100644 --- a/decompiler/config.cpp +++ b/decompiler/config.cpp @@ -1,6 +1,7 @@ #include "config.h" #include "third-party/json.hpp" #include "util/FileIO.h" +#include "common/util/FileUtil.h" Config gConfig; @@ -9,7 +10,7 @@ Config& get_config() { } void set_config(const std::string& path_to_config_file) { - auto config_str = read_text_file(path_to_config_file); + auto config_str = file_util::read_text_file(path_to_config_file); // to ignore comments in json, which may be useful auto cfg = nlohmann::json::parse(config_str, nullptr, true, true); diff --git a/decompiler/main.cpp b/decompiler/main.cpp index 034bc819ad..85d63c64a2 100644 --- a/decompiler/main.cpp +++ b/decompiler/main.cpp @@ -5,6 +5,7 @@ #include "config.h" #include "util/FileIO.h" #include "TypeSystem/TypeInfo.h" +#include "common/util/FileUtil.h" int main(int argc, char** argv) { printf("Jak Disassembler\n"); @@ -26,8 +27,8 @@ int main(int argc, char** argv) { } ObjectFileDB db(dgos); - write_text_file(combine_path(out_folder, "dgo.txt"), db.generate_dgo_listing()); - write_text_file(combine_path(out_folder, "obj.txt"), db.generate_obj_listing()); + file_util::write_text_file(combine_path(out_folder, "dgo.txt"), db.generate_dgo_listing()); + file_util::write_text_file(combine_path(out_folder, "obj.txt"), db.generate_obj_listing()); db.process_link_data(); db.find_code(); diff --git a/decompiler/util/FileIO.cpp b/decompiler/util/FileIO.cpp index 5466d18653..c92bb85524 100644 --- a/decompiler/util/FileIO.cpp +++ b/decompiler/util/FileIO.cpp @@ -3,35 +3,10 @@ #include #include -std::string read_text_file(const std::string& path) { - std::ifstream file(path); - std::stringstream ss; - ss << file.rdbuf(); - return ss.str(); -} - std::string combine_path(const std::string& parent, const std::string& child) { return parent + "/" + child; } -std::vector read_binary_file(const std::string& filename) { - auto fp = fopen(filename.c_str(), "rb"); - if (!fp) - throw std::runtime_error("File " + filename + " cannot be opened"); - fseek(fp, 0, SEEK_END); - auto len = ftell(fp); - rewind(fp); - - std::vector data; - data.resize(len); - - if (fread(data.data(), len, 1, fp) != 1) { - throw std::runtime_error("File " + filename + " cannot be read"); - } - - return data; -} - std::string base_name(const std::string& filename) { size_t pos = 0; assert(!filename.empty()); @@ -70,13 +45,3 @@ uint32_t crc32(const uint8_t* data, size_t size) { uint32_t crc32(const std::vector& data) { return crc32(data.data(), data.size()); } - -void write_text_file(const std::string& file_name, const std::string& text) { - FILE* fp = fopen(file_name.c_str(), "w"); - if (!fp) { - printf("Failed to fopen %s\n", file_name.c_str()); - throw std::runtime_error("Failed to open file"); - } - fprintf(fp, "%s\n", text.c_str()); - fclose(fp); -} \ No newline at end of file diff --git a/decompiler/util/FileIO.h b/decompiler/util/FileIO.h index 8f02577f4c..5893997f25 100644 --- a/decompiler/util/FileIO.h +++ b/decompiler/util/FileIO.h @@ -4,12 +4,8 @@ #include #include -std::string read_text_file(const std::string& path); std::string combine_path(const std::string& parent, const std::string& child); -std::vector read_binary_file(const std::string& filename); std::string base_name(const std::string& filename); -void write_text_file(const std::string& file_name, const std::string& text); - void init_crc(); uint32_t crc32(const uint8_t* data, size_t size); uint32_t crc32(const std::vector& data); diff --git a/game/CMakeLists.txt b/game/CMakeLists.txt index d73e4968fe..6fb1dacae2 100644 --- a/game/CMakeLists.txt +++ b/game/CMakeLists.txt @@ -78,8 +78,8 @@ add_library(runtime ${RUNTIME_SOURCE}) IF (WIN32) # set stuff for windows - target_link_libraries(gk cross_sockets mman) + target_link_libraries(gk cross_sockets mman common_util) ELSE() # set stuff for other systems - target_link_libraries(gk cross_sockets pthread) + target_link_libraries(gk cross_sockets pthread common_util) ENDIF() diff --git a/game/overlord/fake_iso.cpp b/game/overlord/fake_iso.cpp index 45603a03c6..76284f2531 100644 --- a/game/overlord/fake_iso.cpp +++ b/game/overlord/fake_iso.cpp @@ -15,6 +15,7 @@ #include "game/sce/iop.h" #include "isocommon.h" #include "overlord.h" +#include "common/util/FileUtil.h" using namespace iop; @@ -71,25 +72,15 @@ void fake_iso_init_globals() { read_in_progress = false; } -//! will hold prefix for the source folder. -static const char* next_dir = nullptr; -static const char* fake_iso_path = nullptr; - /*! * Initialize the file system. */ int FS_Init(u8* buffer) { (void)buffer; - // get path to next/. Will be set in the gk.sh launch script. - next_dir = std::getenv("NEXT_DIR"); - assert(next_dir); // get path to next/data/fake_iso.txt, the map file. char fakeiso_path[512]; - strcpy(fakeiso_path, next_dir); - fake_iso_path = std::getenv("FAKE_ISO_PATH"); - assert(fake_iso_path); - strcat(fakeiso_path, fake_iso_path); + strcpy(fakeiso_path, file_util::get_file_path({"game", "fake_iso.txt"}).c_str()); // open the map. FILE* fp = fopen(fakeiso_path, "r"); @@ -197,7 +188,7 @@ FileRecord* FS_FindIN(const char* iso_name) { static const char* get_file_path(FileRecord* fr) { assert(fr->location < fake_iso_entry_count); static char path_buffer[1024]; - strcpy(path_buffer, next_dir); + strcpy(path_buffer, file_util::get_project_path().c_str()); strcat(path_buffer, "/"); strcat(path_buffer, fake_iso_entries[fr->location].file_path); return path_buffer; diff --git a/gc.sh b/gc.sh index ac079580a1..1d1e30f93a 100755 --- a/gc.sh +++ b/gc.sh @@ -3,6 +3,4 @@ # Directory of this script DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" -export NEXT_DIR=$DIR -export FAKE_ISO_PATH=/game/fake_iso.txt $DIR/build/goalc/goalc "$@" \ No newline at end of file diff --git a/gk.sh b/gk.sh index d49ccff100..d103729b8f 100755 --- a/gk.sh +++ b/gk.sh @@ -3,6 +3,4 @@ # Directory of this script DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" -export NEXT_DIR=$DIR -export FAKE_ISO_PATH=/game/fake_iso.txt $DIR/build/game/gk "$@" diff --git a/goalc/CMakeLists.txt b/goalc/CMakeLists.txt index 2e8ab64256..5226e04a7f 100644 --- a/goalc/CMakeLists.txt +++ b/goalc/CMakeLists.txt @@ -1,4 +1,3 @@ -add_subdirectory(util) add_subdirectory(goos) add_subdirectory(listener) @@ -28,10 +27,10 @@ add_library(compiler add_executable(goalc main.cpp) IF (WIN32) - target_link_libraries(compiler util goos type_system listener mman) + target_link_libraries(compiler goos type_system listener mman common_util) ELSE () - target_link_libraries(compiler util goos type_system listener) + target_link_libraries(compiler goos type_system listener common_util) ENDIF () -target_link_libraries(goalc util goos compiler type_system) +target_link_libraries(goalc goos compiler type_system) diff --git a/goalc/compiler/Compiler.cpp b/goalc/compiler/Compiler.cpp index 1edede33bd..f950c4e229 100644 --- a/goalc/compiler/Compiler.cpp +++ b/goalc/compiler/Compiler.cpp @@ -15,7 +15,7 @@ Compiler::Compiler() { m_none = std::make_unique(m_ts.make_typespec("none")); // todo - compile library - Object library_code = m_goos.reader.read_from_file("goal_src/goal-lib.gc"); + Object library_code = m_goos.reader.read_from_file({"goal_src", "goal-lib.gc"}); compile_object_file("goal-lib", library_code, false); } @@ -178,7 +178,7 @@ std::vector Compiler::run_test(const std::string& source_code) { } } - auto code = m_goos.reader.read_from_file(source_code); + auto code = m_goos.reader.read_from_file({source_code}); auto compiled = compile_object_file("test-code", code, true); color_object_file(compiled); auto data = codegen_object_file(compiled); diff --git a/goalc/compiler/Compiler.h b/goalc/compiler/Compiler.h index 215ee61f0b..5d63eafb8a 100644 --- a/goalc/compiler/Compiler.h +++ b/goalc/compiler/Compiler.h @@ -48,9 +48,8 @@ class Compiler { void va_check( const goos::Object& form, const goos::Arguments& args, - const std::vector>& unnamed, - const std::unordered_map>>& - named); + const std::vector>& unnamed, + const std::unordered_map>>& named); std::string as_string(const goos::Object& o); std::string symbol_string(const goos::Object& o); const goos::Object& pair_car(const goos::Object& o); diff --git a/goalc/compiler/Util.cpp b/goalc/compiler/Util.cpp index c891c5be29..4795626bca 100644 --- a/goalc/compiler/Util.cpp +++ b/goalc/compiler/Util.cpp @@ -37,9 +37,8 @@ goos::Arguments Compiler::get_va(const goos::Object& form, const goos::Object& r void Compiler::va_check( const goos::Object& form, const goos::Arguments& args, - const std::vector>& unnamed, - const std::unordered_map>>& - named) { + const std::vector>& unnamed, + const std::unordered_map>>& named) { assert(args.rest.empty()); if (unnamed.size() != args.unnamed.size()) { throw_compile_error(form, "Got " + std::to_string(args.unnamed.size()) + diff --git a/goalc/compiler/compilation/CompilerControl.cpp b/goalc/compiler/compilation/CompilerControl.cpp index 6d09bf9067..be3d944d24 100644 --- a/goalc/compiler/compilation/CompilerControl.cpp +++ b/goalc/compiler/compilation/CompilerControl.cpp @@ -1,7 +1,7 @@ #include "goalc/compiler/Compiler.h" #include "goalc/compiler/IR.h" -#include "goalc/util/Timer.h" -#include "goalc/util/file_io.h" +#include "common/util/Timer.h" +#include "common/util/FileUtil.h" Val* Compiler::compile_exit(const goos::Object& form, const goos::Object& rest, Env* env) { (void)env; @@ -59,7 +59,7 @@ Val* Compiler::compile_asm_file(const goos::Object& form, const goos::Object& re }); Timer reader_timer; - auto code = m_goos.reader.read_from_file(filename); + auto code = m_goos.reader.read_from_file({filename}); timing.emplace_back("read", reader_timer.getMs()); Timer compile_timer; @@ -95,7 +95,7 @@ Val* Compiler::compile_asm_file(const goos::Object& form, const goos::Object& re // auto output_dir = as_string(get_constant_or_error(form, "*compiler-output-path*")); // todo, change extension based on v3/v4 auto output_name = m_goos.reader.get_source_dir() + "/out/" + obj_file_name + ".o"; - util::write_binary_file(output_name, (void*)data.data(), data.size()); + file_util::write_binary_file(output_name, (void*)data.data(), data.size()); } } else { if (load) { diff --git a/goalc/goos/CMakeLists.txt b/goalc/goos/CMakeLists.txt index a6b2e6b489..170c8df0d8 100644 --- a/goalc/goos/CMakeLists.txt +++ b/goalc/goos/CMakeLists.txt @@ -1,2 +1,2 @@ add_library(goos SHARED Object.cpp TextDB.cpp Reader.cpp Interpreter.cpp InterpreterEval.cpp) -target_link_libraries(goos util) \ No newline at end of file +target_link_libraries(goos common_util) \ No newline at end of file diff --git a/goalc/goos/Interpreter.cpp b/goalc/goos/Interpreter.cpp index 7e1c411315..3c3eb8569e 100644 --- a/goalc/goos/Interpreter.cpp +++ b/goalc/goos/Interpreter.cpp @@ -379,8 +379,8 @@ ArgumentSpec Interpreter::parse_arg_spec(const Object& form, Object& rest) { void Interpreter::vararg_check( const Object& form, const Arguments& args, - const std::vector>& unnamed, - const std::unordered_map>>& named) { + const std::vector>& unnamed, + const std::unordered_map>>& named) { assert(args.rest.empty()); if (unnamed.size() != args.unnamed.size()) { throw_eval_error(form, "Got " + std::to_string(args.unnamed.size()) + diff --git a/goalc/goos/Interpreter.h b/goalc/goos/Interpreter.h index 3d58a5c369..ebc78906ab 100644 --- a/goalc/goos/Interpreter.h +++ b/goalc/goos/Interpreter.h @@ -9,7 +9,7 @@ #include #include "Object.h" #include "Reader.h" -#include "goalc/util/MatchParam.h" +#include "common/util/MatchParam.h" namespace goos { class Interpreter { @@ -53,8 +53,8 @@ class Interpreter { void vararg_check( const Object& form, const Arguments& args, - const std::vector>& unnamed, - const std::unordered_map>>& named); + const std::vector>& unnamed, + const std::unordered_map>>& named); Object eval_pair(const Object& o, const std::shared_ptr& env); void eval_args(Arguments* args, const std::shared_ptr& env); diff --git a/goalc/goos/InterpreterEval.cpp b/goalc/goos/InterpreterEval.cpp index 8f73c46052..bd3e8eac47 100644 --- a/goalc/goos/InterpreterEval.cpp +++ b/goalc/goos/InterpreterEval.cpp @@ -66,7 +66,7 @@ Object Interpreter::eval_read_file(const Object& form, vararg_check(form, args, {ObjectType::STRING}, {}); try { - return reader.read_from_file(args.unnamed.at(0).as_string()->data); + return reader.read_from_file({args.unnamed.at(0).as_string()->data}); } catch (std::runtime_error& e) { throw_eval_error(form, std::string("reader error inside of read-file:\n") + e.what()); } @@ -84,7 +84,7 @@ Object Interpreter::eval_load_file(const Object& form, Object o; try { - o = reader.read_from_file(args.unnamed.at(0).as_string()->data); + o = reader.read_from_file({args.unnamed.at(0).as_string()->data}); } catch (std::runtime_error& e) { throw_eval_error(form, std::string("reader error inside of load-file:\n") + e.what()); } diff --git a/goalc/goos/Object.cpp b/goalc/goos/Object.cpp index 1023ff3581..000202538c 100644 --- a/goalc/goos/Object.cpp +++ b/goalc/goos/Object.cpp @@ -1,5 +1,5 @@ #include "Object.h" -#include "goalc/util/text_util.h" +#include "common/util/FileUtil.h" namespace goos { @@ -53,7 +53,7 @@ std::string fixed_to_string(FloatType x) { template <> std::string fixed_to_string(char x) { char buff[256]; - if (util::is_printable_char(x) && x != ' ') { + if (file_util::is_printable_char(x) && x != ' ') { // can print directly sprintf(buff, "#\\%c", x); return {buff}; diff --git a/goalc/goos/Reader.cpp b/goalc/goos/Reader.cpp index fa77d54996..a830564831 100644 --- a/goalc/goos/Reader.cpp +++ b/goalc/goos/Reader.cpp @@ -11,8 +11,7 @@ #include "Reader.h" #include "third-party/linenoise.h" -#include "goalc/util/file_io.h" -#include "goalc/util/text_util.h" +#include "common/util/FileUtil.h" namespace goos { @@ -98,15 +97,6 @@ Reader::Reader() { for (const char* c = bonus; *c; c++) { valid_symbols_chars[(int)*c] = true; } - - // find the source directory - auto result = std::getenv("NEXT_DIR"); - if (!result) { - throw std::runtime_error( - "Environment variable NEXT_DIR is not set. Please set this to point to next/"); - } - - source_dir = result; } /*! @@ -147,8 +137,8 @@ Object Reader::read_from_string(const std::string& str) { /*! * Read a file */ -Object Reader::read_from_file(const std::string& filename) { - auto textFrag = std::make_shared(util::combine_path(get_source_dir(), filename)); +Object Reader::read_from_file(const std::vector& file_path) { + auto textFrag = std::make_shared(file_util::get_file_path(file_path)); db.insert(textFrag); auto result = internal_read(textFrag); @@ -670,7 +660,7 @@ bool Reader::try_token_as_integer(const Token& tok, Object& obj) { bool Reader::try_token_as_char(const Token& tok, Object& obj) { if (tok.text.size() >= 3 && tok.text[0] == '#' && tok.text[1] == '\\') { - if (tok.text.size() == 3 && util::is_printable_char(tok.text[2]) && tok.text[2] != ' ') { + if (tok.text.size() == 3 && file_util::is_printable_char(tok.text[2]) && tok.text[2] != ' ') { obj = Object::make_char(tok.text[2]); return true; } @@ -705,6 +695,6 @@ void Reader::throw_reader_error(TextStream& here, const std::string& err, int se * Get the source directory of the current project. */ std::string Reader::get_source_dir() { - return source_dir; + return file_util::get_project_path(); } } // namespace goos diff --git a/goalc/goos/Reader.h b/goalc/goos/Reader.h index 1ac7e6a7a1..36920a737a 100644 --- a/goalc/goos/Reader.h +++ b/goalc/goos/Reader.h @@ -70,7 +70,7 @@ class Reader { Reader(); Object read_from_string(const std::string& str); Object read_from_stdin(const std::string& prompt_name); - Object read_from_file(const std::string& filename); + Object read_from_file(const std::vector& file_path); std::string get_source_dir(); @@ -97,7 +97,6 @@ class Reader { char valid_symbols_chars[256]; - std::string source_dir; std::unordered_map reader_macros; }; } // namespace goos diff --git a/goalc/goos/TextDB.cpp b/goalc/goos/TextDB.cpp index 75ad3369a1..318e6b8968 100644 --- a/goalc/goos/TextDB.cpp +++ b/goalc/goos/TextDB.cpp @@ -11,7 +11,7 @@ * (+ 1 (+ a b)) ; compute the sum */ -#include "goalc/util/file_io.h" +#include "common/util/FileUtil.h" #include "TextDB.h" @@ -79,7 +79,7 @@ std::pair SourceText::get_containing_line(int offset) { * Read text from a file. */ FileText::FileText(std::string filename_) : filename(std::move(filename_)) { - text = util::read_text_file(filename); + text = file_util::read_text_file(filename); build_offsets(); } diff --git a/goalc/listener/Listener.cpp b/goalc/listener/Listener.cpp index 1fdb4bbe3d..3a606d1249 100644 --- a/goalc/listener/Listener.cpp +++ b/goalc/listener/Listener.cpp @@ -151,8 +151,8 @@ bool Listener::connect_to_target(int n_tries, const std::string& ip, int port) { printf("Got version %d.%d", version_buffer[0], version_buffer[1]); if (version_buffer[0] == GOAL_VERSION_MAJOR && version_buffer[1] == GOAL_VERSION_MINOR) { printf(" OK!\n"); - rcv_thread = std::thread(&Listener::receive_func, this); m_connected = true; + rcv_thread = std::thread(&Listener::receive_func, this); receive_thread_running = true; return true; } else { @@ -189,10 +189,9 @@ void Listener::receive_func() { } ListenerMessageHeader* hdr = (ListenerMessageHeader*)buff; - // if(debug_listener) { - // printf("[T -> L] received %d bytes, kind %d\n", - // hdr->deci2_hdr.len, hdr->msg_kind); - // } + if (debug_listener) { + printf("[T -> L] received %d bytes, kind %d\n", hdr->deci2_header.len, int(hdr->msg_kind)); + } switch (hdr->msg_kind) { case ListenerMessageKind::MSG_ACK: diff --git a/goalc/util/CMakeLists.txt b/goalc/util/CMakeLists.txt deleted file mode 100644 index fdca90d0c6..0000000000 --- a/goalc/util/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -add_library(util SHARED text_util.cpp file_io.cpp Timer.cpp) \ No newline at end of file diff --git a/goalc/util/Timer.cpp b/goalc/util/Timer.cpp deleted file mode 100644 index 4ac44ab25c..0000000000 --- a/goalc/util/Timer.cpp +++ /dev/null @@ -1,54 +0,0 @@ -#include "Timer.h" - -#ifdef _WIN32 -#include -#define MS_PER_SEC 1000ULL // MS = milliseconds -#define US_PER_MS 1000ULL // US = microseconds -#define HNS_PER_US 10ULL // HNS = hundred-nanoseconds (e.g., 1 hns = 100 ns) -#define NS_PER_US 1000ULL - -#define HNS_PER_SEC (MS_PER_SEC * US_PER_MS * HNS_PER_US) -#define NS_PER_HNS (100ULL) // NS = nanoseconds -#define NS_PER_SEC (MS_PER_SEC * US_PER_MS * NS_PER_US) - -int Timer::clock_gettime_monotonic(struct timespec* tv) { - static LARGE_INTEGER ticksPerSec; - LARGE_INTEGER ticks; - double seconds; - - if (!ticksPerSec.QuadPart) { - QueryPerformanceFrequency(&ticksPerSec); - if (!ticksPerSec.QuadPart) { - errno = ENOTSUP; - return -1; - } - } - - QueryPerformanceCounter(&ticks); - - seconds = (double)ticks.QuadPart / (double)ticksPerSec.QuadPart; - tv->tv_sec = (time_t)seconds; - tv->tv_nsec = (long)((ULONGLONG)(seconds * NS_PER_SEC) % NS_PER_SEC); - - return 0; -} -#endif - -void Timer::start() { -#ifdef __linux__ - clock_gettime(CLOCK_MONOTONIC, &_startTime); -#elif _WIN32 - clock_gettime_monotonic(&_startTime); -#endif -} - -int64_t Timer::getNs() { - struct timespec now = {}; -#ifdef __linux__ - clock_gettime(CLOCK_MONOTONIC, &now); -#elif _WIN32 - clock_gettime_monotonic(&now); -#endif - return (int64_t)(now.tv_nsec - _startTime.tv_nsec) + - 1000000000 * (now.tv_sec - _startTime.tv_sec); -} diff --git a/goalc/util/Timer.h b/goalc/util/Timer.h deleted file mode 100644 index 5972020339..0000000000 --- a/goalc/util/Timer.h +++ /dev/null @@ -1,47 +0,0 @@ -#ifndef JAK_V2_TIMER_H -#define JAK_V2_TIMER_H - -#include -#include -#include - -/*! - * Timer for measuring time elapsed with clock_monotonic - */ -class Timer { - public: - /*! - * Construct and start timer - */ - explicit Timer() { start(); } - -#ifdef _WIN32 - int clock_gettime_monotonic(struct timespec* tv); -#endif - - /*! - * Start the timer - */ - void start(); - - /*! - * Get milliseconds elapsed - */ - double getMs() { return (double)getNs() / 1.e6; } - - double getUs() { return (double)getNs() / 1.e3; } - - /*! - * Get nanoseconds elapsed - */ - int64_t getNs(); - - /*! - * Get seconds elapsed - */ - double getSeconds() { return (double)getNs() / 1.e9; } - - struct timespec _startTime = {}; -}; - -#endif // JAK_V2_TIMER_H diff --git a/goalc/util/file_io.cpp b/goalc/util/file_io.cpp deleted file mode 100644 index 095ad933a8..0000000000 --- a/goalc/util/file_io.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include "file_io.h" -#include -#include -#include - -namespace util { -std::string read_text_file(const std::string& path) { - std::ifstream file(path); - if (!file.good()) { - throw std::runtime_error("couldn't open " + path); - } - std::stringstream ss; - ss << file.rdbuf(); - return ss.str(); -} - -std::string combine_path(const std::string& parent, const std::string& child) { - return parent + "/" + child; -} - -std::string combine_path(std::vector path) { - if (path.empty()) { - return {}; - } - std::string result = path.front(); - for (size_t i = 1; i < path.size(); i++) { - result = combine_path(result, path.at(i)); - } - return result; -} - -void write_binary_file(const std::string& name, void* data, size_t size) { - FILE* fp = fopen(name.c_str(), "wb"); - if (!fp) { - throw std::runtime_error("couldn't open file " + name); - } - - if (fwrite(data, size, 1, fp) != 1) { - throw std::runtime_error("couldn't write file " + name); - } - - fclose(fp); -} - -} // namespace util diff --git a/goalc/util/file_io.h b/goalc/util/file_io.h deleted file mode 100644 index 9fa7802040..0000000000 --- a/goalc/util/file_io.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef JAK1_FILE_IO_H -#define JAK1_FILE_IO_H - -#include -#include - -namespace util { -std::string read_text_file(const std::string& path); -std::string combine_path(const std::string& parent, const std::string& child); -std::string combine_path(std::vector path); -void write_binary_file(const std::string& name, void* data, size_t size); -} // namespace util - -#endif // JAK1_FILE_IO_H diff --git a/goalc/util/text_util.cpp b/goalc/util/text_util.cpp deleted file mode 100644 index 9139c06584..0000000000 --- a/goalc/util/text_util.cpp +++ /dev/null @@ -1,16 +0,0 @@ -/*! - * @file text_util.cpp - * Utilities for dealing with text. - */ - -#include "text_util.h" - -namespace util { -/*! - * Is c printable? Is true for letters, numbers, symbols, space, false for everything else. - * Note: newline/tab is not considered printable. - */ -bool is_printable_char(char c) { - return c >= ' ' && c <= '~'; -} -} // namespace util diff --git a/goalc/util/text_util.h b/goalc/util/text_util.h deleted file mode 100644 index fc61e5aa29..0000000000 --- a/goalc/util/text_util.h +++ /dev/null @@ -1,13 +0,0 @@ -/*! - * @file text_util.h - * Utilities for dealing with text. - */ - -#ifndef JAK1_TEXT_UTIL_H -#define JAK1_TEXT_UTIL_H - -namespace util { -bool is_printable_char(char c); -} - -#endif // JAK1_TEXT_UTIL_H diff --git a/test-cov.sh b/test-cov.sh index f5b98c515a..4d4e2bcc07 100644 --- a/test-cov.sh +++ b/test-cov.sh @@ -3,8 +3,6 @@ # Directory of this script DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" -export NEXT_DIR=$DIR -export FAKE_ISO_PATH=/game/fake_iso.txt cd $DIR/build/test make init make gcov diff --git a/test.sh b/test.sh index 573dafc226..fa5c17cf30 100755 --- a/test.sh +++ b/test.sh @@ -3,6 +3,4 @@ # Directory of this script DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" -export NEXT_DIR=$DIR -export FAKE_ISO_PATH=/game/fake_iso.txt $DIR/build/test/goalc-test --gtest_color=yes "$@" diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index faf41a4da6..adfec63cc2 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -22,9 +22,9 @@ IF (WIN32) set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) # TODO - split out these declarations for platform specific includes - target_link_libraries(goalc-test cross_sockets listener mman goos util common_util runtime compiler type_system gtest) + target_link_libraries(goalc-test cross_sockets listener mman goos common_util runtime compiler type_system gtest) ELSE() - target_link_libraries(goalc-test cross_sockets goos util common_util listener runtime compiler type_system gtest) + target_link_libraries(goalc-test cross_sockets goos common_util listener runtime compiler type_system gtest) ENDIF() if(CMAKE_COMPILER_IS_GNUCXX AND CODE_COVERAGE) diff --git a/test/test_common_util.cpp b/test/test_common_util.cpp index 96e1492133..91b9f642c7 100644 --- a/test/test_common_util.cpp +++ b/test/test_common_util.cpp @@ -5,7 +5,7 @@ TEST(FileUtil, valid_path) { std::vector test = {"cabbage", "banana", "apple"}; - std::string sampleString = FileUtil::get_file_path(test); + std::string sampleString = file_util::get_file_path(test); // std::cout << sampleString << std::endl; EXPECT_TRUE(true); diff --git a/test/test_reader.cpp b/test/test_reader.cpp index 59ce97e9de..d8f4b68252 100644 --- a/test/test_reader.cpp +++ b/test/test_reader.cpp @@ -6,7 +6,7 @@ #include "gtest/gtest.h" #include "goalc/goos/Reader.h" -#include "goalc/util/file_io.h" +#include "common/util/FileUtil.h" using namespace goos; @@ -316,16 +316,16 @@ TEST(GoosReader, TopLevel) { TEST(GoosReader, FromFile) { Reader reader; - auto result = reader.read_from_file(util::combine_path({"test", "test_reader_file0.gc"})).print(); + auto result = reader.read_from_file({"test", "test_reader_file0.gc"}).print(); EXPECT_TRUE(result == "(top-level (1 2 3 4))"); } TEST(GoosReader, TextDb) { // very specific to this particular test file, but whatever. Reader reader; - auto file_path = util::combine_path({"test", "test_reader_file0.gc"}); - auto result = reader.read_from_file(file_path).as_pair()->cdr.as_pair()->car; - std::string expected = "text from " + util::combine_path(reader.get_source_dir(), file_path) + + auto result = + reader.read_from_file({"test", "test_reader_file0.gc"}).as_pair()->cdr.as_pair()->car; + std::string expected = "text from " + file_util::get_file_path({"test", "test_reader_file0.gc"}) + ", line: 5\n(1 2 3 4)\n"; EXPECT_EQ(expected, reader.db.get_info_for(result)); } diff --git a/test_code_coverage.sh b/test_code_coverage.sh index 482651ed02..82d5464b08 100755 --- a/test_code_coverage.sh +++ b/test_code_coverage.sh @@ -3,7 +3,5 @@ # Directory of this script DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" -export NEXT_DIR=$DIR -export FAKE_ISO_PATH=/game/fake_iso.txt cd $DIR/build/ make goalc-test_coverage