Advertisement
PaulCastellano

sscanf

Apr 18th, 2016
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.57 KB | None | 0 0
  1.  
  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.  
  100. new changestr[16], changepos = 0, strpos = stringPos;
  101. while(changepos < 16 && string[strpos] && string[strpos] != delim)
  102. {
  103. changestr[changepos++] = string[strpos++];
  104. }
  105. changestr[changepos] = '\0';
  106. setarg(paramPos,0,_:floatstr(changestr));
  107. }
  108. case 'p':
  109. {
  110. delim = format[formatPos++];
  111. continue;
  112. }
  113. case '\'':
  114. {
  115. new
  116. end = formatPos - 1,
  117. ch;
  118. while ((ch = format[++end]) && ch != '\'') {}
  119. if (!ch)
  120. {
  121. return -1;
  122. }
  123. format[end] = '\0';
  124. if ((ch = strfind(string, format[formatPos], false, stringPos)) == -1)
  125. {
  126. if (format[end + 1])
  127. {
  128. return -1;
  129. }
  130. return 0;
  131. }
  132. format[end] = '\'';
  133. stringPos = ch + (end - formatPos);
  134. formatPos = end + 1;
  135. }
  136. case 'u':
  137. {
  138. new
  139. end = stringPos - 1,
  140. id = 0,
  141. bool:num = true,
  142. ch;
  143. while ((ch = string[++end]) && ch != delim)
  144. {
  145. if (num)
  146. {
  147. if ('0' <= ch <= '9')
  148. {
  149. id = (id * 10) + (ch - '0');
  150. }
  151. else
  152. {
  153. num = false;
  154. }
  155. }
  156. }
  157. if (num && IsPlayerConnected(id))
  158. {
  159. setarg(paramPos, 0, id);
  160. }
  161. else
  162. {
  163. #if !defined foreach
  164. #define foreach(%1,%2) for (new %2 = 0; %2 < MAX_PLAYERS; %2++) if (IsPlayerConnected(%2))
  165. #define __SSCANF_FOREACH__
  166. #endif
  167. string[end] = '\0';
  168. num = false;
  169. new
  170. name[MAX_PLAYER_NAME];
  171. id = end - stringPos;
  172. foreach (Player, playerid)
  173. {
  174. GetPlayerName(playerid, name, sizeof (name));
  175. if (!strcmp(name, string[stringPos], true, id))
  176. {
  177. setarg(paramPos, 0, playerid);
  178. num = true;
  179. break;
  180. }
  181. }
  182. if (!num)
  183. {
  184. setarg(paramPos, 0, INVALID_PLAYER_ID);
  185. }
  186. string[end] = ch;
  187. #if defined __SSCANF_FOREACH__
  188. #undef foreach
  189. #undef __SSCANF_FOREACH__
  190. #endif
  191. }
  192. stringPos = end;
  193. }
  194. case 's', 'z':
  195. {
  196. new
  197. i = 0,
  198. ch;
  199. if (format[formatPos])
  200. {
  201. while ((ch = string[stringPos++]) && ch != delim)
  202. {
  203. setarg(paramPos, i++, ch);
  204. }
  205. if (!i)
  206. {
  207. return -1;
  208. }
  209. }
  210. else
  211. {
  212. while ((ch = string[stringPos++]))
  213. {
  214. setarg(paramPos, i++, ch);
  215. }
  216. }
  217. stringPos--;
  218. setarg(paramPos, i, '\0');
  219. }
  220. default:
  221. {
  222. continue;
  223. }
  224. }
  225. while (string[stringPos] && string[stringPos] != delim && string[stringPos] > ' ')
  226. {
  227. stringPos++;
  228. }
  229. while (string[stringPos] && (string[stringPos] == delim || string[stringPos] <= ' '))
  230. {
  231. stringPos++;
  232. }
  233. paramPos++;
  234. }
  235. do
  236. {
  237. if ((delim = format[formatPos++]) > ' ')
  238. {
  239. if (delim == '\'')
  240. {
  241. while ((delim = format[formatPos++]) && delim != '\'') {}
  242. }
  243. else if (delim != 'z')
  244. {
  245. return delim;
  246. }
  247. }
  248. }
  249. while (delim > ' ');
  250. return 0;
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement