| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2000-2014 Ericsson Telecom AB |
| 3 | // All rights reserved. This program and the accompanying materials |
| 4 | // are made available under the terms of the Eclipse Public License v1.0 |
| 5 | // which accompanies this distribution, and is available at |
| 6 | // http://www.eclipse.org/legal/epl-v10.html |
| 7 | /////////////////////////////////////////////////////////////////////////////// |
| 8 | #ifndef _TEXT_HH |
| 9 | #define _TEXT_HH |
| 10 | |
| 11 | #include "Types.h" |
| 12 | #include "Encdec.hh" |
| 13 | #include <sys/types.h> |
| 14 | #include <regex.h> |
| 15 | /** |
| 16 | * \defgroup TEXT TEXT-related stuff. |
| 17 | * |
| 18 | * @{ |
| 19 | */ |
| 20 | class CHARSTRING; |
| 21 | class Token_Match; |
| 22 | struct TTCN_TEXTdescriptor_values { |
| 23 | boolean leading_zero; |
| 24 | boolean repeatable; |
| 25 | int min_length; |
| 26 | int max_length; |
| 27 | int convert; |
| 28 | int just; |
| 29 | }; |
| 30 | |
| 31 | struct TTCN_TEXTdescriptor_bool { |
| 32 | const CHARSTRING* true_encode_token; |
| 33 | const Token_Match* true_decode_token; |
| 34 | const CHARSTRING* false_encode_token; |
| 35 | const Token_Match* false_decode_token; |
| 36 | }; |
| 37 | |
| 38 | struct TTCN_TEXTdescriptor_enum { |
| 39 | const CHARSTRING* encode_token; |
| 40 | const Token_Match* decode_token; |
| 41 | }; |
| 42 | |
| 43 | |
| 44 | struct TTCN_TEXTdescriptor_param_values { |
| 45 | TTCN_TEXTdescriptor_values coding_params; |
| 46 | TTCN_TEXTdescriptor_values decoding_params; |
| 47 | }; |
| 48 | |
| 49 | union TTCN_TEXTdescriptor_union { |
| 50 | const TTCN_TEXTdescriptor_param_values* parameters; |
| 51 | const TTCN_TEXTdescriptor_bool* bool_values; |
| 52 | const TTCN_TEXTdescriptor_enum* enum_values; |
| 53 | }; |
| 54 | |
| 55 | struct TTCN_TEXTdescriptor_t { |
| 56 | const CHARSTRING* begin_encode; |
| 57 | const Token_Match* begin_decode; |
| 58 | const CHARSTRING* end_encode; |
| 59 | const Token_Match* end_decode; |
| 60 | const CHARSTRING* separator_encode; |
| 61 | const Token_Match* separator_decode; |
| 62 | const Token_Match* select_token; |
| 63 | TTCN_TEXTdescriptor_union val; |
| 64 | }; |
| 65 | |
| 66 | class Token_Match { |
| 67 | regex_t posix_regexp_begin; ///< regexp for the anchored match |
| 68 | regex_t posix_regexp_first; ///< regexp for floating match |
| 69 | const char *token_str; // not owned or freed by Token_Match |
| 70 | size_t fixed_len; ///< length of fixed string, or zero |
| 71 | boolean null_match; ///< true if the token_str was NULL or empty |
| 72 | public: |
| 73 | Token_Match(const char *posix_str, boolean case_sensitive = TRUE, |
| 74 | boolean fixed = FALSE); |
| 75 | ~Token_Match(); |
| 76 | |
| 77 | inline operator const char*() const { return token_str; } |
| 78 | /** Match anchored at the beginning |
| 79 | * |
| 80 | * @param buf the buffer to search |
| 81 | * @return length of the match or -1 if not matched |
| 82 | * |
| 83 | * It may call TTCN_error() in case of error, causing a DTE |
| 84 | */ |
| 85 | int match_begin(TTCN_Buffer& buf) const; |
| 86 | /** Floating match |
| 87 | * |
| 88 | * @param buf the buffer to search |
| 89 | * @return the offset where the match begins, or -1 if not matched |
| 90 | * |
| 91 | * It may call TTCN_error() in case of error, causing a DTE |
| 92 | */ |
| 93 | int match_first(TTCN_Buffer& buf) const; |
| 94 | }; |
| 95 | |
| 96 | class Limit_Token_List { |
| 97 | private: |
| 98 | size_t num_of_tokens; |
| 99 | size_t size_of_list; |
| 100 | const Token_Match **list; |
| 101 | int *last_match; |
| 102 | int last_ret_val; |
| 103 | const char* last_pos; |
| 104 | /// Copy constructor disabled |
| 105 | Limit_Token_List(const Limit_Token_List&); |
| 106 | /// Assignment disabled |
| 107 | Limit_Token_List& operator=(const Limit_Token_List&); |
| 108 | public: |
| 109 | Limit_Token_List(); |
| 110 | ~Limit_Token_List(); |
| 111 | |
| 112 | void add_token(const Token_Match *); |
| 113 | void remove_tokens(size_t); |
| 114 | int match(TTCN_Buffer&,size_t lim=0); |
| 115 | inline boolean has_token(size_t ml=0) const { return num_of_tokens != ml; } |
| 116 | }; |
| 117 | |
| 118 | extern const TTCN_TEXTdescriptor_t INTEGER_text_; |
| 119 | extern const TTCN_TEXTdescriptor_t BOOLEAN_text_; |
| 120 | extern const TTCN_TEXTdescriptor_t CHARSTRING_text_; |
| 121 | extern const TTCN_TEXTdescriptor_t BITSTRING_text_; |
| 122 | extern const TTCN_TEXTdescriptor_t HEXSTRING_text_; |
| 123 | extern const TTCN_TEXTdescriptor_t OCTETSTRING_text_; |
| 124 | extern const TTCN_TEXTdescriptor_t UNIVERSAL_CHARSTRING_text_; |
| 125 | |
| 126 | /** @} end of TEXT group */ |
| 127 | |
| 128 | #endif // _TEXT_HH |