bg_lib.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. // this include must remain at the top of every bg_xxxx CPP file
  2. #include "common_headers.h"
  3. //#include "q_shared.h"
  4. // this file is excluded from release builds because of intrinsics
  5. size_t strlen( const char *string ) {
  6. const char *s;
  7. s = string;
  8. while ( *s ) {
  9. s++;
  10. }
  11. return s - string;
  12. }
  13. char *strcat( char *strDestination, const char *strSource ) {
  14. char *s;
  15. s = strDestination;
  16. while ( *s ) {
  17. s++;
  18. }
  19. while ( *strSource ) {
  20. *s++ = *strSource++;
  21. }
  22. *s = 0;
  23. return strDestination;
  24. }
  25. char *strcpy( char *strDestination, const char *strSource ) {
  26. char *s;
  27. s = strDestination;
  28. while ( *strSource ) {
  29. *s++ = *strSource++;
  30. }
  31. *s = 0;
  32. return strDestination;
  33. }
  34. int strcmp( const char *string1, const char *string2 ) {
  35. while ( *string1 == *string2 && *string1 && *string2 ) {
  36. string1++;
  37. string2++;
  38. }
  39. return *string1 - *string2;
  40. }
  41. char *strchr( const char *string, int c ) {
  42. do {
  43. if ( *string == c ) {
  44. return ( char * )string;
  45. }
  46. string++;
  47. } while ( *(string-1) );
  48. return (char *)0;
  49. }
  50. char *strstr( const char *string, const char *strCharSet ) {
  51. while ( *string ) {
  52. int i;
  53. for ( i = 0 ; strCharSet[i] ; i++ ) {
  54. if ( string[i] != strCharSet[i] ) {
  55. break;
  56. }
  57. }
  58. if ( !strCharSet[i] ) {
  59. return (char *)string;
  60. }
  61. string++;
  62. }
  63. return (char *)0;
  64. }
  65. #ifndef _MSC_VER
  66. int tolower( int c ) {
  67. if ( c >= 'A' && c <= 'Z' ) {
  68. c += 'a' - 'A';
  69. }
  70. return c;
  71. }
  72. #endif
  73. int toupper( int c ) {
  74. if ( c >= 'a' && c <= 'z' ) {
  75. c += 'A' - 'a';
  76. }
  77. return c;
  78. }
  79. //#ifndef _MSC_VER
  80. #if 0
  81. double floor( double x ) {
  82. return (int)(x + 0x40000000) - 0x40000000;
  83. }
  84. void *memset( void *dest, int c, size_t count ) {
  85. while ( count-- ) {
  86. ((char *)dest)[count] = c;
  87. }
  88. return dest;
  89. }
  90. void *memcpy( void *dest, const void *src, size_t count ) {
  91. while ( count-- ) {
  92. ((char *)dest)[count] = ((char *)src)[count];
  93. }
  94. return dest;
  95. }
  96. char *strncpy( char *strDest, const char *strSource, size_t count ) {
  97. char *s;
  98. s = strDest;
  99. while ( *strSource && count ) {
  100. *s++ = *strSource++;
  101. count--;
  102. }
  103. while ( count-- ) {
  104. *s++ = 0;
  105. }
  106. return strDest;
  107. }
  108. double sqrt( double x ) {
  109. float y;
  110. float delta;
  111. float maxError;
  112. if ( x <= 0 ) {
  113. return 0;
  114. }
  115. // initial guess
  116. y = x / 2;
  117. // refine
  118. maxError = x * 0.001;
  119. do {
  120. delta = ( y * y ) - x;
  121. y -= delta / ( 2 * y );
  122. } while ( delta > maxError || delta < -maxError );
  123. return y;
  124. }
  125. float sintable[1024] = {
  126. 0.000000,0.001534,0.003068,0.004602,0.006136,0.007670,0.009204,0.010738,
  127. 0.012272,0.013805,0.015339,0.016873,0.018407,0.019940,0.021474,0.023008,
  128. 0.024541,0.026075,0.027608,0.029142,0.030675,0.032208,0.033741,0.035274,
  129. 0.036807,0.038340,0.039873,0.041406,0.042938,0.044471,0.046003,0.047535,
  130. 0.049068,0.050600,0.052132,0.053664,0.055195,0.056727,0.058258,0.059790,
  131. 0.061321,0.062852,0.064383,0.065913,0.067444,0.068974,0.070505,0.072035,
  132. 0.073565,0.075094,0.076624,0.078153,0.079682,0.081211,0.082740,0.084269,
  133. 0.085797,0.087326,0.088854,0.090381,0.091909,0.093436,0.094963,0.096490,
  134. 0.098017,0.099544,0.101070,0.102596,0.104122,0.105647,0.107172,0.108697,
  135. 0.110222,0.111747,0.113271,0.114795,0.116319,0.117842,0.119365,0.120888,
  136. 0.122411,0.123933,0.125455,0.126977,0.128498,0.130019,0.131540,0.133061,
  137. 0.134581,0.136101,0.137620,0.139139,0.140658,0.142177,0.143695,0.145213,
  138. 0.146730,0.148248,0.149765,0.151281,0.152797,0.154313,0.155828,0.157343,
  139. 0.158858,0.160372,0.161886,0.163400,0.164913,0.166426,0.167938,0.169450,
  140. 0.170962,0.172473,0.173984,0.175494,0.177004,0.178514,0.180023,0.181532,
  141. 0.183040,0.184548,0.186055,0.187562,0.189069,0.190575,0.192080,0.193586,
  142. 0.195090,0.196595,0.198098,0.199602,0.201105,0.202607,0.204109,0.205610,
  143. 0.207111,0.208612,0.210112,0.211611,0.213110,0.214609,0.216107,0.217604,
  144. 0.219101,0.220598,0.222094,0.223589,0.225084,0.226578,0.228072,0.229565,
  145. 0.231058,0.232550,0.234042,0.235533,0.237024,0.238514,0.240003,0.241492,
  146. 0.242980,0.244468,0.245955,0.247442,0.248928,0.250413,0.251898,0.253382,
  147. 0.254866,0.256349,0.257831,0.259313,0.260794,0.262275,0.263755,0.265234,
  148. 0.266713,0.268191,0.269668,0.271145,0.272621,0.274097,0.275572,0.277046,
  149. 0.278520,0.279993,0.281465,0.282937,0.284408,0.285878,0.287347,0.288816,
  150. 0.290285,0.291752,0.293219,0.294685,0.296151,0.297616,0.299080,0.300543,
  151. 0.302006,0.303468,0.304929,0.306390,0.307850,0.309309,0.310767,0.312225,
  152. 0.313682,0.315138,0.316593,0.318048,0.319502,0.320955,0.322408,0.323859,
  153. 0.325310,0.326760,0.328210,0.329658,0.331106,0.332553,0.334000,0.335445,
  154. 0.336890,0.338334,0.339777,0.341219,0.342661,0.344101,0.345541,0.346980,
  155. 0.348419,0.349856,0.351293,0.352729,0.354164,0.355598,0.357031,0.358463,
  156. 0.359895,0.361326,0.362756,0.364185,0.365613,0.367040,0.368467,0.369892,
  157. 0.371317,0.372741,0.374164,0.375586,0.377007,0.378428,0.379847,0.381266,
  158. 0.382683,0.384100,0.385516,0.386931,0.388345,0.389758,0.391170,0.392582,
  159. 0.393992,0.395401,0.396810,0.398218,0.399624,0.401030,0.402435,0.403838,
  160. 0.405241,0.406643,0.408044,0.409444,0.410843,0.412241,0.413638,0.415034,
  161. 0.416430,0.417824,0.419217,0.420609,0.422000,0.423390,0.424780,0.426168,
  162. 0.427555,0.428941,0.430326,0.431711,0.433094,0.434476,0.435857,0.437237,
  163. 0.438616,0.439994,0.441371,0.442747,0.444122,0.445496,0.446869,0.448241,
  164. 0.449611,0.450981,0.452350,0.453717,0.455084,0.456449,0.457813,0.459177,
  165. 0.460539,0.461900,0.463260,0.464619,0.465976,0.467333,0.468689,0.470043,
  166. 0.471397,0.472749,0.474100,0.475450,0.476799,0.478147,0.479494,0.480839,
  167. 0.482184,0.483527,0.484869,0.486210,0.487550,0.488889,0.490226,0.491563,
  168. 0.492898,0.494232,0.495565,0.496897,0.498228,0.499557,0.500885,0.502212,
  169. 0.503538,0.504863,0.506187,0.507509,0.508830,0.510150,0.511469,0.512786,
  170. 0.514103,0.515418,0.516732,0.518045,0.519356,0.520666,0.521975,0.523283,
  171. 0.524590,0.525895,0.527199,0.528502,0.529804,0.531104,0.532403,0.533701,
  172. 0.534998,0.536293,0.537587,0.538880,0.540171,0.541462,0.542751,0.544039,
  173. 0.545325,0.546610,0.547894,0.549177,0.550458,0.551738,0.553017,0.554294,
  174. 0.555570,0.556845,0.558119,0.559391,0.560662,0.561931,0.563199,0.564466,
  175. 0.565732,0.566996,0.568259,0.569521,0.570781,0.572040,0.573297,0.574553,
  176. 0.575808,0.577062,0.578314,0.579565,0.580814,0.582062,0.583309,0.584554,
  177. 0.585798,0.587040,0.588282,0.589521,0.590760,0.591997,0.593232,0.594466,
  178. 0.595699,0.596931,0.598161,0.599389,0.600616,0.601842,0.603067,0.604290,
  179. 0.605511,0.606731,0.607950,0.609167,0.610383,0.611597,0.612810,0.614022,
  180. 0.615232,0.616440,0.617647,0.618853,0.620057,0.621260,0.622461,0.623661,
  181. 0.624859,0.626056,0.627252,0.628446,0.629638,0.630829,0.632019,0.633207,
  182. 0.634393,0.635578,0.636762,0.637944,0.639124,0.640303,0.641481,0.642657,
  183. 0.643832,0.645005,0.646176,0.647346,0.648514,0.649681,0.650847,0.652011,
  184. 0.653173,0.654334,0.655493,0.656651,0.657807,0.658961,0.660114,0.661266,
  185. 0.662416,0.663564,0.664711,0.665856,0.667000,0.668142,0.669283,0.670422,
  186. 0.671559,0.672695,0.673829,0.674962,0.676093,0.677222,0.678350,0.679476,
  187. 0.680601,0.681724,0.682846,0.683965,0.685084,0.686200,0.687315,0.688429,
  188. 0.689541,0.690651,0.691759,0.692866,0.693971,0.695075,0.696177,0.697278,
  189. 0.698376,0.699473,0.700569,0.701663,0.702755,0.703845,0.704934,0.706021,
  190. 0.707107,0.708191,0.709273,0.710353,0.711432,0.712509,0.713585,0.714659,
  191. 0.715731,0.716801,0.717870,0.718937,0.720003,0.721066,0.722128,0.723188,
  192. 0.724247,0.725304,0.726359,0.727413,0.728464,0.729514,0.730563,0.731609,
  193. 0.732654,0.733697,0.734739,0.735779,0.736817,0.737853,0.738887,0.739920,
  194. 0.740951,0.741980,0.743008,0.744034,0.745058,0.746080,0.747101,0.748119,
  195. 0.749136,0.750152,0.751165,0.752177,0.753187,0.754195,0.755201,0.756206,
  196. 0.757209,0.758210,0.759209,0.760207,0.761202,0.762196,0.763188,0.764179,
  197. 0.765167,0.766154,0.767139,0.768122,0.769103,0.770083,0.771061,0.772036,
  198. 0.773010,0.773983,0.774953,0.775922,0.776888,0.777853,0.778817,0.779778,
  199. 0.780737,0.781695,0.782651,0.783605,0.784557,0.785507,0.786455,0.787402,
  200. 0.788346,0.789289,0.790230,0.791169,0.792107,0.793042,0.793975,0.794907,
  201. 0.795837,0.796765,0.797691,0.798615,0.799537,0.800458,0.801376,0.802293,
  202. 0.803208,0.804120,0.805031,0.805940,0.806848,0.807753,0.808656,0.809558,
  203. 0.810457,0.811355,0.812251,0.813144,0.814036,0.814926,0.815814,0.816701,
  204. 0.817585,0.818467,0.819348,0.820226,0.821103,0.821977,0.822850,0.823721,
  205. 0.824589,0.825456,0.826321,0.827184,0.828045,0.828904,0.829761,0.830616,
  206. 0.831470,0.832321,0.833170,0.834018,0.834863,0.835706,0.836548,0.837387,
  207. 0.838225,0.839060,0.839894,0.840725,0.841555,0.842383,0.843208,0.844032,
  208. 0.844854,0.845673,0.846491,0.847307,0.848120,0.848932,0.849742,0.850549,
  209. 0.851355,0.852159,0.852961,0.853760,0.854558,0.855354,0.856147,0.856939,
  210. 0.857729,0.858516,0.859302,0.860085,0.860867,0.861646,0.862424,0.863199,
  211. 0.863973,0.864744,0.865514,0.866281,0.867046,0.867809,0.868571,0.869330,
  212. 0.870087,0.870842,0.871595,0.872346,0.873095,0.873842,0.874587,0.875329,
  213. 0.876070,0.876809,0.877545,0.878280,0.879012,0.879743,0.880471,0.881197,
  214. 0.881921,0.882643,0.883363,0.884081,0.884797,0.885511,0.886223,0.886932,
  215. 0.887640,0.888345,0.889048,0.889750,0.890449,0.891146,0.891841,0.892534,
  216. 0.893224,0.893913,0.894599,0.895284,0.895966,0.896646,0.897325,0.898001,
  217. 0.898674,0.899346,0.900016,0.900683,0.901349,0.902012,0.902673,0.903332,
  218. 0.903989,0.904644,0.905297,0.905947,0.906596,0.907242,0.907886,0.908528,
  219. 0.909168,0.909806,0.910441,0.911075,0.911706,0.912335,0.912962,0.913587,
  220. 0.914210,0.914830,0.915449,0.916065,0.916679,0.917291,0.917901,0.918508,
  221. 0.919114,0.919717,0.920318,0.920917,0.921514,0.922109,0.922701,0.923291,
  222. 0.923880,0.924465,0.925049,0.925631,0.926210,0.926787,0.927363,0.927935,
  223. 0.928506,0.929075,0.929641,0.930205,0.930767,0.931327,0.931884,0.932440,
  224. 0.932993,0.933544,0.934093,0.934639,0.935184,0.935726,0.936266,0.936803,
  225. 0.937339,0.937872,0.938404,0.938932,0.939459,0.939984,0.940506,0.941026,
  226. 0.941544,0.942060,0.942573,0.943084,0.943593,0.944100,0.944605,0.945107,
  227. 0.945607,0.946105,0.946601,0.947094,0.947586,0.948075,0.948561,0.949046,
  228. 0.949528,0.950008,0.950486,0.950962,0.951435,0.951906,0.952375,0.952842,
  229. 0.953306,0.953768,0.954228,0.954686,0.955141,0.955594,0.956045,0.956494,
  230. 0.956940,0.957385,0.957826,0.958266,0.958703,0.959139,0.959572,0.960002,
  231. 0.960431,0.960857,0.961280,0.961702,0.962121,0.962538,0.962953,0.963366,
  232. 0.963776,0.964184,0.964590,0.964993,0.965394,0.965793,0.966190,0.966584,
  233. 0.966976,0.967366,0.967754,0.968139,0.968522,0.968903,0.969281,0.969657,
  234. 0.970031,0.970403,0.970772,0.971139,0.971504,0.971866,0.972226,0.972584,
  235. 0.972940,0.973293,0.973644,0.973993,0.974339,0.974684,0.975025,0.975365,
  236. 0.975702,0.976037,0.976370,0.976700,0.977028,0.977354,0.977677,0.977999,
  237. 0.978317,0.978634,0.978948,0.979260,0.979570,0.979877,0.980182,0.980485,
  238. 0.980785,0.981083,0.981379,0.981673,0.981964,0.982253,0.982539,0.982824,
  239. 0.983105,0.983385,0.983662,0.983937,0.984210,0.984480,0.984749,0.985014,
  240. 0.985278,0.985539,0.985798,0.986054,0.986308,0.986560,0.986809,0.987057,
  241. 0.987301,0.987544,0.987784,0.988022,0.988258,0.988491,0.988722,0.988950,
  242. 0.989177,0.989400,0.989622,0.989841,0.990058,0.990273,0.990485,0.990695,
  243. 0.990903,0.991108,0.991311,0.991511,0.991710,0.991906,0.992099,0.992291,
  244. 0.992480,0.992666,0.992850,0.993032,0.993212,0.993389,0.993564,0.993737,
  245. 0.993907,0.994075,0.994240,0.994404,0.994565,0.994723,0.994879,0.995033,
  246. 0.995185,0.995334,0.995481,0.995625,0.995767,0.995907,0.996045,0.996180,
  247. 0.996313,0.996443,0.996571,0.996697,0.996820,0.996941,0.997060,0.997176,
  248. 0.997290,0.997402,0.997511,0.997618,0.997723,0.997825,0.997925,0.998023,
  249. 0.998118,0.998211,0.998302,0.998390,0.998476,0.998559,0.998640,0.998719,
  250. 0.998795,0.998870,0.998941,0.999011,0.999078,0.999142,0.999205,0.999265,
  251. 0.999322,0.999378,0.999431,0.999481,0.999529,0.999575,0.999619,0.999660,
  252. 0.999699,0.999735,0.999769,0.999801,0.999831,0.999858,0.999882,0.999905,
  253. 0.999925,0.999942,0.999958,0.999971,0.999981,0.999989,0.999995,0.999999
  254. };
  255. double sin( double x ) {
  256. int index;
  257. int quad;
  258. index = 1024 * x / (M_PI * 0.5);
  259. quad = ( index >> 10 ) & 3;
  260. index &= 1023;
  261. switch ( quad ) {
  262. case 0:
  263. return sintable[index];
  264. case 1:
  265. return sintable[1023-index];
  266. case 2:
  267. return -sintable[index];
  268. case 3:
  269. return -sintable[1023-index];
  270. }
  271. return 0;
  272. }
  273. double cos( double x ) {
  274. int index;
  275. int quad;
  276. index = 1024 * x / (M_PI * 0.5);
  277. quad = ( index >> 10 ) & 3;
  278. index &= 1023;
  279. switch ( quad ) {
  280. case 3:
  281. return sintable[index];
  282. case 0:
  283. return sintable[1023-index];
  284. case 1:
  285. return -sintable[index];
  286. case 2:
  287. return -sintable[1023-index];
  288. }
  289. return 0;
  290. }
  291. double atan2( double y, double x ) {
  292. float base;
  293. float temp;
  294. float dir;
  295. float test;
  296. int i;
  297. if ( x < 0 ) {
  298. if ( y >= 0 ) {
  299. // quad 1
  300. base = M_PI / 2;
  301. temp = x;
  302. x = y;
  303. y = -temp;
  304. } else {
  305. // quad 2
  306. base = M_PI;
  307. x = -x;
  308. y = -y;
  309. }
  310. } else {
  311. if ( y < 0 ) {
  312. // quad 3
  313. base = 3 * M_PI / 2;
  314. temp = x;
  315. x = -y;
  316. y = temp;
  317. }
  318. }
  319. if ( y > x ) {
  320. base += M_PI/2;
  321. temp = x;
  322. x = y;
  323. y = temp;
  324. dir = -1;
  325. } else {
  326. dir = 1;
  327. }
  328. // calcualte angle in octant 0
  329. if ( x == 0 ) {
  330. return base;
  331. }
  332. y /= x;
  333. for ( i = 0 ; i < 512 ; i++ ) {
  334. test = sintable[i] / sintable[1023-i];
  335. if ( test > y ) {
  336. break;
  337. }
  338. }
  339. return base + dir * i * ( M_PI/2048);
  340. }
  341. #endif
  342. double tan( double x ) {
  343. return sin(x) / cos(x);
  344. }
  345. static int randSeed = 0;
  346. int rand( void ) {
  347. randSeed = (69069 * randSeed + 1);
  348. return randSeed & 0x7fff;
  349. }
  350. double atof( const char *string ) {
  351. double sign;
  352. double value;
  353. int c;
  354. // skip whitespace
  355. while ( *string <= ' ' ) {
  356. if ( !*string ) {
  357. return 0;
  358. }
  359. string++;
  360. }
  361. // check sign
  362. switch ( *string ) {
  363. case '+':
  364. string++;
  365. sign = 1;
  366. break;
  367. case '-':
  368. string++;
  369. sign = -1;
  370. break;
  371. default:
  372. sign = 1;
  373. break;
  374. }
  375. // read digits
  376. value = 0;
  377. do {
  378. c = *string++;
  379. if ( c < '0' || c > '9' ) {
  380. break;
  381. }
  382. c -= '0';
  383. value = value * 10 + c;
  384. } while ( 1 );
  385. // check for decimal point
  386. if ( c == '.' ) {
  387. double fraction;
  388. string++;
  389. fraction = 0.1;
  390. do {
  391. c = *string++;
  392. if ( c < '0' || c > '9' ) {
  393. break;
  394. }
  395. c -= '0';
  396. value += c * fraction;
  397. fraction *= 0.1;
  398. } while ( 1 );
  399. }
  400. // not handling 10e10 notation...
  401. return value * sign;
  402. }
  403. #ifndef _MSC_VER
  404. int atoi( const char *string ) {
  405. int sign;
  406. int value;
  407. int c;
  408. // skip whitespace
  409. while ( *string <= ' ' ) {
  410. if ( !*string ) {
  411. return 0;
  412. }
  413. string++;
  414. }
  415. // check sign
  416. switch ( *string ) {
  417. case '+':
  418. string++;
  419. sign = 1;
  420. break;
  421. case '-':
  422. string++;
  423. sign = -1;
  424. break;
  425. default:
  426. sign = 1;
  427. break;
  428. }
  429. // read digits
  430. value = 0;
  431. do {
  432. c = *string++;
  433. if ( c < '0' || c > '9' ) {
  434. break;
  435. }
  436. c -= '0';
  437. value = value * 10 + c;
  438. } while ( 1 );
  439. // not handling 10e10 notation...
  440. return value * sign;
  441. }
  442. #endif
  443. int abs( int n ) {
  444. return n < 0 ? -n : n;
  445. }
  446. double fabs( double x ) {
  447. return x < 0 ? -x : x;
  448. }
  449. //=========================================================
  450. void AddInt( char **buf_p, int val, int width ) {
  451. char text[32];
  452. int digits;
  453. int signedVal;
  454. char *buf;
  455. digits = 0;
  456. signedVal = val;
  457. if ( val < 0 ) {
  458. val = -val;
  459. }
  460. do {
  461. text[digits++] = '0' + val % 10;
  462. val /= 10;
  463. } while ( val );
  464. if ( signedVal < 0 ) {
  465. text[digits++] = '-';
  466. }
  467. buf = *buf_p;
  468. while ( digits < width ) {
  469. *buf++ = ' ';
  470. width--;
  471. }
  472. while ( digits-- ) {
  473. *buf++ = text[digits];
  474. }
  475. *buf_p = buf;
  476. }
  477. void AddFloat( char **buf_p, float fval, int width ) {
  478. char text[32];
  479. int digits;
  480. float signedVal;
  481. char *buf;
  482. int val;
  483. // FIXME!!!! handle fractions
  484. digits = 0;
  485. signedVal = fval;
  486. if ( fval < 0 ) {
  487. fval = -fval;
  488. }
  489. val = (int)fval;
  490. do {
  491. text[digits++] = '0' + val % 10;
  492. val /= 10;
  493. } while ( val );
  494. if ( signedVal < 0 ) {
  495. text[digits++] = '-';
  496. }
  497. buf = *buf_p;
  498. while ( digits < width ) {
  499. *buf++ = ' ';
  500. width--;
  501. }
  502. while ( digits-- ) {
  503. *buf++ = text[digits];
  504. }
  505. *buf_p = buf;
  506. }
  507. int vsprintf( char *buffer, const char *fmt, va_list argptr ) {
  508. char *buf_p;
  509. int cmd;
  510. int *arg;
  511. char *s;
  512. int width;
  513. buf_p = buffer;
  514. arg = (int *)argptr;
  515. while ( *fmt ) {
  516. if ( fmt[0] != '%' ) {
  517. *buf_p++ = *fmt++;
  518. continue;
  519. }
  520. cmd = fmt[1];
  521. if ( cmd >= '0' && cmd <= '9' ) {
  522. width = cmd - '0';
  523. cmd = fmt[2];
  524. fmt++;
  525. } else {
  526. width = 0;
  527. }
  528. fmt += 2;
  529. switch ( cmd ) {
  530. case 'i':
  531. case 'd':
  532. case 'u':
  533. AddInt( &buf_p, *arg, width );
  534. break;
  535. case 'f':
  536. AddFloat( &buf_p, *(float *)arg, width );
  537. break;
  538. case 's':
  539. s = (char *)*arg;
  540. if ( !s ) {
  541. s = "<NULL>";
  542. }
  543. while ( *s ) {
  544. *buf_p++ = *s++;
  545. }
  546. break;
  547. }
  548. arg++;
  549. }
  550. *buf_p = 0;
  551. return buf_p - buffer;
  552. }