Emulates dirent functionality for windows

This commit is contained in:
Jörg Hinrichs 2007-05-18 19:01:54 +00:00
parent addc6fc4e4
commit 5dc946d388
4 changed files with 135 additions and 2 deletions

View File

@ -13,6 +13,12 @@
#ifndef COLOR_RANGE_H_INCLUDED
#define COLOR_RANGE_H_INCLUDED
//These macros interfere with MS VC++
#ifdef _MSC_VER
#undef max
#undef min
#endif
#include "global.hpp"
#include <map>
#include <vector>
@ -25,7 +31,7 @@ std::vector<Uint32> string2rgb(std::string s);
class color_range
{
public:
color_range(Uint32 mid , Uint32 max , Uint32 min , Uint32 rep):mid_(mid),max_(max),min_(min),rep_(rep){};
color_range(Uint32 mid , Uint32 max = 0x00FFFFFF , Uint32 min = 0x00000000 , Uint32 rep = 0x00808080):mid_(mid),max_(max),min_(min),rep_(rep){};
color_range(const std::vector<Uint32>& v)
{
mid_ = v.size() ? v[0] : 0x00808080;

118
src/dirent_port.h Normal file
View File

@ -0,0 +1,118 @@
/*
* 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.
*
* May 18, 2007, YogiHH
* Made all the modifications to adapt this file for wesnoth. In particular
* removing the function definitions (they are provided by wesnoth already)
* and renaming/adding some of the DIR-struct members.
*
* 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
#include <windows.h>
#include <tchar.h>
#include <string.h>
#include <assert.h>
typedef struct dirent {
/* name of current directory entry (a multi-byte character string) */
char d_name[MAX_PATH + 1];
/* file attributes */
WIN32_FIND_DATA data;
int d_mode;
} dirent;
typedef struct wdirent {
/* name of current directory entry (a multi-byte character string) */
wchar_t d_name[MAX_PATH + 1];
/* file attributes */
WIN32_FIND_DATA data;
} wdirent;
typedef struct DIR {
/* current directory entry */
dirent dirent;
/* is there an un-processed entry in current? */
int cached;
/* file search handle */
HANDLE hFind;
/* search pattern (3 = zero terminator + pattern "\\*") */
TCHAR patt[MAX_PATH + 3];
_WIN32_FIND_DATAA find_data;
LPSTR directory;
} DIR;
static DIR *opendir (const char *dirname);
static struct dirent *readdir (DIR *dirp);
static int closedir (DIR *dirp);
#define _IFMT 0170000
#define _IFREG 0100000
#define _IFDIR 0040000
#define S_ISREG(m) (((m)&_IFMT) == _IFREG)
#define S_ISDIR(m) (((m)&_IFMT) == _IFDIR)
/* use the new safe string functions introduced in Visual Studio 2005 */
#if defined(_MSC_VER) && _MSC_VER >= 1400
# define STRNCPY(dest,src,size) strncpy_s((dest),(size),(src),_TRUNCATE)
#else
# define STRNCPY(dest,src,size) strncpy((dest),(src),(size))
#endif
#endif /*DIRENT_H*/

View File

@ -28,8 +28,14 @@
* This code swiped from dirent.c in the unixem library, version 1.7.3.
* See http://synesis.com.au/software/unixem.html for full sources.
* It's under BSD license.
* YogiHH, 05/17/2007:
* I added the include for dirent.h which originates from a port of a guy named
* Toni Ronkko (http://www.softagalleria.net/dirent/index.en.html) to make this
* work with Windows.
*/
#include <direct.h>
#include <io.h>
#include <errno.h>
#include <stdlib.h>
#include <windows.h>
@ -60,6 +66,7 @@
* Constants and definitions
*/
#include "dirent_port.h"
#ifndef FILE_ATTRIBUTE_ERROR
# define FILE_ATTRIBUTE_ERROR (0xFFFFFFFF)
#endif /* FILE_ATTRIBUTE_ERROR */
@ -68,6 +75,8 @@
* Typedefs
*/
typedef int mode_t;
struct dirent_dir
{
char directory[_MAX_DIR + 1]; /* . */

View File

@ -43,7 +43,7 @@ void get_files_in_dir(const std::string& dir,
std::vector<std::string>* files,
std::vector<std::string>* dirs=NULL,
FILE_NAME_MODE mode=FILE_NAME_ONLY,
FILE_REORDER_OPTION mode=DONT_REORDER);
FILE_REORDER_OPTION reorder=DONT_REORDER);
std::string get_dir(const std::string &dir);
//the location of various important files