/ source / blood / src / misc.cpp
misc.cpp
  1  //-------------------------------------------------------------------------
  2  /*
  3  Copyright (C) 2010-2019 EDuke32 developers and contributors
  4  Copyright (C) 2019 Nuke.YKT
  5  
  6  This file is part of NBlood.
  7  
  8  NBlood is free software; you can redistribute it and/or
  9  modify it under the terms of the GNU General Public License version 2
 10  as published by the Free Software Foundation.
 11  
 12  This program is distributed in the hope that it will be useful,
 13  but WITHOUT ANY WARRANTY; without even the implied warranty of
 14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 15  
 16  See the GNU General Public License for more details.
 17  
 18  You should have received a copy of the GNU General Public License
 19  along with this program; if not, write to the Free Software
 20  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 21  */
 22  //-------------------------------------------------------------------------
 23  #include <stdio.h>
 24  #include <string.h>
 25  #include "common_game.h"
 26  
 27  #include "misc.h"
 28  
 29  void *ResReadLine(char *buffer, unsigned int nBytes, void **pRes)
 30  {
 31      unsigned int i;
 32      char ch;
 33      if (!pRes || !*pRes || *((char*)*pRes) == 0)
 34          return NULL;
 35      for (i = 0; i < nBytes; i++)
 36      {
 37          ch = *((char*)*pRes);
 38          if(ch == 0 || ch == '\n')
 39              break;
 40          buffer[i] = ch;
 41          *pRes = ((char*)*pRes)+1;
 42      }
 43      if (*((char*)*pRes) == '\n' && i < nBytes)
 44      {
 45          ch = *((char*)*pRes);
 46          buffer[i] = ch;
 47          *pRes = ((char*)*pRes)+1;
 48          i++;
 49      }
 50      else
 51      {
 52          while (true)
 53          {
 54              ch = *((char*)*pRes);
 55              if (ch == 0 || ch == '\n')
 56                  break;
 57              *pRes = ((char*)*pRes)+1;
 58          }
 59          if (*((char*)*pRes) == '\n')
 60              *pRes = ((char*)*pRes)+1;
 61      }
 62      if (i < nBytes)
 63          buffer[i] = 0;
 64      return *pRes;
 65  }
 66  
 67  bool FileRead(FILE *handle, void *buffer, unsigned int size)
 68  {
 69      return fread(buffer, 1, size, handle) == size;
 70  }
 71  
 72  bool FileWrite(FILE *handle, void *buffer, unsigned int size)
 73  {
 74      return fwrite(buffer, 1, size, handle) == size;
 75  }
 76  
 77  bool FileLoad(const char *name, void *buffer, unsigned int size)
 78  {
 79      dassert(buffer != NULL);
 80  
 81      FILE *handle = fopen(name, "rb");
 82      if (!handle)
 83          return false;
 84  
 85      unsigned int nread = fread(buffer, 1, size, handle);
 86      fclose(handle);
 87      return nread == size;
 88  }
 89  
 90  int FileLength(FILE *handle)
 91  {
 92      if (!handle)
 93          return 0;
 94      int nPos = ftell(handle);
 95      fseek(handle, 0, SEEK_END);
 96      int nLength = ftell(handle);
 97      fseek(handle, nPos, SEEK_SET);
 98      return nLength;
 99  }
100  
101  unsigned int randSeed = 1;
102  
103  unsigned int qrand(void)
104  {
105      if (randSeed&0x80000000)
106          randSeed = ((randSeed<<1)^0x20000004)|0x1;
107      else
108          randSeed = randSeed<<1;
109      return randSeed&0x7fff;
110  }
111  
112  void ChangeExtension(char *pzFile, const char *pzExt)
113  {
114  #if 0
115      char drive[BMAX_PATH];
116      char dir[BMAX_PATH];
117      char filename[BMAX_PATH];
118      _splitpath(pzFile, drive, dir, filename, NULL);
119      _makepath(pzFile, drive, dir, filename, pzExt);
120  #else
121      int const nLength = Bstrlen(pzFile);
122      char * pDot = pzFile+nLength;
123      for (int i = nLength-1; i >= 0; i--)
124      {
125          if (pzFile[i] == '/' || pzFile[i] == '\\')
126              break;
127          if (pzFile[i] == '.')
128          {
129              pDot = pzFile+i;
130              break;
131          }
132      }
133      *pDot = '\0';
134      Bstrcat(pDot, pzExt);
135  #endif
136  }
137  
138  void SplitPath(const char *pzPath, char *pzDirectory, char *pzFile, char *pzType)
139  {
140      int const nLength = Bstrlen(pzPath);
141      //const char *pDirectory = pzPath+nLength;
142      const char *pDot = NULL;
143      for (int i = nLength-1; i >= 0; i--)
144      {
145          if (pzPath[i] == '/' || pzPath[i] == '\\')
146          {
147              Bstrncpy(pzDirectory, pzPath, i);
148              pzDirectory[i] = 0;
149              if (!pDot)
150              {
151                  Bstrcpy(pzFile, pzPath+i+1);
152                  Bstrcpy(pzType, "");
153              }
154              else
155              {
156                  Bstrncpy(pzFile, pzPath+i+1, pDot-(pzPath+i+1));
157                  pzFile[pDot-(pzPath+i+1)] = 0;
158                  Bstrcpy(pzType, pDot+1);
159              }
160             
161              return;
162          }
163          else if (pzPath[i] == '.')
164          {
165              pDot = pzPath+i;
166          }
167      }
168      Bstrcpy(pzDirectory, "/");
169      if (!pDot)
170      {
171          Bstrcpy(pzFile, pzPath);
172          Bstrcpy(pzType, "");
173      }
174      else
175      {
176          Bstrncpy(pzFile, pzPath, pDot-pzPath);
177          pzFile[pDot-pzPath] = 0;
178          Bstrcpy(pzType, pDot+1);
179      }
180  }
181  
182  void ConcatPath(const char *pzPath1, const char *pzPath2, char *pzConcatPath)
183  {
184      int n1 = Bstrlen(pzPath1), n2 = Bstrlen(pzPath2);
185      int i = n1, j = 0;
186      while (i > 0 && (pzPath1[i-1] == '/' || pzPath1[i-1] == '\\'))
187      {
188          i--;
189      }
190      while (j < n2 && (pzPath2[j] == '/' || pzPath1[j] == '\\'))
191      {
192          j++;
193      }
194      Bstrncpy(pzConcatPath, pzPath1, i);
195      pzConcatPath[i] = 0;
196      Bstrcat(pzConcatPath, "/");
197      Bstrcat(pzConcatPath, pzPath2+j);
198  }
199  
200