gcsx_datatype.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* GCSx
  2. ** DATATYPE.CPP
  3. **
  4. ** Simple data-type conversion
  5. */
  6. /*****************************************************************************
  7. ** Copyright (C) 2003-2006 Janson
  8. **
  9. ** This program is free software; you can redistribute it and/or modify
  10. ** it under the terms of the GNU General Public License as published by
  11. ** the Free Software Foundation; either version 2 of the License, or
  12. ** (at your option) any later version.
  13. **
  14. ** This program is distributed in the hope that it will be useful,
  15. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ** GNU General Public License for more details.
  18. **
  19. ** You should have received a copy of the GNU General Public License
  20. ** along with this program; if not, write to the Free Software
  21. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
  22. *****************************************************************************/
  23. #include "all.h"
  24. const string blankString("");
  25. int vPtrSize = 1;
  26. int bcFloatSize = 1;
  27. // Buffer for formatString and float/intToStr
  28. char fsBuffer[256];
  29. const char* intToStr(long value) {
  30. snprintf(fsBuffer, 256, "%ld", value);
  31. return fsBuffer;
  32. }
  33. const char* floatToStr(BCfloat value) {
  34. snprintf(fsBuffer, 256, "%"BC_FLOAT_PRINTF, value);
  35. return fsBuffer;
  36. }
  37. long strToInt(const string& value) {
  38. return strtol(value.c_str(), NULL, 10);
  39. }
  40. long strToIntErr(const string& value) throw_int {
  41. const char* src = value.c_str();
  42. char* end = NULL;
  43. long result = strtol(src, &end, 10);
  44. if (src == end) throw 2;
  45. if (*end != 0) throw 1;
  46. return result;
  47. }
  48. BCfloat strToFloat(const string& value) {
  49. return BC_FLOAT_CONVERT(value.c_str(), NULL);
  50. }
  51. string formatString(const char* format, ...) {
  52. string value;
  53. assert(format);
  54. va_list arglist;
  55. va_start(arglist, format);
  56. vsnprintf(fsBuffer, 256, format, arglist);
  57. value = fsBuffer;
  58. va_end(arglist);
  59. return value;
  60. }
  61. void matrixCopy(const void* source, void* dest, int w, int h, int sourcePitch, int destPitch) {
  62. assert(source);
  63. assert(dest);
  64. assert(w <= sourcePitch);
  65. assert(w <= destPitch);
  66. assert(w >= 0);
  67. assert(h >= 0);
  68. if ((w == 0) || (h == 0)) return;
  69. // Optimized form
  70. if ((w == sourcePitch) && (w == destPitch)) {
  71. memcpy(dest, source, w * h);
  72. }
  73. else {
  74. for (; h > 0; --h) {
  75. memcpy(dest, source, w);
  76. source = (const char*)source + sourcePitch;
  77. dest = (char*)dest + destPitch;
  78. }
  79. }
  80. }
  81. void memSet32(Uint32* dest, Uint32 value, int size) {
  82. assert(dest);
  83. for (; size > 0; --size) {
  84. *dest++ = value;
  85. }
  86. }
  87. int myStricmp(const char* a, const char* b) {
  88. while ((*a) && (*b) && (tolower(*a) == tolower(*b))) {
  89. ++a;
  90. ++b;
  91. }
  92. return (int)tolower(*a) - (int)tolower(*b);
  93. }
  94. void toLower(string& str) {
  95. for (int pos = str.size() - 1; pos >= 0; --pos) {
  96. str[pos] = tolower(str[pos]);
  97. }
  98. }
  99. char* newCpCopy(const string& fromStr) {
  100. char* str = new char[fromStr.size() + 1];
  101. strcpy(str, fromStr.c_str());
  102. return str;
  103. }