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

Generated on Wed Nov 23 19:01:08 2011 for FreeCAD by  doxygen 1.6.1