Guest User

sscanf

a guest
Feb 27th, 2015
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. stock sscanf(string[], format[], {Float,_}:...)
  2. {
  3. #if defined isnull
  4. if (isnull(string))
  5. #else
  6. if (string[0] == 0 || (string[0] == 1 && string[1] == 0))
  7. #endif
  8. {
  9. return format[0];
  10. }
  11. #pragma tabsize 4
  12. new
  13. formatPos = 0,
  14. stringPos = 0,
  15. paramPos = 2,
  16. paramCount = numargs(),
  17. delim = ' ';
  18. while (string[stringPos] && string[stringPos] <= ' ')
  19. {
  20. stringPos++;
  21. }
  22. while (paramPos < paramCount && string[stringPos])
  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 = string[stringPos];
  36. if (ch == '-')
  37. {
  38. neg = -1;
  39. ch = string[++stringPos];
  40. }
  41. do
  42. {
  43. stringPos++;
  44. if ('0' <= ch <= '9')
  45. {
  46. num = (num * 10) + (ch - '0');
  47. }
  48. else
  49. {
  50. return -1;
  51. }
  52. }
  53. while ((ch = string[stringPos]) > ' ' && ch != delim);
  54. setarg(paramPos, 0, num * neg);
  55. }
  56. case 'h', 'x':
  57. {
  58. new
  59. num = 0,
  60. ch = string[stringPos];
  61. do
  62. {
  63. stringPos++;
  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 = string[stringPos]) > ' ' && ch != delim);
  90. setarg(paramPos, 0, num);
  91. }
  92. case 'c':
  93. {
  94. setarg(paramPos, 0, string[stringPos++]);
  95. }
  96. case 'f':
  97. {
  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. string[end] = '\0';
  163. num = false;
  164. new name[MAX_PLAYER_NAME];
  165. id = end - stringPos;
  166. for(new i = 0; i < MAX_PLAYERS; i++)
  167. {
  168. if(IsPlayerConnected(i))
  169. {
  170. GetPlayerName(playerid, name, sizeof (name));
  171. if (!strcmp(name, string[stringPos], true, id))
  172. {
  173. setarg(paramPos, 0, playerid);
  174. num = true;
  175. break;
  176. }
  177. }
  178. }
  179. if (!num)
  180. {
  181. setarg(paramPos, 0, INVALID_PLAYER_ID);
  182. }
  183. string[end] = ch;
  184. }
  185. stringPos = end;
  186. }
  187. case 's', 'z':
  188. {
  189. new
  190. i = 0,
  191. ch;
  192. if (format[formatPos])
  193. {
  194. while ((ch = string[stringPos++]) && ch != delim)
  195. {
  196. setarg(paramPos, i++, ch);
  197. }
  198. if (!i)
  199. {
  200. return -1;
  201. }
  202. }
  203. else
  204. {
  205. while ((ch = string[stringPos++]))
  206. {
  207. setarg(paramPos, i++, ch);
  208. }
  209. }
  210. stringPos--;
  211. setarg(paramPos, i, '\0');
  212. }
  213. default:
  214. {
  215. continue;
  216. }
  217. }
  218. while (string[stringPos] && string[stringPos] != delim && string[stringPos] > ' ')
  219. {
  220. stringPos++;
  221. }
  222. while (string[stringPos] && (string[stringPos] == delim || string[stringPos] <= ' '))
  223. {
  224. stringPos++;
  225. }
  226. paramPos++;
  227. }
  228. do
  229. {
  230. if ((delim = format[formatPos++]) > ' ')
  231. {
  232. if (delim == '\'')
  233. {
  234. while ((delim = format[formatPos++]) && delim != '\'') {}
  235. }
  236. else if (delim != 'z')
  237. {
  238. return delim;
  239. }
  240. }
  241. }
  242. while (delim > ' ');
  243. return 0;
  244. }
Advertisement
Add Comment
Please, Sign In to add comment