From: jzawodn@bgsu.edu (jeremy zawodny)
Subject: Help needed in writing a word-wrap function in a unit (BP 7.0)
Summary: why doesn't this work?
Keywords: pascal, word, bp7.0
Organization: Bowling Green State Univ.
Distribution: na
Date: Wed, 23 Dec 1992 02:15:48 GMT

Hey Netters!  Now that all the crap has cleared up about my encryption
problem, I have a new problem...

I'm using Borland Pascal w/ Objects 7.0 to write a function (in a unit so
that other programs can use it) that takes a null-terminated string as a
parameter and retuns a new-null terminated string that is formatted
differently.  The new string should have returns stuck in the proper
location so that when it is displayed on a screen of width x, none of the
words will be split at the end of a line.  Basically, that's word-wrap.

I've made a few attempts at it, none of which have been successful.  I
have appended my most recent trial.  I'd really appreciate it if somebody
could look it over and tell me what's wrong with it.  Right now, if I pass
it a string of length 90 or so to be formatted for a 80 column output, it
sends crap.

Ideally, the formatted string should be able to display with just a
writeln statement.

Thanks for any help or suggestions.

Jeremy

----------------------------begin function here----------------------------

uses strings;

function WrapString(OldArray : PChar; MaxLine : integer) : PChar;
         var
            OldArrayPos, OldArrayLen, CWord, NewArrayPos, x : integer;
            inchar : char;
            NewArray : Array[0..8192] of char;
 
         begin
              OldArrayLen := strlen(OldArray);
              OldArrayPos := 0;  NewArrayPos := 0;  CWord := 0;
              while (OldArrayPos <= OldArrayLen) do
                    begin
                         CWord := 0;
                         inchar := OldArray[OldArrayPos];
                         while (inchar <> ' ') and (inchar <> #00) do
                               begin
                                    CWord := CWord+1;
                                    OldArrayPos := OldArrayPos+1;
                                    inchar := OldArray[OldArrayPos];
                               end;
                         if (OldArrayPos) < MaxLine then
                            begin
                                 for x:=1 to CWord do
                                     begin
                                          NewArray[NewArrayPos] :=
OldArray[(OldArrayPos - CWord - 1 + x)];
                                          NewArrayPos := NewArrayPos + 1;
                                     end;
                                 NewArray[NewArrayPos] := ' '; 
Inc(NewArrayPos);
                                 Inc (OldArrayPos);
                            end
                         else
                             begin
                                  NewArray[NewArrayPos] := #10; 
Inc(NewArrayPos);
                                  NewArray[NewArrayPos] := #13; 
Inc(NewArrayPos);
                                  for x:=1 to CWord do
                                     begin
                                          NewArray[NewArrayPos] :=
OldArray[(OldArrayPos - CWord - 1 + x)];
                                          NewArrayPos := NewArrayPos + 1;
                                     end;
                                  NewArray[NewArrayPos] := ' '; 
Inc(NewArrayPos);
                                  Inc (OldArrayPos);
                             end;
                    end;
              end;

-------------------------------end function here-------------------------




-- 
------------------------------------------------------------------------------
Jeremy Daniel Zawodny                           Computer Science Undergraduate
                      Bowling Green State University
------------------------------------------------------------------------------