root/branches/branch-5-0/mapserver/mapcpl.c
| Revision 6428, 9.4 kB (checked in by dmorissette, 1 year ago) | |
|---|---|
| |
| Line | |
|---|---|
| 1 | /****************************************************************************** |
| 2 | * |
| 3 | * Project: MapServer |
| 4 | * Purpose: Functions copied from GDAL's CPL. |
| 5 | * Author: Y. Assefa, DM Solutions Group (assefa@dmsolutions.ca) |
| 6 | * |
| 7 | * This file contain utility functions that come from the GDAL/OGR cpl |
| 8 | * library. The idea behind it is to have access in mapserver to all these |
| 9 | * utilities, without being constarined to link with GDAL/OGR. |
| 10 | * Note : Names of functions used here are the same as those in the cpl |
| 11 | * library with the exception the the CPL prefix is changed to ms |
| 12 | * (eg : CPLGetBasename() would become msGetBasename()) |
| 13 | * |
| 14 | ****************************************************************************** |
| 15 | * Copyright (c) 1996-2005 Regents of the University of Minnesota. |
| 16 | * |
| 17 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 18 | * copy of this software and associated documentation files (the "Software"), |
| 19 | * to deal in the Software without restriction, including without limitation |
| 20 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 21 | * and/or sell copies of the Software, and to permit persons to whom the |
| 22 | * Software is furnished to do so, subject to the following conditions: |
| 23 | * |
| 24 | * The above copyright notice and this permission notice shall be included in |
| 25 | * all copies of this Software or works derived from this Software. |
| 26 | * |
| 27 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 28 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 29 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 30 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 31 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 32 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 33 | * DEALINGS IN THE SOFTWARE. |
| 34 | ****************************************************************************/ |
| 35 | |
| 36 | /* $Id$ */ |
| 37 | |
| 38 | #include <assert.h> |
| 39 | #include "mapserver.h" |
| 40 | |
| 41 | MS_CVSID("$Id$") |
| 42 | |
| 43 | /* should be size of largest possible filename */ |
| 44 | #define MS_PATH_BUF_SIZE 2048 |
| 45 | static char szStaticResult[MS_PATH_BUF_SIZE]; |
| 46 | |
| 47 | |
| 48 | /************************************************************************/ |
| 49 | /* msFindFilenameStart() */ |
| 50 | /************************************************************************/ |
| 51 | |
| 52 | static int msFindFilenameStart( const char * pszFilename ) |
| 53 | |
| 54 | { |
| 55 | int iFileStart; |
| 56 | |
| 57 | for( iFileStart = strlen(pszFilename); |
| 58 | iFileStart > 0 |
| 59 | && pszFilename[iFileStart-1] != '/' |
| 60 | && pszFilename[iFileStart-1] != '\\'; |
| 61 | iFileStart-- ) {} |
| 62 | |
| 63 | return iFileStart; |
| 64 | } |
| 65 | |
| 66 | /************************************************************************/ |
| 67 | /* msGetBasename() */ |
| 68 | /************************************************************************/ |
| 69 | |
| 70 | /** |
| 71 | * Extract basename (non-directory, non-extension) portion of filename. |
| 72 | * |
| 73 | * Returns a string containing the file basename portion of the passed |
| 74 | * name. If there is no basename (passed value ends in trailing directory |
| 75 | * separator, or filename starts with a dot) an empty string is returned. |
| 76 | * |
| 77 | * <pre> |
| 78 | * msGetBasename( "abc/def.xyz" ) == "def" |
| 79 | * msGetBasename( "abc/def" ) == "def" |
| 80 | * msGetBasename( "abc/def/" ) == "" |
| 81 | * </pre> |
| 82 | * |
| 83 | * @param pszFullFilename the full filename potentially including a path. |
| 84 | * |
| 85 | * @return just the non-directory, non-extension portion of the path in |
| 86 | * an internal string which must not be freed. The string |
| 87 | * may be destroyed by the next ms filename handling call. |
| 88 | */ |
| 89 | |
| 90 | const char *msGetBasename( const char *pszFullFilename ) |
| 91 | |
| 92 | { |
| 93 | int iFileStart = msFindFilenameStart( pszFullFilename ); |
| 94 | int iExtStart, nLength; |
| 95 | |
| 96 | for( iExtStart = strlen(pszFullFilename); |
| 97 | iExtStart > iFileStart && pszFullFilename[iExtStart] != '.'; |
| 98 | iExtStart-- ) {} |
| 99 | |
| 100 | if( iExtStart == iFileStart ) |
| 101 | iExtStart = strlen(pszFullFilename); |
| 102 | |
| 103 | nLength = iExtStart - iFileStart; |
| 104 | |
| 105 | assert( nLength < MS_PATH_BUF_SIZE ); |
| 106 | |
| 107 | strncpy( szStaticResult, pszFullFilename + iFileStart, nLength ); |
| 108 | szStaticResult[nLength] = '\0'; |
| 109 | |
| 110 | return szStaticResult; |
| 111 | } |
| 112 | |
| 113 | /* Id: GDAL/port/cplgetsymbol.cpp,v 1.14 2004/11/11 20:40:38 fwarmerdam Exp */ |
| 114 | /* ==================================================================== */ |
| 115 | /* Unix Implementation */ |
| 116 | /* ==================================================================== */ |
| 117 | #if defined(HAVE_DLFCN_H) |
| 118 | |
| 119 | #define GOT_GETSYMBOL |
| 120 | |
| 121 | #include <dlfcn.h> |
| 122 | |
| 123 | /************************************************************************/ |
| 124 | /* msGetSymbol() */ |
| 125 | /************************************************************************/ |
| 126 | |
| 127 | /** |
| 128 | * Fetch a function pointer from a shared library / DLL. |
| 129 | * |
| 130 | * This function is meant to abstract access to shared libraries and |
| 131 | * DLLs and performs functions similar to dlopen()/dlsym() on Unix and |
| 132 | * LoadLibrary() / GetProcAddress() on Windows. |
| 133 | * |
| 134 | * If no support for loading entry points from a shared library is available |
| 135 | * this function will always return NULL. Rules on when this function |
| 136 | * issues a msError() or not are not currently well defined, and will have |
| 137 | * to be resolved in the future. |
| 138 | * |
| 139 | * Currently msGetSymbol() doesn't try to: |
| 140 | * <ul> |
| 141 | * <li> prevent the reference count on the library from going up |
| 142 | * for every request, or given any opportunity to unload |
| 143 | * the library. |
| 144 | * <li> Attempt to look for the library in non-standard |
| 145 | * locations. |
| 146 | * <li> Attempt to try variations on the symbol name, like |
| 147 | * pre-prending or post-pending an underscore. |
| 148 | * </ul> |
| 149 | * |
| 150 | * Some of these issues may be worked on in the future. |
| 151 | * |
| 152 | * @param pszLibrary the name of the shared library or DLL containing |
| 153 | * the function. May contain path to file. If not system supplies search |
| 154 | * paths will be used. |
| 155 | * @param pszSymbolName the name of the function to fetch a pointer to. |
| 156 | * @return A pointer to the function if found, or NULL if the function isn't |
| 157 | * found, or the shared library can't be loaded. |
| 158 | */ |
| 159 | |
| 160 | void *msGetSymbol( const char * pszLibrary, const char * pszSymbolName ) |
| 161 | { |
| 162 | void *pLibrary; |
| 163 | void *pSymbol; |
| 164 | |
| 165 | pLibrary = dlopen(pszLibrary, RTLD_LAZY); |
| 166 | if( pLibrary == NULL ) |
| 167 | { |
| 168 | msSetError(MS_MISCERR, |
| 169 | "Dynamic loading failed: %s", |
| 170 | "msGetSymbol()", dlerror()); |
| 171 | return NULL; |
| 172 | } |
| 173 | |
| 174 | pSymbol = dlsym( pLibrary, pszSymbolName ); |
| 175 | |
| 176 | #if (defined(__APPLE__) && defined(__MACH__)) |
| 177 | /* On mach-o systems, C symbols have a leading underscore and depending |
| 178 | * on how dlcompat is configured it may or may not add the leading |
| 179 | * underscore. So if dlsym() fails add an underscore and try again. |
| 180 | */ |
| 181 | if( pSymbol == NULL ) |
| 182 | { |
| 183 | char withUnder[strlen(pszSymbolName) + 2]; |
| 184 | withUnder[0] = '_'; withUnder[1] = 0; |
| 185 | strcat(withUnder, pszSymbolName); |
| 186 | pSymbol = dlsym( pLibrary, withUnder ); |
| 187 | } |
| 188 | #endif |
| 189 | |
| 190 | if( pSymbol == NULL ) |
| 191 | { |
| 192 | msSetError(MS_MISCERR, |
| 193 | "Dynamic loading failed: %s", |
| 194 | "msGetSymbol()", dlerror()); |
| 195 | return NULL; |
| 196 | } |
| 197 | |
| 198 | return( pSymbol ); |
| 199 | } |
| 200 | |
| 201 | #endif /* def __unix__ && defined(HAVE_DLFCN_H) */ |
| 202 | |
| 203 | /* ==================================================================== */ |
| 204 | /* Windows Implementation */ |
| 205 | /* ==================================================================== */ |
| 206 | #ifdef WIN32 |
| 207 | |
| 208 | #define GOT_GETSYMBOL |
| 209 | |
| 210 | #include <windows.h> |
| 211 | |
| 212 | /************************************************************************/ |
| 213 | /* msGetSymbol() */ |
| 214 | /************************************************************************/ |
| 215 | |
| 216 | void *msGetSymbol( const char * pszLibrary, const char * pszSymbolName ) |
| 217 | { |
| 218 | void *pLibrary; |
| 219 | void *pSymbol; |
| 220 | |
| 221 | pLibrary = LoadLibrary(pszLibrary); |
| 222 | if( pLibrary == NULL ) |
| 223 | { |
| 224 | msSetError(MS_MISCERR, |
| 225 | "Can't load requested dynamic library: %s", |
| 226 | "msGetSymbol()", pszLibrary); |
| 227 | return NULL; |
| 228 | } |
| 229 | |
| 230 | pSymbol = (void *) GetProcAddress( (HINSTANCE) pLibrary, pszSymbolName ); |
| 231 | |
| 232 | if( pSymbol == NULL ) |
| 233 | { |
| 234 | msSetError(MS_MISCERR, |
| 235 | "Can't find requested entry point: %s in lib %s", |
| 236 | "msGetSymbol()", pszSymbolName, pLibrary); |
| 237 | return NULL; |
| 238 | } |
| 239 | |
| 240 | return( pSymbol ); |
| 241 | } |
| 242 | |
| 243 | #endif /* def _WIN32 */ |
| 244 | |
| 245 | /* ==================================================================== */ |
| 246 | /* Dummy implementation. */ |
| 247 | /* ==================================================================== */ |
| 248 | |
| 249 | #ifndef GOT_GETSYMBOL |
| 250 | |
| 251 | /************************************************************************/ |
| 252 | /* msGetSymbol() */ |
| 253 | /* */ |
| 254 | /* Dummy implementation. */ |
| 255 | /************************************************************************/ |
| 256 | |
| 257 | void *msGetSymbol(const char *pszLibrary, const char *pszEntryPoint) |
| 258 | { |
| 259 | msSetError(MS_MISCERR, |
| 260 | "msGetSymbol(%s,%s) called. Failed as this is stub implementation.", |
| 261 | "msGetSymbol()", pszLibrary, pszEntryPoint); |
| 262 | return NULL; |
| 263 | } |
| 264 | #endif |
Note: See TracBrowser for help on using the browser.
