add_str Subroutine

private subroutine add_str(me, str)

Add a string to the buffer.

Type Bound

pyplot

Arguments

Type IntentOptional Attributes Name
class(pyplot), intent(inout) :: me

pyplot handler

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

str to be added to pyplot handler buffer


Called by

proc~~add_str~~CalledByGraph proc~add_str pyplot%add_str proc~add_3d_plot pyplot%add_3d_plot proc~add_3d_plot->proc~add_str proc~add_bar pyplot%add_bar proc~add_bar->proc~add_str proc~add_contour pyplot%add_contour proc~add_contour->proc~add_str proc~add_errorbar pyplot%add_errorbar proc~add_errorbar->proc~add_str proc~add_hist pyplot%add_hist proc~add_hist->proc~add_str proc~add_imshow pyplot%add_imshow proc~add_imshow->proc~add_str proc~add_plot pyplot%add_plot proc~add_plot->proc~add_str proc~add_sphere pyplot%add_sphere proc~add_sphere->proc~add_str proc~add_text pyplot%add_text proc~add_text->proc~add_str proc~finish_ops pyplot%finish_ops proc~finish_ops->proc~add_str proc~initialize pyplot%initialize proc~initialize->proc~add_str proc~plot_surface pyplot%plot_surface proc~plot_surface->proc~add_str proc~plot_wireframe pyplot%plot_wireframe proc~plot_wireframe->proc~add_str proc~savefig pyplot%savefig proc~savefig->proc~add_str proc~savefig->proc~finish_ops proc~showfig pyplot%showfig proc~showfig->proc~add_str proc~showfig->proc~finish_ops

Source Code

    subroutine add_str(me,str)

    class(pyplot),    intent(inout) :: me  !! pyplot handler
    character(len=*), intent(in)    :: str !! str to be added to pyplot handler buffer

    integer :: n_old !! current `me%str` length
    integer :: n_str !! length of input `str`
    character(len=:),allocatable :: tmp !! tmp string for building the result

    if (len(str)==0) return

    ! original
    !me%str = me%str//str//new_line(' ')
    ! the above can sometimes cause a stack overflow in the
    ! intel Fortran compiler, so we replace with this:
    if (allocated(me%str)) then

        ! if there is room in the current string, then
        ! insert str into it. otherwise, allocate a new string
        ! with enough room and move the old string into it before adding str.

        if (me%str_len + len(str) + 1 <= len(me%str)) then
            me%str(me%str_len+1:me%str_len+len(str)) = str
            me%str_len = me%str_len + len(str)
            me%str(me%str_len+1:me%str_len+1) = new_line(' ')
            me%str_len = me%str_len + 1
        else
            n_old = me%str_len
            n_str = len(str)
            ! double size to avoid too many allocations
            allocate(character(len=2*(n_old+n_str)) :: tmp)
            tmp(1:n_old) = me%str
            tmp(n_old+1:) = str//new_line(' ')
            call move_alloc(tmp, me%str)
            me%str_len = n_old + n_str + 1
        end if
    else
        allocate(me%str, source = str//new_line(' '))
        me%str_len = len(me%str)
    end if

    end subroutine add_str