Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ CMakeCache.txt
CMakeFiles/
cmake_install.cmake
dist/
build/
build*/


# User-specific files
Expand Down
39 changes: 9 additions & 30 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,24 @@ project(
)

option(SIMPLEINI_USE_SYSTEM_GTEST "Use system GoogleTest dependency" OFF)
option(SIMPLEINI_BUILD_TESTS "Build tests" ON)
option(SIMPLEINI_BUILD_EXAMPLES "Build examples" ON)

# disable in-source builds
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR)
message(FATAL_ERROR "In-source builds are not allowed, use cmake -S . -B build.")
endif()

set(EXPORT_NAMESPACE "${PROJECT_NAME}::")
set(SIMPLEINI_HEADERS SimpleIni.h)

# ConvertUTF files are only needed on Windows for SI_CONVERT_WIN32
if(WIN32)
list(APPEND SIMPLEINI_HEADERS ConvertUTF.h)
set(SIMPLEINI_SOURCES ConvertUTF.c)
endif()

# SimpleIni is a header-only library. The ConvertUTF.c/h files in this repository
# are only required if users define SI_CONVERT_GENERIC. In that case, users must
# manually copy and compile ConvertUTF.c into their own projects.
add_library(${PROJECT_NAME} INTERFACE)
add_library(${EXPORT_NAMESPACE}${PROJECT_NAME} ALIAS ${PROJECT_NAME})
target_include_directories(${PROJECT_NAME} INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)

# On Windows, add ConvertUTF.c as a source file
if(WIN32)
target_sources(${PROJECT_NAME} INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/ConvertUTF.c>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/ConvertUTF.c>
)
endif()

include(GNUInstallDirs)

include(CMakePackageConfigHelpers)
Expand All @@ -49,28 +35,21 @@ write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Conf
)
configure_package_config_file(${PROJECT_NAME}Config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_DATADIR}/cmake/${PROJECT_NAME}
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
)

