home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2007 September / maximum-cd-2007-09.iso / Assets / data / AssaultCube_v0.93.exe / source / src / tools.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2007-06-01  |  2.3 KB  |  93 lines

  1. // implementation of generic tools
  2.  
  3. #include "cube.h"
  4.  
  5. //////////////////////////// pool ///////////////////////////
  6.  
  7. ///////////////////////// misc tools ///////////////////////
  8.  
  9. char *path(char *s)
  10. {
  11.     for(char *t = s; (t = strpbrk(t, "/\\")); *t++ = PATHDIV);
  12.     for(char *prevdir = NULL, *curdir = s;;)
  13.     {
  14.         prevdir = curdir[0]==PATHDIV ? curdir+1 : curdir;
  15.         curdir = strchr(prevdir, PATHDIV);
  16.         if(!curdir) break;
  17.         if(prevdir+1==curdir && prevdir[0]=='.')
  18.         {
  19.             memmove(prevdir, curdir+1, strlen(curdir+1)+1);
  20.             curdir = prevdir;
  21.         }
  22.         else if(curdir[1]=='.' && curdir[2]=='.' && curdir[3]==PATHDIV)
  23.         {
  24.             if(prevdir+2==curdir && prevdir[0]=='.' && prevdir[1]=='.') continue;
  25.             memmove(prevdir, curdir+4, strlen(curdir+4)+1);
  26.             curdir = prevdir;
  27.         }
  28.     }
  29.     return s;
  30. }
  31.  
  32. char *parentdir(char *directory)
  33. {
  34.     char *p = strrchr(directory, '/');
  35.     if(!p) p = directory;
  36.     size_t len = p-directory+1;
  37.     char *parent = new char[len];
  38.     s_strncpy(parent, directory, len);
  39.     return parent;
  40. }
  41.  
  42. char *loadfile(char *fn, int *size)
  43. {
  44.     FILE *f = fopen(fn, "rb");
  45.     if(!f) return NULL;
  46.     fseek(f, 0, SEEK_END);
  47.     int len = ftell(f);
  48.     if(len<=0) { fclose(f); return NULL; };
  49.     fseek(f, 0, SEEK_SET);
  50.     char *buf = new char[len+1];
  51.     if(!buf) { fclose(f); return NULL; };
  52.     buf[len] = 0;
  53.     size_t rlen = fread(buf, 1, len, f);
  54.     fclose(f);
  55.     if(size_t(len)!=rlen)
  56.     {
  57.         delete[] buf;
  58.         return NULL;
  59.     }
  60.     if(size!=NULL) *size = len;
  61.     return buf;
  62. }
  63.  
  64. bool cmpb(void *b, int n, enet_uint32 c)
  65. {
  66.     ENetBuffer buf;
  67.     buf.data = b;
  68.     buf.dataLength = n;
  69.     return enet_crc32(&buf, 1)==c;
  70. }
  71.  
  72. bool cmpf(char *fn, enet_uint32 c)
  73. {
  74.     int n = 0;
  75.     char *b = loadfile(fn, &n);
  76.     bool r = cmpb(b, n, c);
  77.     delete[] b;
  78.     return r;
  79. }
  80.  
  81. void endianswap(void *memory, int stride, int length)   // little endian as storage format
  82. {
  83. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  84.     loop(w, length) loop(i, stride/2)
  85.     {
  86.         uchar *p = (uchar *)memory+w*stride;
  87.         uchar t = p[i];
  88.         p[i] = p[stride-i-1];
  89.         p[stride-i-1] = t;
  90.     }
  91. #endif
  92. }
  93.