2000-02-25 10:15:17 +00:00
|
|
|
// This may look like C code, but it is really -*- C++ -*-
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------
|
|
|
|
// The Goldware Library
|
|
|
|
// Copyright (C) 1990-1999 Odinn Sorensen
|
|
|
|
// Copyright (C) 1999-2000 Alexander S. Aganichev
|
|
|
|
// ------------------------------------------------------------------
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Library General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Library General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Library General Public
|
|
|
|
// License along with this program; if not, write to the Free
|
|
|
|
// Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
|
|
|
// MA 02111-1307, USA
|
|
|
|
// ------------------------------------------------------------------
|
|
|
|
// $Id$
|
|
|
|
// ------------------------------------------------------------------
|
|
|
|
// File/disk handling functions.
|
|
|
|
// ------------------------------------------------------------------
|
|
|
|
|
|
|
|
#ifndef __gfilutil_h
|
|
|
|
#define __gfilutil_h
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2002-09-23 09:23:45 +00:00
|
|
|
#ifndef _MSC_VER
|
2000-02-25 10:15:17 +00:00
|
|
|
#include <unistd.h>
|
2002-09-23 06:16:34 +00:00
|
|
|
#else
|
|
|
|
#include <cstdlib>
|
2002-09-24 10:16:04 +00:00
|
|
|
#include <direct.h>
|
2002-09-23 06:16:34 +00:00
|
|
|
#endif
|
2000-02-25 10:15:17 +00:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <string>
|
|
|
|
#include <gshare.h>
|
2001-04-15 19:24:44 +00:00
|
|
|
#if !defined(__UNIX__)
|
2000-02-25 10:15:17 +00:00
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------M
|
|
|
|
|
|
|
|
#if defined(__UNIX__)
|
2003-04-16 08:43:49 +00:00
|
|
|
#if !defined(__BEOS__) && !defined(__QNXNTO__)
|
2000-02-25 10:15:17 +00:00
|
|
|
#define O_TEXT 0
|
|
|
|
#define O_BINARY 0
|
|
|
|
#endif
|
2003-03-29 21:51:19 +00:00
|
|
|
#endif
|
2000-02-25 10:15:17 +00:00
|
|
|
|
|
|
|
#ifndef S_IWUSR
|
|
|
|
#define S_IWUSR S_IWRITE
|
|
|
|
#define S_IRUSR S_IREAD
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__UNIX__)
|
|
|
|
#define S_STDRW (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
|
|
|
|
#define S_STDRD (S_IRUSR|S_IRGRP|S_IROTH)
|
|
|
|
#else
|
|
|
|
#define S_STDRW (S_IRUSR|S_IWUSR)
|
|
|
|
#define S_STDRD S_IRUSR
|
|
|
|
#endif
|
|
|
|
|
2002-09-24 10:16:04 +00:00
|
|
|
#ifndef S_ISDIR
|
|
|
|
#define S_ISDIR(st_mode) ((st_mode)&_S_IFDIR)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef S_ISREG
|
|
|
|
#define S_ISREG(st_mode) ((st_mode)&_S_IFREG)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef R_OK
|
|
|
|
#define R_OK 0
|
|
|
|
#endif
|
2000-02-25 10:15:17 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------
|
|
|
|
|
2000-05-22 17:17:43 +00:00
|
|
|
#define GMAXPATH (FILENAME_MAX+1) /* ANSI C */
|
2000-02-25 10:15:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------
|
|
|
|
// Standardized split/merge defines
|
|
|
|
|
|
|
|
#define GMAXDRIVE 265
|
|
|
|
#define GMAXDIR 256
|
|
|
|
#define GMAXFILE 256
|
|
|
|
#define GMAXEXT 256
|
|
|
|
#define GMAXFILEEXT 256
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------
|
|
|
|
// Misc. defines
|
|
|
|
|
2001-04-15 19:24:44 +00:00
|
|
|
#if defined(__MSDOS__) || defined(__OS2__) || defined(__WIN32__)
|
2000-02-25 10:15:17 +00:00
|
|
|
#define GOLD_SLASH_CHR '\\' // Backslash
|
|
|
|
#define GOLD_SLASH_STR "\\"
|
|
|
|
#define GOLD_WRONG_SLASH_CHR '/' // Fwrdslash
|
|
|
|
#define GOLD_WRONG_SLASH_STR "/"
|
|
|
|
#define GOLD_SHELL_ENV "COMSPEC"
|
|
|
|
#else
|
|
|
|
#define GOLD_SLASH_CHR '/' // Fwrdslash
|
|
|
|
#define GOLD_SLASH_STR "/"
|
|
|
|
#define GOLD_WRONG_SLASH_CHR '\\' // Backslash
|
|
|
|
#define GOLD_WRONG_SLASH_STR "\\"
|
|
|
|
#define GOLD_SHELL_ENV "SHELL"
|
|
|
|
#endif
|
|
|
|
|
2002-11-04 18:59:51 +00:00
|
|
|
inline bool isslash(char c) { return (c == '\\') or (c == '/'); }
|
2000-02-25 10:15:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------
|
|
|
|
// Path typedefs
|
|
|
|
|
|
|
|
typedef char Path[GMAXPATH];
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------
|
|
|
|
// Structure for filetime stamp checking
|
|
|
|
|
|
|
|
struct Stamp {
|
|
|
|
dword ft; // Timestamp
|
|
|
|
Path fn; // Filename
|
|
|
|
byte fc; // Filecheck
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------
|
|
|
|
// Prototypes
|
|
|
|
|
2001-04-15 19:24:44 +00:00
|
|
|
#if !defined(__GNUC__) || defined(__MINGW32__)
|
2000-02-25 10:15:17 +00:00
|
|
|
#define mkdir(path,unused) mkdir(path)
|
|
|
|
#endif
|
|
|
|
|
2001-04-15 19:24:44 +00:00
|
|
|
#ifdef __EMX__
|
2000-04-05 15:44:02 +00:00
|
|
|
#define getcwd _getcwd2
|
|
|
|
#define chdir _chdir2
|
|
|
|
#endif
|
|
|
|
|
2000-02-25 10:15:17 +00:00
|
|
|
FILE* fsopen(const char* path, const char* type, int shflag);
|
2001-07-16 20:35:42 +00:00
|
|
|
inline FILE* fsopen(const std::string& path, const char* type, int shflag) { return fsopen(path.c_str(), type, shflag); }
|
2000-02-25 10:15:17 +00:00
|
|
|
|
|
|
|
int is_dir(const char* path);
|
2001-07-16 20:35:42 +00:00
|
|
|
inline int is_dir(const std::string& path) { return is_dir(path.c_str()); }
|
2000-02-25 10:15:17 +00:00
|
|
|
|
2002-09-24 10:16:04 +00:00
|
|
|
inline bool fexist(const char* filename) { return *filename ? ((access(filename, R_OK) == 0) and not is_dir(filename)) : false; }
|
2001-07-16 20:35:42 +00:00
|
|
|
inline bool fexist(const std::string& filename) { return fexist(filename.c_str()); }
|
2000-02-25 10:15:17 +00:00
|
|
|
|
2005-10-20 21:10:42 +00:00
|
|
|
dword gfixstattime(time32_t st_time);
|
2000-02-25 10:15:17 +00:00
|
|
|
|
|
|
|
dword GetFiletime(const char* file);
|
2001-07-16 20:35:42 +00:00
|
|
|
inline dword GetFiletime(const std::string& file) { return GetFiletime(file.c_str()); }
|
2000-02-25 10:15:17 +00:00
|
|
|
|
|
|
|
inline long FiletimeCmp(const char* file1, const char* file2) { return long(GetFiletime(file1) - GetFiletime(file2)); }
|
2001-07-16 20:35:42 +00:00
|
|
|
inline long FiletimeCmp(const std::string& file1, const std::string& file2) { return FiletimeCmp(file1.c_str(), file2.c_str()); }
|
2000-02-25 10:15:17 +00:00
|
|
|
|
|
|
|
long fsize(FILE* fp);
|
|
|
|
long GetFilesize(const char* file);
|
|
|
|
|
|
|
|
const char* AddPath(const char* path, const char* file);
|
2001-07-16 20:35:42 +00:00
|
|
|
inline const char* AddPath(const std::string& path, const char* file) { return AddPath(path.c_str(), file); }
|
|
|
|
inline const char* AddPath(const std::string& path, const std::string& file) { return AddPath(path.c_str(), file.c_str()); }
|
2000-02-25 10:15:17 +00:00
|
|
|
|
|
|
|
void MakePathname(char* pathname, const char* path, const char* name);
|
2001-07-16 20:35:42 +00:00
|
|
|
void MakePathname(std::string& pathname, const std::string& path, const std::string& name);
|
2000-02-25 10:15:17 +00:00
|
|
|
|
|
|
|
char* AddBackslash(char* p);
|
2001-07-16 20:35:42 +00:00
|
|
|
std::string& AddBackslash(std::string& p);
|
2000-02-25 10:15:17 +00:00
|
|
|
char* StripBackslash(char* p);
|
|
|
|
|
|
|
|
char* PathCopy(char* dst, const char* src);
|
2001-07-16 20:35:42 +00:00
|
|
|
void PathCopy(std::string& dst, const char* src);
|
2000-02-25 10:15:17 +00:00
|
|
|
|
|
|
|
void TouchFile(const char* __filename);
|
|
|
|
|
|
|
|
int TestLockPath(const char* __path);
|
|
|
|
void WipeFile(const char* file, int options);
|
|
|
|
|
|
|
|
const char* CleanFilename(const char* __file);
|
|
|
|
|
|
|
|
int strschg_environ(char* s);
|
2001-07-16 20:35:42 +00:00
|
|
|
int strschg_environ(std::string& s);
|
2000-02-25 10:15:17 +00:00
|
|
|
|
2005-09-15 19:59:44 +00:00
|
|
|
char* MapPath(char* map, bool reverse = false); // gcarea.cpp
|
2005-05-11 10:58:51 +00:00
|
|
|
inline char* ReMapPath(char* map) { return MapPath(map, true); }
|
2000-02-25 10:15:17 +00:00
|
|
|
|
|
|
|
inline long lseekset(int fh, long offset) { return lseek(fh, offset, SEEK_SET); }
|
|
|
|
inline long lseekset(int fh, long record, long recordsize) { return lseek(fh, record*recordsize, SEEK_SET); }
|
|
|
|
|
|
|
|
int gchdir(const char* dir);
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------
|
|
|
|
|
2001-04-15 19:24:44 +00:00
|
|
|
#if (defined(__BORLANDC__) && defined(__OS2__)) || defined(__UNIX__) || defined(__EMX__)
|
2000-02-25 10:15:17 +00:00
|
|
|
long filelength(int fh);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------
|
|
|
|
|
|
|
|
void replaceextension(char *destpath, const char *srcpath, const char *ext);
|
|
|
|
void extractdirname(char *dir, const char *path);
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------
|
|
|
|
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
int lock(int handle, long offset, long length);
|
|
|
|
int unlock(int handle, long offset, long length);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------
|
|
|
|
|
|
|
|
#if defined(__DJGPP__)
|
|
|
|
#undef sopen
|
|
|
|
#endif
|
|
|
|
|
2001-04-15 19:24:44 +00:00
|
|
|
#if !defined(__DJGPP__) && defined(__GNUC__)
|
2000-02-25 10:15:17 +00:00
|
|
|
int lock(int handle, long offset, long length);
|
|
|
|
int unlock(int handle, long offset, long length);
|
2003-04-16 08:43:49 +00:00
|
|
|
#if !defined(__QNXNTO__) && !defined(__MINGW32__)
|
2000-02-25 10:15:17 +00:00
|
|
|
inline long tell(int fh) { return lseek(fh, 0, SEEK_CUR); }
|
|
|
|
#endif
|
2003-04-16 08:43:49 +00:00
|
|
|
#endif
|
2000-02-25 10:15:17 +00:00
|
|
|
|
2003-04-16 08:43:49 +00:00
|
|
|
#if !defined(sopen) && !defined(__MINGW32__) && !defined(__EMX__) && !defined(__QNXNTO__) && defined(__GNUC__)
|
2000-02-25 10:15:17 +00:00
|
|
|
inline int sopen(const char* path, int access, int shflag, int mode) {
|
2001-03-04 14:14:33 +00:00
|
|
|
#ifdef __UNIX__
|
|
|
|
shflag = 0;
|
|
|
|
#endif
|
2000-02-25 10:15:17 +00:00
|
|
|
return open(path, access|shflag, mode);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-04-15 19:24:44 +00:00
|
|
|
#if defined(__UNIX__) || defined(__CYGWIN__)
|
2000-02-25 10:15:17 +00:00
|
|
|
inline int chsize(int handle, long size) { return ftruncate(handle, size); }
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------
|
|
|
|
|
2001-07-16 20:35:42 +00:00
|
|
|
bool maketruepath(std::string &dirname);
|
2000-02-25 10:15:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------
|