utils.inc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. IsNumeric(const string[])
  2. {
  3. for (new i = 0, j = strlen(string); i < j; i++)
  4. {
  5. if (string[i] > '9' || string[i] < '0') return 0;
  6. }
  7. return 1;
  8. }
  9. ReturnUser(text[], playerid = INVALID_PLAYER_ID)
  10. {
  11. new pos = 0;
  12. while (text[pos] < 0x21) // Strip out leading spaces
  13. {
  14. if (text[pos] == 0) return INVALID_PLAYER_ID; // No passed text
  15. pos++;
  16. }
  17. new userid = INVALID_PLAYER_ID;
  18. if (IsNumeric(text[pos])) // Check whole passed string
  19. {
  20. // If they have a numeric name you have a problem (although names are checked on id failure)
  21. userid = strval(text[pos]);
  22. if (userid >=0 && userid < MAX_PLAYERS)
  23. {
  24. if(!IsPlayerConnected(userid))
  25. {
  26. /*if (playerid != INVALID_PLAYER_ID)
  27. {
  28. SendClientMessage(playerid, 0xFF0000AA, "User not connected");
  29. }*/
  30. userid = INVALID_PLAYER_ID;
  31. }
  32. else
  33. {
  34. return userid; // A player was found
  35. }
  36. }
  37. /*else
  38. {
  39. if (playerid != INVALID_PLAYER_ID)
  40. {
  41. SendClientMessage(playerid, 0xFF0000AA, "Invalid user ID");
  42. }
  43. userid = INVALID_PLAYER_ID;
  44. }
  45. return userid;*/
  46. // Removed for fallthrough code
  47. }
  48. // They entered [part of] a name or the id search failed (check names just incase)
  49. new len = strlen(text[pos]);
  50. new count = 0;
  51. new name[MAX_PLAYER_NAME];
  52. for (new i = 0; i < MAX_PLAYERS; i++)
  53. {
  54. if (IsPlayerConnected(i))
  55. {
  56. GetPlayerName(i, name, sizeof (name));
  57. if (strcmp(name, text[pos], true, len) == 0) // Check segment of name
  58. {
  59. if (len == strlen(name)) // Exact match
  60. {
  61. return i; // Return the exact player on an exact match
  62. // Otherwise if there are two players:
  63. // Me and MeYou any time you entered Me it would find both
  64. // And never be able to return just Me's id
  65. }
  66. else // Partial match
  67. {
  68. count++;
  69. userid = i;
  70. }
  71. }
  72. }
  73. }
  74. if (count != 1)
  75. {
  76. if (playerid != INVALID_PLAYER_ID)
  77. {
  78. if (count)
  79. {
  80. SendClientMessage(playerid, 0xFF0000AA, "Multiple users found, please narrow earch");
  81. }
  82. else
  83. {
  84. SendClientMessage(playerid, 0xFF0000AA, "No matching user found");
  85. }
  86. }
  87. userid = INVALID_PLAYER_ID;
  88. }
  89. return userid; // INVALID_USER_ID for bad return
  90. }
  91. stock sscanf(string[], format[], {Float,_}:...)
  92. {
  93. #if defined isnull
  94. if (isnull(string))
  95. #else
  96. if (string[0] == 0 || (string[0] == 1 && string[1] == 0))
  97. #endif
  98. {
  99. return format[0];
  100. }
  101. #pragma tabsize 4
  102. new
  103. formatPos = 0,
  104. stringPos = 0,
  105. paramPos = 2,
  106. paramCount = numargs(),
  107. delim = ' ';
  108. while (string[stringPos] && string[stringPos] <= ' ')
  109. {
  110. stringPos++;
  111. }
  112. while (paramPos < paramCount && string[stringPos])
  113. {
  114. switch (format[formatPos++])
  115. {
  116. case '\0':
  117. {
  118. return 0;
  119. }
  120. case 'i', 'd':
  121. {
  122. new
  123. neg = 1,
  124. num = 0,
  125. ch = string[stringPos];
  126. if (ch == '-')
  127. {
  128. neg = -1;
  129. ch = string[++stringPos];
  130. }
  131. do
  132. {
  133. stringPos++;
  134. if ('0' <= ch <= '9')
  135. {
  136. num = (num * 10) + (ch - '0');
  137. }
  138. else
  139. {
  140. return -1;
  141. }
  142. }
  143. while ((ch = string[stringPos]) > ' ' && ch != delim);
  144. setarg(paramPos, 0, num * neg);
  145. }
  146. case 'h', 'x':
  147. {
  148. new
  149. num = 0,
  150. ch = string[stringPos];
  151. do
  152. {
  153. stringPos++;
  154. switch (ch)
  155. {
  156. case 'x', 'X':
  157. {
  158. num = 0;
  159. continue;
  160. }
  161. case '0' .. '9':
  162. {
  163. num = (num << 4) | (ch - '0');
  164. }
  165. case 'a' .. 'f':
  166. {
  167. num = (num << 4) | (ch - ('a' - 10));
  168. }
  169. case 'A' .. 'F':
  170. {
  171. num = (num << 4) | (ch - ('A' - 10));
  172. }
  173. default:
  174. {
  175. return -1;
  176. }
  177. }
  178. }
  179. while ((ch = string[stringPos]) > ' ' && ch != delim);
  180. setarg(paramPos, 0, num);
  181. }
  182. case 'c':
  183. {
  184. setarg(paramPos, 0, string[stringPos++]);
  185. }
  186. case 'f':
  187. {
  188. setarg(paramPos, 0, _:floatstr(string[stringPos]));
  189. }
  190. case 'p':
  191. {
  192. delim = format[formatPos++];
  193. continue;
  194. }
  195. case '\'':
  196. {
  197. new
  198. end = formatPos - 1,
  199. ch;
  200. while ((ch = format[++end]) && ch != '\'') {}
  201. if (!ch)
  202. {
  203. return -1;
  204. }
  205. format[end] = '\0';
  206. if ((ch = strfind(string, format[formatPos], false, stringPos)) == -1)
  207. {
  208. if (format[end + 1])
  209. {
  210. return -1;
  211. }
  212. return 0;
  213. }
  214. format[end] = '\'';
  215. stringPos = ch + (end - formatPos);
  216. formatPos = end + 1;
  217. }
  218. case 'u':
  219. {
  220. new
  221. end = stringPos - 1,
  222. id = 0,
  223. bool:num = true,
  224. ch;
  225. while ((ch = string[++end]) && ch != delim)
  226. {
  227. if (num)
  228. {
  229. if ('0' <= ch <= '9')
  230. {
  231. id = (id * 10) + (ch - '0');
  232. }
  233. else
  234. {
  235. num = false;
  236. }
  237. }
  238. }
  239. if (num && IsPlayerConnected(id))
  240. {
  241. setarg(paramPos, 0, id);
  242. }
  243. else
  244. {
  245. #if !defined foreach
  246. #define foreach(%1,%2) for (new %2 = 0; %2 < MAX_PLAYERS; %2++) if (IsPlayerConnected(%2))
  247. #define __SSCANF_FOREACH__
  248. #endif
  249. string[end] = '\0';
  250. num = false;
  251. new
  252. name[MAX_PLAYER_NAME];
  253. id = end - stringPos;
  254. foreach (Player, playerid)
  255. {
  256. GetPlayerName(playerid, name, sizeof (name));
  257. if (!strcmp(name, string[stringPos], true, id))
  258. {
  259. setarg(paramPos, 0, playerid);
  260. num = true;
  261. break;
  262. }
  263. }
  264. if (!num)
  265. {
  266. setarg(paramPos, 0, INVALID_PLAYER_ID);
  267. }
  268. string[end] = ch;
  269. #if defined __SSCANF_FOREACH__
  270. #undef foreach
  271. #undef __SSCANF_FOREACH__
  272. #endif
  273. }
  274. stringPos = end;
  275. }
  276. case 's', 'z':
  277. {
  278. new
  279. i = 0,
  280. ch;
  281. if (format[formatPos])
  282. {
  283. while ((ch = string[stringPos++]) && ch != delim)
  284. {
  285. setarg(paramPos, i++, ch);
  286. }
  287. if (!i)
  288. {
  289. return -1;
  290. }
  291. }
  292. else
  293. {
  294. while ((ch = string[stringPos++]))
  295. {
  296. setarg(paramPos, i++, ch);
  297. }
  298. }
  299. stringPos--;
  300. setarg(paramPos, i, '\0');
  301. }
  302. default:
  303. {
  304. continue;
  305. }
  306. }
  307. while (string[stringPos] && string[stringPos] != delim && string[stringPos] > ' ')
  308. {
  309. stringPos++;
  310. }
  311. while (string[stringPos] && (string[stringPos] == delim || string[stringPos] <= ' '))
  312. {
  313. stringPos++;
  314. }
  315. paramPos++;
  316. }
  317. do
  318. {
  319. if ((delim = format[formatPos++]) > ' ')
  320. {
  321. if (delim == '\'')
  322. {
  323. while ((delim = format[formatPos++]) && delim != '\'') {}
  324. }
  325. else if (delim != 'z')
  326. {
  327. return delim;
  328. }
  329. }
  330. }
  331. while (delim > ' ');
  332. return 0;
  333. }