MercuryUtil.cpp

Go to the documentation of this file.
00001 #include "MercuryFiles.h"
00002 #include "MercuryUtil.h"
00003 #include "Crash/archCrash.h"
00004 
00005 #if defined(WIN32)
00006 #include <windows.h>
00007 #endif
00008 
00009 void Sleep( float msec )
00010 {
00011 #if defined( WIN32 )
00012     #if defined(__GNUC__)
00013         Sleep( DWORD(msec) );
00014     #else
00015         Sleep( unsigned long(msec) );
00016     #endif
00017 #else
00018 #if !defined( _EE )
00019     usleep(((unsigned long)msec)*1000);
00020 #else
00021     //XXX: MUST WRITE SLEEP FUNCTION FOR EE
00022 #endif
00023 #endif
00024 }
00025 
00026 MString ConvertToCFormat( const MString & ncf )
00027 {
00028     MString ret;
00029     const char* nccf = ncf.c_str();
00030 
00031     for ( int i = 0; (unsigned)i < ncf.length(); i++ )
00032     {
00033         switch ( nccf[i] )
00034         {
00035         case '\t':  ret += "\\t";   break;
00036         case '\n':  ret += "\\n";   break;
00037         case '\r':  ret += "\\r";   break;
00038         case '\f':  ret += "\\f";   break;
00039         case '\b':  ret += "\\b";   break;
00040         case '\\':  ret += "\\\\";  break;
00041         case '\'':  ret += "\\\'";  break;
00042         case '\"':  ret += "\\\"";  break;
00043         default:
00044             if( nccf[i] < 32 )
00045             {
00046                 ret += "\\"; 
00047                 ret += ((unsigned char)nccf[i]/64)%8 + '0';
00048                 ret += ((unsigned char)nccf[i]/8)%8 + '0';
00049                 ret += (unsigned char)nccf[i]%8 + '0';
00050             } else
00051                 ret += nccf[i];
00052         }
00053     }
00054     return ret;
00055 }
00056 
00057 MString ConvertToUnformatted( const MString & cf )
00058 {
00059     MString ret;
00060     const char* ccf = cf.c_str();
00061 
00062     for ( int i = 0; (unsigned)i < cf.length(); i++ )
00063     {
00064         switch ( ccf[i] )
00065         {
00066         case '\\':
00067             i++;
00068             if ( (unsigned)i >= cf.length() )
00069                 return ret;
00070             switch ( ccf[i] )
00071             {
00072             case 't':   ret += '\t';    break;
00073             case 'n':   ret += '\n';    break;
00074             case 'r':   ret += '\r';    break;
00075             case 'f':   ret += '\f';    break;
00076             case 'b':   ret += '\b';    break;
00077             case '\\':  ret += '\\';    break;
00078             case '\'':  ret += '\'';    break;
00079             case '\"':  ret += '\"';    break;
00080             default:
00081                 if( ccf[i] >= '0' && ccf[i] <= '7' )
00082                 {
00083                     char c = ccf[i] - '0';
00084                     if( ccf[i+1] >= '0' && ccf[i+1] <= '8' )
00085                     {
00086                         i++;
00087                         c = c * 8 + ccf[i] - '0';
00088                     }
00089                     if( ccf[i+1] >= '0' && ccf[i+1] <= '8' )
00090                     {
00091                         i++;
00092                         c = c * 8 + ccf[i] - '0';
00093                     }
00094                     ret += c;
00095                 }
00096             }
00097             break;
00098         default:
00099             ret += ccf[i];
00100         }
00101     }
00102     return ret;
00103 }
00104 
00105 long    DumpFromFile( const MString & filename, char * & data )
00106 {
00107     data = NULL;
00108     MercuryFile * f = FILEMAN.Open( filename );
00109 
00110     if ( f == NULL )
00111         return -1;
00112 
00113     unsigned long length = f->Length();
00114 
00115     data = (char*)malloc( length );
00116 
00117     if ( f->Read( data, length ) != length )
00118     {
00119         free( data );
00120         data = NULL;
00121         SAFE_DELETE(f);
00122         return -1;
00123     }
00124     SAFE_DELETE(f);
00125     return length;
00126 }
00127 
00128 bool    DumpToFile( const MString & filename, const char * data, long bytes )
00129 {
00130     MercuryFile * f = FILEMAN.Open( filename, MFP_WRITE_ONLY );
00131     if ( f == NULL )
00132         return false;
00133     
00134     bool ret = f->Write( (void*) data, bytes );
00135 
00136     SAFE_DELETE(f);
00137 
00138     return ret;
00139 }
00140 
00141 void    FileToMString( const MString & filename, MString & data )
00142 {
00143     char * tdata;
00144     long len = DumpFromFile( filename, tdata );
00145     data.assign( tdata, len );
00146     free ( tdata );
00147 }
00148 
00149 void    MStringToFile( const MString & filename, const MString & data )
00150 {
00151     DumpToFile( filename, data.c_str(), data.length() );
00152 }
00153 
00154 bool    FileExists( const MString & filename )
00155 {
00156     MercuryFile * f = FILEMAN.Open( filename.c_str() );
00157     if ( f == NULL )
00158         return false;
00159     SAFE_DELETE( f );
00160     return true;
00161 }
00162 
00163 long    BytesUntil( const char* strin, const char * termin, long start, long slen, long termlen )
00164 {
00165     int i;
00166     for ( i = start; i < slen; i++ )
00167         for ( int j = 0; j < termlen; j++ )
00168             if ( termin[j] == strin[i] )
00169                 return i - start;
00170     return i - start;
00171 }
00172 
00173 long    BytesNUntil( const char* strin, const char * termin, long start, long slen, long termlen )
00174 {
00175     int i;
00176     for ( i = start; i < slen; i++ )
00177     {
00178         bool found = false;
00179         for ( int j = 0; j < termlen; j++ )
00180         {
00181             if ( termin[j] == strin[i] )
00182                 found = true;
00183         }
00184         if ( !found ) 
00185             return i - start;
00186     }
00187     return i - start;
00188 }
00189 
00190 void    SplitStrings( const MString & in, MVector < MString > & out, 
00191                      const char * termin, const char * whitespace, 
00192                      long termlen, long wslen )
00193 {
00194     const char * inStr = in.c_str();
00195     long    curPos = 0;
00196     long    inLen = in.length();
00197     while ( curPos < inLen )
00198     {
00199         curPos += BytesNUntil( inStr, whitespace, curPos, inLen, wslen );
00200         long NextPos = BytesUntil( inStr, termin, curPos, inLen, termlen );
00201         out.push_back( in.substr( curPos, NextPos ) );
00202         curPos += NextPos + 1;
00203     }
00204 }
00205 
00206 void mercury_crash( const char *reason  )
00207 {
00208 #if defined( WIN32 )
00209     MessageBox( 0, reason, "Mercury Game Engine Crash", 0 );
00210     ForceCrashHandler( reason );
00211 #endif
00212     ForceCrashHandler( reason );
00213 }
00214 
00215 
00216 int GeneralUsePrimes[] = { 3, 13, 37, 73, 131, 229, 337, 821, 2477, 4594, 8941, 14797, 24953, 39041, 60811, 104729 };
00217 
00218 int GetAPrime( int ith )
00219 {
00220     return (ith<0)?3:(ith>15)?GeneralUsePrimes[15]:GeneralUsePrimes[ith];
00221 }
00222 
00223 /* 
00224  * Copyright (c) 2005-2006, Charles Lohr
00225  * All rights reserved.
00226  *
00227  * Redistribution and use in source and binary forms, with or
00228  * without modification, are permitted provided that the following
00229  * conditions are met:
00230  *  -   Redistributions of source code must retain the above
00231  *      copyright notice, this list of conditions and the following disclaimer.
00232  *  -   Redistributions in binary form must reproduce the above copyright
00233  *      notice, this list of conditions and the following disclaimer in
00234  *      the documentation and/or other materials provided with the distribution.
00235  *  -   Neither the name of the Mercury Engine nor the names of its
00236  *      contributors may be used to endorse or promote products derived from
00237  *      this software without specific prior written permission.
00238  *
00239  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00240  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00241  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00242  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00243  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00244  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00245  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00246  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00247  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00248  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00249  */

Hosted by SourceForge.net Logo