[6] | 1 | module m_buffer |
---|
| 2 | |
---|
| 3 | ! |
---|
| 4 | ! At this point we use a fixed-size buffer. |
---|
| 5 | ! Note however that buffer overflows will only be |
---|
| 6 | ! triggered by overly long *unbroken* pcdata values, or |
---|
| 7 | ! by overly long attribute values. Hopefully |
---|
| 8 | ! element or attribute names are "short enough". |
---|
| 9 | ! There is code in m_fsm to avoid buffer overflows |
---|
| 10 | ! caused by pcdata with whitespace. |
---|
| 11 | ! |
---|
| 12 | ! In a forthcoming implementation it could be made dynamical... |
---|
| 13 | ! |
---|
| 14 | integer, parameter, public :: MAX_BUFF_SIZE = 10000 |
---|
| 15 | integer, parameter, private :: BUFF_SIZE_WARNING = 900 |
---|
| 16 | |
---|
| 17 | ! |
---|
| 18 | |
---|
| 19 | type, public :: buffer_t |
---|
| 20 | private |
---|
| 21 | integer :: size |
---|
| 22 | character(len=MAX_BUFF_SIZE) :: str |
---|
| 23 | end type buffer_t |
---|
| 24 | |
---|
| 25 | public :: add_to_buffer |
---|
| 26 | public :: print_buffer, str, len !! , char |
---|
| 27 | public :: operator (.equal.) |
---|
| 28 | public :: buffer_nearly_full, reset_buffer, init_buffer |
---|
| 29 | public :: buffer_to_character |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | !---------------------------------------------------------------- |
---|
| 33 | interface add_to_buffer |
---|
| 34 | module procedure add_str_to_buffer |
---|
| 35 | end interface |
---|
| 36 | private :: add_char_to_buffer, add_str_to_buffer |
---|
| 37 | |
---|
| 38 | interface operator (.equal.) |
---|
| 39 | module procedure compare_buffers, compare_buffer_str, & |
---|
| 40 | compare_str_buffer |
---|
| 41 | end interface |
---|
| 42 | private :: compare_buffers, compare_buffer_str, compare_str_buffer |
---|
| 43 | |
---|
| 44 | interface str |
---|
| 45 | module procedure buffer_to_str |
---|
| 46 | end interface |
---|
| 47 | interface char ! Experimental |
---|
| 48 | module procedure buffer_to_str |
---|
| 49 | end interface |
---|
| 50 | private :: buffer_to_str |
---|
| 51 | |
---|
| 52 | interface len |
---|
| 53 | module procedure buffer_length |
---|
| 54 | end interface |
---|
| 55 | private :: buffer_length |
---|
| 56 | |
---|
| 57 | CONTAINS |
---|
| 58 | !================================================================== |
---|
| 59 | |
---|
| 60 | !---------------------------------------------------------------- |
---|
| 61 | function compare_buffers(a,b) result(equal) ! .equal. generic |
---|
| 62 | type(buffer_t), intent(in) :: a |
---|
| 63 | type(buffer_t), intent(in) :: b |
---|
| 64 | logical :: equal |
---|
| 65 | |
---|
| 66 | equal = ((a%size == b%size) .and. (a%str(1:a%size) == b%str(1:b%size))) |
---|
| 67 | |
---|
| 68 | end function compare_buffers |
---|
| 69 | |
---|
| 70 | !---------------------------------------------------------------- |
---|
| 71 | function compare_buffer_str(buffer,str) result(equal) ! .equal. generic |
---|
| 72 | type(buffer_t), intent(in) :: buffer |
---|
| 73 | character(len=*), intent(in) :: str |
---|
| 74 | logical :: equal |
---|
| 75 | |
---|
| 76 | equal = (buffer%str(1:buffer%size) == trim(str)) |
---|
| 77 | |
---|
| 78 | end function compare_buffer_str |
---|
| 79 | |
---|
| 80 | !---------------------------------------------------------------- |
---|
| 81 | function compare_str_buffer(str,buffer) result(equal) ! .equal. generic |
---|
| 82 | character(len=*), intent(in) :: str |
---|
| 83 | type(buffer_t), intent(in) :: buffer |
---|
| 84 | logical :: equal |
---|
| 85 | |
---|
| 86 | equal = (buffer%str(1:buffer%size) == trim(str)) |
---|
| 87 | |
---|
| 88 | end function compare_str_buffer |
---|
| 89 | |
---|
| 90 | !---------------------------------------------------------------- |
---|
| 91 | subroutine add_char_to_buffer(c,buffer) |
---|
| 92 | character(len=1), intent(in) :: c |
---|
| 93 | type(buffer_t), intent(inout) :: buffer |
---|
| 94 | |
---|
| 95 | integer :: n |
---|
| 96 | buffer%size = buffer%size + 1 |
---|
| 97 | n = buffer%size |
---|
| 98 | |
---|
| 99 | if (n> MAX_BUFF_SIZE) then |
---|
| 100 | !! RETURN |
---|
| 101 | ! |
---|
| 102 | ! It will only affect long comments and sgml declarations |
---|
| 103 | STOP "sax 1 Buffer overflow: long unbroken string of pcdata or attribute value..." |
---|
| 104 | endif |
---|
| 105 | |
---|
| 106 | buffer%str(n:n) = c |
---|
| 107 | end subroutine add_char_to_buffer |
---|
| 108 | |
---|
| 109 | !---------------------------------------------------------------- |
---|
| 110 | subroutine add_str_to_buffer(s,buffer) |
---|
| 111 | character(len=*), intent(in) :: s |
---|
| 112 | type(buffer_t), intent(inout) :: buffer |
---|
| 113 | |
---|
| 114 | integer :: n, len_s, last_pos |
---|
| 115 | |
---|
| 116 | len_s = len(s) |
---|
| 117 | last_pos = buffer%size |
---|
| 118 | buffer%size = buffer%size + len_s |
---|
| 119 | n = buffer%size |
---|
| 120 | |
---|
| 121 | if (n> MAX_BUFF_SIZE) then |
---|
| 122 | !! RETURN |
---|
| 123 | ! |
---|
| 124 | ! It will only affect long comments and sgml declarations |
---|
| 125 | STOP "sax 2 Buffer overflow: long unbroken string of pcdata or attribute value..." |
---|
| 126 | endif |
---|
| 127 | |
---|
| 128 | buffer%str(last_pos+1:n) = s |
---|
| 129 | end subroutine add_str_to_buffer |
---|
| 130 | |
---|
| 131 | !---------------------------------------------------------------- |
---|
| 132 | subroutine init_buffer(buffer) |
---|
| 133 | type(buffer_t), intent(inout) :: buffer |
---|
| 134 | |
---|
| 135 | buffer%size = 0 |
---|
| 136 | buffer%str="" ! To avoid "undefined" status |
---|
| 137 | |
---|
| 138 | end subroutine init_buffer |
---|
| 139 | !---------------------------------------------------------------- |
---|
| 140 | subroutine reset_buffer(buffer) |
---|
| 141 | type(buffer_t), intent(inout) :: buffer |
---|
| 142 | buffer%size = 0 |
---|
| 143 | end subroutine reset_buffer |
---|
| 144 | !---------------------------------------------------------------- |
---|
| 145 | subroutine print_buffer(buffer) |
---|
| 146 | type(buffer_t), intent(in) :: buffer |
---|
| 147 | |
---|
| 148 | integer :: i |
---|
| 149 | |
---|
| 150 | do i = 1, buffer%size |
---|
| 151 | write(unit=6,fmt="(a1)",advance="no") buffer%str(i:i) |
---|
| 152 | enddo |
---|
| 153 | |
---|
| 154 | end subroutine print_buffer |
---|
| 155 | !---------------------------------------------------------------- |
---|
| 156 | ! This is better... but could it lead to memory leaks? |
---|
| 157 | ! |
---|
| 158 | function buffer_to_str(buffer) result(str) |
---|
| 159 | type(buffer_t), intent(in) :: buffer |
---|
| 160 | character(len=buffer%size) :: str |
---|
| 161 | |
---|
| 162 | str = buffer%str(1:buffer%size) |
---|
| 163 | end function buffer_to_str |
---|
| 164 | |
---|
| 165 | !---------------------------------------------------------------- |
---|
| 166 | ! |
---|
| 167 | subroutine buffer_to_character(buffer,str) |
---|
| 168 | type(buffer_t), intent(in) :: buffer |
---|
| 169 | character(len=*), intent(out) :: str |
---|
| 170 | |
---|
| 171 | str = buffer%str(1:buffer%size) |
---|
| 172 | end subroutine buffer_to_character |
---|
| 173 | |
---|
| 174 | !---------------------------------------------------------------- |
---|
| 175 | function buffer_nearly_full(buffer) result(warn) |
---|
| 176 | type(buffer_t), intent(in) :: buffer |
---|
| 177 | logical :: warn |
---|
| 178 | |
---|
| 179 | warn = buffer%size > BUFF_SIZE_WARNING |
---|
| 180 | |
---|
| 181 | end function buffer_nearly_full |
---|
| 182 | |
---|
| 183 | !---------------------------------------------------------------- |
---|
| 184 | function buffer_length(buffer) result(length) |
---|
| 185 | type(buffer_t), intent(in) :: buffer |
---|
| 186 | integer :: length |
---|
| 187 | |
---|
| 188 | length = buffer%size |
---|
| 189 | |
---|
| 190 | end function buffer_length |
---|
| 191 | |
---|
| 192 | |
---|
| 193 | end module m_buffer |
---|
| 194 | |
---|
| 195 | |
---|
| 196 | |
---|
| 197 | |
---|
| 198 | |
---|
| 199 | |
---|
| 200 | |
---|