mirror of
https://github.com/rwengine/openrw.git
synced 2024-11-22 02:12:45 +01:00
Merge pull request #130 from haphzd/mingw-w64
Make rwgame build and run on windows
This commit is contained in:
commit
7c8cdba196
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -1,3 +1,6 @@
|
||||
[submodule "external/inih"]
|
||||
path = external/inih
|
||||
url = https://github.com/benhoyt/inih.git
|
||||
[submodule "external/mman-win32"]
|
||||
path = external/mman-win32
|
||||
url = https://github.com/witwall/mman-win32.git
|
||||
|
@ -26,6 +26,9 @@ option(TESTS_NODATA "Build tests for no-data testing")
|
||||
#
|
||||
# Build configuration
|
||||
#
|
||||
if(NOT CMAKE_BUILD_TYPE)
|
||||
set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build, options are: Debug Release" FORCE)
|
||||
endif()
|
||||
|
||||
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
|
||||
add_definitions(-DRW_LINUX)
|
||||
@ -33,6 +36,10 @@ elseif (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
|
||||
add_definitions(-DRW_OSX)
|
||||
elseif(CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
|
||||
add_definitions(-DRW_FREEBSD)
|
||||
elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")
|
||||
add_definitions(-DRW_WINDOWS)
|
||||
set(BULLET_ROOT "$ENV{BULLET_ROOT}")
|
||||
include_directories("$ENV{BULLET_ROOT}/src")
|
||||
else ()
|
||||
message(FATAL_ERROR "Unknown platform \"${CMAKE_SYSTEM_NAME}\". please update CMakeLists.txt.")
|
||||
endif ()
|
||||
@ -41,6 +48,10 @@ if (${CMAKE_CXX_COMPILER_ID} STREQUAL Clang)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-gnu-array-member-paren-init")
|
||||
endif()
|
||||
|
||||
if(MINGW)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fpermissive")
|
||||
endif()
|
||||
|
||||
# Make GLM use radians
|
||||
add_definitions(-DGLM_FORCE_RADIANS)
|
||||
|
||||
|
3
cmake/external/CMakeLists.txt
vendored
3
cmake/external/CMakeLists.txt
vendored
@ -3,3 +3,6 @@
|
||||
set(EXTERNAL_PREFIX "${CMAKE_BINARY_DIR}/external")
|
||||
|
||||
include(inih.cmake)
|
||||
if(MINGW)
|
||||
include(mman-win32.cmake)
|
||||
endif()
|
||||
|
28
cmake/external/mman-win32.cmake
vendored
Normal file
28
cmake/external/mman-win32.cmake
vendored
Normal file
@ -0,0 +1,28 @@
|
||||
include(ExternalProject)
|
||||
SET(MMAN_W32_DIR ${PROJECT_SOURCE_DIR}/external/mman-win32)
|
||||
|
||||
SET(MMAN_W32_SOURCES
|
||||
${MMAN_W32_DIR}/mman.c)
|
||||
file(GLOB MMAN_W32_HEADERS
|
||||
${MMAN_W32_DIR}/mman.h)
|
||||
|
||||
add_library(mman
|
||||
${MMAN_W32_SOURCES})
|
||||
find_path(MINGWW64_ROOT NAMES include/sys/time.h PATH_SUFFIXES i686-w64-mingw32 mingw mingw32 )
|
||||
if (MINGWW64_ROOT)
|
||||
message(STATUS "MINGWW64_ROOT found - ${MINGWW64_ROOT}")
|
||||
else()
|
||||
message(FATAL_ERROR "MINGWW64_ROOT not found")
|
||||
endif()
|
||||
|
||||
target_include_directories(mman INTERFACE SYSTEM ${MMAN_W32_DIR})
|
||||
set_target_properties( mman
|
||||
PROPERTIES
|
||||
ARCHIVE_OUTPUT_DIRECTORY "${MINGWW64_ROOT}/lib"
|
||||
LIBRARY_OUTPUT_DIRECTORY "${MINGWW64_ROOT}/lib"
|
||||
)
|
||||
add_custom_command(
|
||||
TARGET mman
|
||||
COMMAND ${CMAKE_COMMAND} -E copy "${MMAN_W32_HEADERS}" "${MINGWW64_ROOT}/include/sys"
|
||||
COMMENT "Installing headers for libmman-win32..."
|
||||
)
|
1
external/mman-win32
vendored
Submodule
1
external/mman-win32
vendored
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit b7ec3707b738acf760a2cd1356b2b65c4877348e
|
@ -9,6 +9,10 @@ add_library(rwengine
|
||||
${RENDERWARE_HEADERS}
|
||||
)
|
||||
|
||||
if(MINGW)
|
||||
add_definitions(-D _USE_MATH_DEFINES)
|
||||
endif()
|
||||
|
||||
target_link_libraries(rwengine
|
||||
rwlib
|
||||
${MAD_LIBRARY}
|
||||
|
@ -8,6 +8,9 @@
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include <data/PathData.hpp>
|
||||
#ifdef RW_WINDOWS
|
||||
#include <rw_mingw.hpp>
|
||||
#endif
|
||||
|
||||
typedef uint16_t ObjectID;
|
||||
|
||||
@ -96,7 +99,7 @@ struct VehicleData : public ObjectInformation
|
||||
|
||||
VehicleData()
|
||||
: ObjectInformation(_class("CARS")) { }
|
||||
|
||||
|
||||
enum VehicleClass
|
||||
{
|
||||
IGNORE = 0,
|
||||
|
@ -2,6 +2,9 @@
|
||||
#define _VIEWFRUSTUM_HPP_
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#ifdef RW_WINDOWS
|
||||
#include <rw_mingw.hpp>
|
||||
#endif
|
||||
|
||||
class ViewFrustum
|
||||
{
|
||||
|
3
rwengine/include/rw_mingw.hpp
Normal file
3
rwengine/include/rw_mingw.hpp
Normal file
@ -0,0 +1,3 @@
|
||||
#undef IGNORE
|
||||
#undef near
|
||||
#undef far
|
@ -11,6 +11,9 @@
|
||||
|
||||
#include <glm/gtx/string_cast.hpp>
|
||||
#include <glm/gtx/norm.hpp>
|
||||
#ifdef RW_WINDOWS
|
||||
#include <rw_mingw.hpp>
|
||||
#endif
|
||||
|
||||
TrafficDirector::TrafficDirector(AIGraph* g, GameWorld* w)
|
||||
: graph( g ), world( w ), pedDensity(1.f), carDensity(1.f),
|
||||
|
@ -20,7 +20,11 @@
|
||||
#include <sstream>
|
||||
#include <algorithm>
|
||||
|
||||
#ifndef RW_WINDOWS
|
||||
#include <dirent.h>
|
||||
#else
|
||||
#include <platform/msdirent.h>
|
||||
#endif
|
||||
#include <sys/types.h>
|
||||
|
||||
/**
|
||||
@ -30,7 +34,7 @@
|
||||
*/
|
||||
std::string findPathRealCase(const std::string& base, const std::string& path)
|
||||
{
|
||||
#ifndef _WIN32
|
||||
#ifndef RW_WINDOWS
|
||||
size_t endslash = path.find("/");
|
||||
bool isDirectory = true;
|
||||
if(endslash == path.npos) {
|
||||
@ -65,7 +69,7 @@ std::string findPathRealCase(const std::string& base, const std::string& path)
|
||||
return "";
|
||||
#else
|
||||
// Is anything other than Windows likely to fall here?
|
||||
return path;
|
||||
return base + "/" + path;
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -133,7 +137,9 @@ void GameData::parseDAT(const std::string& path)
|
||||
for(std::string line, cmd; std::getline(datfile, line);)
|
||||
{
|
||||
if(line.size() == 0 || line[0] == '#') continue;
|
||||
line.erase(line.size()-1);
|
||||
#ifndef RW_WINDOWS
|
||||
line.erase(line.size()-1);
|
||||
#endif
|
||||
|
||||
size_t space = line.find_first_of(' ');
|
||||
if(space != line.npos)
|
||||
|
@ -23,6 +23,9 @@
|
||||
|
||||
#include <data/CutsceneData.hpp>
|
||||
#include <loaders/LoaderCutsceneDAT.hpp>
|
||||
#ifdef RW_WINDOWS
|
||||
#include <rw_mingw.hpp>
|
||||
#endif
|
||||
|
||||
#include <render/ViewCamera.hpp>
|
||||
|
||||
|
@ -1216,8 +1216,11 @@ bool SaveGame::loadGame(GameState& state, const std::string& file)
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#ifndef RW_WINDOWS
|
||||
#include <dirent.h>
|
||||
#else
|
||||
#include <platform/msdirent.h>
|
||||
#endif
|
||||
bool SaveGame::getSaveInfo(const std::string& file, BasicState *basicState)
|
||||
{
|
||||
std::FILE* loadFile = std::fopen(file.c_str(), "rb");
|
||||
|
@ -14,6 +14,9 @@
|
||||
#include <objects/PickupObject.hpp>
|
||||
#include <objects/CutsceneObject.hpp>
|
||||
#include <items/InventoryItem.hpp>
|
||||
#ifdef RW_WINDOWS
|
||||
#include <rw_mingw.hpp>
|
||||
#endif
|
||||
|
||||
constexpr float kDrawDistanceFactor = 1.0f;
|
||||
constexpr float kWorldDrawDistanceFactor = kDrawDistanceFactor;
|
||||
|
@ -23,6 +23,7 @@ add_executable(rwgame
|
||||
|
||||
include_directories(SYSTEM
|
||||
${BULLET_INCLUDE_DIR}
|
||||
${OPENAL_INCLUDE_DIR}
|
||||
)
|
||||
include_directories(
|
||||
"${CMAKE_SOURCE_DIR}/rwengine/include"
|
||||
@ -35,5 +36,12 @@ target_link_libraries(rwgame
|
||||
${BULLET_LIBRARIES}
|
||||
${SDL2_LIBRARY}
|
||||
)
|
||||
if(MINGW)
|
||||
add_definitions(-D _USE_MATH_DEFINES)
|
||||
target_link_libraries(rwgame
|
||||
iconv
|
||||
mman
|
||||
ws2_32)
|
||||
endif()
|
||||
|
||||
install(TARGETS rwgame RUNTIME DESTINATION "${BIN_DIR}")
|
||||
|
@ -59,7 +59,7 @@ std::string GameConfig::getDefaultConfigPath()
|
||||
return std::string(home) + "/Library/Preferences/" + kConfigDirectoryName;
|
||||
|
||||
#else
|
||||
#error Dont know how to find default config path
|
||||
return ".";
|
||||
#endif
|
||||
|
||||
// Well now we're stuck.
|
||||
|
@ -1,6 +1,4 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
|
||||
@ -65,15 +63,19 @@ size_t TcpSocket::send(const std::string& str)
|
||||
|
||||
void TcpSocket::disconnect()
|
||||
{
|
||||
close(sock);
|
||||
#ifndef RW_WINDOWS
|
||||
close(sock);
|
||||
#else
|
||||
closesocket(sock);
|
||||
#endif
|
||||
sock = -1;
|
||||
}
|
||||
|
||||
|
||||
std::string TcpSocket::getRemoteAddress() const
|
||||
char* TcpSocket::getRemoteAddress() const
|
||||
{
|
||||
char buffer[INET_ADDRSTRLEN+1] = { };
|
||||
return inet_ntop(AF_INET, &addr, buffer, INET_ADDRSTRLEN+1);
|
||||
return inet_ntoa(addr.sin_addr);
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,8 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#ifndef RW_WINDOWS
|
||||
#include <netinet/in.h>
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <arpa/inet.h>
|
||||
#else
|
||||
#include <ws2tcpip.h>
|
||||
#endif
|
||||
|
||||
class TcpSocket
|
||||
{
|
||||
@ -17,7 +22,7 @@ public:
|
||||
size_t send(const std::string& str);
|
||||
void disconnect();
|
||||
|
||||
std::string getRemoteAddress() const;
|
||||
char* getRemoteAddress() const;
|
||||
short getRemotePort() const;
|
||||
|
||||
private:
|
||||
|
@ -3,7 +3,12 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#ifndef RW_WINDOWS
|
||||
#include <dirent.h>
|
||||
#else
|
||||
#include <platform/msdirent.h>
|
||||
#endif
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
|
372
rwlib/source/platform/msdirent.h
Normal file
372
rwlib/source/platform/msdirent.h
Normal file
@ -0,0 +1,372 @@
|
||||
/*****************************************************************************
|
||||
* dirent.h - dirent API for Microsoft Visual Studio
|
||||
*
|
||||
* Copyright (C) 2006 Toni Ronkko
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* ``Software''), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included
|
||||
* in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL TONI RONKKO BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
||||
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
* OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* Mar 15, 2011, Toni Ronkko
|
||||
* Defined FILE_ATTRIBUTE_DEVICE for MSVC 6.0.
|
||||
*
|
||||
* Aug 11, 2010, Toni Ronkko
|
||||
* Added d_type and d_namlen fields to dirent structure. The former is
|
||||
* especially useful for determining whether directory entry represents a
|
||||
* file or a directory. For more information, see
|
||||
* http://www.delorie.com/gnu/docs/glibc/libc_270.html
|
||||
*
|
||||
* Aug 11, 2010, Toni Ronkko
|
||||
* Improved conformance to the standards. For example, errno is now set
|
||||
* properly on failure and assert() is never used. Thanks to Peter Brockam
|
||||
* for suggestions.
|
||||
*
|
||||
* Aug 11, 2010, Toni Ronkko
|
||||
* Fixed a bug in rewinddir(): when using relative directory names, change
|
||||
* of working directory no longer causes rewinddir() to fail.
|
||||
*
|
||||
* Dec 15, 2009, John Cunningham
|
||||
* Added rewinddir member function
|
||||
*
|
||||
* Jan 18, 2008, Toni Ronkko
|
||||
* Using FindFirstFileA and WIN32_FIND_DATAA to avoid converting string
|
||||
* between multi-byte and unicode representations. This makes the
|
||||
* code simpler and also allows the code to be compiled under MingW. Thanks
|
||||
* to Azriel Fasten for the suggestion.
|
||||
*
|
||||
* Mar 4, 2007, Toni Ronkko
|
||||
* Bug fix: due to the strncpy_s() function this file only compiled in
|
||||
* Visual Studio 2005. Using the new string functions only when the
|
||||
* compiler version allows.
|
||||
*
|
||||
* Nov 2, 2006, Toni Ronkko
|
||||
* Major update: removed support for Watcom C, MS-DOS and Turbo C to
|
||||
* simplify the file, updated the code to compile cleanly on Visual
|
||||
* Studio 2005 with both unicode and multi-byte character strings,
|
||||
* removed rewinddir() as it had a bug.
|
||||
*
|
||||
* Aug 20, 2006, Toni Ronkko
|
||||
* Removed all remarks about MSVC 1.0, which is antiqued now. Simplified
|
||||
* comments by removing SGML tags.
|
||||
*
|
||||
* May 14 2002, Toni Ronkko
|
||||
* Embedded the function definitions directly to the header so that no
|
||||
* source modules need to be included in the Visual Studio project. Removed
|
||||
* all the dependencies to other projects so that this very header can be
|
||||
* used independently.
|
||||
*
|
||||
* May 28 1998, Toni Ronkko
|
||||
* First version.
|
||||
*****************************************************************************/
|
||||
#ifndef DIRENT_H
|
||||
#define DIRENT_H
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <errno.h>
|
||||
|
||||
/* Entries missing from MSVC 6.0 */
|
||||
#if !defined(FILE_ATTRIBUTE_DEVICE)
|
||||
# define FILE_ATTRIBUTE_DEVICE 0x40
|
||||
#endif
|
||||
|
||||
/* File type and permission flags for stat() */
|
||||
#if defined(_MSC_VER) && !defined(S_IREAD)
|
||||
# define S_IFMT _S_IFMT /* file type mask */
|
||||
# define S_IFDIR _S_IFDIR /* directory */
|
||||
# define S_IFCHR _S_IFCHR /* character device */
|
||||
# define S_IFFIFO _S_IFFIFO /* pipe */
|
||||
# define S_IFREG _S_IFREG /* regular file */
|
||||
# define S_IREAD _S_IREAD /* read permission */
|
||||
# define S_IWRITE _S_IWRITE /* write permission */
|
||||
# define S_IEXEC _S_IEXEC /* execute permission */
|
||||
#endif
|
||||
#define S_IFBLK 0 /* block device */
|
||||
#define S_IFLNK 0 /* link */
|
||||
#define S_IFSOCK 0 /* socket */
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
# define S_IRUSR S_IREAD /* read, user */
|
||||
# define S_IWUSR S_IWRITE /* write, user */
|
||||
# define S_IXUSR 0 /* execute, user */
|
||||
# define S_IRGRP 0 /* read, group */
|
||||
# define S_IWGRP 0 /* write, group */
|
||||
# define S_IXGRP 0 /* execute, group */
|
||||
# define S_IROTH 0 /* read, others */
|
||||
# define S_IWOTH 0 /* write, others */
|
||||
# define S_IXOTH 0 /* execute, others */
|
||||
#endif
|
||||
|
||||
/* Indicates that d_type field is available in dirent structure */
|
||||
#define _DIRENT_HAVE_D_TYPE
|
||||
|
||||
/* File type flags for d_type */
|
||||
#define DT_UNKNOWN 0
|
||||
#define DT_REG S_IFREG
|
||||
#define DT_DIR S_IFDIR
|
||||
#define DT_FIFO S_IFFIFO
|
||||
#define DT_SOCK S_IFSOCK
|
||||
#define DT_CHR S_IFCHR
|
||||
#define DT_BLK S_IFBLK
|
||||
|
||||
/* Macros for converting between st_mode and d_type */
|
||||
#define IFTODT(mode) ((mode) & S_IFMT)
|
||||
#define DTTOIF(type) (type)
|
||||
|
||||
/*
|
||||
* File type macros. Note that block devices, sockets and links cannot be
|
||||
* distinguished on Windows and the macros S_ISBLK, S_ISSOCK and S_ISLNK are
|
||||
* only defined for compatibility. These macros should always return false
|
||||
* on Windows.
|
||||
*/
|
||||
#define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFFIFO)
|
||||
#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
|
||||
#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
|
||||
#define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
|
||||
#define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
|
||||
#define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)
|
||||
#define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct dirent
|
||||
{
|
||||
char d_name[MAX_PATH + 1]; /* File name */
|
||||
size_t d_namlen; /* Length of name without \0 */
|
||||
int d_type; /* File type */
|
||||
} dirent;
|
||||
|
||||
|
||||
typedef struct DIR
|
||||
{
|
||||
dirent curentry; /* Current directory entry */
|
||||
WIN32_FIND_DATAA find_data; /* Private file data */
|
||||
int cached; /* True if data is valid */
|
||||
HANDLE search_handle; /* Win32 search handle */
|
||||
char patt[MAX_PATH + 3]; /* Initial directory name */
|
||||
} DIR;
|
||||
|
||||
|
||||
/* Forward declarations */
|
||||
static DIR *opendir(const char *dirname);
|
||||
static struct dirent *readdir(DIR *dirp);
|
||||
static int closedir(DIR *dirp);
|
||||
static void rewinddir(DIR* dirp);
|
||||
|
||||
|
||||
/* Use the new safe string functions introduced in Visual Studio 2005 */
|
||||
#if defined(_MSC_VER) && _MSC_VER >= 1400
|
||||
# define DIRENT_STRNCPY(dest,src,size) strncpy_s((dest),(size),(src),_TRUNCATE)
|
||||
#else
|
||||
# define DIRENT_STRNCPY(dest,src,size) strncpy((dest),(src),(size))
|
||||
#endif
|
||||
|
||||
/* Set errno variable */
|
||||
#if defined(_MSC_VER)
|
||||
#define DIRENT_SET_ERRNO(x) _set_errno (x)
|
||||
#else
|
||||
#define DIRENT_SET_ERRNO(x) (errno = (x))
|
||||
#endif
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* Open directory stream DIRNAME for read and return a pointer to the
|
||||
* internal working area that is used to retrieve individual directory
|
||||
* entries.
|
||||
*/
|
||||
static DIR *opendir(const char *dirname)
|
||||
{
|
||||
DIR *dirp;
|
||||
|
||||
/* ensure that the resulting search pattern will be a valid file name */
|
||||
if (dirname == NULL) {
|
||||
DIRENT_SET_ERRNO (ENOENT);
|
||||
return NULL;
|
||||
}
|
||||
if (strlen (dirname) + 3 >= MAX_PATH) {
|
||||
DIRENT_SET_ERRNO (ENAMETOOLONG);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* construct new DIR structure */
|
||||
dirp = (DIR*) malloc (sizeof (struct DIR));
|
||||
if (dirp != NULL) {
|
||||
int error;
|
||||
|
||||
/*
|
||||
* Convert relative directory name to an absolute one. This
|
||||
* allows rewinddir() to function correctly when the current working
|
||||
* directory is changed between opendir() and rewinddir().
|
||||
*/
|
||||
if (GetFullPathNameA (dirname, MAX_PATH, dirp->patt, NULL)) {
|
||||
char *p;
|
||||
|
||||
/* append the search pattern "\\*\0" to the directory name */
|
||||
p = strchr (dirp->patt, '\0');
|
||||
if (dirp->patt < p && *(p-1) != '\\' && *(p-1) != ':') {
|
||||
*p++ = '\\';
|
||||
}
|
||||
*p++ = '*';
|
||||
*p = '\0';
|
||||
|
||||
/* open directory stream and retrieve the first entry */
|
||||
dirp->search_handle = FindFirstFileA (dirp->patt, &dirp->find_data);
|
||||
if (dirp->search_handle != INVALID_HANDLE_VALUE) {
|
||||
/* a directory entry is now waiting in memory */
|
||||
dirp->cached = 1;
|
||||
error = 0;
|
||||
} else {
|
||||
/* search pattern is not a directory name? */
|
||||
DIRENT_SET_ERRNO (ENOENT);
|
||||
error = 1;
|
||||
}
|
||||
} else {
|
||||
/* buffer too small */
|
||||
DIRENT_SET_ERRNO (ENOMEM);
|
||||
error = 1;
|
||||
}
|
||||
|
||||
if (error) {
|
||||
free (dirp);
|
||||
dirp = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return dirp;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* Read a directory entry, and return a pointer to a dirent structure
|
||||
* containing the name of the entry in d_name field. Individual directory
|
||||
* entries returned by this very function include regular files,
|
||||
* sub-directories, pseudo-directories "." and "..", but also volume labels,
|
||||
* hidden files and system files may be returned.
|
||||
*/
|
||||
static struct dirent *readdir(DIR *dirp)
|
||||
{
|
||||
DWORD attr;
|
||||
if (dirp == NULL) {
|
||||
/* directory stream did not open */
|
||||
DIRENT_SET_ERRNO (EBADF);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* get next directory entry */
|
||||
if (dirp->cached != 0) {
|
||||
/* a valid directory entry already in memory */
|
||||
dirp->cached = 0;
|
||||
} else {
|
||||
/* get the next directory entry from stream */
|
||||
if (dirp->search_handle == INVALID_HANDLE_VALUE) {
|
||||
return NULL;
|
||||
}
|
||||
if (FindNextFileA (dirp->search_handle, &dirp->find_data) == FALSE) {
|
||||
/* the very last entry has been processed or an error occured */
|
||||
FindClose (dirp->search_handle);
|
||||
dirp->search_handle = INVALID_HANDLE_VALUE;
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* copy as a multibyte character string */
|
||||
DIRENT_STRNCPY ( dirp->curentry.d_name,
|
||||
dirp->find_data.cFileName,
|
||||
sizeof(dirp->curentry.d_name) );
|
||||
dirp->curentry.d_name[MAX_PATH] = '\0';
|
||||
|
||||
/* compute the length of name */
|
||||
dirp->curentry.d_namlen = strlen (dirp->curentry.d_name);
|
||||
|
||||
/* determine file type */
|
||||
attr = dirp->find_data.dwFileAttributes;
|
||||
if ((attr & FILE_ATTRIBUTE_DEVICE) != 0) {
|
||||
dirp->curentry.d_type = DT_CHR;
|
||||
} else if ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0) {
|
||||
dirp->curentry.d_type = DT_DIR;
|
||||
} else {
|
||||
dirp->curentry.d_type = DT_REG;
|
||||
}
|
||||
return &dirp->curentry;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* Close directory stream opened by opendir() function. Close of the
|
||||
* directory stream invalidates the DIR structure as well as any previously
|
||||
* read directory entry.
|
||||
*/
|
||||
static int closedir(DIR *dirp)
|
||||
{
|
||||
if (dirp == NULL) {
|
||||
/* invalid directory stream */
|
||||
DIRENT_SET_ERRNO (EBADF);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* release search handle */
|
||||
if (dirp->search_handle != INVALID_HANDLE_VALUE) {
|
||||
FindClose (dirp->search_handle);
|
||||
dirp->search_handle = INVALID_HANDLE_VALUE;
|
||||
}
|
||||
|
||||
/* release directory structure */
|
||||
free (dirp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*****************************************************************************
|
||||
* Resets the position of the directory stream to which dirp refers to the
|
||||
* beginning of the directory. It also causes the directory stream to refer
|
||||
* to the current state of the corresponding directory, as a call to opendir()
|
||||
* would have done. If dirp does not refer to a directory stream, the effect
|
||||
* is undefined.
|
||||
*/
|
||||
static void rewinddir(DIR* dirp)
|
||||
{
|
||||
if (dirp != NULL) {
|
||||
/* release search handle */
|
||||
if (dirp->search_handle != INVALID_HANDLE_VALUE) {
|
||||
FindClose (dirp->search_handle);
|
||||
}
|
||||
|
||||
/* open new search handle and retrieve the first entry */
|
||||
dirp->search_handle = FindFirstFileA (dirp->patt, &dirp->find_data);
|
||||
if (dirp->search_handle != INVALID_HANDLE_VALUE) {
|
||||
/* a directory entry is now waiting in memory */
|
||||
dirp->cached = 1;
|
||||
} else {
|
||||
/* failed to re-open directory: no directory entry in memory */
|
||||
dirp->cached = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif /*DIRENT_H*/
|
Loading…
Reference in New Issue
Block a user