y_utils.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 SA:MP script information include.
  18. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  19. Portions created by the Initial Developer are Copyright (C) 2008
  20. the Initial Developer. All Rights Reserved.
  21. Contributors:
  22. ZeeX, koolk
  23. Thanks:
  24. Peter, Cam - Support.
  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.
  39. Kye/Kalcor - SA:MP.
  40. SA:MP Team past, present and future - SA:MP.
  41. Version:
  42. 0.1.3
  43. Changelog:
  44. 08/09/10:
  45. Added strcpy and StripNL.
  46. 08/08/10:
  47. Scrapped almost everything. Only VERY usefult things go in now.
  48. Functions:
  49. Stock:
  50. StripNL - Strips the newline characters from the end of a string.
  51. Inline:
  52. iseven - Checks if a number is even.
  53. isodd - Checks if a number is odd.
  54. isnull - Checks if a string is NULL ("\1\0").
  55. strcpy - Copy one string to another.
  56. Variables:
  57. Global:
  58. TRUE - True hack for infinate loops.
  59. FALSE - False hack for one-time loops.
  60. NULL - 1 long string for passing via Call(Remote|Local)Function.
  61. -*----------------------------------------------------------------------------*/
  62. #include <YSI\internal\y_version>
  63. //#tryinclude <sscanf>
  64. #if !defined TRUE
  65. new stock
  66. bool:TRUE = true;
  67. #endif
  68. #if !defined FALSE
  69. new stock
  70. bool:FALSE = false;
  71. #endif
  72. #if !defined NULL
  73. new stock
  74. NULL[2] = {1, 0};
  75. #endif
  76. //#pragma unused TRUE, FALSE, NULL
  77. /*----------------------------------------------------------------------------*-
  78. Function:
  79. isnull
  80. Params:
  81. str - String to check if is null.
  82. Return:
  83. -
  84. Notes:
  85. -
  86. -*----------------------------------------------------------------------------*/
  87. #if !defined isnull
  88. #define isnull(%1) \
  89. ((%1[0] == 0) || (%1[0] == 1 && %1[1] == 0))
  90. #endif
  91. /*----------------------------------------------------------------------------*-
  92. Function:
  93. isodd
  94. Params:
  95. value - Value to check if is odd.
  96. Return:
  97. -
  98. Notes:
  99. -
  100. -*----------------------------------------------------------------------------*/
  101. #define isodd(%1) \
  102. ((%1) & 1)
  103. /*----------------------------------------------------------------------------*-
  104. Function:
  105. iseven
  106. Params:
  107. value - Value to check if is even.
  108. Return:
  109. -
  110. Notes:
  111. -
  112. -*----------------------------------------------------------------------------*/
  113. #define iseven(%1) \
  114. (!isodd(%1))
  115. /*----------------------------------------------------------------------------*-
  116. Function:
  117. strcpy
  118. Params:
  119. dest - Destination string.
  120. src - Source string.
  121. len - Maximum length of the destination.
  122. Return:
  123. -
  124. Notes:
  125. -
  126. -*----------------------------------------------------------------------------*/
  127. #define strcpy(%0,%1,%2) \
  128. strcat((%0[0] = '\0', %0), %1, %2)
  129. // memcpy(%0,%1,0,strlen(%1)*4+4,%2)
  130. /*----------------------------------------------------------------------------*-
  131. Function:
  132. StripNL
  133. Params:
  134. str[] - The string to remove the newline characters from
  135. Return:
  136. -
  137. Notes:
  138. Updated from old versions, should be more efficient
  139. -*----------------------------------------------------------------------------*/
  140. stock StripNL(str[])
  141. {
  142. new
  143. i = strlen(str);
  144. while (i-- && str[i] <= ' ') str[i] = '\0';
  145. }
  146. /*----------------------------------------------------------------------------*-
  147. Function:
  148. endofline
  149. Params:
  150. line[] - String to check.
  151. pos - Postion to start from.
  152. Return:
  153. -
  154. Notes:
  155. Checks if the current point in a line is the end of non-whitespace data.
  156. -*----------------------------------------------------------------------------*/
  157. stock endofline(line[], pos)
  158. {
  159. while (line[pos]) if (line[pos++] > ' ') return 0;
  160. return 1;
  161. }
  162. /*----------------------------------------------------------------------------*-
  163. Function:
  164. chrfind
  165. Params:
  166. needle - The character to find.
  167. haystack[] - The string to find it in.
  168. start - The offset to start from.
  169. Return:
  170. Fail - -1, Success - pos
  171. Notes:
  172. -
  173. -*----------------------------------------------------------------------------*/
  174. stock chrfind(needle, haystack[], start = 0)
  175. {
  176. if (start < 0)
  177. {
  178. start = 0;
  179. }
  180. while (haystack[start]) if (haystack[start++] == needle) return start - 1;
  181. return -1;
  182. }
  183. /*----------------------------------------------------------------------------*-
  184. Function:
  185. bernstein
  186. Params:
  187. string[] - the string to hash.
  188. Return:
  189. the bernstein hash of the input string
  190. Notes:
  191. This is a 32bit hash system so is not very secure, however we're only
  192. using this as a string enumerator to uniquely identify strings easilly
  193. and allow for a binary search of strings based on the hash of their name.
  194. crc32, then jenkins were originally used however this is far faster, if a
  195. little collision prone, but we're checking the strings manually anyway.
  196. This doesn't matter as it would be done regardless of hash method, so this
  197. doesn't need to be accounted for. Speed is all that matters with at
  198. least a bit of non collision (the number of strings we're dealing with,
  199. this should have none-few collisions).
  200. I modified it slightly from the original code pasted by aru, to code
  201. closer to the code http://www.burtleburtle.net/bob/hash/doobs.html and
  202. to work with PAWN (and shaved 0.2µs off the time for one call :D).
  203. Uber reduced version (just for fun):
  204. b(s[]){new h=-1,i,j;while((j=s[i++]))h=h*33+j;return h;}
  205. Update: Contrary to what I said above this is also used to identify colour
  206. strings for the updated text system involving file based styling and this
  207. is not checked for collisions as it's unimportant. But this doesn't affect
  208. the function at all, I just mentioned it here for "interest".
  209. -*----------------------------------------------------------------------------*/
  210. stock bernstein(string[])
  211. {
  212. new
  213. hash = -1,
  214. i,
  215. j;
  216. while ((j = string[i++]))
  217. {
  218. hash = hash * 33 + j;
  219. }
  220. return hash;
  221. }
  222. /*----------------------------------------------------------------------------*-
  223. Function:
  224. ishex
  225. Params:
  226. str[] - String to check.
  227. Return:
  228. -
  229. Notes:
  230. -
  231. -*----------------------------------------------------------------------------*/
  232. stock ishex(str[])
  233. {
  234. new
  235. i,
  236. cur;
  237. if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) i = 2;
  238. while (str[i])
  239. {
  240. cur = str[i++];
  241. if (!(('0' <= cur <= '9') || ('A' <= cur <= 'F') || ('a' <= cur <= 'f'))) return 0;
  242. //if ((cur < '0') || ('9' < cur < 'A') || ('F' < cur < 'a') || (cur > 'f')) return 0;
  243. }
  244. return 1;
  245. }
  246. /*----------------------------------------------------------------------------*-
  247. Function:
  248. isnumeric
  249. Params:
  250. str[] - String to check
  251. Return:
  252. -
  253. Notes:
  254. Checks if a given string is numeric.
  255. -*----------------------------------------------------------------------------*/
  256. stock isnumeric(str[])
  257. {
  258. new
  259. ch,
  260. i;
  261. while ((ch = str[i++])) if (!('0' <= ch <= '9')) return 0;
  262. return 1;
  263. }
  264. #if !defined _inc_sscanf || 1
  265. /*------------------------------------------------------------------------*-
  266. Function:
  267. hexstr
  268. Params:
  269. string[] - String to convert to a number.
  270. Return:
  271. value of the passed hex string.
  272. Notes:
  273. -
  274. -*------------------------------------------------------------------------*/
  275. stock hexstr(string[])
  276. {
  277. new
  278. ret,
  279. val,
  280. i;
  281. if (string[0] == '0' && (string[1] == 'x' || string[1] == 'X')) i = 2;
  282. while (string[i])
  283. {
  284. ret <<= 4;
  285. val = string[i++] - '0';
  286. if (val > 0x09) val -= 0x07;
  287. if (val > 0x0F) val -= 0x20;
  288. if (val < 0x01) continue;
  289. if (val < 0x10) ret += val;
  290. }
  291. return ret;
  292. }
  293. /*------------------------------------------------------------------------*-
  294. Function:
  295. boolstr
  296. Params:
  297. string[] - String to try convert to a boolean.
  298. Return:
  299. bool: passed boolean.
  300. Notes:
  301. This can take a number of ways of representing booleans - 0, false and
  302. nothing there. Anything not one of those things (false is not case
  303. sensitive) is assumed true.
  304. -*------------------------------------------------------------------------*/
  305. stock bool:boolstr(string[])
  306. {
  307. if (!string[0] || string[0] == '0' || !strcmp(string, "false", true)) return false;
  308. return true;
  309. }
  310. /*------------------------------------------------------------------------*-
  311. Function:
  312. binstr
  313. Params:
  314. string[] - String to try convert to a boolean.
  315. Return:
  316. bool: passed boolean.
  317. Notes:
  318. This takes a value in 0110101 (boolean) format and returns it as a
  319. regular value.
  320. -*------------------------------------------------------------------------*/
  321. stock binstr(string[])
  322. {
  323. new
  324. pos = 0;
  325. switch (string[0])
  326. {
  327. case '0':
  328. {
  329. if (string[1] == 'b' || string[1] == 'B')
  330. {
  331. pos = 2;
  332. }
  333. }
  334. case '1':
  335. {
  336. }
  337. default:
  338. {
  339. return 0;
  340. }
  341. }
  342. new
  343. value = 0;
  344. for ( ; ; )
  345. {
  346. switch (string[pos++])
  347. {
  348. case '0':
  349. {
  350. value <<= 1;
  351. }
  352. case '1':
  353. {
  354. value = (value << 1) | 1;
  355. }
  356. default:
  357. {
  358. break;
  359. }
  360. }
  361. }
  362. return value;
  363. }
  364. #endif