1
0

sscanf.inc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #include <a_samp>
  2. stock sscanf(string[], format[], {Float,_}:...)
  3. {
  4. #if defined isnull
  5. if (isnull(string))
  6. #else
  7. if (string[0] == 0 || (string[0] == 1 && string[1] == 0))
  8. #endif
  9. {
  10. return format[0];
  11. }
  12. #pragma tabsize 4
  13. new
  14. formatPos = 0,
  15. stringPos = 0,
  16. paramPos = 2,
  17. paramCount = numargs(),
  18. delim = ' ';
  19. while (string[stringPos] && string[stringPos] <= ' ')
  20. {
  21. stringPos++;
  22. }
  23. while (paramPos < paramCount && string[stringPos])
  24. {
  25. switch (format[formatPos++])
  26. {
  27. case '\0':
  28. {
  29. return 0;
  30. }
  31. case 'i', 'd':
  32. {
  33. new
  34. neg = 1,
  35. num = 0,
  36. ch = string[stringPos];
  37. if (ch == '-')
  38. {
  39. neg = -1;
  40. ch = string[++stringPos];
  41. }
  42. do
  43. {
  44. stringPos++;
  45. if ('0' <= ch <= '9')
  46. {
  47. num = (num * 10) + (ch - '0');
  48. }
  49. else
  50. {
  51. return -1;
  52. }
  53. }
  54. while ((ch = string[stringPos]) > ' ' && ch != delim);
  55. setarg(paramPos, 0, num * neg);
  56. }
  57. case 'h', 'x':
  58. {
  59. new
  60. num = 0,
  61. ch = string[stringPos];
  62. do
  63. {
  64. stringPos++;
  65. switch (ch)
  66. {
  67. case 'x', 'X':
  68. {
  69. num = 0;
  70. continue;
  71. }
  72. case '0' .. '9':
  73. {
  74. num = (num << 4) | (ch - '0');
  75. }
  76. case 'a' .. 'f':
  77. {
  78. num = (num << 4) | (ch - ('a' - 10));
  79. }
  80. case 'A' .. 'F':
  81. {
  82. num = (num << 4) | (ch - ('A' - 10));
  83. }
  84. default:
  85. {
  86. return -1;
  87. }
  88. }
  89. }
  90. while ((ch = string[stringPos]) > ' ' && ch != delim);
  91. setarg(paramPos, 0, num);
  92. }
  93. case 'c':
  94. {
  95. setarg(paramPos, 0, string[stringPos++]);
  96. }
  97. case 'f':
  98. {
  99. new changestr[16], changepos = 0, strpos = stringPos;
  100. while(changepos < 16 && string[strpos] && string[strpos] != delim)
  101. {
  102. changestr[changepos++] = string[strpos++];
  103. }
  104. changestr[changepos] = '\0';
  105. setarg(paramPos,0,_:floatstr(changestr));
  106. }
  107. case 'p':
  108. {
  109. delim = format[formatPos++];
  110. continue;
  111. }
  112. case '\'':
  113. {
  114. new
  115. end = formatPos - 1,
  116. ch;
  117. while ((ch = format[++end]) && ch != '\'') {}
  118. if (!ch)
  119. {
  120. return -1;
  121. }
  122. format[end] = '\0';
  123. if ((ch = strfind(string, format[formatPos], false, stringPos)) == -1)
  124. {
  125. if (format[end + 1])
  126. {
  127. return -1;
  128. }
  129. return 0;
  130. }
  131. format[end] = '\'';
  132. stringPos = ch + (end - formatPos);
  133. formatPos = end + 1;
  134. }
  135. case 'u':
  136. {
  137. new
  138. end = stringPos - 1,
  139. id = 0,
  140. bool:num = true,
  141. ch;
  142. while ((ch = string[++end]) && ch != delim)
  143. {
  144. if (num)
  145. {
  146. if ('0' <= ch <= '9')
  147. {
  148. id = (id * 10) + (ch - '0');
  149. }
  150. else
  151. {
  152. num = false;
  153. }
  154. }
  155. }
  156. if (num && IsPlayerConnected(id))
  157. {
  158. setarg(paramPos, 0, id);
  159. }
  160. else
  161. {
  162. #if !defined foreach
  163. #define foreach(%1,%2) for (new %2 = 0; %2 < MAX_PLAYERS; %2++) if (IsPlayerConnected(%2))
  164. #define __SSCANF_FOREACH__
  165. #endif
  166. string[end] = '\0';
  167. num = false;
  168. new
  169. name[MAX_PLAYER_NAME];
  170. id = end - stringPos;
  171. foreach (Player, playerid)
  172. {
  173. GetPlayerName(playerid, name, sizeof (name));
  174. if (!strcmp(name, string[stringPos], true, id))
  175. {
  176. setarg(paramPos, 0, playerid);
  177. num = true;
  178. break;
  179. }
  180. }
  181. if (!num)
  182. {
  183. setarg(paramPos, 0, INVALID_PLAYER_ID);
  184. }
  185. string[end] = ch;
  186. #if defined __SSCANF_FOREACH__
  187. #undef foreach
  188. #undef __SSCANF_FOREACH__
  189. #endif
  190. }
  191. stringPos = end;
  192. }
  193. case 's', 'z':
  194. {
  195. new
  196. i = 0,
  197. ch;
  198. if (format[formatPos])
  199. {
  200. while ((ch = string[stringPos++]) && ch != delim)
  201. {
  202. setarg(paramPos, i++, ch);
  203. }
  204. if (!i)
  205. {
  206. return -1;
  207. }
  208. }
  209. else
  210. {
  211. while ((ch = string[stringPos++]))
  212. {
  213. setarg(paramPos, i++, ch);
  214. }
  215. }
  216. stringPos--;
  217. setarg(paramPos, i, '\0');
  218. }
  219. default:
  220. {
  221. continue;
  222. }
  223. }
  224. while (string[stringPos] && string[stringPos] != delim && string[stringPos] > ' ')
  225. {
  226. stringPos++;
  227. }
  228. while (string[stringPos] && (string[stringPos] == delim || string[stringPos] <= ' '))
  229. {
  230. stringPos++;
  231. }
  232. paramPos++;
  233. }
  234. do
  235. {
  236. if ((delim = format[formatPos++]) > ' ')
  237. {
  238. if (delim == '\'')
  239. {
  240. while ((delim = format[formatPos++]) && delim != '\'') {}
  241. }
  242. else if (delim != 'z')
  243. {
  244. return delim;
  245. }
  246. }
  247. }
  248. while (delim > ' ');
  249. return 0;
  250. }