y_utils.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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. 06/10/12:
  45. Upgraded "memset" to use "FILL".
  46. 22/12/11:
  47. Changed "ceildiv" to only evaluate arguments once.
  48. 05/12/11:
  49. Added NO_VALUE to test if macros have no value.
  50. 08/09/10:
  51. Added strcpy and StripNL.
  52. 08/08/10:
  53. Scrapped almost everything. Only VERY usefult things go in now.
  54. Functions:
  55. Stock:
  56. StripNL - Strips the newline characters from the end of a string.
  57. Inline:
  58. iseven - Checks if a number is even.
  59. isodd - Checks if a number is odd.
  60. isnull - Checks if a string is NULL ("\1\0").
  61. strcpy - Copy one string to another.
  62. Variables:
  63. Global:
  64. TRUE - True hack for infinate loops.
  65. FALSE - False hack for one-time loops.
  66. NULL - 1 long string for passing via Call(Remote|Local)Function.
  67. \*----------------------------------------------------------------------------*/
  68. #include "internal\y_version"
  69. #include "y_debug"
  70. #include "y_amx"
  71. //#tryinclude <sscanf>
  72. /*// "File,Float," must remain last always.
  73. #if defined CUSTOM_TAG_TYPES
  74. #define File,Float, Float,File,CUSTOM_TAG_TYPES
  75. #else
  76. #define File,Float, Float,File
  77. #endif*/
  78. // Add new tags to the START of this list.
  79. #include "internal\y_globaltags"
  80. // VERY VERY VERY IMPORTANT!!! y_inline uses "130" instead of "YSI_MAX_STRING"
  81. // for two lines (one is "520" for "130 * 4").
  82. #define YSI_MAX_STRING (130)
  83. #define FUNCTION_LENGTH (32)
  84. #define NO_VALUE(%0) ((2*%0-1+1)==-1)
  85. #if !defined TRUE
  86. new stock
  87. bool:TRUE = true;
  88. #endif
  89. #if !defined FALSE
  90. new stock
  91. bool:FALSE = false;
  92. #endif
  93. #if !defined NULL
  94. new stock
  95. NULL[2] = {1, 0};
  96. #endif
  97. // Define "volatile" as nothing.
  98. #if !defined volatile
  99. #define volatile
  100. #endif
  101. #define YSIM_MASTER #M
  102. #define YSIM_RETURN #R
  103. #define YSIM_CALLER #C
  104. #define YSIM_TEXT_D #T
  105. #define YSIM_TEXT_L #L
  106. #define YSIM_TEXT_S #Y
  107. #define YSIM_TEXT_P #Z
  108. #define YSIM_ORDERS #O
  109. #define YSIM_HFIRST #H
  110. #define YSIM_OPDRET #D
  111. #define YSIM_TXTFND #X
  112. #define YSIM_TXTIND #I
  113. #define YSIM_TXTLEN #E
  114. #define YSIM_LOG_IN #U
  115. #if !defined YSIM_STRING
  116. #define YSIM_STRING (42)
  117. #endif
  118. #define FLOAT_INFINITY (Float:0x7F800000)
  119. #define FLOAT_NEG_INFINITY (Float:0xFF800000)
  120. #define FLOAT_NEGATIVE_INFINITY (Float:0xFF800000)
  121. #define FLOAT_NAN (Float:0x7FFFFFFF)
  122. #define FLOAT_NOT_A_NUMBER (Float:0x7FFFFFFF)
  123. #define FLOAT_QNAN (Float:0x7FFFFFFF)
  124. #define FLOAT_QUIET_NAN (Float:0x7FFFFFFF)
  125. #define FLOAT_QUIET_NOT_A_NUMBER (Float:0x7FFFFFFF)
  126. #define FLOAT_SNAN (Float:0x7FBFFFFF)
  127. #define FLOAT_SIGNALING_NAN (Float:0x7FBFFFFF)
  128. #define FLOAT_SIGNALING_NOT_A_NUMBER (Float:0x7FBFFFFF)
  129. //#pragma unused TRUE, FALSE, NULL
  130. #define ceildiv(%0,%1) \
  131. (((%0)-1)/(%1)+1)
  132. #define floordiv(%0,%1) \
  133. ((%0)/(%1))
  134. /*----------------------------------------------------------------------------*\
  135. Function:
  136. isnull
  137. Params:
  138. str - String to check if is null.
  139. Return:
  140. -
  141. Notes:
  142. -
  143. \*----------------------------------------------------------------------------*/
  144. #if !defined isnull
  145. #define isnull(%1) \
  146. ((%1[0] == 0) || (%1[0] == 1 && %1[1] == 0))
  147. #endif
  148. /*----------------------------------------------------------------------------*\
  149. Function:
  150. isodd
  151. Params:
  152. value - Value to check if is odd.
  153. Return:
  154. -
  155. Notes:
  156. -
  157. \*----------------------------------------------------------------------------*/
  158. #define isodd(%1) \
  159. ((%1) & 1)
  160. /*----------------------------------------------------------------------------*\
  161. Function:
  162. iseven
  163. Params:
  164. value - Value to check if is even.
  165. Return:
  166. -
  167. Notes:
  168. -
  169. \*----------------------------------------------------------------------------*/
  170. #define iseven(%1) \
  171. (!isodd(%1))
  172. /*----------------------------------------------------------------------------*\
  173. Function:
  174. strcpy
  175. Params:
  176. dest - Destination string.
  177. src - Source string.
  178. len - (Implicit) maximum length of the destination.
  179. Return:
  180. -
  181. Notes:
  182. -
  183. \*----------------------------------------------------------------------------*/
  184. #define strcpy(%0,%1) \
  185. strcat((%0[0] = '\0', %0), %1)
  186. // memcpy(%0,%1,0,strlen(%1)*4+4,%2)
  187. /*----------------------------------------------------------------------------*\
  188. Function:
  189. StripNL
  190. Params:
  191. str[] - The string to remove the newline characters from
  192. Return:
  193. -
  194. Notes:
  195. Updated from old versions, should be more efficient
  196. \*----------------------------------------------------------------------------*/
  197. stock StripNL(str[])
  198. {
  199. P:3("StripNL called: \"%s\"", str);
  200. new
  201. i = strlen(str);
  202. while (i-- && str[i] <= ' ') str[i] = '\0';
  203. }
  204. /*----------------------------------------------------------------------------*\
  205. Function:
  206. endofline
  207. Params:
  208. line[] - String to check.
  209. pos - Postion to start from.
  210. Return:
  211. -
  212. Notes:
  213. Checks if the current point in a line is the end of non-whitespace data.
  214. \*----------------------------------------------------------------------------*/
  215. stock endofline(line[], pos)
  216. {
  217. P:3("endofline called: \"%s\", %i", line, pos);
  218. if (pos < 0 || pos > strlen(line)) return 0;
  219. while (line[pos]) if (line[pos++] > ' ') return 0;
  220. return 1;
  221. }
  222. /*----------------------------------------------------------------------------*\
  223. Function:
  224. chrfind
  225. Params:
  226. needle - The character to find.
  227. haystack[] - The string to find it in.
  228. start - The offset to start from.
  229. Return:
  230. Fail - -1, Success - pos
  231. Notes:
  232. -
  233. \*----------------------------------------------------------------------------*/
  234. stock chrfind(needle, haystack[], start = 0)
  235. {
  236. P:3("chrfind called: %c, \"%s\", %i", needle, haystack, start);
  237. if (start < 0)
  238. {
  239. start = 0;
  240. }
  241. else if (start > strlen(haystack)) return -1;
  242. while (haystack[start]) if (haystack[start++] == needle) return start - 1;
  243. return -1;
  244. }
  245. //#define chrfind(%0'%1'%2,%3) str
  246. stock chrfindp(needle, haystack[], start = 0)
  247. {
  248. P:3("chrfind called: %c, \"%s\", %i", needle, haystack, start);
  249. if (start < 0)
  250. {
  251. start = 0;
  252. }
  253. while (haystack{start}) if (haystack{start++} == needle) return start - 1;
  254. return -1;
  255. }
  256. /*----------------------------------------------------------------------------*\
  257. Function:
  258. bernstein
  259. Params:
  260. string[] - the string to hash.
  261. Return:
  262. the bernstein hash of the input string
  263. Notes:
  264. This is a 32bit hash system so is not very secure, however we're only
  265. using this as a string enumerator to uniquely identify strings easilly
  266. and allow for a binary search of strings based on the hash of their name.
  267. crc32, then jenkins were originally used however this is far faster, if a
  268. little collision prone, but we're checking the strings manually anyway.
  269. This doesn't matter as it would be done regardless of hash method, so this
  270. doesn't need to be accounted for. Speed is all that matters with at
  271. least a bit of non collision (the number of strings we're dealing with,
  272. this should have none-few collisions).
  273. I modified it slightly from the original code pasted by aru, to code
  274. closer to the code http://www.burtleburtle.net/bob/hash/doobs.html and
  275. to work with PAWN (and shaved 0.2�s off the time for one call :D).
  276. Uber reduced version (just for fun):
  277. b(s[]){new h=-1,i,j;while((j=s[i++]))h=h*33+j;return h;}
  278. Update: Contrary to what I said above this is also used to identify colour
  279. strings for the updated text system involving file based styling and this
  280. is not checked for collisions as it's unimportant. But this doesn't affect
  281. the function at all, I just mentioned it here for "interest".
  282. \*----------------------------------------------------------------------------*/
  283. stock bernstein(string[])
  284. {
  285. P:3("bernstein called: \"%s\"", string);
  286. new
  287. hash = -1,
  288. i,
  289. j;
  290. while ((j = string[i++]))
  291. {
  292. hash = hash * 33 + j;
  293. //printf("Hash stage %d: %d", i - 1, hash);
  294. }
  295. return hash;
  296. }
  297. /*----------------------------------------------------------------------------*\
  298. Function:
  299. ishex
  300. Params:
  301. str[] - String to check.
  302. Return:
  303. -
  304. Notes:
  305. -
  306. \*----------------------------------------------------------------------------*/
  307. stock ishex(str[])
  308. {
  309. P:3("ishex called: \"%s\"", str);
  310. new
  311. i,
  312. cur;
  313. if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) i = 2;
  314. while (str[i])
  315. {
  316. cur = str[i++];
  317. if (!(('0' <= cur <= '9') || ('A' <= cur <= 'F') || ('a' <= cur <= 'f'))) return 0;
  318. //if ((cur < '0') || ('9' < cur < 'A') || ('F' < cur < 'a') || (cur > 'f')) return 0;
  319. }
  320. return 1;
  321. }
  322. /*----------------------------------------------------------------------------*\
  323. Function:
  324. unpack
  325. Params:
  326. str[] - String to unpack
  327. Return:
  328. unpacked string
  329. Notes:
  330. Mainly used for debugging.
  331. \*----------------------------------------------------------------------------*/
  332. stock unpack(const str[])
  333. {
  334. P:3("unpack called: \"%s\"", str);
  335. new
  336. ret[YSI_MAX_STRING] = {0};
  337. if (strlen(str) <= YSI_MAX_STRING)
  338. {
  339. strunpack(ret, str);
  340. }
  341. return ret;
  342. }
  343. /*----------------------------------------------------------------------------*\
  344. Function:
  345. GetIP
  346. Params:
  347. playerid - Player to get IP of.
  348. Return:
  349. IP as a 32bit int.
  350. Notes:
  351. -
  352. \*----------------------------------------------------------------------------*/
  353. stock GetIP(playerid)
  354. {
  355. new
  356. ip[16];
  357. GetPlayerIp(playerid, ip, sizeof (ip));
  358. new
  359. ipv = strval(ip) << 24,
  360. pos = 0;
  361. while (pos < 15 && ip[pos++] != '.') {}
  362. ipv += strval(ip[pos]) << 16;
  363. while (pos < 15 && ip[pos++] != '.') {}
  364. ipv += strval(ip[pos]) << 8;
  365. while (pos < 15 && ip[pos++] != '.') {}
  366. ipv += strval(ip[pos]);
  367. return ipv;
  368. }
  369. /*----------------------------------------------------------------------------*\
  370. Function:
  371. getstring
  372. Params:
  373. addr - Address of the string on the heap.
  374. Return:
  375. string
  376. Notes:
  377. Is passed the result of getarg, which will be the address of a string (in
  378. theory) and uses that for DMA to get the string.
  379. \*----------------------------------------------------------------------------*/
  380. stock getstring(addr)
  381. {
  382. new
  383. ret[YSI_MAX_STRING];
  384. va_getstring(ret, addr);
  385. return ret;
  386. }
  387. stock getstringarg(addr)
  388. {
  389. new
  390. ret[YSI_MAX_STRING];
  391. va_getstring(ret, addr);
  392. return ret;
  393. }
  394. /*----------------------------------------------------------------------------*\
  395. Function:
  396. isnumeric
  397. Params:
  398. str[] - String to check
  399. Return:
  400. -
  401. Notes:
  402. Checks if a given string is numeric.
  403. \*----------------------------------------------------------------------------*/
  404. stock isnumeric(str[])
  405. {
  406. P:3("isnumeric called: \"%s\"", str);
  407. new
  408. ch,
  409. i;
  410. while ((ch = str[i++])) if (!('0' <= ch <= '9')) return 0;
  411. return 1;
  412. }
  413. #if !defined _inc_sscanf || 1
  414. /*------------------------------------------------------------------------*-
  415. Function:
  416. hexstr
  417. Params:
  418. string[] - String to convert to a number.
  419. Return:
  420. value of the passed hex string.
  421. Notes:
  422. -
  423. -*------------------------------------------------------------------------*/
  424. stock hexstr(string[])
  425. {
  426. new
  427. ret,
  428. val,
  429. i;
  430. if (string[0] == '0' && string[1] | 0x20 == 'x') i = 2;
  431. while (string[i])
  432. {
  433. ret <<= 4;
  434. val = string[i++] - '0';
  435. if (val > 0x09) val -= 0x07;
  436. if (val > 0x0F) val -= 0x20;
  437. if (val < 0x01) continue;
  438. if (val < 0x10) ret += val;
  439. }
  440. return ret;
  441. }
  442. /*------------------------------------------------------------------------*-
  443. Function:
  444. boolstr
  445. Params:
  446. string[] - String to try convert to a boolean.
  447. Return:
  448. bool: passed boolean.
  449. Notes:
  450. This can take a number of ways of representing booleans - 0, false and
  451. nothing there. Anything not one of those things (false is not case
  452. sensitive) is assumed true.
  453. -*------------------------------------------------------------------------*/
  454. stock bool:boolstr(string[])
  455. {
  456. if (!string[0] || string[0] == '0' || !strcmp(string, "false", true)) return false;
  457. return true;
  458. }
  459. /*------------------------------------------------------------------------*-
  460. Function:
  461. binstr
  462. Params:
  463. string[] - String to try convert to a boolean.
  464. Return:
  465. bool: passed boolean.
  466. Notes:
  467. This takes a value in 0110101 (boolean) format and returns it as a
  468. regular value.
  469. -*------------------------------------------------------------------------*/
  470. stock binstr(string[])
  471. {
  472. new
  473. pos = 0;
  474. switch (string[0])
  475. {
  476. case '0':
  477. {
  478. if (string[1] | 0x20 == 'b')
  479. {
  480. pos = 2;
  481. }
  482. }
  483. case '1':
  484. {
  485. }
  486. default:
  487. {
  488. return 0;
  489. }
  490. }
  491. new
  492. value = 0;
  493. for ( ; ; )
  494. {
  495. switch (string[pos++])
  496. {
  497. case '0':
  498. {
  499. value <<= 1;
  500. }
  501. case '1':
  502. {
  503. value = (value << 1) | 1;
  504. }
  505. default:
  506. {
  507. break;
  508. }
  509. }
  510. }
  511. return value;
  512. }
  513. #endif
  514. /*----------------------------------------------------------------------------*\
  515. Function:
  516. memset
  517. rawMemset
  518. Params:
  519. arr[], iAddress - Array or address to set to a value.
  520. iSize - Number of cells to fill.
  521. iValue - What to set the cells to.
  522. Return:
  523. -
  524. Notes:
  525. Based on code by Slice:
  526. http://forum.sa-mp.com/showthread.php?p=1606781#post1606781
  527. Modified to use binary flags instead of a loop.
  528. "memset" takes an array, the size of the array, and a value to fill it with
  529. and sets the whole array to that value.
  530. "rawmemset" is similar, but takes an AMX data segment address instead and
  531. the size is in bytes, not cells.
  532. \*----------------------------------------------------------------------------*/
  533. stock memset(arr[], size = sizeof (arr), val = 0)
  534. {
  535. new
  536. addr;
  537. #emit LOAD.S.pri arr
  538. #emit STOR.S.pri addr
  539. // Convert the size from cells to bytes.
  540. rawMemset(addr, size * 4, val);
  541. return 0;
  542. }
  543. stock rawMemset(iAddress, iSize, iValue)
  544. {
  545. // Loop until there are only little bits left to fill.
  546. while (iSize >= 4096)
  547. {
  548. // I have to do this because the FILL instruction doesn't accept a
  549. // dynamic number.
  550. #emit LOAD.S.alt iAddress
  551. #emit LOAD.S.pri iValue
  552. #emit FILL 4096
  553. iSize -= 4096;
  554. iAddress += 4096;
  555. }
  556. if (iSize & 2048)
  557. {
  558. #emit LOAD.S.alt iAddress
  559. #emit LOAD.S.pri iValue
  560. #emit FILL 2048
  561. iAddress += 2048;
  562. }
  563. if (iSize & 1024)
  564. {
  565. #emit LOAD.S.alt iAddress
  566. #emit LOAD.S.pri iValue
  567. #emit FILL 1024
  568. iAddress += 1024;
  569. }
  570. if (iSize & 512)
  571. {
  572. #emit LOAD.S.alt iAddress
  573. #emit LOAD.S.pri iValue
  574. #emit FILL 512
  575. iAddress += 512;
  576. }
  577. if (iSize & 256)
  578. {
  579. #emit LOAD.S.alt iAddress
  580. #emit LOAD.S.pri iValue
  581. #emit FILL 256
  582. iAddress += 256;
  583. }
  584. if (iSize & 128)
  585. {
  586. #emit LOAD.S.alt iAddress
  587. #emit LOAD.S.pri iValue
  588. #emit FILL 128
  589. iAddress += 128;
  590. }
  591. if (iSize & 64)
  592. {
  593. #emit LOAD.S.alt iAddress
  594. #emit LOAD.S.pri iValue
  595. #emit FILL 64
  596. iAddress += 64;
  597. }
  598. if (iSize & 32)
  599. {
  600. #emit LOAD.S.alt iAddress
  601. #emit LOAD.S.pri iValue
  602. #emit FILL 32
  603. iAddress += 32;
  604. }
  605. if (iSize & 16)
  606. {
  607. #emit LOAD.S.alt iAddress
  608. #emit LOAD.S.pri iValue
  609. #emit FILL 16
  610. iAddress += 16;
  611. }
  612. if (iSize & 8)
  613. {
  614. #emit LOAD.S.alt iAddress
  615. #emit LOAD.S.pri iValue
  616. #emit FILL 8
  617. iAddress += 8;
  618. }
  619. if (iSize & 4)
  620. {
  621. #emit LOAD.S.alt iAddress
  622. #emit LOAD.S.pri iValue
  623. #emit FILL 4
  624. iAddress += 4;
  625. }
  626. }
  627. #if !defined ReturnPlayerName
  628. stock ReturnPlayerName(playerid)
  629. {
  630. new
  631. str[MAX_PLAYER_NAME];
  632. GetPlayerName(playerid, str, sizeof (str));
  633. return str;
  634. }
  635. #endif
  636. stock ftouch(const filename[])
  637. {
  638. if (fexist(filename))
  639. {
  640. return 0;
  641. }
  642. else
  643. {
  644. new
  645. File:f = fopen(filename, io_write);
  646. if (f)
  647. {
  648. fclose(f);
  649. return 1;
  650. }
  651. else
  652. {
  653. return -1;
  654. }
  655. }
  656. }
  657. #include "y_va"