From 9f1d6792e278ba8865988ce7190a12f24c60725d Mon Sep 17 00:00:00 2001 From: blahpy Date: Wed, 9 Sep 2020 18:41:45 +1200 Subject: [PATCH] Fix errors in common_util --- common/util/CMakeLists.txt | 2 -- common/util/FileUtil.cpp | 48 +++++++++++++++++++++++--------------- common/util/FileUtil.h | 10 ++++---- test/CMakeLists.txt | 6 ++--- test/test_common_util.cpp | 12 ++++++---- 5 files changed, 43 insertions(+), 35 deletions(-) diff --git a/common/util/CMakeLists.txt b/common/util/CMakeLists.txt index d6434a56b8..1e2bef4c7e 100644 --- a/common/util/CMakeLists.txt +++ b/common/util/CMakeLists.txt @@ -1,5 +1,3 @@ add_library(common_util SHARED FileUtil.cpp) - -target_link_libraries(common_util fmt) \ No newline at end of file diff --git a/common/util/FileUtil.cpp b/common/util/FileUtil.cpp index e6f19d6b4d..b4b9640b1a 100644 --- a/common/util/FileUtil.cpp +++ b/common/util/FileUtil.cpp @@ -1,26 +1,36 @@ #include "FileUtil.h" #include -#include +#include /* defines FILENAME_MAX */ -std::string FileUtil::get_file_path(std::string input[]) { - int arrSize = std::sizeof(input); - std::string currentPath = std::filesystem::current_path(); - char dirSeparator; +#ifdef _WIN32 + #include + #define GetCurrentDir _getcwd +#else + #include + #define GetCurrentDir getcwd +#endif - #ifdef _WIN32 - dirSeparator = '\'; - #else - dirSeparator = '/'; - #endif +std::string FileUtil::GetCurrentWorkingDir() { + char buff[FILENAME_MAX]; + GetCurrentDir(buff, FILENAME_MAX); + std::string current_working_dir(buff); + return current_working_dir; +} - std::string filePath = currentPath; - for (int i = 0; i < arrSize; i++) { - if (arrSize = i+1) { - filePath = filePath << input[i]; - } else { - filePath = filePath << input[i] << dirSeparator; - } +std::string FileUtil::get_file_path(std::vector input) { + std::string currentPath = FileUtil::GetCurrentWorkingDir(); // std::filesystem::current_path(); + char dirSeparator; + + #ifdef _WIN32 + dirSeparator = '\\'; + #else + dirSeparator = '/'; + #endif + + std::string filePath = currentPath; + for (int i = 0; i < input.size(); i++) { + filePath = filePath + dirSeparator + input[i]; } - return filePath; -} \ No newline at end of file + return filePath; +} diff --git a/common/util/FileUtil.h b/common/util/FileUtil.h index 925cd3d017..46906534d4 100644 --- a/common/util/FileUtil.h +++ b/common/util/FileUtil.h @@ -1,11 +1,9 @@ #pragma once #include +#include namespace FileUtil { - class FileUtil - { - public: - std::string get_file_path(); - }; -} \ No newline at end of file + std::string GetCurrentWorkingDir(); + std::string get_file_path(std::vector input); +} diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 1b8a271300..55f1ea2f17 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -14,13 +14,13 @@ add_executable(goalc-test test_emitter_loads_and_store.cpp test_emitter_xmm32.cpp test_emitter_integer_math.cpp - "test_common_util.cpp") + test_common_util.cpp) IF (WIN32) set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) # TODO - implement windows listener message("Windows Listener Not Implemented!") - target_link_libraries(goalc-test mman goos util runtime compiler type_system gtest) + target_link_libraries(goalc-test mman goos util common_util runtime compiler type_system gtest) ELSE() - target_link_libraries(goalc-test goos util listener runtime compiler type_system gtest) + target_link_libraries(goalc-test goos util common_util listener runtime compiler type_system gtest) ENDIF() diff --git a/test/test_common_util.cpp b/test/test_common_util.cpp index 02a3985f6f..e9862573c8 100644 --- a/test/test_common_util.cpp +++ b/test/test_common_util.cpp @@ -1,11 +1,13 @@ #include "common/util/FileUtil.h" -#include #include "gtest/gtest.h" +#include +#include -TEST(test, test) { +TEST(FileUtil, valid_path) { - std::string test[] = {"cabbage", "banana", "apple"}; - std::cout << FileUtil::get_file_path(test) << std::endl; + std::vector test = {"cabbage", "banana", "apple"}; + std::string sampleString = FileUtil::get_file_path(test); + // std::cout << sampleString << std::endl; EXPECT_TRUE(true); -} \ No newline at end of file +}