|
| 1 | +/* |
| 2 | +https://github.com/peterix/dfhack |
| 3 | +Copyright (c) 2009-2012 Petr Mrázek ([email protected]) |
| 4 | +
|
| 5 | +This software is provided 'as-is', without any express or implied |
| 6 | +warranty. In no event will the authors be held liable for any |
| 7 | +damages arising from the use of this software. |
| 8 | +
|
| 9 | +Permission is granted to anyone to use this software for any |
| 10 | +purpose, including commercial applications, and to alter it and |
| 11 | +redistribute it freely, subject to the following restrictions: |
| 12 | +
|
| 13 | +1. The origin of this software must not be misrepresented; you must |
| 14 | +not claim that you wrote the original software. If you use this |
| 15 | +software in a product, an acknowledgment in the product documentation |
| 16 | +would be appreciated but is not required. |
| 17 | +
|
| 18 | +2. Altered source versions must be plainly marked as such, and |
| 19 | +must not be misrepresented as being the original software. |
| 20 | +
|
| 21 | +3. This notice may not be removed or altered from any source |
| 22 | +distribution. |
| 23 | +*/ |
| 24 | +/* Based on luafilesystem |
| 25 | +Copyright © 2003-2014 Kepler Project. |
| 26 | +
|
| 27 | +Permission is hereby granted, free of charge, to any person |
| 28 | +obtaining a copy of this software and associated documentation |
| 29 | +files (the "Software"), to deal in the Software without |
| 30 | +restriction, including without limitation the rights to use, copy, |
| 31 | +modify, merge, publish, distribute, sublicense, and/or sell copies |
| 32 | +of the Software, and to permit persons to whom the Software is |
| 33 | +furnished to do so, subject to the following conditions: |
| 34 | +
|
| 35 | +The above copyright notice and this permission notice shall be |
| 36 | +included in all copies or substantial portions of the Software. |
| 37 | +
|
| 38 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 39 | +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 40 | +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 41 | +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 42 | +BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 43 | +ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 44 | +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 45 | +SOFTWARE. |
| 46 | +*/ |
| 47 | + |
| 48 | +#pragma once |
| 49 | +#include "Export.h" |
| 50 | + |
| 51 | +#ifndef _WIN32 |
| 52 | +#ifndef _AIX |
| 53 | +#define _FILE_OFFSET_BITS 64 /* Linux, Solaris and HP-UX */ |
| 54 | +#else |
| 55 | +#define _LARGE_FILES 1 /* AIX */ |
| 56 | +#endif |
| 57 | +#endif |
| 58 | + |
| 59 | +#ifndef _LARGEFILE64_SOURCE |
| 60 | +#define _LARGEFILE64_SOURCE |
| 61 | +#endif |
| 62 | + |
| 63 | +#include <errno.h> |
| 64 | +#include <stdio.h> |
| 65 | +#include <string.h> |
| 66 | +#include <stdlib.h> |
| 67 | +#include <time.h> |
| 68 | +#include <sys/stat.h> |
| 69 | + |
| 70 | +#ifdef _WIN32 |
| 71 | +#include <direct.h> |
| 72 | +#include <windows.h> |
| 73 | +#include <io.h> |
| 74 | +#include <sys/locking.h> |
| 75 | +#ifdef __BORLANDC__ |
| 76 | + #include <utime.h> |
| 77 | +#else |
| 78 | + #include <sys/utime.h> |
| 79 | +#endif |
| 80 | +#include <fcntl.h> |
| 81 | +#else |
| 82 | +#include <unistd.h> |
| 83 | +#include <dirent.h> |
| 84 | +#include <fcntl.h> |
| 85 | +#include <sys/types.h> |
| 86 | +#include <utime.h> |
| 87 | +#endif |
| 88 | + |
| 89 | +#define LFS_VERSION "1.6.2" |
| 90 | +#define LFS_LIBNAME "lfs" |
| 91 | + |
| 92 | +#if LUA_VERSION_NUM < 502 |
| 93 | +# define luaL_newlib(L,l) (lua_newtable(L), luaL_register(L,NULL,l)) |
| 94 | +#endif |
| 95 | + |
| 96 | +/* Define 'strerror' for systems that do not implement it */ |
| 97 | +#ifdef NO_STRERROR |
| 98 | +#define strerror(_) "System unable to describe the error" |
| 99 | +#endif |
| 100 | + |
| 101 | +/* Define 'getcwd' for systems that do not implement it */ |
| 102 | +#ifdef NO_GETCWD |
| 103 | +#define getcwd(p,s) NULL |
| 104 | +#define getcwd_error "Function 'getcwd' not provided by system" |
| 105 | +#else |
| 106 | +#define getcwd_error strerror(errno) |
| 107 | + #ifdef _WIN32 |
| 108 | + /* MAX_PATH seems to be 260. Seems kind of small. Is there a better one? */ |
| 109 | + #define LFS_MAXPATHLEN MAX_PATH |
| 110 | + #else |
| 111 | + /* For MAXPATHLEN: */ |
| 112 | + #include <sys/param.h> |
| 113 | + #define LFS_MAXPATHLEN MAXPATHLEN |
| 114 | + #endif |
| 115 | +#endif |
| 116 | + |
| 117 | +typedef struct dir_data { |
| 118 | + int closed; |
| 119 | +#ifdef _WIN32 |
| 120 | + intptr_t hFile; |
| 121 | + char pattern[MAX_PATH+1]; |
| 122 | +#else |
| 123 | + DIR *dir; |
| 124 | +#endif |
| 125 | +} dir_data; |
| 126 | + |
| 127 | +#ifdef _WIN32 |
| 128 | + #ifdef __BORLANDC__ |
| 129 | + #define lfs_setmode(L,file,m) ((void)L, setmode(_fileno(file), m)) |
| 130 | + #define STAT_STRUCT struct stati64 |
| 131 | + #else |
| 132 | + #define lfs_setmode(L,file,m) ((void)L, _setmode(_fileno(file), m)) |
| 133 | + #define STAT_STRUCT struct _stati64 |
| 134 | + #endif |
| 135 | +#define STAT_FUNC _stati64 |
| 136 | +#define LSTAT_FUNC STAT_FUNC |
| 137 | +#else |
| 138 | +#define _O_TEXT 0 |
| 139 | +#define _O_BINARY 0 |
| 140 | +#define lfs_setmode(L,file,m) ((void)L, (void)file, (void)m, 0) |
| 141 | +#define STAT_STRUCT struct stat |
| 142 | +#define STAT_FUNC stat |
| 143 | +#define LSTAT_FUNC lstat |
| 144 | +#endif |
| 145 | + |
| 146 | +#ifdef _WIN32 |
| 147 | + #ifndef S_ISDIR |
| 148 | + #define S_ISDIR(mode) (mode&_S_IFDIR) |
| 149 | + #endif |
| 150 | + #ifndef S_ISREG |
| 151 | + #define S_ISREG(mode) (mode&_S_IFREG) |
| 152 | + #endif |
| 153 | + #ifndef S_ISLNK |
| 154 | + #define S_ISLNK(mode) (0) |
| 155 | + #endif |
| 156 | + #ifndef S_ISSOCK |
| 157 | + #define S_ISSOCK(mode) (0) |
| 158 | + #endif |
| 159 | + #ifndef S_ISFIFO |
| 160 | + #define S_ISFIFO(mode) (0) |
| 161 | + #endif |
| 162 | + #ifndef S_ISCHR |
| 163 | + #define S_ISCHR(mode) (mode&_S_IFCHR) |
| 164 | + #endif |
| 165 | + #ifndef S_ISBLK |
| 166 | + #define S_ISBLK(mode) (0) |
| 167 | + #endif |
| 168 | +#endif |
| 169 | + |
| 170 | +enum _filetype { |
| 171 | + FILETYPE_NONE = -2, |
| 172 | + FILETYPE_UNKNOWN = -1, |
| 173 | + FILETYPE_FILE = 1, |
| 174 | + FILETYPE_DIRECTORY, |
| 175 | + FILETYPE_LINK, |
| 176 | + FILETYPE_SOCKET, |
| 177 | + FILETYPE_NAMEDPIPE, |
| 178 | + FILETYPE_CHAR_DEVICE, |
| 179 | + FILETYPE_BLOCK_DEVICE |
| 180 | +}; |
| 181 | + |
| 182 | +namespace DFHack { |
| 183 | + namespace Filesystem { |
| 184 | + DFHACK_EXPORT bool chdir (std::string path); |
| 185 | + DFHACK_EXPORT char * getcwd (); |
| 186 | + DFHACK_EXPORT bool mkdir (std::string path); |
| 187 | + DFHACK_EXPORT bool rmdir (std::string path); |
| 188 | + DFHACK_EXPORT bool stat (std::string path, STAT_STRUCT &info); |
| 189 | + DFHACK_EXPORT bool exists (std::string path); |
| 190 | + DFHACK_EXPORT _filetype filetype (std::string path); |
| 191 | + DFHACK_EXPORT bool isfile (std::string path); |
| 192 | + DFHACK_EXPORT bool isdir (std::string path); |
| 193 | + } |
| 194 | +} |
0 commit comments