Advertisement
Guest User

Aldys - Codigo Sscanf

a guest
Aug 7th, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.77 KB | None | 0 0
  1.  
  2.  
  3. /*----------------------------------------------------------------------------*-
  4. Function:
  5. sscanf
  6. Params:
  7. string[] - String to extract parameters from.
  8. format[] - Parameter types to get.
  9. {Float,_}:... - Data return variables.
  10. Return:
  11. 0 - Successful, not 0 - fail.
  12. Notes:
  13. A fail is either insufficient variables to store the data or insufficient
  14. data for the format string - excess data is disgarded.
  15.  
  16. A string in the middle of the input data is extracted as a single word, a
  17. string at the end of the data collects all remaining text.
  18.  
  19. The format codes are:
  20.  
  21. c - A character.
  22. d, i - An integer.
  23. h, x - A hex number (e.g. a colour).
  24. f - A float.
  25. s - A string.
  26. z - An optional string.
  27. pX - An additional delimiter where X is another character.
  28. '' - Encloses a litteral string to locate.
  29. u - User, takes a name, part of a name or an id and returns the id if they're connected.
  30.  
  31. Now has IsNumeric integrated into the code.
  32.  
  33. Added additional delimiters in the form of all whitespace and an
  34. optioanlly specified one in the format string.
  35. -*----------------------------------------------------------------------------*/
  36.  
  37. stock sscanf(string[], format[], {Float,_}:...)
  38. {
  39. #if defined isnull
  40. if (isnull(string))
  41. #else
  42. if (string[0] == 0 || (string[0] == 1 && string[1] == 0))
  43. #endif
  44. {
  45. return format[0];
  46. }
  47. #pragma tabsize 4
  48. new
  49. formatPos = 0,
  50. stringPos = 0,
  51. paramPos = 2,
  52. paramCount = numargs(),
  53. delim = ' ';
  54. while (string[stringPos] && string[stringPos] <= ' ')
  55. {
  56. stringPos++;
  57. }
  58. while (paramPos < paramCount && string[stringPos])
  59. {
  60. switch (format[formatPos++])
  61. {
  62. case '\0':
  63. {
  64. return 0;
  65. }
  66. case 'i', 'd':
  67. {
  68. new
  69. neg = 1,
  70. num = 0,
  71. ch = string[stringPos];
  72. if (ch == '-')
  73. {
  74. neg = -1;
  75. ch = string[++stringPos];
  76. }
  77. do
  78. {
  79. stringPos++;
  80. if ('0' <= ch <= '9')
  81. {
  82. num = (num * 10) + (ch - '0');
  83. }
  84. else
  85. {
  86. return -1;
  87. }
  88. }
  89. while ((ch = string[stringPos]) > ' ' && ch != delim);
  90. setarg(paramPos, 0, num * neg);
  91. }
  92. case 'h', 'x':
  93. {
  94. new
  95. num = 0,
  96. ch = string[stringPos];
  97. do
  98. {
  99. stringPos++;
  100. switch (ch)
  101. {
  102. case 'x', 'X':
  103. {
  104. num = 0;
  105. continue;
  106. }
  107. case '0' .. '9':
  108. {
  109. num = (num << 4) | (ch - '0');
  110. }
  111. case 'a' .. 'f':
  112. {
  113. num = (num << 4) | (ch - ('a' - 10));
  114. }
  115. case 'A' .. 'F':
  116. {
  117. num = (num << 4) | (ch - ('A' - 10));
  118. }
  119. default:
  120. {
  121. return -1;
  122. }
  123. }
  124. }
  125. while ((ch = string[stringPos]) > ' ' && ch != delim);
  126. setarg(paramPos, 0, num);
  127. }
  128. case 'c':
  129. {
  130. setarg(paramPos, 0, string[stringPos++]);
  131. }
  132. case 'f':
  133. {
  134.  
  135. new changestr[16], changepos = 0, strpos = stringPos;
  136. while(changepos < 16 && string[strpos] && string[strpos] != delim)
  137. {
  138. changestr[changepos++] = string[strpos++];
  139. }
  140. changestr[changepos] = '\0';
  141. setarg(paramPos,0,_:floatstr(changestr));
  142. }
  143. case 'p':
  144. {
  145. delim = format[formatPos++];
  146. continue;
  147. }
  148. case '\'':
  149. {
  150. new
  151. end = formatPos - 1,
  152. ch;
  153. while ((ch = format[++end]) && ch != '\'') {}
  154. if (!ch)
  155. {
  156. return -1;
  157. }
  158. format[end] = '\0';
  159. if ((ch = strfind(string, format[formatPos], false, stringPos)) == -1)
  160. {
  161. if (format[end + 1])
  162. {
  163. return -1;
  164. }
  165. return 0;
  166. }
  167. format[end] = '\'';
  168. stringPos = ch + (end - formatPos);
  169. formatPos = end + 1;
  170. }
  171. case 'u':
  172. {
  173. new
  174. end = stringPos - 1,
  175. id = 0,
  176. bool:num = true,
  177. ch;
  178. while ((ch = string[++end]) && ch != delim)
  179. {
  180. if (num)
  181. {
  182. if ('0' <= ch <= '9')
  183. {
  184. id = (id * 10) + (ch - '0');
  185. }
  186. else
  187. {
  188. num = false;
  189. }
  190. }
  191. }
  192. if (num && IsPlayerConnected(id))
  193. {
  194. setarg(paramPos, 0, id);
  195. }
  196. else
  197. {
  198. #if !defined foreach
  199. #define foreach(%1,%2) for (new %2 = 0; %2 < MAX_PLAYERS; %2++) if (IsPlayerConnected(%2))
  200. #define __SSCANF_FOREACH__
  201. #endif
  202. string[end] = '\0';
  203. num = false;
  204. new
  205. name[MAX_PLAYER_NAME];
  206. id = end - stringPos;
  207. foreach (Player, playerid)
  208. {
  209. GetPlayerName(playerid, name, sizeof (name));
  210. if (!strcmp(name, string[stringPos], true, id))
  211. {
  212. setarg(paramPos, 0, playerid);
  213. num = true;
  214. break;
  215. }
  216. }
  217. if (!num)
  218. {
  219. setarg(paramPos, 0, INVALID_PLAYER_ID);
  220. }
  221. string[end] = ch;
  222. #if defined __SSCANF_FOREACH__
  223. #undef foreach
  224. #undef __SSCANF_FOREACH__
  225. #endif
  226. }
  227. stringPos = end;
  228. }
  229. case 's', 'z':
  230. {
  231. new
  232. i = 0,
  233. ch;
  234. if (format[formatPos])
  235. {
  236. while ((ch = string[stringPos++]) && ch != delim)
  237. {
  238. setarg(paramPos, i++, ch);
  239. }
  240. if (!i)
  241. {
  242. return -1;
  243. }
  244. }
  245. else
  246. {
  247. while ((ch = string[stringPos++]))
  248. {
  249. setarg(paramPos, i++, ch);
  250. }
  251. }
  252. stringPos--;
  253. setarg(paramPos, i, '\0');
  254. }
  255. default:
  256. {
  257. continue;
  258. }
  259. }
  260. while (string[stringPos] && string[stringPos] != delim && string[stringPos] > ' ')
  261. {
  262. stringPos++;
  263. }
  264. while (string[stringPos] && (string[stringPos] == delim || string[stringPos] <= ' '))
  265. {
  266. stringPos++;
  267. }
  268. paramPos++;
  269. }
  270. do
  271. {
  272. if ((delim = format[formatPos++]) > ' ')
  273. {
  274. if (delim == '\'')
  275. {
  276. while ((delim = format[formatPos++]) && delim != '\'') {}
  277. }
  278. else if (delim != 'z')
  279. {
  280. return delim;
  281. }
  282. }
  283. }
  284. while (delim > ' ');
  285. return 0;
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement