Guest User

Untitled

a guest
May 26th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. stock sscanf(sstring[], format[], {Float,_}:...)
  2. {
  3. #if defined isnull
  4. if (isnull(sstring))
  5. #else
  6. if (sstring[0] == 0 || (sstring[0] == 1 && sstring[1] == 0))
  7. #endif
  8. {
  9. return format[0];
  10. }
  11. #pragma tabsize 4
  12. new
  13. formatPos = 0,
  14. sstringPos = 0,
  15. paramPos = 2,
  16. paramCount = numargs(),
  17. delim = ' ';
  18. while (sstring[sstringPos] && sstring[sstringPos] <= ' ')
  19. {
  20. sstringPos++;
  21. }
  22. while (paramPos < paramCount && sstring[sstringPos])
  23. {
  24. switch (format[formatPos++])
  25. {
  26. case '\0':
  27. {
  28. return 0;
  29. }
  30. case 'i', 'd':
  31. {
  32. new
  33. neg = 1,
  34. num = 0,
  35. ch = sstring[sstringPos];
  36. if (ch == '-')
  37. {
  38. neg = -1;
  39. ch = sstring[++sstringPos];
  40. }
  41. do
  42. {
  43. sstringPos++;
  44. if ('0' <= ch <= '9')
  45. {
  46. num = (num * 10) + (ch - '0');
  47. }
  48. else
  49. {
  50. return -1;
  51. }
  52. }
  53. while ((ch = sstring[sstringPos]) > ' ' && ch != delim);
  54. setarg(paramPos, 0, num * neg);
  55. }
  56. case 'h', 'x':
  57. {
  58. new
  59. num = 0,
  60. ch = sstring[sstringPos];
  61. do
  62. {
  63. sstringPos++;
  64. switch (ch)
  65. {
  66. case 'x', 'X':
  67. {
  68. num = 0;
  69. continue;
  70. }
  71. case '0' .. '9':
  72. {
  73. num = (num << 4) | (ch - '0');
  74. }
  75. case 'a' .. 'f':
  76. {
  77. num = (num << 4) | (ch - ('a' - 10));
  78. }
  79. case 'A' .. 'F':
  80. {
  81. num = (num << 4) | (ch - ('A' - 10));
  82. }
  83. default:
  84. {
  85. return -1;
  86. }
  87. }
  88. }
  89. while ((ch = sstring[sstringPos]) > ' ' && ch != delim);
  90. setarg(paramPos, 0, num);
  91. }
  92. case 'c':
  93. {
  94. setarg(paramPos, 0, sstring[sstringPos++]);
  95. }
  96. case 'f':
  97. {
  98.  
  99. new changestr[16], changepos = 0, strpos = sstringPos;
  100. while(changepos < 16 && sstring[strpos] && sstring[strpos] != delim)
  101. {
  102. changestr[changepos++] = sstring[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(sstring, format[formatPos], false, sstringPos)) == -1)
  124. {
  125. if (format[end + 1])
  126. {
  127. return -1;
  128. }
  129. return 0;
  130. }
  131. format[end] = '\'';
  132. sstringPos = ch + (end - formatPos);
  133. formatPos = end + 1;
  134. }
  135. case 'u':
  136. {
  137. new
  138. end = sstringPos - 1,
  139. id = 0,
  140. bool:num = true,
  141. ch;
  142. while ((ch = sstring[++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. sstring[end] = '\0';
  167. num = false;
  168. new
  169. name[MAX_PLAYER_NAME];
  170. id = end - sstringPos;
  171. foreach (Player, playerid)
  172. {
  173. GetPlayerName(playerid, name, sizeof (name));
  174. if (!strcmp(name, sstring[sstringPos], 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. sstring[end] = ch;
  186. #if defined __SSCANF_FOREACH__
  187. #undef foreach
  188. #undef __SSCANF_FOREACH__
  189. #endif
  190. }
  191. sstringPos = end;
  192. }
  193. case 's', 'z':
  194. {
  195. new
  196. i = 0,
  197. ch;
  198. if (format[formatPos])
  199. {
  200. while ((ch = sstring[sstringPos++]) && ch != delim)
  201. {
  202. setarg(paramPos, i++, ch);
  203. }
  204. if (!i)
  205. {
  206. return -1;
  207. }
  208. }
  209. else
  210. {
  211. while ((ch = sstring[sstringPos++]))
  212. {
  213. setarg(paramPos, i++, ch);
  214. }
  215. }
  216. sstringPos--;
  217. setarg(paramPos, i, '\0');
  218. }
  219. default:
  220. {
  221. continue;
  222. }
  223. }
  224. while (sstring[sstringPos] && sstring[sstringPos] != delim && sstring[sstringPos] > ' ')
  225. {
  226. sstringPos++;
  227. }
  228. while (sstring[sstringPos] && (sstring[sstringPos] == delim || sstring[sstringPos] <= ' '))
  229. {
  230. sstringPos++;
  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. }
Add Comment
Please, Sign In to add comment