Main Page   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields   Globals   Related Pages  

o_shared.cpp

Go to the documentation of this file.
00001 // OMICRON ENGINE HEADER FILE
00002 //
00003 // --------------------------------------------------------------------------
00004 // Copyright (C) 2001-2002 by Bjoern Paetzel <kolrabi@gmx.de>
00005 //
00006 // This file is part of the Omicron Engine.
00007 //
00008 // The Omicron Engine is free software; you can redistribute it and/or modify
00009 // it under the terms of the  GNU General Public License  as published by the
00010 // Free Software Foundation;  either version  2  of the License,  or (at your
00011 // option) any later version.
00012 //
00013 // The Omicron Engine  is distributed in the hope that it will be useful, but
00014 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00015 // or  FITNESS FOR A PARTICULAR PURPOSE.  See the  GNU General Public License
00016 // for more details.
00017 //
00018 // You should have  received a copy of the  GNU General Public License  along
00019 // with The Omicron Engine;  if not,  write to the  Free Software Foundation,
00020 // Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
00021 //
00022 // --------------------------------------------------------------------------
00023 // Last modified:       $Date: 2002/12/07 19:02:06 $
00024 // By           :       $Author: kolrabi $
00025 // $Id: o_shared.cpp,v 1.1.1.1 2002/12/07 19:02:06 kolrabi Exp $ 
00026 
00027 /*
00028 
00029   $Log: o_shared.cpp,v $
00030   Revision 1.1.1.1  2002/12/07 19:02:06  kolrabi
00031   initial release
00032 
00033 
00034 */
00035 
00040 #include            "omicron/internal.h"
00041 
00042 /****************************************************************************
00043  ****************************************************************************
00044  * MISC ROUTINES ************************************************************
00045  ****************************************************************************
00046  ****************************************************************************/ 
00047 
00048 /****************************************************************************
00049  * _replace_char                                 replaces chars in a string *
00050  ****************************************************************************/
00051 void _replace_char
00052 (
00053     char *string, 
00054     char findchar, 
00055     char replacechar 
00056 )
00057 {
00058     char  *p; 
00059 
00060     // replace
00061     p = strchr( string, findchar ); 
00062     while (p) 
00063     { 
00064         *p = replacechar; 
00065         p = strchr( p, findchar ); 
00066     } 
00067 }
00068 
00069 /****************************************************************************
00070  * _convert_file_path                  convert a path to os specific format *
00071  ****************************************************************************/
00072 void  _convert_file_path
00073 (
00074     char *path
00075 ) 
00076 { 
00077 #if defined(WIN32) 
00078     _replace_char( path, '/', '\\');
00079 #else
00080     _replace_char( path, '\\', '/');
00081 #endif
00082 } 
00083  
00084 /****************************************************************************
00085  * _convert_pak_path                           convert a path to pak format *
00086  ****************************************************************************/
00087 void  _convert_pak_path
00088 (
00089     char *path
00090 ) 
00091 { 
00092     _replace_char( path, '\\', '/');
00093 } 
00094  
00095 /****************************************************************************
00096  * _strtrml                                              left trim a string *
00097  ****************************************************************************/
00098 char  *_strtrml
00099 ( 
00100     char *string, 
00101     char *trimchrs 
00102 )
00103 { 
00104     ulong   l = strlen(string); 
00105     char  *p; 
00106  
00107     p = (char*)string; 
00108  
00109     while(strchr( trimchrs, *p ) && p<(string+l)) p++; 
00110  
00111     return p; 
00112 } 
00113  
00114 /****************************************************************************
00115  * _strtrmr                                             right trim a string *
00116  ****************************************************************************/
00117 char  *_strtrmr
00118 ( 
00119     char *string, 
00120     char *trimchrs 
00121 )
00122 { 
00123   ulong   l = strlen(string); 
00124   char  *p; 
00125  
00126   p = (char*)string+l-1; 
00127  
00128   while(strchr( trimchrs, *p ) && p>(string+1)) p--; 
00129  
00130   if (p<string+l-1 && p>=string && *p) 
00131   { 
00132     p++; 
00133     *p = 0; 
00134   } 
00135  
00136   return (char*)string; 
00137 } 
00138  
00139 /****************************************************************************
00140  * _strtrm                                                    trim a string *
00141  ****************************************************************************/
00142 char  *_strtrm
00143 ( 
00144     char *string, 
00145     char *trimchrs
00146 ) 
00147 { 
00148   char  *p; 
00149  
00150   if (!strlen(string)) 
00151     return string; 
00152  
00153   p = _strtrmr( string, trimchrs ); 
00154   p = _strtrml( p, trimchrs ); 
00155  
00156   return p; 
00157 } 
00158  
00159 /****************************************************************************
00160  * _time_get                                        get current system time *
00161  ****************************************************************************/
00162 ulong _time_get
00163 () 
00164 { 
00165     ulong ticks = 0; 
00166 
00167 #if defined(WIN32) 
00168 
00169     // NOTE: LARGE_INTEGER sucks
00170 
00171     static LARGE_INTEGER pcounterfreq;
00172     LARGE_INTEGER count;
00173   
00174     if (!pcounterfreq.QuadPart)
00175     {
00176         QueryPerformanceFrequency(&pcounterfreq);
00177         if (!pcounterfreq.QuadPart)
00178             pcounterfreq.QuadPart = -1;
00179     }
00180 
00181     if (pcounterfreq.QuadPart>0)
00182     {
00183         QueryPerformanceCounter(&count);
00184         ticks = (ulong)(count.QuadPart*1000/pcounterfreq.QuadPart);
00185     }
00186     else
00187     {
00188         ticks = GetTickCount();
00189     }
00190 
00191 #else
00192 
00193     timezone tz; 
00194     timeval  tv; 
00195  
00196     gettimeofday(&tv, &tz); 
00197  
00198     ticks = tv.tv_sec * 1000 + tv.tv_usec / 1000; 
00199 
00200 #endif 
00201  
00202     return ticks; 
00203 } 
00204  
00205 /****************************************************************************
00206  * _interpolate_float                                interpolate two floats *
00207  ****************************************************************************/
00208 float           _interpolate_float_linear
00209 ( 
00210     float       a, 
00211     float       b, 
00212     float       frac 
00213 ) 
00214 { 
00215     float       invfrac = 1-frac; 
00216  
00217     return a*invfrac+b*frac; 
00218 } 
00219 
00220 /****************************************************************************
00221  * _sgn                                             get the sign of a float *
00222  ****************************************************************************/
00223 schar           _sgn
00224 (
00225     float       f
00226 )
00227 {
00228     return f>0?(1):(f<0?-1:0);
00229 }
00230 
00231 
00232 /****************************************************************************
00233  * _swap_long                                          swaps hi and lo word *
00234  ****************************************************************************/
00235 void            _swap_long
00236 (
00237     ulong       &l
00238 )
00239 {
00240     char b;
00241     char *p = (char*)(&l);
00242 
00243     b = p[0]; p[0] = p[3]; p[3] = b;
00244     b = p[1]; p[1] = p[2]; p[2] = b;
00245 }

Generated on Wed Dec 18 15:48:47 2002 for omicron engine by doxygen1.2.18