install(FILES ${SIMPLEINI_HEADERS}
install(FILES SimpleIni.h
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

# Install ConvertUTF.c on Windows (needed for SI_CONVERT_WIN32)
if(WIN32)
install(FILES ConvertUTF.c
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
endif()

install(TARGETS ${PROJECT_NAME}
EXPORT SimpleIniTargets
)

install(EXPORT SimpleIniTargets
FILE SimpleIniTargets.cmake
NAMESPACE SimpleIni::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/SimpleIni
NAMESPACE ${EXPORT_NAMESPACE}
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
)

install(FILES
Expand All @@ -79,8 +58,8 @@ install(FILES
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
)

# only build tests when top level and testing enabled
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND SIMPLEINI_BUILD_TESTS)
# only build tests when top level
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
include(CTest)
if(BUILD_TESTING)
add_subdirectory(tests)
Expand Down
2 changes: 2 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ set(TEST_SOURCES
ts-edgecases.cpp
ts-multiline.cpp
ts-casesensitivity.cpp
ts-generic.cpp
${CMAKE_SOURCE_DIR}/ConvertUTF.c
)

# ts-wchar.cpp uses wchar_t which is primarily for Windows
Expand Down
138 changes: 138 additions & 0 deletions tests/ts-generic.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#define SI_CONVERT_GENERIC
#include "../SimpleIni.h"
#include "gtest/gtest.h"

// Test SI_CONVERT_GENERIC with char interface
class TestGenericChar : public ::testing::Test {
protected:
void SetUp() override;
protected:
CSimpleIniA ini;
};

void TestGenericChar::SetUp() {
ini.SetUnicode();
SI_Error err = ini.LoadFile("tests.ini");
ASSERT_EQ(err, SI_OK);
}

TEST_F(TestGenericChar, TestSectionAKeyAValA) {
const char* result = ini.GetValue("section1", "key1");
ASSERT_STREQ(result, "value1");
}

TEST_F(TestGenericChar, TestSectionAKeyAValU) {
const char tesuto2[] = u8"テスト2";
const char* result = ini.GetValue("section2", "test2");
ASSERT_STREQ(result, tesuto2);
}

TEST_F(TestGenericChar, TestSectionAKeyUValA) {
const char tesuto[] = u8"テスト";
const char* result = ini.GetValue("section2", tesuto);
ASSERT_STREQ(result, "test");
}

TEST_F(TestGenericChar, TestSectionAKeyUValU) {
const char tesuto2[] = u8"テスト2";
const char tesutoni[] = u8"テスト二";
const char* result = ini.GetValue("section2", tesuto2);
ASSERT_STREQ(result, tesutoni);
}

TEST_F(TestGenericChar, TestSectionUKeyAValA) {
const char kensa[] = u8"検査";
const char* result = ini.GetValue(kensa, "key2");
ASSERT_STREQ(result, "value2");
}

TEST_F(TestGenericChar, TestSectionUKeyAValU) {
const char kensa[] = u8"検査";
const char tesuto2[] = u8"テスト2";
const char* result = ini.GetValue(kensa, "test2");
ASSERT_STREQ(result, tesuto2);
}

TEST_F(TestGenericChar, TestSectionUKeyUValA) {
const char kensa[] = u8"検査";
const char tesuto[] = u8"テスト";
const char* result = ini.GetValue(kensa, tesuto);
ASSERT_STREQ(result, "test");
}

TEST_F(TestGenericChar, TestSectionUKeyUValU) {
const char kensa[] = u8"検査";
const char tesuto2[] = u8"テスト2";
const char tesutoni[] = u8"テスト二";
const char* result = ini.GetValue(kensa, tesuto2);
ASSERT_STREQ(result, tesutoni);
}

#ifdef _WIN32
// Test SI_CONVERT_GENERIC with wchar_t interface (Windows only)
// On non-Windows platforms, wchar_t support with SI_CONVERT_GENERIC doesn't work the same way
class TestGenericWide : public ::testing::Test {
protected:
void SetUp() override;
protected:
CSimpleIniW ini;
};

void TestGenericWide::SetUp() {
ini.SetUnicode();
SI_Error err = ini.LoadFile("tests.ini");
ASSERT_EQ(err, SI_OK);
}

TEST_F(TestGenericWide, TestSectionAKeyAValA) {
const wchar_t* result = ini.GetValue(L"section1", L"key1");
ASSERT_STREQ(result, L"value1");
}

TEST_F(TestGenericWide, TestSectionAKeyAValU) {
const wchar_t tesuto2[] = L"テスト2";
const wchar_t* result = ini.GetValue(L"section2", L"test2");
ASSERT_STREQ(result, tesuto2);
}

TEST_F(TestGenericWide, TestSectionAKeyUValA) {
const wchar_t tesuto[] = L"テスト";
const wchar_t* result = ini.GetValue(L"section2", tesuto);
ASSERT_STREQ(result, L"test");
}

TEST_F(TestGenericWide, TestSectionAKeyUValU) {
const wchar_t tesuto2[] = L"テスト2";
const wchar_t tesutoni[] = L"テスト二";
const wchar_t* result = ini.GetValue(L"section2", tesuto2);
ASSERT_STREQ(result, tesutoni);
}

TEST_F(TestGenericWide, TestSectionUKeyAValA) {
const wchar_t kensa[] = L"検査";
const wchar_t* result = ini.GetValue(kensa, L"key2");
ASSERT_STREQ(result, L"value2");
}

TEST_F(TestGenericWide, TestSectionUKeyAValU) {
const wchar_t kensa[] = L"検査";
const wchar_t tesuto2[] = L"テスト2";
const wchar_t* result = ini.GetValue(kensa, L"test2");
ASSERT_STREQ(result, tesuto2);
}

TEST_F(TestGenericWide, TestSectionUKeyUValA) {
const wchar_t kensa[] = L"検査";
const wchar_t tesuto[] = L"テスト";
const wchar_t* result = ini.GetValue(kensa, tesuto);
ASSERT_STREQ(result, L"test");
}

TEST_F(TestGenericWide, TestSectionUKeyUValU) {
const wchar_t kensa[] = L"検査";
const wchar_t tesuto2[] = L"テスト2";
const wchar_t tesutoni[] = L"テスト二";
const wchar_t* result = ini.GetValue(kensa, tesuto2);
ASSERT_STREQ(result, tesutoni);
}
#endif // _WIN32