utils.h

Go to the documentation of this file.
00001 /*
00002  *  utils.h - Common utility routines.
00003  *
00004  *  Copyright (C) 1998-1999  Jeffrey S. Freedman
00005  *  Copyright (C) 2000-2002  The Exult Team
00006  *
00007  *  This program is free software; you can redistribute it and/or modify
00008  *  it under the terms of the GNU General Public License as published by
00009  *  the Free Software Foundation; either version 2 of the License, or
00010  *  (at your option) any later version.
00011  *
00012  *  This program is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *  GNU General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU General Public License
00018  *  along with this program; if not, write to the Free Software
00019  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00020  */
00021 
00022 #ifndef _UTILS_H_
00023 #define _UTILS_H_
00024 
00025 #include <iostream>
00026 #include <string>
00027 #ifndef ALPHA_LINUX_CXX
00028 #  include <iosfwd>
00029 #endif
00030 
00031 #include "common_types.h"
00032 
00033 #ifndef HAVE_SNPRINTF
00034 extern int snprintf(char *, size_t, const char *, /*args*/ ...);
00035 #endif
00036 
00037 
00038 /*
00039  *  Read a 1-byte value.
00040  */
00041 
00042 inline uint8 Read1 (std::istream &in)
00043 {
00044   return static_cast<uint8>(in.get());
00045 }
00046 
00047 /*
00048  *  Read a 2-byte value, lsb first.
00049  */
00050 
00051 inline uint16 Read2 (std::istream &in)
00052 {
00053   uint16 val = 0;
00054   val |= static_cast<uint16>(in.get());
00055   val |= static_cast<uint16>(in.get()<<8);
00056   return val;
00057 }
00058 
00059 /*
00060  *  Read a 2-byte value from a buffer.
00061  */
00062 
00063 inline uint16 Read2
00064   (
00065   uint8 *& in
00066   )
00067   {
00068   uint8 b0 = *in++;
00069   uint8 b1 = *in++;
00070   return (b0 | (b1 << 8));
00071   }
00072 
00073 #ifdef BUFSIZ /* Kludgy, but I don't want to include stdio.h all the time.*/
00074 /*
00075  *  Read a 2-byte value from a file.
00076  */
00077 
00078 inline uint16 Read2
00079   (
00080   std::FILE* in
00081   )
00082   {
00083   uint8 b0, b1;
00084   std::fread(&b0,sizeof(uint8),1,in);
00085   std::fread(&b1,sizeof(uint8),1,in);
00086   return (b0 | (b1 << 8));
00087   }
00088 #endif
00089 
00090 /*
00091  *  Read a 2-byte value, hsb first.
00092  */
00093 
00094 inline uint16 Read2high (std::istream &in)
00095 {
00096   uint16 val = 0;
00097   val |= static_cast<uint16>(in.get()<<8);
00098   val |= static_cast<uint16>(in.get());
00099   return val;
00100 }
00101 
00102 /*
00103  *  Read a 2-byte value from a buffer.
00104  */
00105 
00106 inline uint16 Read2high
00107   (
00108   uint8 *& in
00109   )
00110   {
00111   uint8 b0 = *in++;
00112   uint8 b1 = *in++;
00113   return ((b0 << 8) | b1);
00114   }
00115 
00116 #ifdef BUFSIZ /* Kludgy, but I don't want to include stdio.h all the time.*/
00117 /*
00118  *  Read a 2-byte value from a file.
00119  */
00120 
00121 inline uint16 Read2high
00122   (
00123   std::FILE* in
00124   )
00125   {
00126   uint8 b0, b1;
00127   std::fread(&b0,sizeof(uint8),1,in);
00128   std::fread(&b1,sizeof(uint8),1,in);
00129   return ((b0 << 8) | b1);
00130   }
00131 #endif
00132 
00133 /*
00134  *  Read a 4-byte long value, lsb first.
00135  */
00136 
00137 inline uint32 Read4 (std::istream &in)
00138 {
00139   uint32 val = 0;
00140   val |= static_cast<uint32>(in.get());
00141   val |= static_cast<uint32>(in.get()<<8);
00142   val |= static_cast<uint32>(in.get()<<16);
00143   val |= static_cast<uint32>(in.get()<<24);
00144   return val;
00145 }
00146 
00147 /*
00148  *  Read a 4-byte value from a buffer.
00149  */
00150 
00151 inline uint32 Read4
00152   (
00153   uint8 *& in
00154   )
00155   {
00156   uint8 b0 = *in++;
00157   uint8 b1 = *in++;
00158   uint8 b2 = *in++;
00159   uint8 b3 = *in++;
00160   return (b0 | (b1<<8) | (b2<<16) | (b3<<24));
00161   }
00162 
00163 #ifdef BUFSIZ /* Kludgy, but I don't want to include stdio.h all the time.*/
00164 /*
00165  *  Read a 4-byte value from a file.
00166  */
00167 
00168 inline uint32 Read4
00169   (
00170   std::FILE* in
00171   )
00172   {
00173   uint8 b0, b1, b2, b3;
00174   std::fread(&b0,sizeof(uint8),1,in);
00175   std::fread(&b1,sizeof(uint8),1,in);
00176   std::fread(&b2,sizeof(uint8),1,in);
00177   std::fread(&b3,sizeof(uint8),1,in);
00178   return (b0 | (b1<<8) | (b2<<16) | (b3<<24));
00179   }
00180 #endif
00181 /*
00182  *  Read a 4-byte long value, hsb first.
00183  */
00184 
00185 inline uint32 Read4high (std::istream &in)
00186 {
00187   uint32 val = 0;
00188   val |= static_cast<uint32>(in.get()<<24);
00189   val |= static_cast<uint32>(in.get()<<16);
00190   val |= static_cast<uint32>(in.get()<<8);
00191   val |= static_cast<uint32>(in.get());
00192   return val;
00193 }
00194 
00195 /*
00196  *  Read a 4-byte value from a buffer.
00197  */
00198 
00199 inline uint32 Read4high
00200   (
00201   uint8 *& in
00202   )
00203   {
00204   uint8 b0 = *in++;
00205   uint8 b1 = *in++;
00206   uint8 b2 = *in++;
00207   uint8 b3 = *in++;
00208   return ((b0<<24) | (b1<<16) | (b2<<8) | b3);
00209   }
00210 
00211 #ifdef BUFSIZ /* Kludgy, but I don't want to include stdio.h all the time.*/
00212 /*
00213  *  Read a 4-byte value from a file.
00214  */
00215 
00216 inline uint32 Read4high
00217   (
00218   std::FILE* in
00219   )
00220   {
00221   uint8 b0, b1, b2, b3;
00222   std::fread(&b0,sizeof(uint8),1,in);
00223   std::fread(&b1,sizeof(uint8),1,in);
00224   std::fread(&b2,sizeof(uint8),1,in);
00225   std::fread(&b3,sizeof(uint8),1,in);
00226   return ((b0<<24) | (b1<<16) | (b2<<8) | b3);
00227   }
00228 #endif
00229 
00230 /*
00231  *  Write a 1-byte value.
00232  */
00233 
00234 inline void Write1(std::ostream& out, uint16 val)
00235 {
00236   out.put(static_cast<char> (val&0xff));
00237 }
00238 
00239 /*
00240  *  Write a 2-byte value, lsb first.
00241  */
00242 
00243 inline void Write2
00244   (
00245   std::ostream& out,
00246   uint16 val
00247   )
00248   {
00249   out.put(static_cast<char> (val&0xff));
00250   out.put(static_cast<char> ((val>>8)&0xff));
00251   }
00252 
00253 /*
00254  *  Write a 2-byte value, msb first.
00255  */
00256 
00257 inline void Write2high
00258   (
00259   std::ostream& out,
00260   uint16 val
00261   )
00262   {
00263   out.put(static_cast<char> ((val>>8)&0xff));
00264   out.put(static_cast<char> (val&0xff));
00265   }
00266 
00267 /*
00268  *  Write a 2-byte value to a buffer, lsb first.
00269  */
00270 
00271 inline void Write2
00272   (
00273   uint8 *& out,   // Write here and update.
00274   uint16 val
00275   )
00276   {
00277   *out++ = val & 0xff;
00278   *out++ = (val>>8) & 0xff;
00279   }
00280 
00281 /*
00282  *  Write a 4-byte value, lsb first.
00283  */
00284 
00285 inline void Write4
00286   (
00287   std::ostream& out,
00288   uint32 val
00289   )
00290   {
00291   out.put(static_cast<char> (val&0xff));
00292   out.put(static_cast<char> ((val>>8)&0xff));
00293   out.put(static_cast<char> ((val>>16)&0xff));
00294   out.put(static_cast<char> ((val>>24)&0xff));
00295   }
00296 
00297 /*
00298  *  Write a 4-byte value, msb first.
00299  */
00300 
00301 inline void Write4high
00302   (
00303   std::ostream& out,
00304   uint32 val
00305   )
00306   {
00307   out.put(static_cast<char> ((val>>24)&0xff));
00308   out.put(static_cast<char> ((val>>16)&0xff));
00309   out.put(static_cast<char> ((val>>8)&0xff));
00310   out.put(static_cast<char> (val&0xff));
00311   }
00312 
00313 /*
00314  *  Write a 4-byte value to a buffer, lsb first.
00315  */
00316 
00317 inline void Write4
00318   (
00319   uint8 *& out,   // Write here and update.
00320   uint32 val
00321   )
00322   {
00323   *out++ = val & 0xff;
00324   *out++ = (val>>8) & 0xff;
00325   *out++ = (val>>16)&0xff;
00326   *out++ = (val>>24)&0xff;
00327   }
00328 
00329 inline void Write4s(std::ostream& out, sint32 val)
00330 {
00331   Write4(out, static_cast<uint32>(val));
00332 }
00333 
00334 bool U7open
00335   (
00336   std::ifstream& in,      // Input stream to open.
00337   const char *fname,      // May be converted to upper-case.
00338   bool is_text = false      // Should the file be opened in text mode
00339   );
00340 bool U7open
00341   (
00342   std::ofstream& out,     // Output stream to open.
00343   const char *fname,      // May be converted to upper-case.
00344   bool is_text = false      // Should the file be opened in text mode
00345   );
00346 
00347 #ifdef BUFSIZ /* Kludgy, but I don't want to include stdio.h all the time.*/
00348 std::FILE* U7open
00349   (
00350   const char *fname,      // May be converted to upper-case.
00351   const char *mode      // File access mode.
00352   );
00353 #endif
00354 void U7remove(
00355   const char *fname
00356   );
00357 
00358 bool U7exists(
00359   const char *fname
00360   );
00361 
00362 inline bool U7exists(std::string fname) { return U7exists(fname.c_str()); }
00363 
00364 
00365 int U7mkdir(
00366   const char *dirname,
00367   int mode
00368   );
00369 
00370 // These are not supported in WinCE (PocketPC) for now
00371 #ifndef UNDER_CE
00372 
00373 int U7chdir(
00374   const char *dirname
00375   );
00376 
00377 void U7copy
00378   (
00379   const char *src,
00380   const char *dest
00381   );
00382 
00383 #endif //UNDER_CE
00384 
00385 bool is_system_path_defined(const std::string& key);
00386 void store_system_paths();
00387 void reset_system_paths();
00388 void clear_system_path(const std::string& key);
00389 void add_system_path(const std::string& key, const std::string& value);
00390 void clone_system_path(const std::string& new_key, const std::string& old_key);
00391 std::string get_system_path(const std::string &path);
00392 
00393 void to_uppercase(std::string &str);
00394 std::string to_uppercase(const std::string &str);
00395 
00396 int Log2(uint32 n);
00397 
00398 char *newstrdup(const char *s);
00399 
00400 #endif  /* _UTILS_H_ */

Generated on Mon Jul 9 14:42:45 2007 for ExultEngine by  doxygen 1.5.1