y_utils.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /*----------------------------------------------------------------------------*\
  2. =================================
  3. Y Sever Includes - Misc Functions
  4. =================================
  5. Description:
  6. Misc functions used throughout.
  7. Legal:
  8. Version: MPL 1.1
  9. The contents of this file are subject to the Mozilla Public License Version
  10. 1.1 (the "License"); you may not use this file except in compliance with
  11. the License. You may obtain a copy of the License at
  12. http://www.mozilla.org/MPL/
  13. Software distributed under the License is distributed on an "AS IS" basis,
  14. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  15. for the specific language governing rights and limitations under the
  16. License.
  17. The Original Code is the YSI utils include.
  18. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  19. Portions created by the Initial Developer are Copyright (C) 2011
  20. the Initial Developer. All Rights Reserved.
  21. Contributors:
  22. ZeeX, koolk, JoeBullet/Google63, g_aSlice/Slice
  23. Thanks:
  24. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  25. ZeeX - Very productive conversations.
  26. koolk - IsPlayerinAreaEx code.
  27. TheAlpha - Danish translation.
  28. breadfish - German translation.
  29. Fireburn - Dutch translation.
  30. yom - French translation.
  31. 50p - Polish translation.
  32. Zamaroht - Spanish translation.
  33. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
  34. for me to strive to better.
  35. Pixels^ - Running XScripters where the idea was born.
  36. Matite - Pestering me to release it and using it.
  37. Very special thanks to:
  38. Thiadmer - PAWN, whose limits continue to amaze me!
  39. Kye/Kalcor - SA:MP.
  40. SA:MP Team past, present and future - SA:MP.
  41. Version:
  42. 0.1.3
  43. Changelog:
  44. 22/12/11:
  45. Changed "ceildiv" to only evaluate arguments once.
  46. 05/12/11:
  47. Added NO_VALUE to test if macros have no value.
  48. 08/09/10:
  49. Added strcpy and StripNL.
  50. 08/08/10:
  51. Scrapped almost everything. Only VERY usefult things go in now.
  52. Functions:
  53. Stock:
  54. StripNL - Strips the newline characters from the end of a string.
  55. Inline:
  56. iseven - Checks if a number is even.
  57. isodd - Checks if a number is odd.
  58. isnull - Checks if a string is NULL ("\1\0").
  59. strcpy - Copy one string to another.
  60. Variables:
  61. Global:
  62. TRUE - True hack for infinate loops.
  63. FALSE - False hack for one-time loops.
  64. NULL - 1 long string for passing via Call(Remote|Local)Function.
  65. \*----------------------------------------------------------------------------*/
  66. #include "internal\y_version"
  67. #include "y_debug"
  68. #include "y_amx"
  69. //#tryinclude <sscanf>
  70. /*// "File,Float," must remain last always.
  71. #if defined CUSTOM_TAG_TYPES
  72. #define File,Float, Float,File,CUSTOM_TAG_TYPES
  73. #else
  74. #define File,Float, Float,File
  75. #endif*/
  76. // Add new tags to the START of this list.
  77. #include "internal\y_globaltags"
  78. // VERY VERY VERY IMPORTANT!!! y_inline uses "130" instead of "YSI_MAX_STRING"
  79. // for two lines (one is "520" for "130 * 4").
  80. #define YSI_MAX_STRING (130)
  81. #define FUNCTION_LENGTH (32)
  82. #define NO_VALUE(%0) ((2*%0-1+1)==-1)
  83. #if !defined TRUE
  84. new stock
  85. bool:TRUE = true;
  86. #endif
  87. #if !defined FALSE
  88. new stock
  89. bool:FALSE = false;
  90. #endif
  91. #if !defined NULL
  92. new stock
  93. NULL[2] = {1, 0};
  94. #endif
  95. // Define "volatile" as nothing.
  96. #if !defined volatile
  97. #define volatile
  98. #endif
  99. #define YSIM_MASTER #M
  100. #define YSIM_RETURN #R
  101. #define YSIM_CALLER #C
  102. #define YSIM_TEXT_D #T
  103. #define YSIM_TEXT_L #L
  104. #define YSIM_TEXT_S #Y
  105. #define YSIM_TEXT_P #Z
  106. #define YSIM_ORDERS #O
  107. #define YSIM_HFIRST #H
  108. #define YSIM_OPDRET #D
  109. #define YSIM_TXTFND #X
  110. #define YSIM_TXTIND #I
  111. #define YSIM_TXTLEN #E
  112. #define YSIM_LOG_IN #U
  113. #if !defined YSIM_STRING
  114. #define YSIM_STRING (42)
  115. #endif
  116. #define FLOAT_INFINITY (Float:0x7F800000)
  117. #define FLOAT_NEG_INFINITY (Float:0xFF800000)
  118. #define FLOAT_NEGATIVE_INFINITY (Float:0xFF800000)
  119. #define FLOAT_NAN (Float:0x7FFFFFFF)
  120. #define FLOAT_NOT_A_NUMBER (Float:0x7FFFFFFF)
  121. #define FLOAT_QNAN (Float:0x7FFFFFFF)
  122. #define FLOAT_QUIET_NAN (Float:0x7FFFFFFF)
  123. #define FLOAT_QUIET_NOT_A_NUMBER (Float:0x7FFFFFFF)
  124. #define FLOAT_SNAN (Float:0x7FBFFFFF)
  125. #define FLOAT_SIGNALING_NAN (Float:0x7FBFFFFF)
  126. #define FLOAT_SIGNALING_NOT_A_NUMBER (Float:0x7FBFFFFF)
  127. //#pragma unused TRUE, FALSE, NULL
  128. #define ceildiv(%0,%1) \
  129. (((%0)-1)/(%1)+1)
  130. #define floordiv(%0,%1) \
  131. ((%0)/(%1))
  132. /*----------------------------------------------------------------------------*\
  133. Function:
  134. isnull
  135. Params:
  136. str - String to check if is null.
  137. Return:
  138. -
  139. Notes:
  140. -
  141. \*----------------------------------------------------------------------------*/
  142. #if !defined isnull
  143. #define isnull(%1) \
  144. ((%1[0] == 0) || (%1[0] == 1 && %1[1] == 0))
  145. #endif
  146. /*----------------------------------------------------------------------------*\
  147. Function:
  148. isodd
  149. Params:
  150. value - Value to check if is odd.
  151. Return:
  152. -
  153. Notes:
  154. -
  155. \*----------------------------------------------------------------------------*/
  156. #define isodd(%1) \
  157. ((%1) & 1)
  158. /*----------------------------------------------------------------------------*\
  159. Function:
  160. iseven
  161. Params:
  162. value - Value to check if is even.
  163. Return:
  164. -
  165. Notes:
  166. -
  167. \*----------------------------------------------------------------------------*/
  168. #define iseven(%1) \
  169. (!isodd(%1))
  170. /*----------------------------------------------------------------------------*\
  171. Function:
  172. strcpy
  173. Params:
  174. dest - Destination string.
  175. src - Source string.
  176. len - (Implicit) maximum length of the destination.
  177. Return:
  178. -
  179. Notes:
  180. -
  181. \*----------------------------------------------------------------------------*/
  182. #define strcpy(%0,%1) \
  183. strcat((%0[0] = '\0', %0), %1)
  184. // memcpy(%0,%1,0,strlen(%1)*4+4,%2)
  185. /*----------------------------------------------------------------------------*\
  186. Function:
  187. StripNL
  188. Params:
  189. str[] - The string to remove the newline characters from
  190. Return:
  191. -
  192. Notes:
  193. Updated from old versions, should be more efficient
  194. \*----------------------------------------------------------------------------*/
  195. stock StripNL(str[])
  196. {
  197. P:3("StripNL called: \"%s\"", str);
  198. new
  199. i = strlen(str);
  200. while (i-- && str[i] <= ' ') str[i] = '\0';
  201. }
  202. /*----------------------------------------------------------------------------*\
  203. Function:
  204. endofline
  205. Params:
  206. line[] - String to check.
  207. pos - Postion to start from.
  208. Return:
  209. -
  210. Notes:
  211. Checks if the current point in a line is the end of non-whitespace data.
  212. \*----------------------------------------------------------------------------*/
  213. stock endofline(line[], pos)
  214. {
  215. P:3("endofline called: \"%s\", %i", line, pos);
  216. if (pos < 0 || pos > strlen(line)) return 0;
  217. while (line[pos]) if (line[pos++] > ' ') return 0;
  218. return 1;
  219. }
  220. /*----------------------------------------------------------------------------*\
  221. Function:
  222. chrfind
  223. Params:
  224. needle - The character to find.
  225. haystack[] - The string to find it in.
  226. start - The offset to start from.
  227. Return:
  228. Fail - -1, Success - pos
  229. Notes:
  230. -
  231. \*----------------------------------------------------------------------------*/
  232. stock chrfind(needle, haystack[], start = 0)
  233. {
  234. P:3("chrfind called: %c, \"%s\", %i", needle, haystack, start);
  235. if (start < 0)
  236. {
  237. start = 0;
  238. }
  239. else if (start > strlen(haystack)) return -1;
  240. while (haystack[start]) if (haystack[start++] == needle) return start - 1;
  241. return -1;
  242. }
  243. //#define chrfind(%0'%1'%2,%3) str
  244. stock chrfindp(needle, haystack[], start = 0)
  245. {
  246. P:3("chrfind called: %c, \"%s\", %i", needle, haystack, start);
  247. if (start < 0)
  248. {
  249. start = 0;
  250. }
  251. while (haystack{start}) if (haystack{start++} == needle) return start - 1;
  252. return -1;
  253. }
  254. /*----------------------------------------------------------------------------*\
  255. Function:
  256. bernstein
  257. Params:
  258. string[] - the string to hash.
  259. Return:
  260. the bernstein hash of the input string
  261. Notes:
  262. This is a 32bit hash system so is not very secure, however we're only
  263. using this as a string enumerator to uniquely identify strings easilly
  264. and allow for a binary search of strings based on the hash of their name.
  265. crc32, then jenkins were originally used however this is far faster, if a
  266. little collision prone, but we're checking the strings manually anyway.
  267. This doesn't matter as it would be done regardless of hash method, so this
  268. doesn't need to be accounted for. Speed is all that matters with at
  269. least a bit of non collision (the number of strings we're dealing with,
  270. this should have none-few collisions).
  271. I modified it slightly from the original code pasted by aru, to code
  272. closer to the code http://www.burtleburtle.net/bob/hash/doobs.html and
  273. to work with PAWN (and shaved 0.2µs off the time for one call :D).
  274. Uber reduced version (just for fun):
  275. b(s[]){new h=-1,i,j;while((j=s[i++]))h=h*33+j;return h;}
  276. Update: Contrary to what I said above this is also used to identify colour
  277. strings for the updated text system involving file based styling and this
  278. is not checked for collisions as it's unimportant. But this doesn't affect
  279. the function at all, I just mentioned it here for "interest".
  280. \*----------------------------------------------------------------------------*/
  281. stock bernstein(string[])
  282. {
  283. P:3("bernstein called: \"%s\"", string);
  284. new
  285. hash = -1,
  286. i,
  287. j;
  288. while ((j = string[i++]))
  289. {
  290. hash = hash * 33 + j;
  291. //printf("Hash stage %d: %d", i - 1, hash);
  292. }
  293. return hash;
  294. }
  295. /*----------------------------------------------------------------------------*\
  296. Function:
  297. ishex
  298. Params:
  299. str[] - String to check.
  300. Return:
  301. -
  302. Notes:
  303. -
  304. \*----------------------------------------------------------------------------*/
  305. stock ishex(str[])
  306. {
  307. P:3("ishex called: \"%s\"", str);
  308. new
  309. i,
  310. cur;
  311. if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) i = 2;
  312. while (str[i])
  313. {
  314. cur = str[i++];
  315. if (!(('0' <= cur <= '9') || ('A' <= cur <= 'F') || ('a' <= cur <= 'f'))) return 0;
  316. //if ((cur < '0') || ('9' < cur < 'A') || ('F' < cur < 'a') || (cur > 'f')) return 0;
  317. }
  318. return 1;
  319. }
  320. /*----------------------------------------------------------------------------*\
  321. Function:
  322. unpack
  323. Params:
  324. str[] - String to unpack
  325. Return:
  326. unpacked string
  327. Notes:
  328. Mainly used for debugging.
  329. \*----------------------------------------------------------------------------*/
  330. stock unpack(const str[])
  331. {
  332. P:3("unpack called: \"%s\"", str);
  333. new
  334. ret[YSI_MAX_STRING] = {0};
  335. if (strlen(str) <= YSI_MAX_STRING)
  336. {
  337. strunpack(ret, str);
  338. }
  339. return ret;
  340. }
  341. /*----------------------------------------------------------------------------*\
  342. Function:
  343. GetIP
  344. Params:
  345. playerid - Player to get IP of.
  346. Return:
  347. IP as a 32bit int.
  348. Notes:
  349. -
  350. \*----------------------------------------------------------------------------*/
  351. stock GetIP(playerid)
  352. {
  353. new
  354. ip[16];
  355. GetPlayerIp(playerid, ip, sizeof (ip));
  356. new
  357. ipv = strval(ip) << 24,
  358. pos = 0;
  359. while (pos < 15 && ip[pos++] != '.') {}
  360. ipv += strval(ip[pos]) << 16;
  361. while (pos < 15 && ip[pos++] != '.') {}
  362. ipv += strval(ip[pos]) << 8;
  363. while (pos < 15 && ip[pos++] != '.') {}
  364. ipv += strval(ip[pos]);
  365. return ipv;
  366. }
  367. /*----------------------------------------------------------------------------*\
  368. Function:
  369. getstring
  370. Params:
  371. addr - Address of the string on the heap.
  372. Return:
  373. string
  374. Notes:
  375. Is passed the result of getarg, which will be the address of a string (in
  376. theory) and uses that for DMA to get the string.
  377. \*----------------------------------------------------------------------------*/
  378. stock getstring(addr)
  379. {
  380. new
  381. ret[YSI_MAX_STRING];
  382. va_getstring(ret, addr);
  383. return ret;
  384. }
  385. stock getstringarg(addr)
  386. {
  387. new
  388. ret[YSI_MAX_STRING];
  389. va_getstring(ret, addr);
  390. return ret;
  391. }
  392. /*----------------------------------------------------------------------------*\
  393. Function:
  394. isnumeric
  395. Params:
  396. str[] - String to check
  397. Return:
  398. -
  399. Notes:
  400. Checks if a given string is numeric.
  401. \*----------------------------------------------------------------------------*/
  402. stock isnumeric(str[])
  403. {
  404. P:3("isnumeric called: \"%s\"", str);
  405. new
  406. ch,
  407. i;
  408. while ((ch = str[i++])) if (!('0' <= ch <= '9')) return 0;
  409. return 1;
  410. }
  411. #if !defined _inc_sscanf || 1
  412. /*------------------------------------------------------------------------*-
  413. Function:
  414. hexstr
  415. Params:
  416. string[] - String to convert to a number.
  417. Return:
  418. value of the passed hex string.
  419. Notes:
  420. -
  421. -*------------------------------------------------------------------------*/
  422. stock hexstr(string[])
  423. {
  424. new
  425. ret,
  426. val,
  427. i;
  428. if (string[0] == '0' && string[1] | 0x20 == 'x') i = 2;
  429. while (string[i])
  430. {
  431. ret <<= 4;
  432. val = string[i++] - '0';
  433. if (val > 0x09) val -= 0x07;
  434. if (val > 0x0F) val -= 0x20;
  435. if (val < 0x01) continue;
  436. if (val < 0x10) ret += val;
  437. }
  438. return ret;
  439. }
  440. /*------------------------------------------------------------------------*-
  441. Function:
  442. boolstr
  443. Params:
  444. string[] - String to try convert to a boolean.
  445. Return:
  446. bool: passed boolean.
  447. Notes:
  448. This can take a number of ways of representing booleans - 0, false and
  449. nothing there. Anything not one of those things (false is not case
  450. sensitive) is assumed true.
  451. -*------------------------------------------------------------------------*/
  452. stock bool:boolstr(string[])
  453. {
  454. if (!string[0] || string[0] == '0' || !strcmp(string, "false", true)) return false;
  455. return true;
  456. }
  457. /*------------------------------------------------------------------------*-
  458. Function:
  459. binstr
  460. Params:
  461. string[] - String to try convert to a boolean.
  462. Return:
  463. bool: passed boolean.
  464. Notes:
  465. This takes a value in 0110101 (boolean) format and returns it as a
  466. regular value.
  467. -*------------------------------------------------------------------------*/
  468. stock binstr(string[])
  469. {
  470. new
  471. pos = 0;
  472. switch (string[0])
  473. {
  474. case '0':
  475. {
  476. if (string[1] | 0x20 == 'b')
  477. {
  478. pos = 2;
  479. }
  480. }
  481. case '1':
  482. {
  483. }
  484. default:
  485. {
  486. return 0;
  487. }
  488. }
  489. new
  490. value = 0;
  491. for ( ; ; )
  492. {
  493. switch (string[pos++])
  494. {
  495. case '0':
  496. {
  497. value <<= 1;
  498. }
  499. case '1':
  500. {
  501. value = (value << 1) | 1;
  502. }
  503. default:
  504. {
  505. break;
  506. }
  507. }
  508. }
  509. return value;
  510. }
  511. #endif
  512. stock memset(dest[], size = sizeof (dest), val = 0, blocksize = 8)
  513. {
  514. new
  515. i;
  516. if (size < 0 || blocksize < 0)
  517. {
  518. return 0;
  519. }
  520. if (size <= blocksize)
  521. {
  522. while (i != size)
  523. {
  524. dest[i++] = val;
  525. }
  526. }
  527. else
  528. {
  529. //printf("%d %d %d", i, blocksize, size);
  530. // Set up the initial set area.
  531. while (i != blocksize)
  532. {
  533. dest[i++] = val;
  534. }
  535. //printf("%d %d %d", i, blocksize, size);
  536. size -= blocksize;
  537. // Copy this blank area over the rest of the array.
  538. while (i < size)
  539. {
  540. memcpy(dest[i], dest, 0, blocksize * 4, blocksize);
  541. i += blocksize;
  542. }
  543. //printf("%d %d %d", i, blocksize, size);
  544. size += blocksize;
  545. // Do any small remaining bits.
  546. while (i != size)
  547. {
  548. dest[i++] = val;
  549. }
  550. //printf("%d %d %d", i, blocksize, size);
  551. }
  552. return 1;
  553. }
  554. #if !defined ReturnPlayerName
  555. stock ReturnPlayerName(playerid)
  556. {
  557. new
  558. str[MAX_PLAYER_NAME];
  559. GetPlayerName(playerid, str, sizeof (str));
  560. return str;
  561. }
  562. #endif
  563. stock ftouch(const filename[])
  564. {
  565. if (fexist(filename))
  566. {
  567. return 0;
  568. }
  569. else
  570. {
  571. new
  572. File:f = fopen(filename, io_write);
  573. if (f)
  574. {
  575. fclose(f);
  576. return 1;
  577. }
  578. else
  579. {
  580. return -1;
  581. }
  582. }
  583. }
  584. #include "y_va"