Wm4System.h

Go to the documentation of this file.
00001 // Wild Magic Source Code
00002 // David Eberly
00003 // http://www.geometrictools.com
00004 // Copyright (c) 1998-2008
00005 //
00006 // This library is free software; you can redistribute it and/or modify it
00007 // under the terms of the GNU Lesser General Public License as published by
00008 // the Free Software Foundation; either version 2.1 of the License, or (at
00009 // your option) any later version.  The license is available for reading at
00010 // either of the locations:
00011 //     http://www.gnu.org/copyleft/lgpl.html
00012 //     http://www.geometrictools.com/License/WildMagicLicense.pdf
00013 //
00014 // Version: 4.0.2 (2008/09/02)
00015 
00016 #ifndef WM4SYSTEM_H
00017 #define WM4SYSTEM_H
00018 
00019 #include "Wm4FoundationLIB.h"
00020 #include "Wm4Platforms.h"
00021 
00022 // common standard library headers
00023 #include <cassert>
00024 #include <cctype>
00025 #include <cfloat>
00026 #include <climits>
00027 #include <cmath>
00028 #include <cstdarg>
00029 #include <cstddef>
00030 #include <cstdio>
00031 #include <cstdlib>
00032 #include <cstring>
00033 #include <ctime>
00034 #include <iostream>
00035 #include <fstream>
00036 
00037 // STL headers
00038 #include <algorithm>
00039 #include <list>
00040 #include <map>
00041 #include <queue>
00042 #include <set>
00043 #include <stack>
00044 #include <string>
00045 #include <utility>
00046 #include <vector>
00047 
00048 // memory management
00049 #include "Wm4Memory.h"
00050 
00051 namespace Wm4
00052 {
00053 
00054 class WM4_FOUNDATION_ITEM System
00055 {
00056 public:
00057     // swap byte order (size = 2, 4, or 8)
00058     static void SwapBytes (int iSize, void* pvValue);
00059     static void SwapBytes (int iSize, int iQuantity, void* pvValue);
00060 
00061     // The output is stored in little endian format.
00062     static bool IsBigEndian ();
00063     static void EndianCopy (int iSize, const void* pvSrc, void* pvDst);
00064     static void EndianCopy (int iSize, int iQuantity, const void* pvSrc,
00065         void* pvDst);
00066 
00067     // time utility (return value is seconds)
00068     static double GetTime ();
00069 
00070     // File support for buffer load and save.
00071     static bool Load (const char* acFilename, char*& racBuffer, int& riSize);
00072     static bool Save (const char* acFilename, const char* acBuffer,
00073         int iSize);
00074     static bool Append (const char* acFilename, char* acBuffer, int iSize);
00075 
00076     // Buffer read and write, file read and write, for character data.  The
00077     // endianness is irrelevant.
00078     static int Read1 (const char* acBuffer, int iQuantity, void* pvData);
00079     static int Write1 (char* acBuffer, int iQuantity, const void* pvData);
00080     static int Read1 (FILE* pkFile, int iQuantity, void* pvData);
00081     static int Write1 (FILE* pkFile, int iQuantity, const void* pvData);
00082 
00083     // Buffer read and write, file read and write.  The operations store the
00084     // results in little-endian order.  The return value is the number of
00085     // bytes read or written.  The function name suffix indicates the number
00086     // of bytes transferred per item.
00087     static int Read2le (const char* acBuffer, int iQuantity, void* pvData);
00088     static int Read4le (const char* acBuffer, int iQuantity, void* pvData);
00089     static int Read8le (const char* acBuffer, int iQuantity, void* pvData);
00090     static int Write2le (char* acBuffer, int iQuantity, const void* pvData);
00091     static int Write4le (char* acBuffer, int iQuantity, const void* pvData);
00092     static int Write8le (char* acBuffer, int iQuantity, const void* pvData);
00093     static int Read2le (FILE* pkFile, int iQuantity, void* pvData);
00094     static int Read4le (FILE* pkFile, int iQuantity, void* pvData);
00095     static int Read8le (FILE* pkFile, int iQuantity, void* pvData);
00096     static int Write2le (FILE* pkFile, int iQuantity, const void* pvData);
00097     static int Write4le (FILE* pkFile, int iQuantity, const void* pvData);
00098     static int Write8le (FILE* pkFile, int iQuantity, const void* pvData);
00099 
00100     // Buffer read and write, file read and write.  The operations store the
00101     // results in big-endian order.  The return value is the number of
00102     // bytes read or written.  The function name suffix indicates the number
00103     // of bytes transferred per item.
00104     static int Read2be (const char* acBuffer, int iQuantity, void* pvData);
00105     static int Read4be (const char* acBuffer, int iQuantity, void* pvData);
00106     static int Read8be (const char* acBuffer, int iQuantity, void* pvData);
00107     static int Write2be (char* acBuffer, int iQuantity, const void* pvData);
00108     static int Write4be (char* acBuffer, int iQuantity, const void* pvData);
00109     static int Write8be (char* acBuffer, int iQuantity, const void* pvData);
00110     static int Read2be (FILE* pkFile, int iQuantity, void* pvData);
00111     static int Read4be (FILE* pkFile, int iQuantity, void* pvData);
00112     static int Read8be (FILE* pkFile, int iQuantity, void* pvData);
00113     static int Write2be (FILE* pkFile, int iQuantity, const void* pvData);
00114     static int Write4be (FILE* pkFile, int iQuantity, const void* pvData);
00115     static int Write8be (FILE* pkFile, int iQuantity, const void* pvData);
00116 
00117     // This is needed on the Macintosh because of its complicated application
00118     // structure.  In particular, this function is used in Xcode projects and
00119     // ignores the directory entry, but assumes that the data files required
00120     // by an application are added to the Resources folder of the projects.
00121     // The other platforms concatenate the directory and filename, the result
00122     // stored in class-static memory (so be careful with threads).
00123     static const char* GetPath (const char* acDirectory,
00124         const char* acFilename);
00125 
00126     // Maintain a list of directories that applications can search to find
00127     // data files.  The list is initially empty when an application starts,
00128     // so you must insert directories of interest, even the current working
00129     // directory.  The directory name should *not* end with a "/".  The
00130     // InsertDirectory and RemoveDirectory functions will manage that
00131     // character themselves.
00132     static void Initialize ();
00133     static void Terminate ();
00134     static int GetDirectoryQuantity ();
00135     static const char* GetDirectory (int i);
00136     static bool InsertDirectory (const char* acDirectory);
00137     static bool RemoveDirectory (const char* acDirectory);
00138     static void RemoveAllDirectories ();
00139 
00140     // The GetPath function searches the list of directories and returns the
00141     // fully decorated file name, if it exists, or return null, if it does
00142     // not exist.  The input iMode is selected from one of the three
00143     // enumerated values listed below.
00144     enum
00145     {
00146         SM_READ,
00147         SM_WRITE,
00148         SM_READ_WRITE
00149     };
00150     static const char* GetPath (const char* acFilename, int eMode);
00151 
00152     // Creation of colors, hides endianness.
00153     static unsigned int MakeRGB (unsigned char ucR, unsigned char ucG,
00154         unsigned char ucB);
00155     static unsigned int MakeRGBA (unsigned char ucR, unsigned char ucG,
00156         unsigned char ucB, unsigned char ucA);
00157 
00158     // Wrappers for standard I/O functions to support security enhancements
00159     // related to buffer overruns.  TO DO:  For now these support MSVC8.0
00160     // and fall back to the older standard I/O functions on other platforms.
00161     // Implementations may be provided for those other platforms to adhere
00162     // to the destination buffer size specification.
00163     static FILE* Fopen (const char* acFilename, const char* acMode);
00164     static int Fprintf (FILE* pkFile, const char* acFormat, ...);
00165     static int Fclose (FILE* pkFile);
00166     static const char* GetEnv (const char* acEnvVarName);
00167     static void* Memcpy (void* pvDst, size_t uiDstSize, const void* pvSrc,
00168         size_t uiSrcSize);
00169     static int Sprintf (char* acDst, size_t uiDstSize, const char* acFormat,
00170         ...);
00171     static char* Strcpy (char* acDst, size_t uiDstSize, const char* acSrc);
00172     static char* Strcat (char* acDst, size_t uiDstSize, const char* acSrc);
00173     static char* Strncpy (char* acDst, size_t uiDstSize, const char* acSrc,
00174         size_t uiSrcSize);
00175     static char* Strtok (char* acToken, const char* acDelimiters,
00176         char*& racNextToken);
00177 
00178 private:
00179     enum { SYSTEM_MAX_PATH = 1024, SYSTEM_MAX_ENVVAR = 1024 };
00180     static char ms_acPath[SYSTEM_MAX_PATH];
00181     static char ms_acEnvVar[SYSTEM_MAX_ENVVAR];
00182     static std::vector<std::string>* ms_pkDirectories;
00183 
00184 public:
00185     static char WM4_PATH[SYSTEM_MAX_ENVVAR];
00186 };
00187 
00188 // Allocation and deallocation of 2D arrays.  On deallocation, the array
00189 // pointer is set to null.
00190 template <class T> void Allocate (int iCols, int iRows, T**& raatArray);
00191 template <class T> static void Deallocate (T**& raatArray);
00192 
00193 // Allocation and deallocation of 3D arrays.  On deallocation, the array
00194 // pointer is set to null.
00195 template <class T> void Allocate (int iCols, int iRows, int iSlices,
00196     T***& raaatArray);
00197 template <class T> void Deallocate (T***& raaatArray);
00198 
00199 #include "Wm4System.inl"
00200 
00201 }
00202 
00203 // Miscellaneous support.  STL does not yet formally have hash structures.
00204 // The includes are placed here because the hash structures use System
00205 // wrappers for memcpy.
00206 #include "Wm4THashSet.h"
00207 #include "Wm4THashTable.h"
00208 #include "Wm4TMinHeap.h"
00209 #include "Wm4TStringHashTable.h"
00210 #include "Wm4TSmallUnorderedSet.h"  // useful for meshes
00211 #include "Wm4TTuple.h"  // tuples in any type and any dimension
00212 
00213 #endif

Generated on Fri Feb 13 13:58:10 2009 for meshmorph by  doxygen 1.5.1