{***********************************************************************}
{									}
{ __MidStr    Place characters within a string				}
{									}
{ function  __MidStr(Source, Target : string; ChPos : word) : string;	}
{									}
{	      Source	The string to insert in the target string.	}
{	      Target	The string in which the source string is placed.}
{	      ChPos	The character position within Target at which	}
{			the source string is inserted.			}
{									}
{ Description __MidStr returns a string with the string Source inserted }
{	      in Target beginning at character position ChPos.	The	}
{	      original strings Source and Target are not altered.  If	}
{	      ChPos is less than 1, Source is concatenated to the front }
{	      of Target, and if ChPos is greater than the length of	}
{	      Target, Source is concatenated to the end of Target.  If	}
{	      the resulting string has length greater than the maximum	}
{	      String length (MAXSTR_), the left most MAXSTR_ characters }
{	      are returned.						}
{									}
{ Version     4.00 (C)Copyright Blaise Computing Inc.  1987		}
{_______________________________________________________________________}

function  __MidStr(Source, Target : string; ChPos : word) : string;

  var
    NewTarget : string;
    NewTarLen : byte;
    NewLen    : word;
    NewStrPtr : ^_MemoryChar;
    LenSource : word;
    LenTarget : word;

  begin {__MidStr}

    { First allocate storage to hold the "combined" strings.  By	}
    { allocating an array of characters (rather than a string), we	}
    { do not need to worry about the restrictions on string lengths.	}

    LenSource := Length(Source);
    LenTarget := Length(Target);
    NewLen    := LenSource + LenTarget;
    GetMem(NewStrPtr,NewLen);

    if (ChPos < 1) then 	       { Force reasonable values for	}
       ChPos := 1		       { ChPos, the position to insert. }
    else if (ChPos > LenTarget) then
       ChPos := LenTarget + 1;

    { Now just move the strings into the array template, and then re-	}
    { turn the maximum allowable.					}

    Move(Target[1],NewStrPtr^[1],ChPos - 1);
    Move(Source[1],NewStrPtr^[ChPos],LenSource);
    Move(Target[ChPos],NewStrPtr^[ChPos + LenSource],
					  LenTarget - ChPos + 1);
    if (NewLen > MAXSTR_) then
       Length(NewTarget) := MAXSTR_
    else
       Length(NewTarget) := byte(NewLen);

    Move(NewStrPtr^,NewTarget[1],Length(NewTarget));
    __MidStr := NewTarget;

    FreeMem(NewStrPtr,NewLen)

  end;	{__MidStr}
