| 1 | /* |
| 2 | www.sourceforge.net/projects/tinyxpath |
| 3 | Copyright (c) 2002-2004 Yves Berquin ([email protected]) |
| 4 | |
| 5 | This software is provided 'as-is', without any express or implied |
| 6 | warranty. In no event will the authors be held liable for any |
| 7 | damages arising from the use of this software. |
| 8 | |
| 9 | Permission is granted to anyone to use this software for any |
| 10 | purpose, including commercial applications, and to alter it and |
| 11 | redistribute it freely, subject to the following restrictions: |
| 12 | |
| 13 | 1. The origin of this software must not be misrepresented; you must |
| 14 | not claim that you wrote the original software. If you use this |
| 15 | software in a product, an acknowledgment in the product documentation |
| 16 | would be appreciated but is not required. |
| 17 | |
| 18 | 2. Altered source versions must be plainly marked as such, and |
| 19 | must not be misrepresented as being the original software. |
| 20 | |
| 21 | 3. This notice may not be removed or altered from any source |
| 22 | distribution. |
| 23 | */ |
| 24 | #ifndef __TINYBYTESTREAM_H |
| 25 | #define __TINYBYTESTREAM_H |
| 26 | |
| 27 | #include "lex_util.h" |
| 28 | |
| 29 | namespace TinyXPath |
| 30 | { |
| 31 | |
| 32 | /** |
| 33 | A Byte stream class, very crude. The only purpose of this class is to walk in a byte stream, |
| 34 | available at construction time. |
| 35 | */ |
| 36 | class byte_stream |
| 37 | { |
| 38 | /// Length of the total string, + 1 |
| 39 | unsigned u_length; |
| 40 | /// Total string |
| 41 | _byte_ * bp_in; |
| 42 | /// Current read position |
| 43 | _byte_ * bp_current; |
| 44 | /// First invalid position |
| 45 | _byte_ * bp_end; |
| 46 | /// true when there are still some byte to read |
| 47 | bool o_valid; |
| 48 | public : |
| 49 | /// constructor |
| 50 | byte_stream (const char * cp_in) |
| 51 | { |
| 52 | u_length = strlen (cp_in) + 1; |
| 53 | bp_in = new _byte_ [u_length] ; |
| 54 | memcpy (bp_in, cp_in, u_length); |
| 55 | bp_current = bp_in; |
| 56 | bp_end = bp_in + u_length - 1; |
| 57 | o_valid = (bp_current != bp_end); |
| 58 | } |
| 59 | /// destructor |
| 60 | ~ byte_stream () |
| 61 | { |
| 62 | if (bp_in) |
| 63 | delete [] bp_in; |
| 64 | } |
| 65 | /// Returns the byte on top |
| 66 | _byte_ b_top () |
| 67 | { |
| 68 | return * bp_current; |
| 69 | } |
| 70 | /// Consumes one byte |
| 71 | _byte_ b_pop () |
| 72 | { |
| 73 | if (! o_is_valid ()) |
| 74 | return 0; |
| 75 | bp_current++; |
| 76 | o_valid = (bp_current != bp_end); |
| 77 | return * (bp_current - 1); |
| 78 | } |
| 79 | /// true if there are still some byte to consume |
| 80 | bool o_is_valid () |
| 81 | { |
| 82 | return o_valid; |
| 83 | } |
| 84 | /// number of bytes still to consume |
| 85 | unsigned u_remain () |
| 86 | { |
| 87 | return (unsigned) (bp_end - bp_current); |
| 88 | } |
| 89 | /// peek a byte a little further down the stream |
| 90 | _byte_ b_forward (unsigned u_nb_char) |
| 91 | { |
| 92 | if (u_remain () > u_nb_char) |
| 93 | return bp_current [u_nb_char]; |
| 94 | return 0; |
| 95 | } |
| 96 | /// get a byte backward pointer to the stream |
| 97 | const _byte_ * bp_get_backward (unsigned u_amount) |
| 98 | { |
| 99 | return bp_current - u_amount + 1; |
| 100 | } |
| 101 | } ; |
| 102 | |
| 103 | } |
| 104 | |
| 105 | #endif |