module string_module
!----- LGPL --------------------------------------------------------------------
!
! Copyright (C) Stichting Deltares, 2011-2013.
!
! This library is free software; you can redistribute it and/or
! modify it under the terms of the GNU Lesser General Public
! License as published by the Free Software Foundation version 2.1.
!
! This library is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
! Lesser General Public License for more details.
!
! You should have received a copy of the GNU Lesser General Public
! License along with this library; if not, see .
!
! contact: delft3d.support@deltares.nl
! Stichting Deltares
! P.O. Box 177
! 2600 MH Delft, The Netherlands
!
! All indications and logos of, and references to, "Delft3D" and "Deltares"
! are registered trademarks of Stichting Deltares, and remain the property of
! Stichting Deltares. All rights reserved.
!
!-------------------------------------------------------------------------------
! $Id: string_module.f90 2711 2013-07-19 09:41:06Z jagers $
! $HeadURL: https://svn.oss.deltares.nl/repos/delft3d/trunk/src/utils_lgpl/deltares_common/packages/deltares_common/src/string_module.f90 $
!!--description-----------------------------------------------------------------
!
! Function: - Various string processing routines
!
!!--pseudo code and references--------------------------------------------------
! NONE
!!--declarations----------------------------------------------------------------
private
!
! functions and subroutines
!
public string_module_info
public str_token
public str_lower
public str_upper
public strcmpi
public remove_leading_spaces
public remove_all_spaces
contains
! ------------------------------------------------------------------------------
! Subroutine: string_module_info
! Purpose: Add info about this string module to the messages stack
! Summary: Add id string and URL
! Arguments:
! messages Stack of messages to add the info to
! ------------------------------------------------------------------------------
subroutine string_module_info(messages)
use message_module
!
! Call variables
!
type(message_stack), pointer :: messages
!
!! executable statements ---------------------------------------------------
!
call addmessage(messages,'$Id: string_module.f90 2711 2013-07-19 09:41:06Z jagers $')
call addmessage(messages,'$URL: https://svn.oss.deltares.nl/repos/delft3d/ ... /string_module.f90 $')
end subroutine string_module_info
! ------------------------------------------------------------------------------
! Subroutine: str_token
! Purpose: Obtain first token from string
! Summary: Scan string for non-space characters and return first set found.
! Arguments:
! string on input : String to be scanned
! on output: Remainder of string
! token on output: String containing token
! quote on input : Optional quote character
! ------------------------------------------------------------------------------
subroutine str_token(string, token, quote)
implicit none
!
! Call variables
!
character(*) , intent(inout) :: string
character(*) , intent(out) :: token
character(1), optional, intent(in) :: quote
!
! Local variables
!
integer :: i
integer :: i1
integer :: i2
integer :: j
integer :: strlen
logical :: quoted
!
!! executable statements ---------------------------------------------------
!
i1 = -1
i2 = -1
quoted = .false.
strlen = len_trim(string)
! find start of token
do i = 1, strlen
j = ichar(string(i:i))
if (j == 32 .or. j == 9 .or. j == 10 .or. j == 13) then
! a space
if (i1>0 .and. .not.quoted) then
! token ends here
i2 = i-1
exit
endif
else
! not a space
if (i1<0) then
! token starts here and may continue till the end of the string
if (present(quote)) then
if (string(i:i) == quote) then
quoted = .true.
endif
endif
i1 = i
i2 = strlen
elseif (quoted) then
if (string(i:i) == quote) then
quoted = .false.
endif
endif
endif
enddo
!
if (i1<0) then
! empty string: no token found
token = ' '
else
! token found
token = string(i1:i2)
if (present(quote)) then
! remove quotes
if (string(i1:i1)==quote .and. string(i2:i2)==quote) then
token = string(i1+1:i2-1)
endif
endif
string = string(i2+1:strlen)
endif
end subroutine str_token
! ------------------------------------------------------------------------------
! Subroutine: str_lower
! Purpose: Convert upper case characters to lower case
! Summary: Scan string for upper case characters and
! convert them.
! Arguments:
! string String to be converted
! lenstr Optional length of string to be converted
! ------------------------------------------------------------------------------
subroutine str_lower(string, lenstr)
implicit none
!
! Call variables
!
integer , optional, intent(in) :: lenstr
character(*) :: string
!
! Local variables
!
integer :: i
integer :: j
integer :: newlen
!
!! executable statements ---------------------------------------------------
!
if (present(lenstr)) then
newlen = min(lenstr, len_trim(string))
else
newlen = len_trim(string)
endif
do i = 1, newlen
j = ichar(string(i:i))
if ((j>64) .and. (j<91)) then
j = j + 32
string(i:i) = char(j)
endif
enddo
end subroutine str_lower
! ------------------------------------------------------------------------------
! Subroutine: str_upper
! Purpose: Convert lower case characters to upper case
! Summary: Scan string for lower case characters and
! convert them.
! Arguments:
! string String to be converted
! lenstr Optional length of string to be converted
! ------------------------------------------------------------------------------
subroutine str_upper(string, lenstr)
implicit none
!
! Call variables
!
integer , optional, intent(in) :: lenstr
character(*) :: string
!
! Local variables
!
integer :: i
integer :: j
integer :: newlen
!
!! executable statements ---------------------------------------------------
!
if (present(lenstr)) then
newlen = min(lenstr, len_trim(string))
else
newlen = len_trim(string)
endif
do i = 1, newlen
j = ichar(string(i:i))
if ((j>96) .and. (j<123)) then
j = j - 32
string(i:i) = char(j)
endif
enddo
end subroutine str_upper
! ------------------------------------------------------------------------------
! Subroutine: remove_all_spaces
! Purpose: Remove all spaces from a string
! Summary: Scan string for space characters and if one exists, move the
! following characters forward.
! Arguments:
! string String to be converted
! lenstr Optional trimmed length of string after removal of spaces
! ------------------------------------------------------------------------------
subroutine remove_all_spaces(string, lenstr)
implicit none
!
! Call variables
!
character(*) :: string
integer , optional, intent(out):: lenstr
!
! Local variables
!
integer :: i
integer :: newlen
!
!! executable statements ---------------------------------------------------
!
newlen = len_trim(string)
!
! loop over all characters in string
! if it is a space character, move remainder of string forward
!
i = 1
do while (i