StandardLibrary.cpp
Go to the documentation of this file.00001 //*** StandardLibrary.cpp **** 00002 00003 #include "StandardLibrary.h" 00004 #include "Debug.h" 00005 #include "Random.h" 00006 00007 #include <stdlib.h> 00008 #include <stdio.h> 00009 #include <stdarg.h> 00010 #include <string.h> 00011 #include <time.h> 00012 #include <math.h> 00013 #include <ctype.h> 00014 00015 //*** Round *** 00016 00017 int Round(float x) 00018 { 00019 #ifdef _WIN32 00020 int t; 00021 00022 __asm 00023 { 00024 fld x 00025 fistp t 00026 } 00027 00028 return t; 00029 #else 00030 return (int)(x+0.5f); 00031 #endif 00032 } 00033 00034 00035 Random standardLibrary_random; 00036 00037 //*** SRand *** 00038 00039 void SRand(unsigned int seed) 00040 { 00041 // srand(seed); 00042 00043 if (seed==1) 00044 { 00045 seed=5489; 00046 } 00047 standardLibrary_random.Seed(seed); // Use the mersienne twister algorithm instead of the stdlib one 00048 } 00049 00050 00051 //*** Rand *** 00052 00053 unsigned int Rand() 00054 { 00055 // return rand(); 00056 00057 return standardLibrary_random.RandomInteger(); // Use the mersienne twister algorithm instead of the stdlib one 00058 } 00059 00060 00061 //*** Randomize *** 00062 00063 void Randomize() 00064 { 00065 // srand((unsigned int)time(NULL)); 00066 00067 unsigned int seed=(unsigned int)time(NULL); // Use current time as seed 00068 standardLibrary_random.Seed(seed); // Use the mersienne twister algorithm instead of the stdlib one 00069 } 00070 00071 00072 //*** ToUpper *** 00073 00074 int ToUpper(int c) 00075 { 00076 return toupper(c); 00077 } 00078 00079 00080 //*** ToLower *** 00081 00082 int ToLower(int c) 00083 { 00084 return tolower(c); 00085 } 00086 00087 00088 //*** StringToInt *** 00089 00090 int StringToInt(const char* string) 00091 { 00092 return atoi(string); 00093 } 00094 00095 00096 //*** StringToLong *** 00097 00098 long StringToLong(const char* string) 00099 { 00100 return atol(string); 00101 } 00102 00103 00104 //*** StringToDouble *** 00105 00106 double StringToDouble(const char* string) 00107 { 00108 return atof(string); 00109 } 00110 00111 00112 //*** StrDup *** 00113 00114 char* StrDup(const char* string) 00115 { 00116 return _strdup(string); 00117 } 00118 00119 00120 //*** IntToString *** 00121 00122 char* IntToString(int value, char* string, int maxLength) 00123 { 00124 #ifdef WIN32 00125 return _itoa(value,string,maxLength); 00126 #else 00127 return 0; 00128 #endif 00129 } 00130 00131 00132 //*** SPrintF *** 00133 00134 /* We want a link error when SPrintF is used, as it is much safer to use SNPrintF 00135 int SPrintF(char* buffer, const char* string, ...) 00136 { 00137 va_list arglist; 00138 va_start(arglist, string); 00139 int result=vsprintf(buffer, string, arglist); 00140 va_end(arglist); 00141 return result; 00142 } 00143 */ 00144 00145 00146 //*** SNPrintF *** 00147 00148 int SNPrintF(char* buffer, int maxLength, const char* string, ...) 00149 { 00150 va_list arglist; 00151 va_start(arglist, string); 00152 int result=_vsnprintf(buffer, maxLength, string, arglist); 00153 va_end(arglist); 00154 return result; 00155 } 00156 00157 00158 //*** StrNICmp *** 00159 00160 int StrNICmp(const char* string1, const char* string2, int count) 00161 { 00162 return _strnicmp(string1,string2,count); 00163 } 00164 00165 00166 //*** StrNCmp *** 00167 00168 int StrNCmp(const char* string1, const char* string2, int count) 00169 { 00170 return strncmp(string1,string2,count); 00171 } 00172 00173 00174 //*** StrICmp *** 00175 00176 int StrICmp(const char* string1, const char* string2) 00177 { 00178 return _stricmp(string1,string2); 00179 } 00180 00181 00182 //*** StrCmp *** 00183 00184 int StrCmp(const char* string1, const char* string2) 00185 { 00186 return strcmp(string1,string2); 00187 } 00188 00189 00190 //*** StrLen *** 00191 00192 unsigned int StrLen(const char* string) 00193 { 00194 return (unsigned int)strlen(string); 00195 } 00196 00197 00198 //*** StrCpy *** 00199 00200 char* StrCpy(char* strDestination, const char* strSource) 00201 { 00202 return strcpy(strDestination,strSource); 00203 } 00204 00205 00206 //*** StrNCpy *** 00207 00208 char* StrNCpy(char* strDestination, const char* strSource,int count) 00209 { 00210 return strncpy(strDestination,strSource,count); 00211 } 00212 00213 00214 //*** StrCat *** 00215 00216 char* StrCat(char* strDestination, const char* strSource) 00217 { 00218 return strcat(strDestination,strSource); 00219 } 00220 00221 00222 //*** StrNCat *** 00223 00224 char* StrNCat(char* strDestination, const char* strSource, int count) 00225 { 00226 return strncat(strDestination,strSource,count); 00227 } 00228 00229 00230 //*** StrUpr *** 00231 00232 char* StrUpr(char* string) 00233 { 00234 return _strupr(string); 00235 } 00236 00237 00238 //*** StrLwr *** 00239 00240 char* StrLwr(char* string) 00241 { 00242 return _strlwr(string); 00243 } 00244 00245 00246 //*** StrChr *** 00247 00248 const char* StrChr(const char* string, int c) 00249 { 00250 return strchr(string,c); 00251 } 00252 00253 00254 //*** StrRChr *** 00255 00256 const char* StrRChr(const char* string, int c) 00257 { 00258 return strrchr(string,c); 00259 } 00260 00261 00262 //*** StrTok *** 00263 00264 char* StrTok(char* strToken, const char* strDelimit) 00265 { 00266 return strtok(strToken,strDelimit); 00267 } 00268 00269 00270 //*** StrStr *** 00271 00272 const char* StrStr(const char* str1, const char* str2) 00273 { 00274 return strstr(str1,str2); 00275 } 00276 00277 00278 //*** Malloc *** 00279 00280 void* Malloc(unsigned int size) 00281 { 00282 void* data=malloc(size); 00283 Assert(data,"Couldn't allocate memory"); 00284 return data; 00285 } 00286 00287 00288 //*** Free *** 00289 00290 void Free(void* memblock) 00291 { 00292 free(memblock); 00293 } 00294 00295 00296 //*** Realloc *** 00297 00298 void* Realloc(void* memblock,unsigned int size) 00299 { 00300 void* newmemblock=realloc(memblock,size); 00301 Assert(newmemblock,"Couldn't allocate memory"); 00302 return newmemblock; 00303 } 00304 00305 00306 //*** MemCpy *** 00307 00308 void* MemCpy(void* dest, const void* src, unsigned int count) 00309 { 00310 return memcpy(dest,src,count); 00311 } 00312 00313 00314 //*** MemSet *** 00315 00316 void* MemSet(void* dest, int c, unsigned int count ) 00317 { 00318 return memset(dest,c,count); 00319 } 00320 00321 00322 //*** MemCmp *** 00323 00324 unsigned int MemCmp(const void* a, const void* b, unsigned int count ) 00325 { 00326 return memcmp(a,b,count); 00327 } 00328 00329 00330 //*** QSort *** 00331 00332 void QSort(void* base, size_t num, size_t width, int (__cdecl* compare )(const void*, const void*) ) 00333 { 00334 qsort(base,num,width,compare); 00335 } 00336 00337 00338 //*** ToRadians *** 00339 00340 float ToRadians(float degrees) 00341 { 00342 return (degrees * PI)/180.0f; 00343 }; 00344 00345 00346 //*** ToDegrees *** 00347 00348 float ToDegrees(float radians) 00349 { 00350 return (radians * 180.0f)/PI; 00351 } 00352 00353 00354 //*** AngleDiff *** 00355 00356 float AngleDiff(float angleA, float angleB) 00357 { 00358 float angleDiff; 00359 00360 //Calculate 00361 angleDiff=angleB-angleA; 00362 00363 // Make sure diff is in the range 0 to TWO_PI 00364 angleDiff=fmodf(angleDiff,TWO_PI); 00365 if (angleDiff<0) 00366 { 00367 angleDiff+=TWO_PI; 00368 } 00369 00370 // Make sure diff is in the range -PI to PI 00371 if (angleDiff>=PI) 00372 { 00373 angleDiff-=TWO_PI; 00374 } 00375 00376 // Return the diff 00377 return angleDiff; 00378 } 00379 00380 00381 //*** Abs *** 00382 00383 float Abs(float x) 00384 { 00385 return fabsf(x); 00386 } 00387 00388 00389 //*** Abs *** 00390 00391 int Abs(int x) 00392 { 00393 return abs(x); 00394 } 00395 00396 00397 //*** Sqrt *** 00398 00399 float Sqrt(float x) 00400 { 00401 return sqrtf(x); 00402 } 00403 00404 00405 //*** Mod *** 00406 00407 float Mod(float x, float y) 00408 { 00409 return fmodf(x,y); 00410 } 00411 00412 00413 //*** Sin *** 00414 00415 float Sin(float x) 00416 { 00417 return sinf(x); 00418 } 00419 00420 00421 //*** ASin *** 00422 00423 float ASin(float x) 00424 { 00425 return asinf(x); 00426 } 00427 00428 00429 //*** Cos *** 00430 00431 float Cos(float x) 00432 { 00433 return cosf(x); 00434 } 00435 00436 00437 //*** ACos *** 00438 00439 float ACos(float x) 00440 { 00441 return acosf(x); 00442 } 00443 00444 00445 //*** Tan *** 00446 00447 float Tan(float x) 00448 { 00449 return tanf(x); 00450 } 00451 00452 00453 //*** ATan *** 00454 00455 float ATan(float x) 00456 { 00457 return atanf(x); 00458 } 00459 00460 00461 //*** Atan2 *** 00462 00463 float Atan2(float x, float y) 00464 { 00465 return atan2f(x,y); 00466 } 00467 00468 00469 //*** Pow *** 00470 00471 float Log10(float x) 00472 { 00473 return log10f(x); 00474 } 00475 00476 00477 //*** Pow *** 00478 00479 int Pow(int x, int y) 00480 { 00481 return (int)pow((float)x,(int)y); 00482 } 00483 00484 00485 //*** Pow *** 00486 00487 float Pow(float x, float y) 00488 { 00489 return pow(x,y); 00490 } 00491 00492 00493 //*** Exp *** 00494 00495 float Exp(float x) 00496 { 00497 return exp(x); 00498 } 00499 00500 00501 //*** Log *** 00502 00503 float Log(float x) 00504 { 00505 return log(x); 00506 } 00507 00508 00509 //*** Max *** 00510 00511 float Max(float a, float b) 00512 { 00513 if (a>b) 00514 { 00515 return a; 00516 } 00517 00518 return b; 00519 } 00520 00521 00522 //*** Min *** 00523 00524 float Min(float a, float b) 00525 { 00526 if (a<b) 00527 { 00528 return a; 00529 } 00530 00531 return b; 00532 } 00533 00534 00535 //*** Max *** 00536 00537 int Max(int a, int b) 00538 { 00539 if (a>b) 00540 { 00541 return a; 00542 } 00543 00544 return b; 00545 } 00546 00547 00548 //*** Min *** 00549 00550 int Min(int a, int b) 00551 { 00552 if (a<b) 00553 { 00554 return a; 00555 } 00556 00557 return b; 00558 } 00559 00560 00561 //*** Clamp *** 00562 00563 int Clamp(int value, int min, int max) 00564 { 00565 if (value<min) 00566 { 00567 return min; 00568 } 00569 if (value>max) 00570 { 00571 return max; 00572 } 00573 return value; 00574 } 00575 00576 00577 //*** Clamp *** 00578 00579 float Clamp(float value, float min, float max) 00580 { 00581 if (value<min) 00582 { 00583 return min; 00584 } 00585 if (value>max) 00586 { 00587 return max; 00588 } 00589 return value; 00590 } 00591 00592 00593 //*** Swap *** 00594 00595 void Swap(int& x, int& y) 00596 { 00597 int t=x; 00598 x=y; 00599 y=t; 00600 } 00601 00602 00603 //*** Swap *** 00604 00605 void Swap(unsigned int& x, unsigned int& y) 00606 { 00607 unsigned int t=x; 00608 x=y; 00609 y=t; 00610 } 00611 00612 00613 //*** Swap *** 00614 00615 void Swap(float& x, float& y) 00616 { 00617 float t=x; 00618 x=y; 00619 y=t; 00620 }
Reproduction/republishing of any material on this site without permission is strictly prohibited.
