public/ILibrarySys.h
author Scott Ehlert <ds@alliedmods.net>
Sun Jul 15 08:00:26 2007 +0000 (2007-07-15)
changeset 1116 40cc5e9f0f1b
parent 794 5947b0ee60bb
child 1192 94ee3f012044
permissions -rw-r--r--
Removed strange debugging code
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * SourceMod, Copyright (C) 2004-2007 AlliedModders LLC. 
     5  * All rights reserved.
     6  * ===============================================================
     7  *
     8  *  This file is part of the SourceMod/SourcePawn SDK.  This file may only be 
     9  * used or modified under the Terms and Conditions of its License Agreement, 
    10  * which is found in public/licenses/LICENSE.txt.  As of this notice, derivative 
    11  * works must be licensed under the GNU General Public License (version 2 or 
    12  * greater).  A copy of the GPL is included under public/licenses/GPL.txt.
    13  * 
    14  * To view the latest information, see: http://www.sourcemod.net/license.php
    15  *
    16  * Version: $Id$
    17  */
    18 
    19 #ifndef _INCLUDE_SOURCEMOD_LIBRARY_INTERFACE_SYS_H_
    20 #define _INCLUDE_SOURCEMOD_LIBRARY_INTERFACE_SYS_H_
    21 
    22 /**
    23  * @file ILibrarySys.h
    24  * @brief Defines platform-dependent operations, such as opening libraries and files.
    25  */
    26 
    27 #include <IShareSys.h>
    28 
    29 namespace SourceMod
    30 {
    31 	#define SMINTERFACE_LIBRARYSYS_NAME		"ILibrarySys"
    32 	#define SMINTERFACE_LIBRARYSYS_VERSION	2
    33 
    34 	class ILibrary
    35 	{
    36 	public:
    37 		/** Virtual destructor (calls CloseLibrary) */
    38 		virtual ~ILibrary()
    39 		{
    40 		};
    41 	public:
    42 		/**
    43 		 * @brief Closes dynamic library and invalidates pointer.
    44 		 */
    45 		virtual void CloseLibrary() =0;
    46 
    47 		/**
    48 		 * @brief Retrieves a symbol pointer from the dynamic library.
    49 		 *
    50 		 * @param symname	Symbol name.
    51 		 * @return			Symbol pointer, NULL if not found.
    52 		 */
    53 		virtual void *GetSymbolAddress(const char *symname) =0;
    54 	};
    55 
    56 	/**
    57 	 * @brief Directory browsing abstraction.
    58 	 */
    59 	class IDirectory
    60 	{
    61 	public:
    62 		/** Virtual destructor */
    63 		virtual ~IDirectory()
    64 		{
    65 		}
    66 	public:
    67 		/**
    68 		 * @brief Returns true if there are more files to read, false otherwise.
    69 		 */
    70 		virtual bool MoreFiles() =0;
    71 
    72 		/**
    73 		 * @brief Advances to the next entry in the stream.
    74 		 */
    75 		virtual void NextEntry() =0;
    76 
    77 		/**
    78 		 * @brief Returns the name of the current entry.
    79 		 */
    80 		virtual const char *GetEntryName() =0;
    81 
    82 		/**
    83 		 * @brief Returns whether the current entry is a directory.
    84 		 */
    85 		virtual bool IsEntryDirectory() =0;
    86 
    87 		/**
    88 		 * @brief Returns whether the current entry is a file.
    89 		 */
    90 		virtual bool IsEntryFile() =0;
    91 
    92 		/**
    93 		 * @brief Returns true if the current entry is valid
    94 		 *        (Used similarly to MoreFiles).
    95 		 */
    96 		virtual bool IsEntryValid() =0;
    97 	};
    98 
    99 	/**
   100 	 * @brief Contains various operating system specific code.
   101 	 */
   102 	class ILibrarySys : public SMInterface
   103 	{
   104 	public:
   105 		virtual const char *GetInterfaceName()
   106 		{
   107 			return SMINTERFACE_LIBRARYSYS_NAME;
   108 		}
   109 		virtual unsigned int GetInterfaceVersion()
   110 		{
   111 			return SMINTERFACE_LIBRARYSYS_VERSION;
   112 		}
   113 	public:
   114 		/**
   115 		 * @brief Opens a dynamic library file.
   116 		 * 
   117 		 * @param path		Path to library file (.dll/.so).
   118 		 * @param error		Buffer for any error message (may be NULL).
   119 		 * @param maxlength	Maximum length of error buffer.
   120 		 * @return			Pointer to an ILibrary, NULL if failed.
   121 		 */
   122 		virtual ILibrary *OpenLibrary(const char *path, char *error, size_t maxlength) =0;
   123 
   124 		/**
   125 		 * @brief Opens a directory for reading.
   126 		 *
   127 		 * @param path		Path to directory.
   128 		 * @return			Pointer to an IDirectory, NULL if failed.
   129 		 */
   130 		virtual IDirectory *OpenDirectory(const char *path) =0;
   131 
   132 		/**
   133 		 * @brief Closes a directory and frees its handle.
   134 		 * 
   135 		 * @param dir		Pointer to IDirectory.
   136 		 */
   137 		virtual void CloseDirectory(IDirectory *dir) =0;
   138 
   139 		/**
   140 		 * @brief Returns true if a path exists.
   141 		 */
   142 		virtual bool PathExists(const char *path) =0;
   143 
   144 		/**
   145 		 * @brief Returns true if the path is a normal file.
   146 		 */
   147 		virtual bool IsPathFile(const char *path) =0;
   148 
   149 		/**
   150 		 * @brief Returns true if the path is a normal directory.
   151 		 */
   152 		virtual bool IsPathDirectory(const char *path) =0;
   153 
   154 		/**
   155 		 * @brief Gets a platform-specific error message.
   156 		 * This should only be called when an ILibrary function fails.
   157 		 * Win32 equivalent: GetLastError() + FormatMessage()
   158 		 * POSIX equivalent: errno + strerror()
   159 		 *
   160 		 * @param error		Error message buffer.
   161 		 * @param maxlength	Maximum length of error buffer.
   162 		 */
   163 		virtual void GetPlatformError(char *error, size_t maxlength) =0;
   164 
   165 		/**
   166 		 * @brief Formats a string similar to snprintf(), except
   167 		 * corrects all non-platform compatible path separators to be
   168 		 * the correct platform character.
   169 		 *
   170 		 * @param buffer	Output buffer pointer.
   171 		 * @param maxlength	Output buffer size.
   172 		 * @param pathfmt	Format string of path.
   173 		 * @param ...		Format string arguments.
   174 		 */
   175 		virtual size_t PathFormat(char *buffer, size_t maxlength, const char *pathfmt, ...) =0;
   176 
   177 		/**
   178 		 * @brief Returns a pointer to the extension in a filename.
   179 		 *
   180 		 * @param filename	Name of file from which the extension should be extracted.
   181 		 * @return			Pointer to file extension.
   182 		 */
   183 		virtual const char *GetFileExtension(const char *filename) =0;
   184 	};
   185 }
   186 
   187 #endif //_INCLUDE_SOURCEMOD_LIBRARY_INTERFACE_SYS_H_