xmlf_read_from_buffer.inc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. ! Part of XML-Fortran library:
  2. !
  3. ! $Id: read_from_buffer.inc,v 1.2 2006/03/26 19:05:48 arjenmarkus Exp $
  4. !
  5. character(len=*), intent(in) :: buffer
  6. integer, intent(inout) :: ierror
  7. integer :: n
  8. integer :: i
  9. integer :: step
  10. integer :: ierr
  11. !
  12. ! First allocate an array that is surely large enough
  13. ! Note:
  14. ! This is not completely failsafe: with list-directed
  15. ! input you can also use repeat counts (10000*1.0 for
  16. ! instance).
  17. !
  18. allocate( work(len(buffer)/2+1) )
  19. !
  20. ! NOTE:
  21. ! This is not portable!!
  22. !
  23. ! read( buffer, *, iostat = ierror ) (work(n), n=1,size(work))
  24. !
  25. ! So, use a different strategy: a binary search
  26. ! First: establish that we have at least one item to read
  27. ! Second: do the binary search
  28. !
  29. ! read( buffer, *, iostat = ierr ) work(1)
  30. ! if ( ierr /= 0 ) then
  31. ! n = 0
  32. ! else
  33. n = 1
  34. do while ( n <= size(work) )
  35. n = 2 * n
  36. enddo
  37. n = n / 2
  38. step = n / 2
  39. ! step = n / 2
  40. do while ( step > 0 )
  41. read( buffer, *, iostat = ierr ) (work(i), i = 1,n)
  42. if ( ierr /= 0 ) then
  43. ierror = ierr ! Store the error code for later use
  44. n = n - step
  45. else
  46. n = n + step
  47. endif
  48. step = step / 2
  49. enddo
  50. ! endif
  51. !
  52. ! Then allocate an array of the actual size needed
  53. ! and copy the data
  54. !
  55. !
  56. if ( associated( var ) ) then
  57. deallocate( var )
  58. endif
  59. !
  60. ! One complication: we may have one too many
  61. ! (consequence of the binary search)
  62. !
  63. read( buffer, *, iostat = ierr ) (work(i), i = 1,n)
  64. if ( ierr < 0 ) then
  65. n = n - 1
  66. endif
  67. allocate( var(n) )
  68. var(1:n) = work(1:n)
  69. deallocate( work )
  70. if ( ierror .lt. 0 ) then
  71. ierror = 0
  72. endif