
Function strrpos ( Needle as string, Haystack as string ) as long

	if len ( Needle ) = 0 then strrpos = len ( Haystack ) + 1 : Exit Function

	if len ( Haystack ) = 0 then strrpos = len ( Haystack ) + 1 : Exit Function

	dim as long temp = 0

	temp = INSTRREV ( Haystack, Needle )

	if temp = 0 then strrpos = len ( Haystack ) + 1 : Exit Function

	strrpos = temp : Exit Function

End Function

Function strpos ( Needle as string, Haystack as string ) as long

	if len ( Needle )=0 then strpos = 0 : Exit Function

	if len ( Haystack )=0 then strpos = 0 : Exit Function

	dim as long temp = 0

	temp = INSTR ( Haystack, Needle )

	if temp = 0 then strpos = 0 : Exit Function

	strpos = temp : Exit Function

End Function

Function Ext_Suffix ( Needle as string, Haystack as string ) as string

	Ext_Suffix = mid ( Haystack, strrpos ( Needle, Haystack )+len ( Needle )) : Exit Function

End Function

Function Ext_Prefix ( Needle as string, Haystack as string ) as string

	Ext_Prefix = left ( Haystack, strrpos ( Needle, Haystack )-1 ) : Exit Function

End Function

Function Suffix ( Needle as string, Haystack as string ) as string

	dim as long o_i

	o_i=Instr ( Haystack, Needle )

	select case false

	case o_i=0

		Suffix = Mid$ ( Haystack, o_i+Len ( Needle )) : Exit Function

	case else

		Suffix = EMPTY : Exit Function

	end select

End Function

Function Prefix ( Needle as string, Haystack as string ) as string

	Prefix = Mid$ ( Haystack, 1, Instr ( Haystack, Needle )-1 ) : Exit Function

End Function

function AxisLabel_s ( Buffer as string, open_s As String = "[", close_s As String = "]" ) as string

	dim as long o1_i

	o1_i=instr ( Buffer, open_s )

	dim as long o2_i

	o2_i=instr ( Buffer, close_s )

	select case cbool ( true )

	case cbool ( o1_i>0 ) andalso cbool ( o2_i=0 )

		AxisLabel_s = left$ ( Buffer, o1_i-1 ) : Exit Function

	case cbool ( o2_i>o1_i )

		AxisLabel_s = left$ ( Buffer, o1_i-1 ) : Exit Function

	case else

		AxisLabel_s = Buffer : Exit Function

	end select

end function

function AxisParam_s ( Buffer as string, open_s As String = "[", close_s As String = "]" ) as string

	dim as long o1_i

	o1_i=instr ( Buffer, open_s )

	dim as long o2_i

	o2_i=instr ( Buffer, close_s )

	select case cbool ( true )

	case cbool ( o1_i>0 ) andalso cbool ( o2_i=0 )

		AxisParam_s = EMPTY : Exit Function

	case cbool ( o1_i>0 ) andalso cbool ( o2_i>o1_i )

		AxisParam_s = mid$ ( Buffer, o1_i+len ( open_s ), o2_i-o1_i-len ( open_s )) : Exit Function

	case else

		AxisParam_s = EMPTY : Exit Function

	end select

end function

function anicursor ( ) as string

	dim as long f=0

	f= ( fix ( timer*4 ) mod 4 ) + 1

	anicursor = mid ( "\|/-", f, 1 ) : Exit Function

end function

