public/ILibrarySys.h
changeset 1952 b7aa2b626807
child 2289 44045ead61cd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/public/ILibrarySys.h	Sun Mar 30 07:00:22 2008 +0000
     1.3 @@ -0,0 +1,229 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * =============================================================================
     1.7 + * SourceMod
     1.8 + * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     1.9 + * =============================================================================
    1.10 + *
    1.11 + * This program is free software; you can redistribute it and/or modify it under
    1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
    1.13 + * Free Software Foundation.
    1.14 + * 
    1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
    1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 + * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.18 + * details.
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License along with
    1.21 + * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.22 + *
    1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
    1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
    1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.26 + * by the Valve Corporation.  You must obey the GNU General Public License in
    1.27 + * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.28 + * this exception to all derivative works.  AlliedModders LLC defines further
    1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.30 + * or <http://www.sourcemod.net/license.php>.
    1.31 + *
    1.32 + * Version: $Id$
    1.33 + */
    1.34 +
    1.35 +#ifndef _INCLUDE_SOURCEMOD_LIBRARY_INTERFACE_SYS_H_
    1.36 +#define _INCLUDE_SOURCEMOD_LIBRARY_INTERFACE_SYS_H_
    1.37 +
    1.38 +/**
    1.39 + * @file ILibrarySys.h
    1.40 + * @brief Defines platform-dependent operations, such as opening libraries and files.
    1.41 + */
    1.42 +
    1.43 +#include <IShareSys.h>
    1.44 +#include <time.h>
    1.45 +
    1.46 +namespace SourceMod
    1.47 +{
    1.48 +	#define SMINTERFACE_LIBRARYSYS_NAME		"ILibrarySys"
    1.49 +	#define SMINTERFACE_LIBRARYSYS_VERSION	4
    1.50 +
    1.51 +	enum FileTimeType
    1.52 +	{
    1.53 +		FileTime_LastAccess = 0,	/* Last access (not available on FAT) */
    1.54 +		FileTime_Created = 1,		/* Creation (not available on FAT) */
    1.55 +		FileTime_LastChange = 2,	/* Last modification */
    1.56 +	};
    1.57 +
    1.58 +	class ILibrary
    1.59 +	{
    1.60 +	public:
    1.61 +		/** Virtual destructor (calls CloseLibrary) */
    1.62 +		virtual ~ILibrary()
    1.63 +		{
    1.64 +		};
    1.65 +	public:
    1.66 +		/**
    1.67 +		 * @brief Closes dynamic library and invalidates pointer.
    1.68 +		 */
    1.69 +		virtual void CloseLibrary() =0;
    1.70 +
    1.71 +		/**
    1.72 +		 * @brief Retrieves a symbol pointer from the dynamic library.
    1.73 +		 *
    1.74 +		 * @param symname	Symbol name.
    1.75 +		 * @return			Symbol pointer, NULL if not found.
    1.76 +		 */
    1.77 +		virtual void *GetSymbolAddress(const char *symname) =0;
    1.78 +	};
    1.79 +
    1.80 +	/**
    1.81 +	 * @brief Directory browsing abstraction.
    1.82 +	 */
    1.83 +	class IDirectory
    1.84 +	{
    1.85 +	public:
    1.86 +		/** Virtual destructor */
    1.87 +		virtual ~IDirectory()
    1.88 +		{
    1.89 +		}
    1.90 +	public:
    1.91 +		/**
    1.92 +		 * @brief Returns true if there are more files to read, false otherwise.
    1.93 +		 */
    1.94 +		virtual bool MoreFiles() =0;
    1.95 +
    1.96 +		/**
    1.97 +		 * @brief Advances to the next entry in the stream.
    1.98 +		 */
    1.99 +		virtual void NextEntry() =0;
   1.100 +
   1.101 +		/**
   1.102 +		 * @brief Returns the name of the current entry.
   1.103 +		 */
   1.104 +		virtual const char *GetEntryName() =0;
   1.105 +
   1.106 +		/**
   1.107 +		 * @brief Returns whether the current entry is a directory.
   1.108 +		 */
   1.109 +		virtual bool IsEntryDirectory() =0;
   1.110 +
   1.111 +		/**
   1.112 +		 * @brief Returns whether the current entry is a file.
   1.113 +		 */
   1.114 +		virtual bool IsEntryFile() =0;
   1.115 +
   1.116 +		/**
   1.117 +		 * @brief Returns true if the current entry is valid
   1.118 +		 *        (Used similarly to MoreFiles).
   1.119 +		 */
   1.120 +		virtual bool IsEntryValid() =0;
   1.121 +	};
   1.122 +
   1.123 +	/**
   1.124 +	 * @brief Contains various operating system specific code.
   1.125 +	 */
   1.126 +	class ILibrarySys : public SMInterface
   1.127 +	{
   1.128 +	public:
   1.129 +		virtual const char *GetInterfaceName()
   1.130 +		{
   1.131 +			return SMINTERFACE_LIBRARYSYS_NAME;
   1.132 +		}
   1.133 +		virtual unsigned int GetInterfaceVersion()
   1.134 +		{
   1.135 +			return SMINTERFACE_LIBRARYSYS_VERSION;
   1.136 +		}
   1.137 +	public:
   1.138 +		/**
   1.139 +		 * @brief Opens a dynamic library file.
   1.140 +		 * 
   1.141 +		 * @param path		Path to library file (.dll/.so).
   1.142 +		 * @param error		Buffer for any error message (may be NULL).
   1.143 +		 * @param maxlength	Maximum length of error buffer.
   1.144 +		 * @return			Pointer to an ILibrary, NULL if failed.
   1.145 +		 */
   1.146 +		virtual ILibrary *OpenLibrary(const char *path, char *error, size_t maxlength) =0;
   1.147 +
   1.148 +		/**
   1.149 +		 * @brief Opens a directory for reading.
   1.150 +		 *
   1.151 +		 * @param path		Path to directory.
   1.152 +		 * @return			Pointer to an IDirectory, NULL if failed.
   1.153 +		 */
   1.154 +		virtual IDirectory *OpenDirectory(const char *path) =0;
   1.155 +
   1.156 +		/**
   1.157 +		 * @brief Closes a directory and frees its handle.
   1.158 +		 * 
   1.159 +		 * @param dir		Pointer to IDirectory.
   1.160 +		 */
   1.161 +		virtual void CloseDirectory(IDirectory *dir) =0;
   1.162 +
   1.163 +		/**
   1.164 +		 * @brief Returns true if a path exists.
   1.165 +		 */
   1.166 +		virtual bool PathExists(const char *path) =0;
   1.167 +
   1.168 +		/**
   1.169 +		 * @brief Returns true if the path is a normal file.
   1.170 +		 */
   1.171 +		virtual bool IsPathFile(const char *path) =0;
   1.172 +
   1.173 +		/**
   1.174 +		 * @brief Returns true if the path is a normal directory.
   1.175 +		 */
   1.176 +		virtual bool IsPathDirectory(const char *path) =0;
   1.177 +
   1.178 +		/**
   1.179 +		 * @brief Gets a platform-specific error message.
   1.180 +		 * This should only be called when an ILibrary function fails.
   1.181 +		 * Win32 equivalent: GetLastError() + FormatMessage()
   1.182 +		 * POSIX equivalent: errno + strerror()
   1.183 +		 *
   1.184 +		 * @param error		Error message buffer.
   1.185 +		 * @param maxlength	Maximum length of error buffer.
   1.186 +		 */
   1.187 +		virtual void GetPlatformError(char *error, size_t maxlength) =0;
   1.188 +
   1.189 +		/**
   1.190 +		 * @brief Formats a string similar to snprintf(), except
   1.191 +		 * corrects all non-platform compatible path separators to be
   1.192 +		 * the correct platform character.
   1.193 +		 *
   1.194 +		 * @param buffer	Output buffer pointer.
   1.195 +		 * @param maxlength	Output buffer size.
   1.196 +		 * @param pathfmt	Format string of path.
   1.197 +		 * @param ...		Format string arguments.
   1.198 +		 */
   1.199 +		virtual size_t PathFormat(char *buffer, size_t maxlength, const char *pathfmt, ...) =0;
   1.200 +
   1.201 +		/**
   1.202 +		 * @brief Returns a pointer to the extension in a filename.
   1.203 +		 *
   1.204 +		 * @param filename	Name of file from which the extension should be extracted.
   1.205 +		 * @return			Pointer to file extension.
   1.206 +		 */
   1.207 +		virtual const char *GetFileExtension(const char *filename) =0;
   1.208 +
   1.209 +		/**
   1.210 +		 * @brief Creates a directory.
   1.211 +		 *
   1.212 +		 * @param path		Full, absolute path of the directory to create.
   1.213 +		 * @return			True on success, false otherwise.
   1.214 +		 */
   1.215 +		virtual bool CreateFolder(const char *path) =0;
   1.216 +
   1.217 +		/**
   1.218 +		 * @brief Returns the requested timestamp of a file.
   1.219 +		 *
   1.220 +		 * NOTE: On FAT file systems, the access and creation times 
   1.221 +		 * may not be valid.
   1.222 +		 *
   1.223 +		 * @param path		Path to file.
   1.224 +		 * @param type		FileTimeType of time value to request.
   1.225 +		 * @param pTime		Pointer to store time.
   1.226 +		 * @return			True on success, false on failure.
   1.227 +		 */
   1.228 +		virtual bool FileTime(const char *path, FileTimeType type, time_t *pTime) =0;
   1.229 +	};
   1.230 +}
   1.231 +
   1.232 +#endif //_INCLUDE_SOURCEMOD_LIBRARY_INTERFACE_SYS_H_