    FUNCTION SearchInput(Input,String,Line,Clean)  Result(Found)
! This function search a string in an input block

!----------------------------------------------------------------------
!  Copyright 2003-2014 Ecole Normale Supérieure de Lyon, 
!  Centre National de la Recherche Scientifique,
!  Université Claude Bernard Lyon 1. All rights reserved.
!
!  This work is registered with the Agency for the Protection of Programs 
!  as IDDN.FR.001.100009.000.S.P.2014.000.30625
!
!  Authors: P. Fleurat-Lessard, P. Dayal
!  Contact: optnpath@gmail.com
!
! This file is part of "Opt'n Path".
!
!  "Opt'n Path" is free software: you can redistribute it and/or modify
!  it under the terms of the GNU Affero General Public License as
!  published by the Free Software Foundation, either version 3 of the License,
!  or (at your option) any later version.
!
!  "Opt'n Path" 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 Affero General Public License for more details.
!
!  You should have received a copy of the GNU Affero General Public License
!  along with "Opt'n Path". If not, see <http://www.gnu.org/licenses/>.
!
! Contact The Office of Technology Licensing, valorisation@ens-lyon.fr,
! for commercial licensing opportunities.
!----------------------------------------------------------------------

      Use VarTypes
      Use io_module

      implicit none


  INTERFACE
     function valid(string) result (isValid)
       CHARACTER(*), intent(in) :: string
       logical                  :: isValid
     END function VALID
  END INTERFACE

! Input
      TYPE (Input_line), POINTER, INTENT(IN) :: Input
      CHARACTER(*), INTENT(IN) :: String
      CHARACTER(*), OPTIONAL, INTENT(IN) :: Clean

! Output
      TYPE (Input_line), POINTER, INTENT(OUT) :: Line

      LOGICAL :: Found, Debug
      CHARACTER(LCHARS) :: LineUp,UpString

      Debug=valid('SearchInput')

      if (debug) Call header('Entering SearchInput')

      UpString=String
      Call upcase(UpString)

      if (debug) WRITE(*,*) 'Looking for:',Upstring

      IF (present(Clean)) THEN
         Call CleanString(UpString,Clean)
      END IF

      if (debug) WRITE(*,*) 'Looking for clean:',TRIM(Upstring)

      Line => Input
      LineUp=Line%line
      Call UpCase(LineUp)
      IF (present(Clean)) THEN
         Call CleanString(LineUp,Clean)
      END IF
      if (debug) WRITE(*,*) 'Comparing with:',Trim(LineUp)

      Found=(INDEX(LineUp,TRIM(UpString))>0)
      DO WHILE (Associated(Line%next).AND.(.NOT.Found))
         Line => Line%Next
         LineUp=Line%line
         Call UpCase(LineUp)
         IF (present(Clean)) THEN
            Call CleanString(LineUp,Clean)
         END IF
         if (debug) WRITE(*,*) 'Comparing with:',Trim(LineUp)

         Found=(INDEX(LineUp,TRIM(UpString))>0)
      END DO

      If (.NOT.Found) Nullify(Line)

      if (debug) Call header('Exiting SearchInput')

      Return
!--------------------------------------------------------------------------- END
    END FUNCTION SearchInput