function prompt ( Label as string ) as string

	dim as string CI, Value

	CI = EMPTY : Value = EMPTY

	locate csrlin, pos

	Print Label;

	do

		CI = EMPTY

		do while len ( CI ) = 0

			Print anicursor ( );

			locate csrlin, pos-1

			CI = InKey ( )

			Sleep 1, 1

			Select Case CI
			Case ESC, MouseCloseEvent
				End 'End 'Outro ( )
			End Select
			
		loop

		select case CI

		case ESC 'escape

			return CI
		
		case MouseCloseEvent 'close window

			End

		case BKSP 'backspace
			
			if len ( Value )>0 then

				Value = left ( Value, len ( Value ) - 1 )

				locate csrlin, pos-1

				print " ";

				locate csrlin, pos-2

			end if

		case CR 'carriage return

			print " "

			return Value

		case is >= SP 'valid text

			Value &= CI

			Print CI;

		end select

	loop
	
	prompt = Value : Exit Function
	
end function

Function Str_Replace ( Needle as string, Replacement as string, Haystack as string ) as string

	dim as long Offset=0

	do

		Offset=strpos ( Needle, Haystack )

		if Offset=0 then exit do

		Haystack=Prefix ( Needle, Haystack ) & Replacement & Suffix ( Needle, Haystack )

	loop
	
	Str_Replace = Haystack : Exit Function

end function

Function PackText ( ByRef Buffer As String = EMPTY, Newline As String = EMPTY ) As String

	If Len ( Buffer ) > 0 Then
		Buffer &= CRLF
	End If

	Buffer &= Newline

	PackText = Buffer : Exit Function

End Function

Function PackTextReverse ( ByRef Buffer As String = EMPTY, Newline As String = EMPTY ) As String

	If Len ( Buffer ) > 0 Then

		Buffer = CRLF & Buffer

	End If

	Buffer = Newline & Buffer

	PackTextReverse = Buffer : Exit Function

End Function

Function NeedleCount ( Needle As String = EMPTY, Haystack As String = EMPTY ) As Long

	Dim As Long Count = 0

	Dim As String Buffer

	Buffer = EMPTY

	Do

		Buffer = Prefix ( Needle, Haystack )

		Haystack = Suffix ( Needle, Haystack )

		If Len ( Haystack ) > 0 Then

			Count += 1

		Else

			Exit Do

		End If

	Loop

	NeedleCount = Count : Exit Function

End Function

Function UCWords ( Subject As String ) As String

	Dim As String Value = EMPTY
	
	Do
	
		If Len ( Value ) > 0 Then
			Value &= SP
		End If
		
		Value &= UCase ( Left ( Prefix ( SP, Subject ) , 1 ) ) & LCase ( Mid ( Prefix ( SP, Subject ) , 2 ) )
		
		Subject = Suffix ( SP, Subject )
	
	Loop
	
	UCWords = Value : Exit Function

End Function

Sub Dump_Buffer ( Buffer As String )

	Dim As String TempPrefix = EMPTY
	Dim As String TempSuffix = Buffer
	
	Do
		
		TempPrefix = Prefix ( CRLF, TempSuffix )
		TempSuffix = Suffix ( CRLF, TempSuffix )
		
		If Len ( TempSuffix ) = 0 Then
			Exit Do
		End If
		
		Print TempPrefix
		
	Loop

End Sub

Function Load_Buffer ( FullFilename As String ) As String

	Dim As String Buffer
	Buffer = EMPTY
	
	If Is_File ( FullFilename ) = 0 Then

		Color RGBA ( 255, 255, 255, 255 )

		Print "Is not a file: " & QUOT & FullFilename & QUOT

		Buffer = EMPTY
		Load_Buffer = Buffer : Exit Function

	End If

	Dim As Long Filemode = Freefile

	If Filemode = 0 Then

		Buffer = EMPTY
		Load_Buffer = Buffer : Exit Function

	End If

	If Open ( FullFilename For Binary As #Filemode ) Then

		Close #Filemode

		Filemode = 0

		Buffer = EMPTY
		Load_Buffer = Buffer : Exit Function

	End If

	Buffer = Space ( Lof ( Filemode ) )

	Get #Filemode, 1, Buffer

	Close #Filemode

	Filemode = 0
	
	Load_Buffer = Buffer : Exit Function
	
End Function

