!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! STRFNC.H V1.5 by Jason Peter Brown
! Additional String Functions for Hugo v2.4+
!
! Licence:
!
! Freely distributable, modifiable, and usable.
! No Warranty expressed or implied.
! Bug Reports to: yu219719@yorku.ca
!
! Usage:
!
! x=right$(source, chars)
!
! Where source is the array from which you want to pull the rightmost
! characters, and chars are the number of characters that you want to
! return.
!
! x = right$(source, chars, replacement)
!
! Where source is the array into which you want to replace characters,
! chars is the number of characters to replace (moving from the right)
! and replacement is the replacement array. This routine replaces
! text only up to the end of the source string, so longer replacement
! strings will be clipped.
!
! x = left$(source, chars)
!
! Where source is the array from which you want to pull the leftmost
! characters, and chars are the number of characters that you want to
! return.
!
! x = left$(source, chars, replacement)
!
! Where source is the array into which you want to replace characters,
! chars are the number of characters to replace, and replacement is
! the array that will replace text in source. Text in the source
! array can only be replaced up to the original length of source, so
! longer replacements will be clipped.
!
! x= mid$(source, offset, chars)
!
! Where source is the array from which you want to extract characters,
! offset is the number of characters from theleft to skip, and chars
! is the number of characters that you wish to return from (offset+1)
!
! x= mid$(source, offset, chars, replacement)
!
! Where source is the array into which you want to replace characters,
! offset is the number of characters to skip before beginning replacement,
! chars is the number of characters to replace, and replacement is an
! array that will replace the characters from (offset+1) to (offset+1+chars)
!
! x= add$(string1, string2)
!
! Adds the contents of two arrays together into a single array.
!
! x = search$(source, search , [offset])
!
! Searches for the first occurence of search in source. Returns the string
! position in source of the beginning of the occurence, or returns false
! if the string is not matched. If included, offset begins the search
! at source+offset.
!
! PrintCenter(string, vert)
!
! prints centered text (string) horizontally. If vert is true, then text is
! also centered vertically.
!
! PrintRight(string)
!
! Prints right justified text (string).
!
! Enjoy!

array _search_string[256]

routine right$(source, chars, replacement)
{
 local tempA
 local tempB
 tempA = string (_temp_string, source, 255)
 tempB = tempA - chars
 if replacement
 {
  string (array _temp_string, array source, tempB)
  _temp_string[tempB] = 1
  string ((array _temp_string+tempB), (array replacement), (chars))
  _temp_string[tempA] = 0
 }
 elseif (tempA > chars)
 {
  string (_temp_string, source+tempB, chars)
 }
 return _temp_string
}

routine left$(source, chars, replacement)
{
 local tempA
 
 tempA = string(_temp_string, source, 255)
 if replacement
 {
  string (array _temp_string, array replacement, chars)
  _temp_string[chars] = 1
  string ((array _temp_string+chars), (array source+chars), (tempA-chars))
  _temp_string[tempA] = 0
 }
 elseif (tempA >= chars)
 {
  string(_temp_string, source, chars)
 }
 return _temp_string
}

routine mid$(source , offset, chars, replacement)
{
 local tempA
 
 tempA = string(_temp_string, source, 255)
 if replacement
 {
  string (array _temp_string, array source, (offset))
  _temp_string[offset] = 1
  string ((array _temp_string + offset), array replacement, chars)
  string ((array _temp_string +offset+chars), (array source+offset+chars), (tempA - 1))
  _temp_string[tempA] = 0
 }
 elseif (tempA >= offset) and (tempA >= (offset+chars))
 {
  string (_temp_string, (source+offset), chars)
 }
 elseif (tempA >= offset) and (tempA < (offset+chars))
 {
  string (_temp_string, (source+offset), (tempA-offset))
 }
 return _temp_string
}

routine Add$(firststring, secondstring)
{
 local tempA , tempB
 tempA = string(_temp_string, firststring, 255)
 tempB = string(_temp_string, secondstring, 255)
 if tempA + tempB > 255
 {
  "[OVERFLOW ERROR IN ROUTINE ADD$]"
  return true
 }
 string(_temp_string, firststring, tempA)
 string(_temp_string+tempA, secondstring,tempB)
 _temp_string[tempA+tempB] = 0
 return _temp_string
}

routine Search$(source, search, offset)
{
 local tempA , tempB, tempC , tempD
 tempA = string(_temp_string, source+offset, 255)
 tempB = string(_search_string, search, 255)
 if not tempB or not tempA
 {
  return 0
 }
 elseif tempB > tempA
 {
  return 0
 }
 for (tempC = 0 ; tempC <= (tempA-tempB) ; tempC ++)
 {
  string(array _temp_string, (array source+offset+tempC), tempB)
  tempD = StringCompare (array _temp_string , array _search_string)
  if not tempD
  {
   return tempC+1+offset
  }
 }
 return 0
}

routine PrintCenter(source, vert)
{
 local l
 if vert
 {
  locate 1 , (display.windowlines/2)
 }
 Font(PROP_OFF)
 l = string(_temp_string, source)
 print to ((display.linelength/2 - l/2));source;to (display.linelength)
 FONT(DEFAULT_FONT)
}

routine PrintRight(source)
{
 local l
 Font(PROP_OFF)
 l = string(_temp_string, source)
 print to (display.linelength - l);source
 FONT(DEFAULT_FONT)
}
