public/ILibrarySys.h
author David Anderson <dvander@alliedmods.net>
Mon Aug 12 00:22:41 2013 +0100 (2013-08-12)
changeset 3989 b73dae36a0da
parent 3872 4c93f74cae51
permissions -rw-r--r--
Eliminate InfoVars::rval (bug 5844 part 6, r=ds).
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #ifndef _INCLUDE_SOURCEMOD_LIBRARY_INTERFACE_SYS_H_
    33 #define _INCLUDE_SOURCEMOD_LIBRARY_INTERFACE_SYS_H_
    34 
    35 /**
    36  * @file ILibrarySys.h
    37  * @brief Defines platform-dependent operations, such as opening libraries and files.
    38  */
    39 
    40 #include <IShareSys.h>
    41 #include <time.h>
    42 
    43 namespace SourceMod
    44 {
    45 	#define SMINTERFACE_LIBRARYSYS_NAME		"ILibrarySys"
    46 	#define SMINTERFACE_LIBRARYSYS_VERSION	5
    47 
    48 	enum FileTimeType
    49 	{
    50 		FileTime_LastAccess = 0,	/* Last access (not available on FAT) */
    51 		FileTime_Created = 1,		/* Creation (not available on FAT) */
    52 		FileTime_LastChange = 2,	/* Last modification */
    53 	};
    54 
    55 	class ILibrary
    56 	{
    57 	public:
    58 		/** Virtual destructor (calls CloseLibrary) */
    59 		virtual ~ILibrary()
    60 		{
    61 		};
    62 	public:
    63 		/**
    64 		 * @brief Closes dynamic library and invalidates pointer.
    65 		 */
    66 		virtual void CloseLibrary() =0;
    67 
    68 		/**
    69 		 * @brief Retrieves a symbol pointer from the dynamic library.
    70 		 *
    71 		 * @param symname	Symbol name.
    72 		 * @return			Symbol pointer, NULL if not found.
    73 		 */
    74 		virtual void *GetSymbolAddress(const char *symname) =0;
    75 	};
    76 
    77 	/**
    78 	 * @brief Directory browsing abstraction.
    79 	 */
    80 	class IDirectory
    81 	{
    82 	public:
    83 		/** Virtual destructor */
    84 		virtual ~IDirectory()
    85 		{
    86 		}
    87 	public:
    88 		/**
    89 		 * @brief Returns true if there are more files to read, false otherwise.
    90 		 */
    91 		virtual bool MoreFiles() =0;
    92 
    93 		/**
    94 		 * @brief Advances to the next entry in the stream.
    95 		 */
    96 		virtual void NextEntry() =0;
    97 
    98 		/**
    99 		 * @brief Returns the name of the current entry.
   100 		 */
   101 		virtual const char *GetEntryName() =0;
   102 
   103 		/**
   104 		 * @brief Returns whether the current entry is a directory.
   105 		 */
   106 		virtual bool IsEntryDirectory() =0;
   107 
   108 		/**
   109 		 * @brief Returns whether the current entry is a file.
   110 		 */
   111 		virtual bool IsEntryFile() =0;
   112 
   113 		/**
   114 		 * @brief Returns true if the current entry is valid
   115 		 *        (Used similarly to MoreFiles).
   116 		 */
   117 		virtual bool IsEntryValid() =0;
   118 	};
   119 
   120 	/**
   121 	 * @brief Contains various operating system specific code.
   122 	 */
   123 	class ILibrarySys : public SMInterface
   124 	{
   125 	public:
   126 		virtual const char *GetInterfaceName()
   127 		{
   128 			return SMINTERFACE_LIBRARYSYS_NAME;
   129 		}
   130 		virtual unsigned int GetInterfaceVersion()
   131 		{
   132 			return SMINTERFACE_LIBRARYSYS_VERSION;
   133 		}
   134 	public:
   135 		/**
   136 		 * @brief Opens a dynamic library file.
   137 		 * 
   138 		 * @param path		Path to library file (.dll/.so).
   139 		 * @param error		Buffer for any error message (may be NULL).
   140 		 * @param maxlength	Maximum length of error buffer.
   141 		 * @return			Pointer to an ILibrary, NULL if failed.
   142 		 */
   143 		virtual ILibrary *OpenLibrary(const char *path, char *error, size_t maxlength) =0;
   144 
   145 		/**
   146 		 * @brief Opens a directory for reading.
   147 		 *
   148 		 * @param path		Path to directory.
   149 		 * @return			Pointer to an IDirectory, NULL if failed.
   150 		 */
   151 		virtual IDirectory *OpenDirectory(const char *path) =0;
   152 
   153 		/**
   154 		 * @brief Closes a directory and frees its handle.
   155 		 * 
   156 		 * @param dir		Pointer to IDirectory.
   157 		 */
   158 		virtual void CloseDirectory(IDirectory *dir) =0;
   159 
   160 		/**
   161 		 * @brief Returns true if a path exists.
   162 		 */
   163 		virtual bool PathExists(const char *path) =0;
   164 
   165 		/**
   166 		 * @brief Returns true if the path is a normal file.
   167 		 */
   168 		virtual bool IsPathFile(const char *path) =0;
   169 
   170 		/**
   171 		 * @brief Returns true if the path is a normal directory.
   172 		 */
   173 		virtual bool IsPathDirectory(const char *path) =0;
   174 
   175 		/**
   176 		 * @brief Gets a platform-specific error message.
   177 		 * This should only be called when an ILibrary function fails.
   178 		 * Win32 equivalent: GetLastError() + FormatMessage()
   179 		 * POSIX equivalent: errno + strerror()
   180 		 *
   181 		 * @param error		Error message buffer.
   182 		 * @param maxlength	Maximum length of error buffer.
   183 		 */
   184 		virtual void GetPlatformError(char *error, size_t maxlength) =0;
   185 
   186 		/**
   187 		 * @brief Formats a string similar to snprintf(), except
   188 		 * corrects all non-platform compatible path separators to be
   189 		 * the correct platform character.
   190 		 *
   191 		 * @param buffer	Output buffer pointer.
   192 		 * @param maxlength	Output buffer size.
   193 		 * @param pathfmt	Format string of path.
   194 		 * @param ...		Format string arguments.
   195 		 */
   196 		virtual size_t PathFormat(char *buffer, size_t maxlength, const char *pathfmt, ...) =0;
   197 
   198 		/**
   199 		 * @brief Returns a pointer to the extension in a filename.
   200 		 *
   201 		 * @param filename	Name of file from which the extension should be extracted.
   202 		 * @return			Pointer to file extension.
   203 		 */
   204 		virtual const char *GetFileExtension(const char *filename) =0;
   205 
   206 		/**
   207 		 * @brief Creates a directory.
   208 		 *
   209 		 * @param path		Full, absolute path of the directory to create.
   210 		 * @return			True on success, false otherwise.
   211 		 */
   212 		virtual bool CreateFolder(const char *path) =0;
   213 
   214 		/**
   215 		 * @brief Returns the requested timestamp of a file.
   216 		 *
   217 		 * NOTE: On FAT file systems, the access and creation times 
   218 		 * may not be valid.
   219 		 *
   220 		 * @param path		Path to file.
   221 		 * @param type		FileTimeType of time value to request.
   222 		 * @param pTime		Pointer to store time.
   223 		 * @return			True on success, false on failure.
   224 		 */
   225 		virtual bool FileTime(const char *path, FileTimeType type, time_t *pTime) =0;
   226 
   227 		/**
   228 		 * @brief Retrieve the file component of the given path.
   229 		 *
   230 		 * @param buffer	Output buffer.
   231 		 * @param maxlength	Length of the output buffer.
   232 		 * @param path		Path to search for a filename.
   233 		 * @return			Number of bytes written to buffer, not including the null terminator.
   234 		 */
   235 		virtual size_t GetFileFromPath(char *buffer, size_t maxlength, const char *path) =0;
   236 	};
   237 }
   238 
   239 #endif //_INCLUDE_SOURCEMOD_LIBRARY_INTERFACE_SYS_H_