csutil/util.h
Go to the documentation of this file.00001 /* 00002 Copyright (C) 1998 by Jorrit Tyberghein 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public 00015 License along with this library; if not, write to the Free 00016 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00017 */ 00018 00019 #ifndef __CS_UTIL_H__ 00020 #define __CS_UTIL_H__ 00021 00022 #include <stdio.h> 00023 #include "csextern.h" 00024 #include "csutil/csunicode.h" 00025 00033 namespace CS 00034 { 00043 CS_CRYSTALSPACE_EXPORT char* StrDup (const char *s); 00049 CS_CRYSTALSPACE_EXPORT char* StrDup (const wchar_t *s); 00055 CS_CRYSTALSPACE_EXPORT wchar_t* StrDupW (const wchar_t *s); 00061 CS_CRYSTALSPACE_EXPORT wchar_t* StrDupW (const char *s); 00062 00072 CS_CRYSTALSPACE_EXPORT const char* StrCaseStr (const char *str1, const char *str2); 00073 } 00074 00076 CS_CRYSTALSPACE_EXPORT size_t cs_wcslen (wchar_t const* s); 00077 00086 inline char *csStrNew (const char *s) 00087 { 00088 if (!s) return 0; 00089 size_t sl = strlen (s) + 1; 00090 char *r = new char [sl]; 00091 memcpy (r, s, sl); 00092 return r; 00093 } 00100 inline char *csStrNew (const wchar_t *s) 00101 { 00102 if (!s) return 0; 00103 char* cs = CS::StrDup (s); 00104 size_t sl = strlen (cs) + 1; 00105 char *r = new char [sl]; 00106 memcpy (r, cs, sl); 00107 cs_free (cs); 00108 return r; 00109 } 00116 inline wchar_t* csStrNewW (const wchar_t *s) 00117 { 00118 if (!s) return 0; 00119 size_t sl = cs_wcslen (s) + 1; 00120 wchar_t *r = new wchar_t [sl]; 00121 memcpy (r, s, sl * sizeof (wchar_t)); 00122 return r; 00123 } 00130 inline wchar_t* csStrNewW (const char *s) 00131 { 00132 if (!s) return 0; 00133 wchar_t* ws = CS::StrDupW (s); 00134 size_t sl = cs_wcslen (ws) + 1; 00135 wchar_t *r = new wchar_t [sl]; 00136 memcpy (r, ws, sl * sizeof (wchar_t)); 00137 cs_free (ws); 00138 return r; 00139 } 00140 00147 CS_CRYSTALSPACE_EXPORT int csStrCaseCmp(char const* str1, char const* str2); 00148 00156 CS_CRYSTALSPACE_EXPORT int csStrNCaseCmp(char const* str1, char const* str2, 00157 size_t n); 00158 00172 struct csWtoC 00173 { 00174 private: 00175 char* s; 00176 public: 00181 csWtoC (const wchar_t* ws) 00182 { s = CS::StrDup (ws); } 00186 ~csWtoC () 00187 { cs_free (s); } 00191 operator const char* () const 00192 { return s; } 00193 }; 00194 00202 struct csCtoW 00203 { 00204 private: 00205 wchar_t* ws; 00206 public: 00211 csCtoW (const char* s) 00212 { ws = CS::StrDupW (s); } 00216 ~csCtoW () 00217 { cs_free (ws); } 00221 operator const wchar_t* () const 00222 { return ws; } 00223 }; 00224 00234 CS_CRYSTALSPACE_EXPORT char *csExpandName (const char *iName); 00235 00239 CS_CRYSTALSPACE_EXPORT void csSplitPath (const char *iPathName, char *oPath, 00240 size_t iPathSize, char *oName, size_t iNameSize); 00241 00250 CS_CRYSTALSPACE_EXPORT bool csGlobMatches (const char *fName, 00251 const char *fMask); 00252 00257 static inline int csFindNearestPowerOf2 (int n) 00258 { 00259 int v=n; 00260 00261 v--; 00262 v |= v >> 1; 00263 v |= v >> 2; 00264 v |= v >> 4; 00265 v |= v >> 8; 00266 v |= v >> 16; 00267 v++; 00268 00269 return v; 00270 } 00271 00273 static inline bool csIsPowerOf2 (int n) 00274 { 00275 return !(n & (n - 1)) && n; // (n-1) ^ n >= n; 00276 } 00277 00279 CS_CRYSTALSPACE_EXPORT int csLog2 (int n); 00280 00286 CS_CRYSTALSPACE_EXPORT void csReplaceAll (char *dest, const char *src, 00287 const char *search, const char *replace, int max); 00288 00289 00292 #endif // __CS_UTIL_H__
Generated for Crystal Space 2.1 by doxygen 1.6.1
