1.1 --- a/core/smn_filesystem.cpp Sat Jan 06 19:35:25 2007 +0000
1.2 +++ b/core/smn_filesystem.cpp Sun Jan 07 01:30:28 2007 +0000
1.3 @@ -1,6 +1,8 @@
1.4 +#include <sys/stat.h>
1.5 #include "sm_globals.h"
1.6 #include "HandleSys.h"
1.7 #include "LibrarySys.h"
1.8 +#include "sm_stringutil.h"
1.9
1.10 HandleType_t g_FileType;
1.11 HandleType_t g_DirType;
1.12 @@ -36,7 +38,7 @@
1.13 };
1.14
1.15
1.16 -cell_t sm_OpenDirectory(IPluginContext *pContext, const cell_t *params)
1.17 +static cell_t sm_OpenDirectory(IPluginContext *pContext, const cell_t *params)
1.18 {
1.19 char *path;
1.20 int err;
1.21 @@ -58,7 +60,7 @@
1.22 return g_HandleSys.CreateHandle(g_DirType, pDir, pContext->GetIdentity(), g_pCoreIdent, NULL);
1.23 }
1.24
1.25 -cell_t sm_ReadDirEntry(IPluginContext *pContext, const cell_t *params)
1.26 +static cell_t sm_ReadDirEntry(IPluginContext *pContext, const cell_t *params)
1.27 {
1.28 Handle_t hndl = static_cast<Handle_t>(params[1]);
1.29 IDirectory *pDir;
1.30 @@ -109,6 +111,324 @@
1.31 return true;
1.32 }
1.33
1.34 +static cell_t sm_OpenFile(IPluginContext *pContext, const cell_t *params)
1.35 +{
1.36 + char *name, *mode;
1.37 + int err;
1.38 + if ((err=pContext->LocalToString(params[1], &name)) != SP_ERROR_NONE)
1.39 + {
1.40 + pContext->ThrowNativeErrorEx(err, NULL);
1.41 + return 0;
1.42 + }
1.43 + if ((err=pContext->LocalToString(params[2], &mode)) != SP_ERROR_NONE)
1.44 + {
1.45 + pContext->ThrowNativeErrorEx(err, NULL);
1.46 + return 0;
1.47 + }
1.48 +
1.49 + char realpath[PLATFORM_MAX_PATH+1];
1.50 + g_LibSys.PathFormat(realpath, sizeof(realpath), "%s/%s", g_SourceMod.GetBaseDir(), name);
1.51 +
1.52 + FILE *pFile = fopen(realpath, mode);
1.53 + if (!pFile)
1.54 + {
1.55 + return 0;
1.56 + }
1.57 +
1.58 + return g_HandleSys.CreateHandle(g_FileType, pFile, pContext->GetIdentity(), g_pCoreIdent, NULL);
1.59 +}
1.60 +
1.61 +static cell_t sm_DeleteFile(IPluginContext *pContext, const cell_t *params)
1.62 +{
1.63 + char *name;
1.64 + int err;
1.65 + if ((err=pContext->LocalToString(params[1], &name)) != SP_ERROR_NONE)
1.66 + {
1.67 + pContext->ThrowNativeErrorEx(err, NULL);
1.68 + return 0;
1.69 + }
1.70 +
1.71 + char realpath[PLATFORM_MAX_PATH+1];
1.72 + g_LibSys.PathFormat(realpath, sizeof(realpath), "%s/%s", g_SourceMod.GetBaseDir(), name);
1.73 +
1.74 + return (unlink(realpath)) ? 0 : 1;
1.75 +}
1.76 +
1.77 +static cell_t sm_ReadFileLine(IPluginContext *pContext, const cell_t *params)
1.78 +{
1.79 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.80 + HandleError herr;
1.81 + HandleSecurity sec;
1.82 + FILE *pFile;
1.83 + int err;
1.84 +
1.85 + sec.pOwner = NULL;
1.86 + sec.pIdentity = g_pCoreIdent;
1.87 +
1.88 + if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile))
1.89 + != HandleError_None)
1.90 + {
1.91 + return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
1.92 + }
1.93 +
1.94 + char *buf;
1.95 + if ((err=pContext->LocalToString(params[2], &buf)) != SP_ERROR_NONE)
1.96 + {
1.97 + pContext->ThrowNativeErrorEx(err, NULL);
1.98 + return 0;
1.99 + }
1.100 +
1.101 + fgets(buf, params[3], pFile);
1.102 +
1.103 + return 1;
1.104 +}
1.105 +
1.106 +static cell_t sm_IsEndOfFile(IPluginContext *pContext, const cell_t *params)
1.107 +{
1.108 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.109 + HandleError herr;
1.110 + HandleSecurity sec;
1.111 + FILE *pFile;
1.112 +
1.113 + sec.pOwner = NULL;
1.114 + sec.pIdentity = g_pCoreIdent;
1.115 +
1.116 + if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile))
1.117 + != HandleError_None)
1.118 + {
1.119 + return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
1.120 + }
1.121 +
1.122 + return (feof(pFile)) ? 1 : 0;
1.123 +}
1.124 +
1.125 +static cell_t sm_FileSeek(IPluginContext *pContext, const cell_t *params)
1.126 +{
1.127 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.128 + HandleError herr;
1.129 + HandleSecurity sec;
1.130 + FILE *pFile;
1.131 +
1.132 + sec.pOwner = NULL;
1.133 + sec.pIdentity = g_pCoreIdent;
1.134 +
1.135 + if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile))
1.136 + != HandleError_None)
1.137 + {
1.138 + return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
1.139 + }
1.140 +
1.141 + fseek(pFile, params[2], params[3]);
1.142 +
1.143 + return 1;
1.144 +}
1.145 +
1.146 +static cell_t sm_FilePosition(IPluginContext *pContext, const cell_t *params)
1.147 +{
1.148 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.149 + HandleError herr;
1.150 + HandleSecurity sec;
1.151 + FILE *pFile;
1.152 +
1.153 + sec.pOwner = NULL;
1.154 + sec.pIdentity = g_pCoreIdent;
1.155 +
1.156 + if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile))
1.157 + != HandleError_None)
1.158 + {
1.159 + return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
1.160 + }
1.161 +
1.162 + return ftell(pFile);
1.163 +}
1.164 +
1.165 +static cell_t sm_FileExists(IPluginContext *pContext, const cell_t *params)
1.166 +{
1.167 + char *name;
1.168 + int err;
1.169 + if ((err=pContext->LocalToString(params[1], &name)) != SP_ERROR_NONE)
1.170 + {
1.171 + pContext->ThrowNativeErrorEx(err, NULL);
1.172 + return 0;
1.173 + }
1.174 +
1.175 + char realpath[PLATFORM_MAX_PATH+1];
1.176 + g_LibSys.PathFormat(realpath, sizeof(realpath), "%s/%s", g_SourceMod.GetBaseDir(), name);
1.177 +#ifdef PLATFORM_WINDOWS
1.178 + struct _stat s;
1.179 + if (_stat(realpath, &s) != 0)
1.180 + {
1.181 + return 0;
1.182 + }
1.183 + if (s.st_mode & S_IFREG)
1.184 + {
1.185 + return 1;
1.186 + }
1.187 + return 0;
1.188 +#elif PLATFORM_LINUX
1.189 + struct stat s;
1.190 + if (stat(realpath, &s) != 0)
1.191 + {
1.192 + return 0;
1.193 + }
1.194 + if (S_ISREG(s.st_mode))
1.195 + {
1.196 + return 1;
1.197 + }
1.198 + return 0;
1.199 +#endif
1.200 +}
1.201 +
1.202 +static cell_t sm_RenameFile(IPluginContext *pContext, const cell_t *params)
1.203 +{
1.204 + char *newpath, *oldpath;
1.205 + int err;
1.206 + if ((err=pContext->LocalToString(params[1], &newpath)) != SP_ERROR_NONE)
1.207 + {
1.208 + pContext->ThrowNativeErrorEx(err, NULL);
1.209 + return 0;
1.210 + }
1.211 + if ((err=pContext->LocalToString(params[2], &oldpath)) != SP_ERROR_NONE)
1.212 + {
1.213 + pContext->ThrowNativeErrorEx(err, NULL);
1.214 + return 0;
1.215 + }
1.216 +
1.217 + char new_realpath[PLATFORM_MAX_PATH+1];
1.218 + g_LibSys.PathFormat(new_realpath, sizeof(new_realpath), "%s/%s", g_SourceMod.GetBaseDir(), newpath);
1.219 + char old_realpath[PLATFORM_MAX_PATH+1];
1.220 + g_LibSys.PathFormat(old_realpath, sizeof(old_realpath), "%s/%s", g_SourceMod.GetBaseDir(), oldpath);
1.221 +
1.222 +#ifdef PLATFORM_WINDOWS
1.223 + return (MoveFileA(old_realpath, new_realpath)) ? 1 : 0;
1.224 +#elif PLATFORM_LINUX
1.225 + return (rename(old_realpath, new_realpath)) ? 0 : 1;
1.226 +#endif
1.227 +}
1.228 +
1.229 +static cell_t sm_DirExists(IPluginContext *pContext, const cell_t *params)
1.230 +{
1.231 + char *name;
1.232 + int err;
1.233 + if ((err=pContext->LocalToString(params[1], &name)) != SP_ERROR_NONE)
1.234 + {
1.235 + pContext->ThrowNativeErrorEx(err, NULL);
1.236 + return 0;
1.237 + }
1.238 +
1.239 + char realpath[PLATFORM_MAX_PATH+1];
1.240 + g_LibSys.PathFormat(realpath, sizeof(realpath), "%s/%s", g_SourceMod.GetBaseDir(), name);
1.241 +#ifdef PLATFORM_WINDOWS
1.242 + struct _stat s;
1.243 + if (_stat(realpath, &s) != 0)
1.244 + {
1.245 + return 0;
1.246 + }
1.247 + if (s.st_mode & S_IFDIR)
1.248 + {
1.249 + return 1;
1.250 + }
1.251 + return 0;
1.252 +#elif PLATFORM_LINUX
1.253 + struct stat s;
1.254 + if (stat(realpath, &s) != 0)
1.255 + {
1.256 + return 0;
1.257 + }
1.258 + if (S_ISDIR(s.st_mode))
1.259 + {
1.260 + return 1;
1.261 + }
1.262 + return 0;
1.263 +#endif
1.264 +}
1.265 +
1.266 +static cell_t sm_FileSize(IPluginContext *pContext, const cell_t *params)
1.267 +{
1.268 + char *name;
1.269 + int err;
1.270 + if ((err=pContext->LocalToString(params[1], &name)) != SP_ERROR_NONE)
1.271 + {
1.272 + pContext->ThrowNativeErrorEx(err, NULL);
1.273 + return -1;
1.274 + }
1.275 +
1.276 + char realpath[PLATFORM_MAX_PATH+1];
1.277 + g_LibSys.PathFormat(realpath, sizeof(realpath), "%s/%s", g_SourceMod.GetBaseDir(), name);
1.278 +#ifdef PLATFORM_WINDOWS
1.279 + struct _stat s;
1.280 + if (_stat(realpath, &s) != 0)
1.281 + {
1.282 + return -1;
1.283 + }
1.284 + if (s.st_mode & S_IFREG)
1.285 + {
1.286 + return static_cast<cell_t>(s.st_size);
1.287 + }
1.288 + return -1;
1.289 +#elif PLATFORM_LINUX
1.290 + struct stat s;
1.291 + if (stat(realpath, &s) != 0)
1.292 + {
1.293 + return -1;
1.294 + }
1.295 + if (S_ISREG(s.st_mode))
1.296 + {
1.297 + return static_cast<cell_t>(s.st_size);
1.298 + }
1.299 + return -1;
1.300 +#endif
1.301 +}
1.302 +
1.303 +static cell_t sm_RemoveDir(IPluginContext *pContext, const cell_t *params)
1.304 +{
1.305 + char *name;
1.306 + int err;
1.307 + if ((err=pContext->LocalToString(params[1], &name)) != SP_ERROR_NONE)
1.308 + {
1.309 + pContext->ThrowNativeErrorEx(err, NULL);
1.310 + return 0;
1.311 + }
1.312 +
1.313 + char realpath[PLATFORM_MAX_PATH+1];
1.314 + g_LibSys.PathFormat(realpath, sizeof(realpath), "%s/%s", g_SourceMod.GetBaseDir(), name);
1.315 +
1.316 + return (rmdir(realpath)) ? 0 : 1;
1.317 +}
1.318 +
1.319 +static cell_t sm_WriteFileLine(IPluginContext *pContext, const cell_t *params)
1.320 +{
1.321 + Handle_t hndl = static_cast<Handle_t>(params[1]);
1.322 + HandleError herr;
1.323 + HandleSecurity sec;
1.324 + FILE *pFile;
1.325 +
1.326 + sec.pOwner = NULL;
1.327 + sec.pIdentity = g_pCoreIdent;
1.328 +
1.329 + if ((herr=g_HandleSys.ReadHandle(hndl, g_FileType, &sec, (void **)&pFile))
1.330 + != HandleError_None)
1.331 + {
1.332 + return pContext->ThrowNativeError("Invalid file handle %x (error %d)", hndl, herr);
1.333 + }
1.334 +
1.335 + char *fmt;
1.336 + int err;
1.337 + if ((err=pContext->LocalToString(params[2], &fmt)) != SP_ERROR_NONE)
1.338 + {
1.339 + pContext->ThrowNativeErrorEx(err, NULL);
1.340 + return 0;
1.341 + }
1.342 +
1.343 + char buffer[2048];
1.344 + int arg = 3;
1.345 + atcprintf(buffer, sizeof(buffer), fmt, pContext, params, &arg);
1.346 + fprintf(pFile, "%s\n", buffer);
1.347 +
1.348 + return 1;
1.349 +}
1.350 +
1.351 +//:TODO: DEBUG CODE HERE
1.352 cell_t PrintStuff(IPluginContext *pContext, const cell_t *params)
1.353 {
1.354 char *stuff;
1.355 @@ -127,6 +447,18 @@
1.356 {
1.357 {"OpenDirectory", sm_OpenDirectory},
1.358 {"ReadDirEntry", sm_ReadDirEntry},
1.359 - {"PrintStuff", PrintStuff},
1.360 + {"PrintStuff", PrintStuff},//:TODO: remove this when no longer needed
1.361 + {"OpenFile", sm_OpenFile},
1.362 + {"DeleteFile", sm_DeleteFile},
1.363 + {"ReadFileLine", sm_ReadFileLine},
1.364 + {"IsEndOfFile", sm_IsEndOfFile},
1.365 + {"FileSeek", sm_FileSeek},
1.366 + {"FilePosition", sm_FilePosition},
1.367 + {"FileExists", sm_FileExists},
1.368 + {"RenameFile", sm_RenameFile},
1.369 + {"DirExists", sm_DirExists},
1.370 + {"FileSize", sm_FileSize},
1.371 + {"RemoveDir", sm_RemoveDir},
1.372 + {"WriteFileLine", sm_WriteFileLine},
1.373 {NULL, NULL},
1.374 };