Skip to content

Commit

Permalink
Refactors miniposix into a compatibility layer.
Browse files Browse the repository at this point in the history
Inital implementation is aimed mostly at windows.
Intention is to provide layer for ports to implement missing C runtime
functions going forward.
  • Loading branch information
OmniBlade committed Jul 22, 2022
1 parent 1f28f43 commit c2167aa
Show file tree
Hide file tree
Showing 31 changed files with 808 additions and 289 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ if(NOT DEFINED CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo" FORCE)
endif()

project(VanillaConquer CXX)
project(VanillaConquer C CXX)

option(BUILD_REMASTERTD "Build Tiberian Dawn remaster dll." OFF)
option(BUILD_REMASTERRA "Build Red Alert remaster dll." OFF)
Expand Down Expand Up @@ -140,6 +140,7 @@ if(BUILD_TESTS)
add_subdirectory(tests)
endif()

add_subdirectory(compat)
add_subdirectory(common)
add_subdirectory(tiberiandawn)
add_subdirectory(redalert)
Expand Down
2 changes: 2 additions & 0 deletions cmake/ClangFormat.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ if(CLANG_FORMAT_FOUND)
endif()

set(GLOB_PATTERNS
compat/*.c
compat/*.h
common/*.cpp
common/*.h
tiberiandawn/*.cpp
Expand Down
2 changes: 1 addition & 1 deletion common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ endif()
file(GLOB_RECURSE COMMON_HEADERS "*.h")

add_library(common STATIC ${COMMON_SRC} ${COMMON_HEADERS})
target_link_libraries(common PUBLIC ${COMMON_LIBS})
target_link_libraries(common PUBLIC vc_compat ${COMMON_LIBS})
target_include_directories(common PUBLIC .)
target_compile_definitions(common PRIVATE FIXIT_FAST_LOAD $<$<CONFIG:Debug>:_DEBUG>)
# Make build check state of git to check for uncommitted changes.
Expand Down
47 changes: 0 additions & 47 deletions common/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,50 +116,3 @@ int Reverse_Long(int number)
unsigned num = number;
return ((num >> 24) & 0xff) | ((num << 8) & 0xff0000) | ((num >> 8) & 0xff00) | ((num << 24) & 0xff000000);
}

void strtrim(char* str)
{
size_t len = 0;
char* frontp = str;
char* endp = NULL;

if (str == NULL || str[0] == '\0') {
return;
}

len = strlen(str);
endp = str + len;

/*
* Move the front and back pointers to address the first non-whitespace
* characters from each end.
*/
while (isspace((unsigned char)*frontp)) {
++frontp;
}

if (endp != frontp) {
while (isspace((unsigned char)*(--endp)) && endp != frontp) {
}
}

if (str + len - 1 != endp) {
*(endp + 1) = '\0';
} else if (frontp != str && endp == frontp) {
*str = '\0';
}

/*
* Shift the string so that it starts at str so that if it's dynamically
* allocated, we can still free it on the returned pointer. Note the reuse
* of endp to mean the front of the string buffer now.
*/
endp = str;
if (frontp != str) {
while (*frontp) {
*endp++ = *frontp++;
}

*endp = '\0';
}
}
1 change: 0 additions & 1 deletion common/miscasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ int First_False_Bit(void const* array);
int _Bound(int original, int min, int max);
#define Bound _Bound
int Reverse_Long(int number);
void strtrim(char* buffer);

#endif
2 changes: 1 addition & 1 deletion common/rawfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ char const* RawFileClass::Set_Name(char const* filename)
** if Resolve_File finds an actual file on-disk we use the real name
** instead.
*/
_strlwr(Filename);
strlwr(Filename);

/*
** Try to locate an existing file ignoring case, updates Filename
Expand Down
4 changes: 4 additions & 0 deletions common/utfargs.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
#ifndef UTFARGS_H
#define UTFARGS_H

#ifdef _WIN32
#include <shellapi.h>
#endif

class UtfArgs
{
public:
Expand Down
1 change: 1 addition & 0 deletions common/vqaaudio.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#ifdef _WIN32
#include <windows.h>
#include <mmsystem.h>
#ifndef OPENAL_BUILD
#include <dsound.h>
#endif
Expand Down
27 changes: 0 additions & 27 deletions common/wwstd.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,33 +258,6 @@ inline static void _splitpath(const char* path, char* drive, char* dir, char* fn
}
}

inline static char* strupr(char* str)
{
for (int i = 0; i < strlen(str); i++)
str[i] = toupper(str[i]);
return str;
}

inline static void strrev(char* str)
{
int len = strlen(str);

for (int i = 0; i < len / 2; i++) {
char c = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = c;
}
}

inline static void _strlwr(char* str)
{
int len = strlen(str);

for (int i = 0; i < len; i++) {
str[i] = tolower(str[i]);
}
}

#endif // not _WIN32

#endif // WWSTD_H
69 changes: 69 additions & 0 deletions compat/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
include(CheckIncludeFile)

# Check if we are missing any posix headers we consider required.
check_include_file(strings.h HAVE_STRINGS_H)
check_include_file(libgen.h HAVE_LIBGEN_H)
check_include_file(dirent.h HAVE_DIRENT_H)
check_include_file(getopt.h HAVE_GETOPT_H)
check_include_file(unistd.h HAVE_UNISTD_H)
check_include_file(fnmatch.h HAVE_FNMATCH_H)
include(CheckSymbolExists)

# Check if we are missing any functions known to exist in platform specific runtimes.
check_symbol_exists(strtrim "string.h" HAVE_STRTRIM)
check_symbol_exists(strlwr "string.h" HAVE_STRLWR)
check_symbol_exists(strupr "string.h" HAVE_STRUPR)
check_symbol_exists(strlcat "string.h" HAVE_STRLCAT)
check_symbol_exists(strlcpy "string.h" HAVE_STRLCPY)
check_symbol_exists(strrev "string.h" HAVE_STRREV)
configure_file(string.h.in string.h @ONLY)

if(HAVE_FNMATCH_H)
check_symbol_exists(fnmatch "fnmatch.h" HAVE_FNMATCH)
endif()

add_library(vc_compat STATIC)

if(WIN32)
target_compile_definitions(vc_compat PUBLIC _CRT_SECURE_NO_WARNINGS WIN32_LEAN_AND_MEAN)
endif()

target_sources(vc_compat PRIVATE
compat/endianness.h
string.c
${CMAKE_CURRENT_BINARY_DIR}/string.h
)

target_include_directories(vc_compat PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/compat ${CMAKE_CURRENT_BINARY_DIR})

if(NOT HAVE_STRINGS_H)
target_sources(vc_compat PRIVATE strings/strings.c strings/strings.h)
target_include_directories(vc_compat PUBLIC strings)
endif()

if(NOT HAVE_LIBGEN_H)
target_sources(vc_compat PRIVATE libgen/libgen.c libgen/libgen.h)
target_include_directories(vc_compat PUBLIC libgen)
endif()

if(NOT HAVE_DIRENT_H)
target_sources(vc_compat PRIVATE dirent/dirent.c dirent/dirent.h)
target_include_directories(vc_compat PUBLIC dirent)
endif()

if(NOT HAVE_UNISTD_H)
target_sources(vc_compat PRIVATE unistd/unistd.h)
target_include_directories(vc_compat PUBLIC unistd)
endif()

if(NOT HAVE_GETOPT_H)
target_sources(vc_compat PRIVATE getopt/getopt.c getopt/getopt.h)
target_include_directories(vc_compat PUBLIC getopt)
endif()

if(NOT HAVE_FNMATCH)
# TODO this should cover both windows and Vita SDK as it currently stands.
# Option remains to split header and imlementation inclusion if needed.
target_sources(vc_compat PRIVATE fnmatch/fnmatch.c fnmatch/fnmatch.h)
target_include_directories(vc_compat PUBLIC fnmatch)
endif()
File renamed without changes.
Loading

0 comments on commit c2167aa

Please sign in to comment.