scanner.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef __PJ_SCANNER_H__
  20. #define __PJ_SCANNER_H__
  21. /**
  22. * @file scanner.h
  23. * @brief Text Scanning.
  24. */
  25. #include <pjlib-util/types.h>
  26. PJ_BEGIN_DECL
  27. /**
  28. * @defgroup PJ_SCAN Fast Text Scanning
  29. * @ingroup PJLIB_TEXT
  30. * @brief Text scanning utility.
  31. *
  32. * This module describes a fast text scanning functions.
  33. *
  34. * @{
  35. */
  36. #if defined(PJ_SCANNER_USE_BITWISE) && PJ_SCANNER_USE_BITWISE != 0
  37. # include <pjlib-util/scanner_cis_bitwise.h>
  38. #else
  39. # include <pjlib-util/scanner_cis_uint.h>
  40. #endif
  41. /**
  42. * Initialize scanner input specification buffer.
  43. *
  44. * @param cs_buf The scanner character specification.
  45. */
  46. PJ_DECL(void) pj_cis_buf_init(pj_cis_buf_t *cs_buf);
  47. /**
  48. * Create a new input specification.
  49. *
  50. * @param cs_buf Specification buffer.
  51. * @param cis Character input specification to be initialized.
  52. *
  53. * @return PJ_SUCCESS if new specification has been successfully
  54. * created, or PJ_ETOOMANY if there are already too many
  55. * specifications in the buffer.
  56. */
  57. PJ_DECL(pj_status_t) pj_cis_init(pj_cis_buf_t *cs_buf, pj_cis_t *cis);
  58. /**
  59. * Create a new input specification based on an existing specification.
  60. *
  61. * @param new_cis The new specification to be initialized.
  62. * @param existing The existing specification, from which the input
  63. * bitmask will be copied to the new specification.
  64. *
  65. * @return PJ_SUCCESS if new specification has been successfully
  66. * created, or PJ_ETOOMANY if there are already too many
  67. * specifications in the buffer.
  68. */
  69. PJ_DECL(pj_status_t) pj_cis_dup(pj_cis_t *new_cis, pj_cis_t *existing);
  70. /**
  71. * Add the characters in the specified range '[cstart, cend)' to the
  72. * specification (the last character itself ('cend') is not added).
  73. *
  74. * @param cis The scanner character specification.
  75. * @param cstart The first character in the range.
  76. * @param cend The next character after the last character in the range.
  77. */
  78. PJ_DECL(void) pj_cis_add_range( pj_cis_t *cis, int cstart, int cend);
  79. /**
  80. * Add alphabetic characters to the specification.
  81. *
  82. * @param cis The scanner character specification.
  83. */
  84. PJ_DECL(void) pj_cis_add_alpha( pj_cis_t *cis);
  85. /**
  86. * Add numeric characters to the specification.
  87. *
  88. * @param cis The scanner character specification.
  89. */
  90. PJ_DECL(void) pj_cis_add_num( pj_cis_t *cis);
  91. /**
  92. * Add the characters in the string to the specification.
  93. *
  94. * @param cis The scanner character specification.
  95. * @param str The string.
  96. */
  97. PJ_DECL(void) pj_cis_add_str( pj_cis_t *cis, const char *str);
  98. /**
  99. * Add specification from another specification.
  100. *
  101. * @param cis The specification is to be set.
  102. * @param rhs The specification to be copied.
  103. */
  104. PJ_DECL(void) pj_cis_add_cis( pj_cis_t *cis, const pj_cis_t *rhs);
  105. /**
  106. * Delete characters in the specified range from the specification.
  107. *
  108. * @param cis The scanner character specification.
  109. * @param cstart The first character in the range.
  110. * @param cend The next character after the last character in the range.
  111. */
  112. PJ_DECL(void) pj_cis_del_range( pj_cis_t *cis, int cstart, int cend);
  113. /**
  114. * Delete characters in the specified string from the specification.
  115. *
  116. * @param cis The scanner character specification.
  117. * @param str The string.
  118. */
  119. PJ_DECL(void) pj_cis_del_str( pj_cis_t *cis, const char *str);
  120. /**
  121. * Invert specification.
  122. *
  123. * @param cis The scanner character specification.
  124. */
  125. PJ_DECL(void) pj_cis_invert( pj_cis_t *cis );
  126. /**
  127. * Check whether the specified character belongs to the specification.
  128. *
  129. * @param cis The scanner character specification.
  130. * @param c The character to check for matching.
  131. *
  132. * @return Non-zero if match (not necessarily one).
  133. */
  134. PJ_INLINE(int) pj_cis_match( const pj_cis_t *cis, pj_uint8_t c )
  135. {
  136. return PJ_CIS_ISSET(cis, c);
  137. }
  138. /**
  139. * Flags for scanner.
  140. */
  141. enum
  142. {
  143. /** This flags specifies that the scanner should automatically skip
  144. whitespaces
  145. */
  146. PJ_SCAN_AUTOSKIP_WS = 1,
  147. /** This flags specifies that the scanner should automatically skip
  148. SIP header continuation. This flag implies PJ_SCAN_AUTOSKIP_WS.
  149. */
  150. PJ_SCAN_AUTOSKIP_WS_HEADER = 3,
  151. /** Auto-skip new lines.
  152. */
  153. PJ_SCAN_AUTOSKIP_NEWLINE = 4
  154. };
  155. /* Forward decl. */
  156. struct pj_scanner;
  157. /**
  158. * The callback function type to be called by the scanner when it encounters
  159. * syntax error.
  160. *
  161. * @param scanner The scanner instance that calls the callback .
  162. */
  163. typedef void (*pj_syn_err_func_ptr)(struct pj_scanner *scanner);
  164. /**
  165. * The text scanner structure.
  166. */
  167. typedef struct pj_scanner
  168. {
  169. char *begin; /**< Start of input buffer. */
  170. char *end; /**< End of input buffer. */
  171. char *curptr; /**< Current pointer. */
  172. int line; /**< Current line. */
  173. char *start_line; /**< Where current line starts. */
  174. int skip_ws; /**< Skip whitespace flag. */
  175. pj_syn_err_func_ptr callback; /**< Syntax error callback. */
  176. } pj_scanner;
  177. /**
  178. * This structure can be used by application to store the state of the parser,
  179. * so that the scanner state can be rollback to this state when necessary.
  180. */
  181. typedef struct pj_scan_state
  182. {
  183. char *curptr; /**< Current scanner's pointer. */
  184. int line; /**< Current line. */
  185. char *start_line; /**< Start of current line. */
  186. } pj_scan_state;
  187. /**
  188. * Initialize the scanner.
  189. * Note that the input string buffer MUST be NULL terminated and have
  190. * length at least buflen+1 (buflen MUST NOT include the NULL terminator).
  191. *
  192. * @param scanner The scanner to be initialized.
  193. * @param bufstart The input buffer to scan, which must be NULL terminated.
  194. * @param buflen The length of the input buffer, which normally is
  195. * strlen(bufstart), hence not counting the NULL terminator.
  196. * @param options Zero, or combination of PJ_SCAN_AUTOSKIP_WS or
  197. * PJ_SCAN_AUTOSKIP_WS_HEADER
  198. * @param callback Callback to be called when the scanner encounters syntax
  199. * error condition.
  200. */
  201. PJ_DECL(void) pj_scan_init( pj_scanner *scanner, char *bufstart,
  202. pj_size_t buflen,
  203. unsigned options,
  204. pj_syn_err_func_ptr callback );
  205. /**
  206. * Call this function when application has finished using the scanner.
  207. *
  208. * @param scanner The scanner.
  209. */
  210. PJ_DECL(void) pj_scan_fini( pj_scanner *scanner );
  211. /**
  212. * Determine whether the EOF condition for the scanner has been met.
  213. *
  214. * @param scanner The scanner.
  215. *
  216. * @return Non-zero if scanner is EOF.
  217. */
  218. PJ_INLINE(int) pj_scan_is_eof( const pj_scanner *scanner)
  219. {
  220. return scanner->curptr >= scanner->end;
  221. }
  222. /**
  223. * Peek strings in current position according to parameter spec, and return
  224. * the strings in parameter out. The current scanner position will not be
  225. * moved. If the scanner is already in EOF state, syntax error callback will
  226. * be called thrown.
  227. *
  228. * @param scanner The scanner.
  229. * @param spec The spec to match input string.
  230. * @param out String to store the result.
  231. *
  232. * @return the character right after the peek-ed position or zero if there's
  233. * no more characters.
  234. */
  235. PJ_DECL(int) pj_scan_peek( pj_scanner *scanner,
  236. const pj_cis_t *spec, pj_str_t *out);
  237. /**
  238. * Peek len characters in current position, and return them in out parameter.
  239. * Note that whitespaces or newlines will be returned as it is, regardless
  240. * of PJ_SCAN_AUTOSKIP_WS settings. If the character left is less than len,
  241. * syntax error callback will be called.
  242. *
  243. * @param scanner The scanner.
  244. * @param len Length to peek.
  245. * @param out String to store the result.
  246. *
  247. * @return the character right after the peek-ed position or zero if there's
  248. * no more characters.
  249. */
  250. PJ_DECL(int) pj_scan_peek_n( pj_scanner *scanner,
  251. pj_size_t len, pj_str_t *out);
  252. /**
  253. * Peek strings in current position until spec is matched, and return
  254. * the strings in parameter out. The current scanner position will not be
  255. * moved. If the scanner is already in EOF state, syntax error callback will
  256. * be called.
  257. *
  258. * @param scanner The scanner.
  259. * @param spec The peeking will stop when the input match this spec.
  260. * @param out String to store the result.
  261. *
  262. * @return the character right after the peek-ed position.
  263. */
  264. PJ_DECL(int) pj_scan_peek_until( pj_scanner *scanner,
  265. const pj_cis_t *spec,
  266. pj_str_t *out);
  267. /**
  268. * Get characters from the buffer according to the spec, and return them
  269. * in out parameter. The scanner will attempt to get as many characters as
  270. * possible as long as the spec matches. If the first character doesn't
  271. * match the spec, or scanner is already in EOF when this function is called,
  272. * an exception will be thrown.
  273. *
  274. * @param scanner The scanner.
  275. * @param spec The spec to match input string.
  276. * @param out String to store the result.
  277. */
  278. PJ_DECL(void) pj_scan_get( pj_scanner *scanner,
  279. const pj_cis_t *spec, pj_str_t *out);
  280. /**
  281. * Just like #pj_scan_get(), but additionally performs unescaping when
  282. * escaped ('%') character is found. The input spec MUST NOT contain the
  283. * specification for '%' characted.
  284. *
  285. * @param scanner The scanner.
  286. * @param spec The spec to match input string.
  287. * @param out String to store the result.
  288. */
  289. PJ_DECL(void) pj_scan_get_unescape( pj_scanner *scanner,
  290. const pj_cis_t *spec, pj_str_t *out);
  291. /**
  292. * Get characters between quotes. If current input doesn't match begin_quote,
  293. * syntax error will be thrown. Note that the resulting string will contain
  294. * the enclosing quote.
  295. *
  296. * @param scanner The scanner.
  297. * @param begin_quote The character to begin the quote.
  298. * @param end_quote The character to end the quote.
  299. * @param out String to store the result.
  300. */
  301. PJ_DECL(void) pj_scan_get_quote( pj_scanner *scanner,
  302. int begin_quote, int end_quote,
  303. pj_str_t *out);
  304. /**
  305. * Get characters between quotes. If current input doesn't match begin_quote,
  306. * syntax error will be thrown. Note that the resulting string will contain
  307. * the enclosing quote.
  308. *
  309. * @param scanner The scanner.
  310. * @param begin_quotes The character array to begin the quotes. For example,
  311. * the two characters " and '.
  312. * @param end_quotes The character array to end the quotes. The position
  313. * found in the begin_quotes array will be used to match
  314. * the end quotes. So if the begin_quotes was the array
  315. * of "'< the end_quotes should be "'>. If begin_array
  316. * matched the ' then the end_quotes will look for ' to
  317. * match at the end.
  318. * @param qsize The size of the begin_quotes and end_quotes arrays.
  319. * @param out String to store the result.
  320. */
  321. PJ_DECL(void) pj_scan_get_quotes(pj_scanner *scanner,
  322. const char *begin_quotes,
  323. const char *end_quotes, int qsize,
  324. pj_str_t *out);
  325. /**
  326. * Get N characters from the scanner.
  327. *
  328. * @param scanner The scanner.
  329. * @param N Number of characters to get.
  330. * @param out String to store the result.
  331. */
  332. PJ_DECL(void) pj_scan_get_n( pj_scanner *scanner,
  333. unsigned N, pj_str_t *out);
  334. /**
  335. * Get one character from the scanner.
  336. *
  337. * @param scanner The scanner.
  338. *
  339. * @return The character.
  340. */
  341. PJ_DECL(int) pj_scan_get_char( pj_scanner *scanner );
  342. /**
  343. * Get characters from the scanner and move the scanner position until the
  344. * current character matches the spec.
  345. *
  346. * @param scanner The scanner.
  347. * @param spec Get until the input match this spec.
  348. * @param out String to store the result.
  349. */
  350. PJ_DECL(void) pj_scan_get_until( pj_scanner *scanner,
  351. const pj_cis_t *spec, pj_str_t *out);
  352. /**
  353. * Get characters from the scanner and move the scanner position until the
  354. * current character matches until_char.
  355. *
  356. * @param scanner The scanner.
  357. * @param until_char Get until the input match this character.
  358. * @param out String to store the result.
  359. */
  360. PJ_DECL(void) pj_scan_get_until_ch( pj_scanner *scanner,
  361. int until_char, pj_str_t *out);
  362. /**
  363. * Get characters from the scanner and move the scanner position until the
  364. * current character matches until_char.
  365. *
  366. * @param scanner The scanner.
  367. * @param until_spec Get until the input match any of these characters.
  368. * @param out String to store the result.
  369. */
  370. PJ_DECL(void) pj_scan_get_until_chr( pj_scanner *scanner,
  371. const char *until_spec, pj_str_t *out);
  372. /**
  373. * Advance the scanner N characters, and skip whitespace
  374. * if necessary.
  375. *
  376. * @param scanner The scanner.
  377. * @param N Number of characters to skip.
  378. * @param skip Flag to specify whether whitespace should be skipped
  379. * after skipping the characters.
  380. */
  381. PJ_DECL(void) pj_scan_advance_n( pj_scanner *scanner,
  382. unsigned N, pj_bool_t skip);
  383. /**
  384. * Compare string in current position with the specified string.
  385. *
  386. * @param scanner The scanner.
  387. * @param s The string to compare with.
  388. * @param len Length of the string to compare.
  389. *
  390. * @return zero, <0, or >0 (just like strcmp()).
  391. */
  392. PJ_DECL(int) pj_scan_strcmp( pj_scanner *scanner, const char *s, int len);
  393. /**
  394. * Case-less string comparison of current position with the specified
  395. * string.
  396. *
  397. * @param scanner The scanner.
  398. * @param s The string to compare with.
  399. * @param len Length of the string to compare with.
  400. *
  401. * @return zero, <0, or >0 (just like strcmp()).
  402. */
  403. PJ_DECL(int) pj_scan_stricmp( pj_scanner *scanner, const char *s, int len);
  404. /**
  405. * Perform case insensitive string comparison of string in current position,
  406. * knowing that the string to compare only consists of alphanumeric
  407. * characters.
  408. *
  409. * Note that unlike #pj_scan_stricmp, this function can only return zero or
  410. * -1.
  411. *
  412. * @param scanner The scanner.
  413. * @param s The string to compare with.
  414. * @param len Length of the string to compare with.
  415. *
  416. * @return zero if equal or -1.
  417. *
  418. * @see strnicmp_alnum, pj_stricmp_alnum
  419. */
  420. PJ_DECL(int) pj_scan_stricmp_alnum( pj_scanner *scanner, const char *s,
  421. int len);
  422. /**
  423. * Get a newline from the scanner. A newline is defined as '\\n', or '\\r', or
  424. * "\\r\\n". If current input is not newline, syntax error will be thrown.
  425. *
  426. * @param scanner The scanner.
  427. */
  428. PJ_DECL(void) pj_scan_get_newline( pj_scanner *scanner );
  429. /**
  430. * Manually skip whitespaces according to flag that was specified when
  431. * the scanner was initialized.
  432. *
  433. * @param scanner The scanner.
  434. */
  435. PJ_DECL(void) pj_scan_skip_whitespace( pj_scanner *scanner );
  436. /**
  437. * Skip current line.
  438. *
  439. * @param scanner The scanner.
  440. */
  441. PJ_DECL(void) pj_scan_skip_line( pj_scanner *scanner );
  442. /**
  443. * Save the full scanner state.
  444. *
  445. * @param scanner The scanner.
  446. * @param state Variable to store scanner's state.
  447. */
  448. PJ_DECL(void) pj_scan_save_state( const pj_scanner *scanner,
  449. pj_scan_state *state);
  450. /**
  451. * Restore the full scanner state.
  452. * Note that this would not restore the string if application has modified
  453. * it. This will only restore the scanner scanning position.
  454. *
  455. * @param scanner The scanner.
  456. * @param state State of the scanner.
  457. */
  458. PJ_DECL(void) pj_scan_restore_state( pj_scanner *scanner,
  459. pj_scan_state *state);
  460. /**
  461. * Get current column position.
  462. *
  463. * @param scanner The scanner.
  464. *
  465. * @return The column position.
  466. */
  467. PJ_INLINE(int) pj_scan_get_col( const pj_scanner *scanner )
  468. {
  469. return (int)(scanner->curptr - scanner->start_line);
  470. }
  471. /**
  472. * @}
  473. */
  474. PJ_END_DECL
  475. #endif