home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / t / tcsel003.zip / WRAP.PAS < prev   
Pascal/Delphi Source File  |  1992-12-12  |  3KB  |  86 lines

  1. var
  2.   S : string;
  3.  
  4. function Wrap(var st: string; maxlen: byte; justify: boolean): string;
  5.   { returns a string of no more than maxlen characters with the last   }
  6.   { character being the last space before maxlen. On return st now has }
  7.   { the remaining characters left after the wrapping.                  }
  8.   const
  9.     space = #32;
  10.   var
  11.     len      : byte absolute st;
  12.     x,
  13.     oldlen,
  14.     newlen   : byte;
  15.  
  16.   function JustifiedStr(s: string; max: byte): string;
  17.  
  18.     { Justifies string s left and right to length max. If there is more }
  19.     { than one trailing space, only the right most space is deleted. The}
  20.     { remaining spaces are considered "hard".  #255 is used as the char }
  21.     { used for padding purposes. This will enable easy removal in any   }
  22.     { editor routine.                                                   }
  23.  
  24.     const
  25.       softSpace = #255;
  26.     var
  27.       jstr      : string;
  28.       len       : byte absolute jstr;
  29.     begin
  30.       jstr := s;
  31.       while (jstr[1] = space) and (len > 0) do   { delete all leading spaces }
  32.         delete(jstr,1,1);
  33.       if jstr[len] = space then
  34.         dec(len);                                { Get rid of trailing space }
  35.       if not ((len = max) or (len = 0)) then begin
  36.         x := pos('.',jstr);     { Attempt to start padding at sentence break }
  37.         if (x = 0) or (x =len) then       { no period or period is at length }
  38.           x := 1;                                    { so start at beginning }
  39.         if pos(space,jstr) <> 0 then repeat        { ensure at least 1 space }
  40.           if jstr[x] = space then                      { so add a soft space }
  41.             insert(softSpace,jstr,x+1);
  42.           x := succ(x mod len);  { if eoln is reached return and do it again }
  43.         until len = max;        { until the wanted string length is achieved }
  44.       end; { if not ... }
  45.       JustifiedStr := jstr;
  46.     end; { JustifiedStr }
  47.  
  48.  
  49.   begin  { Wrap }
  50.     if len <= maxlen then begin                       { no wrapping required }
  51.       Wrap := st;
  52.       len  := 0;
  53.     end else begin
  54.       oldlen := len;                { save the length of the original string }
  55.       len    := succ(maxlen);                        { set length to maximum }
  56.       repeat                     { find last space in st before or at maxlen }
  57.         dec(len);
  58.       until (st[len] = space) or (len = 0);
  59.       if len = 0 then                   { no spaces in st, so chop at maxlen }
  60.         len := maxlen;
  61.       if justify then
  62.         Wrap := JustifiedStr(st,maxlen)
  63.       else
  64.         Wrap := st;
  65.       newlen :=  len;          { save the length of the newly wrapped string }
  66.       len := oldlen;              { and restore it to original length before }
  67.       Delete(st,1,newlen);              { getting rid of the wrapped portion }
  68.     end;
  69.   end; { Wrap }
  70.  
  71. begin
  72.   S :=
  73. 'By far the easiest way to manage a database is to create an '+
  74. 'index file. An index file can take many forms and its size will depend '+
  75. 'upon how many records you want in the db. The routines that follow '+
  76. 'assume no more than 32760 records.';
  77.  
  78. while length(S) <> 0 do
  79.   writeln(Wrap(S,60,true));
  80. end.
  81.  
  82. Whilst this is tested and known to work on the example string, no further
  83. testing than that has been done.  I suggest you test it a great deal more
  84. before being satisfied that it is OK.
  85.  
  86.