parse_number Subroutine

private subroutine parse_number(unit, str, value)

Arguments

Type IntentOptional AttributesName
integer(kind=IK), intent(in) :: unit

file unit number (if parsing from a file)

character(kind=CK,len=*), intent(in) :: str

JSON string (if parsing from a string)

type(json_value), , pointer:: value

Description

Read a numerical value from the file (or string). The routine will determine if it is an integer or a double, and allocate the type accordingly.

Calls

proc~~parse_number~~CallsGraph proc~parse_number parse_number proc~string_to_integer string_to_integer proc~parse_number->proc~string_to_integer interface~throw_exception throw_exception proc~parse_number->interface~throw_exception proc~pop_char pop_char proc~parse_number->proc~pop_char proc~push_char push_char proc~parse_number->proc~push_char proc~to_double to_double proc~parse_number->proc~to_double proc~string_to_double string_to_double proc~parse_number->proc~string_to_double proc~to_integer to_integer proc~parse_number->proc~to_integer proc~string_to_integer->interface~throw_exception proc~json_throw_exception json_throw_exception interface~throw_exception->proc~json_throw_exception proc~push_char->interface~throw_exception proc~integer_to_string integer_to_string proc~push_char->proc~integer_to_string proc~destroy_json_data destroy_json_data proc~to_double->proc~destroy_json_data proc~string_to_double->interface~throw_exception proc~to_integer->proc~destroy_json_data
Help

Called By

proc~~parse_number~~CalledByGraph proc~parse_number parse_number proc~parse_value parse_value proc~parse_value->proc~parse_number proc~parse_object parse_object proc~parse_value->proc~parse_object proc~parse_array parse_array proc~parse_value->proc~parse_array proc~json_parse_string json_parse_string proc~json_parse_string->proc~parse_value proc~json_parse_file json_parse_file proc~json_parse_file->proc~parse_value proc~parse_object->proc~parse_value proc~parse_object->proc~parse_object proc~parse_array->proc~parse_value proc~wrap_json_parse_string wrap_json_parse_string proc~wrap_json_parse_string->proc~json_parse_string interface~json_parse json_parse interface~json_parse->proc~json_parse_string interface~json_parse->proc~json_parse_file proc~test_14 test_14 proc~test_14->interface~json_parse proc~test_8 test_8 proc~test_8->interface~json_parse proc~json_file_load json_file_load proc~json_file_load->interface~json_parse proc~json_file_load_from_string json_file_load_from_string proc~json_file_load_from_string->interface~json_parse program~jf_test_14 jf_test_14 program~jf_test_14->proc~test_14 program~jf_test_8 jf_test_8 program~jf_test_8->proc~test_8 proc~wrap_json_file_load_from_string wrap_json_file_load_from_string proc~wrap_json_file_load_from_string->proc~json_file_load_from_string
Help

Variables

TypeVisibility AttributesNameInitial
character(kind=CK,len=:), public, allocatable:: tmp
character(kind=CK,len=1), public :: c
logical(kind=LK), public :: eof
real(kind=RK), public :: rval
integer(kind=IK), public :: ival
logical(kind=LK), public :: first
logical(kind=LK), public :: is_integer
integer(kind=IK), public :: ip

Source Code

    subroutine parse_number(unit, str, value)

    implicit none

    integer(IK),intent(in)              :: unit   !! file unit number (if parsing from a file)
    character(kind=CK,len=*),intent(in) :: str    !! JSON string (if parsing from a string)
    type(json_value),pointer            :: value

    character(kind=CK,len=:),allocatable :: tmp
    character(kind=CK,len=1) :: c
    logical(LK) :: eof
    real(RK) :: rval
    integer(IK) :: ival
    logical(LK) :: first
    logical(LK) :: is_integer

    !to speed up by reducing the number of character string reallocations:
    integer(IK) :: ip !index to put next character

    if (.not. exception_thrown) then

        tmp = repeat(space, chunk_size)
        ip = 1
        first = .true.
        is_integer = .true.  !assume it may be an integer, unless otherwise determined

        !read one character at a time and accumulate the string:
        do

            !get the next character:
            c = pop_char(unit, str=str, eof = eof, skip_ws = .true.)

            if (eof) then
                call throw_exception('Error in parse_number:'//&
                                     ' Unexpected end of file while parsing number.')
                return
            else

                select case (c)
                case(CK_'-',CK_'+')    !note: allowing a '+' as the first character here.

                    if (is_integer .and. (.not. first)) is_integer = .false.

                    !add it to the string:
                    !tmp = tmp // c   !...original
                    if (ip>len(tmp)) tmp = tmp // repeat(space, chunk_size)
                    tmp(ip:ip) = c
                    ip = ip + 1

                case(CK_'.',CK_'E',CK_'e')    !can be present in real numbers

                    if (is_integer) is_integer = .false.

                    !add it to the string:
                    !tmp = tmp // c   !...original
                    if (ip>len(tmp)) tmp = tmp // repeat(space, chunk_size)
                    tmp(ip:ip) = c
                    ip = ip + 1

                case(CK_'0':CK_'9')    !valid characters for numbers

                    !add it to the string:
                    !tmp = tmp // c   !...original
                    if (ip>len(tmp)) tmp = tmp // repeat(space, chunk_size)
                    tmp(ip:ip) = c
                    ip = ip + 1

                case default

                    !push back the last character read:
                    call push_char(c)

                    !string to value:
                    if (is_integer) then
                        ival = string_to_integer(tmp)
                        call to_integer(value,ival)
                    else
                        rval = string_to_double(tmp)
                        call to_double(value,rval)
                    end if

                    exit    !finished

                end select

            end if
            if (first) first = .false.

        end do

        !cleanup:
        if (allocated(tmp)) deallocate(tmp)

    end if

    end subroutine parse_number