csutil/util.h Source File (Crystal Space 2.0 Public API Reference)

| ## Public API Reference

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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 }
    00063
    00065 CS_CRYSTALSPACE_EXPORT size_t cs_wcslen (wchar_t const* s);
    00066
    00075 inline char *csStrNew (const char *s)
    00076 {
    00077   if (!s) return 0;
    00078   size_t sl = strlen (s) + 1;
    00079   char *r = new char [sl];
    00080   memcpy (r, s, sl);
    00081   return r;
    00082 }
    00089 inline char *csStrNew (const wchar_t *s)
    00090 {
    00091   if (!s) return 0;
    00092   char* cs = CS::StrDup (s);
    00093   size_t sl = strlen (cs) + 1;
    00094   char *r = new char [sl];
    00095   memcpy (r, cs, sl);
    00096   cs_free (cs);
    00097   return r;
    00098 }
    00105 inline wchar_t* csStrNewW (const wchar_t *s)
    00106 {
    00107   if (!s) return 0;
    00108   size_t sl = cs_wcslen (s) + 1;
    00109   wchar_t *r = new wchar_t [sl];
    00110   memcpy (r, s, sl * sizeof (wchar_t));
    00111   return r;
    00112 }
    00119 inline wchar_t* csStrNewW (const char *s)
    00120 {
    00121   if (!s) return 0;
    00122   wchar_t* ws = CS::StrDupW (s);
    00123   size_t sl = cs_wcslen (ws) + 1;
    00124   wchar_t *r = new wchar_t [sl];
    00125   memcpy (r, ws, sl * sizeof (wchar_t));
    00126   cs_free (ws);
    00127   return r;
    00128 }
    00129
    00136 CS_CRYSTALSPACE_EXPORT int csStrCaseCmp(char const* str1, char const* str2);
    00137
    00145 CS_CRYSTALSPACE_EXPORT int csStrNCaseCmp(char const* str1, char const* str2,
    00146   size_t n);
    00147
    00161 struct csWtoC
    00162 {
    00163 private:
    00164   char* s;
    00165 public:
    00170   csWtoC (const wchar_t* ws)
    00171   { s = CS::StrDup (ws); }
    00175   ~csWtoC ()
    00176   { cs_free (s); }
    00180   operator const char* () const
    00181   { return s; }
    00182 };
    00183
    00191 struct csCtoW
    00192 {
    00193 private:
    00194   wchar_t* ws;
    00195 public:
    00200   csCtoW (const char* s)
    00201   { ws = CS::StrDupW (s); }
    00205   ~csCtoW ()
    00206   { cs_free (ws); }
    00210   operator const wchar_t* () const
    00211   { return ws; }
    00212 };
    00213
    00223 CS_CRYSTALSPACE_EXPORT char *csExpandName (const char *iName);
    00224
    00228 CS_CRYSTALSPACE_EXPORT void csSplitPath (const char *iPathName, char *oPath,
    00229   size_t iPathSize, char *oName, size_t iNameSize);
    00230
    00239 CS_CRYSTALSPACE_EXPORT bool csGlobMatches (const char *fName,
    00240         const char *fMask);
    00241
    00246 static inline int csFindNearestPowerOf2 (int n)
    00247 {
    00248   int v=n;
    00249
    00250   v--;
    00251   v |= v >> 1;
    00252   v |= v >> 2;
    00253   v |= v >> 4;
    00254   v |= v >> 8;
    00255   v |= v >> 16;
    00256   v++;
    00257
    00258   return v;
    00259 }
    00260
    00262 static inline bool csIsPowerOf2 (int n)
    00263 {
    00264   return !(n & (n - 1)) && n;   // (n-1) ^ n >= n;
    00265 }
    00266
    00268 CS_CRYSTALSPACE_EXPORT int csLog2 (int n);
    00269
    00275 CS_CRYSTALSPACE_EXPORT void csReplaceAll (char *dest, const char *src,
    00276   const char *search, const char *replace, int max);
    00277
    00278
    00281 #endif // __CS_UTIL_H__