📄 scanner.h
字号:
/* $Id: scanner.h 1142 2007-04-04 09:54:29Z bennylp $ */
/*
* Copyright (C) 2003-2005 Benny Prijono <benny@prijono.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef __PJ_SCANNER_H__
#define __PJ_SCANNER_H__
/**
* @file scanner.h
* @brief Text Scanning.
*/
#include <pjlib-util/types.h>
PJ_BEGIN_DECL
/**
* @defgroup PJ_SCAN Fast Text Scanning
* @ingroup PJLIB_UTIL
* @brief Text scanning utility.
*
* This module describes a fast text scanning functions.
*
* @{
*/
#if defined(PJ_SCANNER_USE_BITWISE) && PJ_SCANNER_USE_BITWISE != 0
# include <pjlib-util/scanner_cis_bitwise.h>
#else
# include <pjlib-util/scanner_cis_uint.h>
#endif
/**
* Initialize scanner input specification buffer.
*
* @param cs_buf The scanner character specification.
*/
PJ_DECL(void) pj_cis_buf_init(pj_cis_buf_t *cs_buf);
/**
* Create a new input specification.
*
* @param cs_buf Specification buffer.
* @param cis Character input specification to be initialized.
*
* @return PJ_SUCCESS if new specification has been successfully
* created, or PJ_ETOOMANY if there are already too many
* specifications in the buffer.
*/
PJ_DECL(pj_status_t) pj_cis_init(pj_cis_buf_t *cs_buf, pj_cis_t *cis);
/**
* Create a new input specification based on an existing specification.
*
* @param new_cis The new specification to be initialized.
* @param existing The existing specification, from which the input
* bitmask will be copied to the new specification.
*
* @return PJ_SUCCESS if new specification has been successfully
* created, or PJ_ETOOMANY if there are already too many
* specifications in the buffer.
*/
PJ_DECL(pj_status_t) pj_cis_dup(pj_cis_t *new_cis, pj_cis_t *existing);
/**
* Add the characters in the specified range '[cstart, cend)' to the
* specification (the last character itself ('cend') is not added).
*
* @param cis The scanner character specification.
* @param cstart The first character in the range.
* @param cend The next character after the last character in the range.
*/
PJ_DECL(void) pj_cis_add_range( pj_cis_t *cis, int cstart, int cend);
/**
* Add alphabetic characters to the specification.
*
* @param cis The scanner character specification.
*/
PJ_DECL(void) pj_cis_add_alpha( pj_cis_t *cis);
/**
* Add numeric characters to the specification.
*
* @param cis The scanner character specification.
*/
PJ_DECL(void) pj_cis_add_num( pj_cis_t *cis);
/**
* Add the characters in the string to the specification.
*
* @param cis The scanner character specification.
* @param str The string.
*/
PJ_DECL(void) pj_cis_add_str( pj_cis_t *cis, const char *str);
/**
* Add specification from another specification.
*
* @param cis The specification is to be set.
* @param rhs The specification to be copied.
*/
PJ_DECL(void) pj_cis_add_cis( pj_cis_t *cis, const pj_cis_t *rhs);
/**
* Delete characters in the specified range from the specification.
*
* @param cis The scanner character specification.
* @param cstart The first character in the range.
* @param cend The next character after the last character in the range.
*/
PJ_DECL(void) pj_cis_del_range( pj_cis_t *cis, int cstart, int cend);
/**
* Delete characters in the specified string from the specification.
*
* @param cis The scanner character specification.
* @param str The string.
*/
PJ_DECL(void) pj_cis_del_str( pj_cis_t *cis, const char *str);
/**
* Invert specification.
*
* @param cis The scanner character specification.
*/
PJ_DECL(void) pj_cis_invert( pj_cis_t *cis );
/**
* Check whether the specified character belongs to the specification.
*
* @param cis The scanner character specification.
* @param c The character to check for matching.
*
* @return Non-zero if match (not necessarily one).
*/
PJ_INLINE(int) pj_cis_match( const pj_cis_t *cis, pj_uint8_t c )
{
return PJ_CIS_ISSET(cis, c);
}
/**
* Flags for scanner.
*/
enum
{
/** This flags specifies that the scanner should automatically skip
whitespaces
*/
PJ_SCAN_AUTOSKIP_WS = 1,
/** This flags specifies that the scanner should automatically skip
SIP header continuation. This flag implies PJ_SCAN_AUTOSKIP_WS.
*/
PJ_SCAN_AUTOSKIP_WS_HEADER = 3,
/** Auto-skip new lines.
*/
PJ_SCAN_AUTOSKIP_NEWLINE = 4
};
/* Forward decl. */
struct pj_scanner;
/**
* The callback function type to be called by the scanner when it encounters
* syntax error.
*
* @param scanner The scanner instance that calls the callback .
*/
typedef void (*pj_syn_err_func_ptr)(struct pj_scanner *scanner);
/**
* The text scanner structure.
*/
typedef struct pj_scanner
{
char *begin; /**< Start of input buffer. */
char *end; /**< End of input buffer. */
char *curptr; /**< Current pointer. */
int line; /**< Current line. */
char *start_line; /**< Where current line starts. */
int skip_ws; /**< Skip whitespace flag. */
pj_syn_err_func_ptr callback; /**< Syntax error callback. */
} pj_scanner;
/**
* This structure can be used by application to store the state of the parser,
* so that the scanner state can be rollback to this state when necessary.
*/
typedef struct pj_scan_state
{
char *curptr; /**< Current scanner's pointer. */
int line; /**< Current line. */
char *start_line; /**< Start of current line. */
} pj_scan_state;
/**
* Initialize the scanner. Note that the input string buffer must have
* length at least buflen+1 because the scanner will NULL terminate the
* string during initialization.
*
* @param scanner The scanner to be initialized.
* @param bufstart The input buffer to scan. Note that buffer[buflen] will be
* filled with NULL char until scanner is destroyed, so
* the actual buffer length must be at least buflen+1.
* @param buflen The length of the input buffer, which normally is
* strlen(bufstart).
* @param options Zero, or combination of PJ_SCAN_AUTOSKIP_WS or
* PJ_SCAN_AUTOSKIP_WS_HEADER
* @param callback Callback to be called when the scanner encounters syntax
* error condition.
*/
PJ_DECL(void) pj_scan_init( pj_scanner *scanner, char *bufstart, int buflen,
unsigned options,
pj_syn_err_func_ptr callback );
/**
* Call this function when application has finished using the scanner.
*
* @param scanner The scanner.
*/
PJ_DECL(void) pj_scan_fini( pj_scanner *scanner );
/**
* Determine whether the EOF condition for the scanner has been met.
*
* @param scanner The scanner.
*
* @return Non-zero if scanner is EOF.
*/
PJ_INLINE(int) pj_scan_is_eof( const pj_scanner *scanner)
{
return scanner->curptr >= scanner->end;
}
/**
* Peek strings in current position according to parameter spec, and return
* the strings in parameter out. The current scanner position will not be
* moved. If the scanner is already in EOF state, syntax error callback will
* be called thrown.
*
* @param scanner The scanner.
* @param spec The spec to match input string.
* @param out String to store the result.
*
* @return the character right after the peek-ed position or zero if there's
* no more characters.
*/
PJ_DECL(int) pj_scan_peek( pj_scanner *scanner,
const pj_cis_t *spec, pj_str_t *out);
/**
* Peek len characters in current position, and return them in out parameter.
* Note that whitespaces or newlines will be returned as it is, regardless
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -