home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / pascal / zindent7.zip / ZINSTR.INC < prev    next >
Encoding:
Text File  |  1987-03-30  |  14.0 KB  |  610 lines

  1.  
  2.  
  3. (********************************************************************)
  4. (*                                                                  *)
  5. (*  Include File  STRING.INC                                        *)
  6. (*  Library of common string  PROCEDURES                            *)
  7. (*  v. 0800am, sun, 28.Mar.87, Glen Ellis                           *)
  8. (*                                                                  *)
  9. (********************************************************************)
  10.  
  11. (*---
  12.  
  13. Major routines summary :
  14.  
  15. pAllCaps (line) upper case full line
  16. pUpCaseFirst (line) upper case first word
  17.  
  18. pTrim* (line) simple trim spaces
  19. pTrim*Cnt (line,x) trim with counter
  20.  
  21. pPad* (line,len) simple pad spaces
  22. pPad*Cnt (line,cnt) pad with counter
  23.  
  24. pExpand* (line,chx,max) complex pad
  25. pShrink* (line,chx,max) complex trim
  26. pJust* (line,len)
  27.  
  28. pIndent complex required by KEYWORD
  29. pLineCount prefixes linecount str
  30.  
  31. pSayLnCJ (line,linelen);
  32. pSayLnLJ (line,linelen);
  33. pSayLnRJ (line,linelen);
  34.  
  35. pSayReadCJ (line,linelen,readlen);
  36. pSayReadLM (line,linelen,readlen);
  37.  
  38. pIndent() left margin restoration used by KeyWord procedures.
  39. pINDENT( var iLine : THEstr; iPos : integer; iMax : integer);
  40.  
  41. ---*)
  42.  
  43.  
  44. (********************************************************************)
  45.  
  46. procedure    pALLCAPS( var LINE : thestr );
  47.  
  48. var i : integer;
  49.  
  50. begin
  51.    FOR i := 1 to length(line)
  52.       do  Line[i] := upcase(Line[i]);
  53. end;
  54.  
  55.  
  56. (********************************************************************)
  57.  
  58. procedure    pUpCaseFirst( var LINE : thestr );
  59.  
  60. var i, max : integer;
  61.  
  62. begin
  63.    IF pos(' ',line) > 1 then max := pos(' ',line)
  64.    ELSE max := length(line);
  65.    FOR i := 1 to max
  66.       do  Line[i] := upcase(Line[i]);
  67. end;
  68.  
  69. (********************************************************************)
  70.  
  71. procedure pTrimL( var line : thestr);
  72.  
  73. (* line length is shortened *)
  74.  
  75. var
  76. byte : string1;
  77. len  : integer;
  78.  
  79. begin (* proc *)
  80.    IF length(line) > 1
  81.    then
  82.    begin
  83.       (* fetch byte on extreme left end *)
  84.       byte := Line[1];
  85.       (* trim left end <space> character, if len > 1 *)
  86.       while byte = ' ' do
  87.       begin
  88.          IF length(line) > 0
  89.          then
  90.          begin
  91.             delete(Line,1,1);
  92.             byte := Line[1];      (* next delete char *)
  93.          end
  94.          ELSE   (* force while loop to exit *)
  95.          byte := '.';
  96.       end; (* while *)
  97.    end; (* if *)
  98. end; (* proc *)
  99.  
  100.  
  101. (********************************************************************)
  102.  
  103. procedure pTrimR(var line : THEstr );
  104.  
  105. (* line length is shortened *)
  106.  
  107. var
  108. byte : string1;
  109. len  : integer;
  110.  
  111. begin (* proc *)
  112.    IF length(line) > 1
  113.    then
  114.    begin
  115.       (* fetch byte on extreme right end *)
  116.       len := length(Line);
  117.       byte := LINE[Len];
  118.       (* trim right end <space> character *)
  119.       WHILE (Byte = ' ') do
  120.       begin
  121.          IF length(line) > 0
  122.          then
  123.          begin
  124.             delete(Line,Len,1);
  125.             Len := length(Line);
  126.             Byte := Line[Len];
  127.          end
  128.          ELSE   (* force while loop to exit *)
  129.          byte := '.';
  130.       end; (* while *)
  131.    end; (* if *)
  132. end; (* proc *)
  133.  
  134.  
  135. (********************************************************************)
  136.  
  137. procedure pTrimLR( var LRLine : thestr );
  138.  
  139. (* trim left / trim right *)
  140. (* line length is shortened *)
  141.  
  142. var
  143. byte : string1;
  144. len  : integer;
  145.  
  146. begin (* proc *)
  147.    IF length(LRline) > 1 then
  148.    begin
  149.       pTrimR( LRLine );
  150.       pTrimL( LRLine );
  151.    end; (* if *)
  152. end; (* proc *)
  153.  
  154.  
  155. (********************************************************************)
  156.  
  157. procedure pTrimLCnt( var Line : thestr ; var Cnt : nbr );
  158.  
  159. (* trim left and count spaces *)
  160. (* line length is shortened *)
  161. (* Count is useful for restoring, or re-margining a text line. *)
  162.  
  163. var
  164. byte : string1;
  165. len  : integer;
  166.  
  167. begin (* proc *)
  168.    IF length(line) > 1
  169.    then
  170.    begin
  171.       (* fetch byte on extreme left end *)
  172.       byte := Line[1];
  173.       Cnt := 0;
  174.       (* trim left end <space> character, if len > 1 *)
  175.       WHILE byte = ' '
  176.       do
  177.       begin
  178.          IF length(line) > 0
  179.          then
  180.          begin
  181.             delete(Line,1,1);
  182.             byte := Line[1];      (* next delete char *)
  183.             Cnt := Cnt+1;
  184.          end
  185.          ELSE   (* force while loop to exit *)
  186.          byte := '.';
  187.       end; (* while *)
  188.    end; (* if *)
  189. end; (* proc *)
  190.  
  191.  
  192. (********************************************************************)
  193.  
  194. procedure pTrimRCnt(var Line : THEstr; var Cnt : nbr );
  195.  
  196. (* trim right and count spaces *)
  197. (* line length is shortened *)
  198. (* Count is usefile for restoring, or re-margining a text line. *)
  199.  
  200. var
  201. byte : string1;
  202. len  : integer;
  203.  
  204. begin (* proc *)
  205.    IF length(line) > 1
  206.    then
  207.    begin
  208.       (* fetch byte on extreme right end *)
  209.       len := length(Line);
  210.       byte := line[Len];
  211.       Cnt := 0;
  212.       (* trim right end <space> character *)
  213.       WHILE (Byte = ' ')
  214.       do
  215.       begin
  216.          IF length(line) > 0
  217.          then
  218.          begin
  219.             delete(Line,Len,1);
  220.             Len := length(Line);
  221.             Byte := Line[Len];
  222.             Cnt := Cnt+1;
  223.          end
  224.          ELSE   (* force while loop to exit *)
  225.          byte := '.';
  226.       end; (* while *)
  227.    end; (* if *)
  228. end; (* proc *)
  229.  
  230.  
  231.  
  232. (********************************************************************)
  233.  
  234. procedure pTrimLCntR( var LCRline : thestr ; var Cnt : nbr );
  235.  
  236. (* trim left and count spaces / trim right and without counting spaces *)
  237. (* line length is shortened *)
  238. (* called by KeyWord procedures *)
  239.  
  240. var
  241. byte : string1;
  242. len  : integer;
  243.  
  244. begin (* proc *)
  245.    IF length(LCRline) > 1
  246.    then
  247.    begin
  248.       pTrimR( LCRline );
  249.       pTrimLCnt( LCRline, Cnt );
  250.    end;
  251. end; (* proc *)
  252.  
  253.  
  254.  
  255. (********************************************************************)
  256.  
  257. procedure pPADL(var LINE : THEstr ; LEN : integer);
  258.  
  259. (* LINE  = incoming string to be altered
  260. (* LEN   = left margin length
  261. *)
  262.  
  263. var
  264. y : integer;
  265. mark : string1;
  266.  
  267. begin (* proc *)
  268.    mark   := ' ';
  269.    FOR y := 1 to len
  270.       do line := mark + line;
  271. end; (* proc *)
  272.  
  273.  
  274. (********************************************************************)
  275.  
  276. procedure pPADR(var LINE : THEstr ; LEN : integer);
  277.  
  278. (* LINE  := incoming string to be altered
  279. (* LEN   := right margin length
  280. *)
  281.  
  282. var
  283. y : integer;
  284. mark : string1;
  285.  
  286. begin (* proc *)
  287.    mark   := ' ';
  288.    FOR y := 1 to len
  289.       do line := line + mark;
  290. end; (* proc *)
  291.  
  292.  
  293. (***************************************************************************)
  294.  
  295. procedure pEXPANDL(var LINE :THEstr; CHX :string1; MAX :integer);
  296.  
  297. (* LINE   = incoming string to be altered
  298. (* CHX   = character to use
  299. (* MAX   = max length of expanded line
  300. *)
  301.  
  302. var
  303. y : integer;
  304.  
  305. begin (* proc *)
  306.    WHILE length(line) < max
  307.       do line := chx + line;
  308. end; (* proc *)
  309.  
  310.  
  311. (***************************************************************************)
  312.  
  313. procedure pEXPANDR(var LINE :THEstr; CHX :string1; MAX :integer);
  314.  
  315. (* LINE   = incoming string to be altered
  316. (* CHX   = character to use
  317. (* MAX   = max length of expanded line
  318. *)
  319.  
  320. var
  321. y : integer;
  322.  
  323. begin (* proc *)
  324.    WHILE length(line) < max
  325.       do line := line + chx;
  326. end; (* proc *)
  327.  
  328.  
  329. (********************************************************************)
  330.  
  331. procedure pSHRINKL(var LINE :THEstr; CHX :string1; MIN :integer);
  332.  
  333. (* shrink the line, not less than minimum length
  334. (* LINE   = incoming string to be altered
  335. (* CHX   = character to use
  336. (* MIN   = min length of shrinked line
  337. *)
  338.  
  339. begin (* proc *)
  340.    pTRIML(LINE);
  341.    pEXPANDL(LINE,CHX,min);
  342. end; (* proc *)
  343.  
  344.  
  345. (********************************************************************)
  346.  
  347. procedure pSHRINKR(var LINE :THEstr; CHX :string1; MIN :integer);
  348.  
  349. (* purpose : shrink line, not less than minimum length
  350. (* LINE   = incoming string to be altered
  351. (* CHX   = character to use
  352. (* MIN   = min length of shrinked line
  353. *)
  354.  
  355. begin (* proc *)
  356.    pTRIMR(LINE);
  357.    pEXPANDR(LINE,CHX,min);
  358. end; (* proc *)
  359.  
  360.  
  361. (********************************************************************)
  362.  
  363. procedure pJUSTL(var LINE :THEstr; LEN :integer);
  364.  
  365. begin (* proc *)
  366.    pTRIML(LINE);
  367.    pEXPANDR(LINE,' ',len);
  368. end; (* proc *)
  369.  
  370.  
  371. (********************************************************************)
  372.  
  373. procedure pJUSTR(var LINE :THEstr; LEN :integer);
  374.  
  375. begin (* proc *)
  376.    pTRIMR(LINE);
  377.    pEXPANDL(LINE,' ',len);
  378. end; (* proc *)
  379.  
  380.  
  381. (********************************************************************)
  382.  
  383. procedure pJUSTC(var LINE :THEstr; LEN :integer);
  384.  
  385. var
  386. x : integer;
  387.  
  388. begin (* proc *)
  389.    (* scalp the line *)
  390.    pTRIML(line);
  391.    pTRIMR(line);
  392.    (* calc left/right offset *)
  393.    x := ( ( len - length(line)  ) - 1 ) div 2 ;
  394.    (* half pad left, half pad right *)
  395.    pPADL(line,x);
  396.    pExpandR(line,' ',len);
  397. end; (* proc *)
  398.  
  399.  
  400. (* procedure ***************************************************************)
  401. (* v. 0200pm, wed, 17.Sep.86, Glen Ellis   *)
  402.  
  403. procedure pSayLnCJ( CJline : THEstr; Len : integer );
  404.  
  405. begin
  406.    pJustC(CJline,Len);
  407.    writeln(CJline);
  408. end;
  409.  
  410.  
  411. (* procedure ***************************************************************)
  412. (* v. 0200pm, wed, 17.Sep.86, Glen Ellis   *)
  413.  
  414. procedure pSayLnLJ( Line : THEstr; Len : integer );
  415.  
  416. begin
  417.    pJustL(Line,Len);
  418.    writeln(line);
  419. end;
  420.  
  421.  
  422. (* procedure ***************************************************************)
  423. (* v. 0200pm, wed, 17.Sep.86, Glen Ellis   *)
  424.  
  425. procedure pSayLnRJ( Line : THEstr; Len : integer );
  426.  
  427. begin
  428.    pJustR(Line,Len);
  429.    writeln(line);
  430. end;
  431.  
  432.  
  433. (* procedure ***************************************************************)
  434. (* v. 0900am, tue, 30.Sep.86, Glen Ellis   *)
  435.  
  436. procedure pSayReadLM( line : TheStr ; Mgn : integer );
  437.  
  438. (* called prior to Readln(xxx);   *)
  439.  
  440. begin
  441.    pTrimL(line);        (* trim off left *)
  442.    pTrimR(line);        (* trim off right side, READLN will fit here *)
  443.    pPadL(line,Mgn);     (* pad left (pseudo center justify *)
  444.    write(line);         (* open line, readln will close line *)
  445. end;
  446.  
  447.  
  448. (* procedure ***************************************************************)
  449. (* v. 0900am, tue, 30.Sep.86, Glen Ellis   *)
  450.  
  451. procedure pSayReadCJ ( line : Thestr ; linelen, readlen : integer );
  452.  
  453. (* called prior to ReadLn(xxx); *)
  454.  
  455. var
  456. mgn : integer;
  457.  
  458. begin
  459.    mgn := ( (linelen - length(line) - readlen ) div 2 )  ;
  460.    pTrimL(line);           (* trim off left *)
  461.    pTrimR(Line);           (* trim off right side, READLN will fit here *)
  462.    pPadL(line,(mgn));      (* pad left (pseudo center justify *)
  463.    write(Line);            (* open line, readln will close line *)
  464. end;
  465.  
  466.  
  467.  
  468. (* procedure ***************************************************************)
  469. (* v. 0700am, fri, 12.Sep.86, Glen Ellis   *)
  470.  
  471. procedure pINDENT( var iLine : THEstr; iPos : integer; iMax : integer);
  472.  
  473. (* *)
  474. (* author's developmental note to himself :
  475. (*
  476. (* similar to EXPANDL() with control for limit of lenMAX length.
  477. (*
  478. (* purpose :
  479. (* so dBASE2 command Lines do not scroll off screen.
  480. (*  ( dBASE-II Modify Command Editor will truncate lines greater than 80 ! )
  481. (*
  482. (*  Calling format from KEYWORD
  483. (*  pINDENT( ILINE, IPOS, ILenMax );
  484. (**)
  485.  
  486. (*  example as called from KEYDB2 :
  487. (*  Iline  = keyline = line string to altered
  488. (*  Ipos  = keyIpos = position of left margin , currently.
  489. (*  MAX  = lineMAX = max length of line
  490. (**)
  491.  
  492. var
  493. y : integer;
  494.  
  495. begin (* proc *)
  496.    (* reset begin/end errors *)
  497.    IF IPOS < 0 then
  498.    begin
  499.       iPos := 0;
  500.       writeln('<----------  Too Many Ends !',chr(7));
  501.    end;
  502.    FOR y := 1 to iPos do
  503.    begin
  504.       (* if SysIndTrace then write(':',y); *)
  505.       IF (length(iLine) < iMax) then
  506.       iLine := ' ' + iLine;
  507.    end;
  508. end; (* proc *)
  509.  
  510. (********************************************************************)
  511.  
  512. procedure pLineCount(var LINE : THEstr; var NUM : integer);
  513.  
  514. (* purpose : prefix line number count
  515. (*
  516. (* as called by SYSTEM.PAS :
  517. (*
  518. (* LINE  = SysOutStr
  519. (* NUM  = SysLineNum
  520. *)
  521.  
  522. var
  523. Cnt3 : string3;
  524.  
  525. begin (* proc *)
  526.    Num := Num + 1;
  527.    str(Num,Cnt3);
  528.    Line := Cnt3 + ': ' + Line
  529. end; (* proc *)
  530.  
  531.  
  532. (********************************************************************)
  533.  
  534. procedure P_NOHIBIT(var HIBITline:string255);
  535.  
  536. (* author's develpmental note to himself :
  537. (*
  538. (* purpose :
  539. (*    replaces hibit ascii.
  540. (*    used for text error correction.
  541. (*
  542. (* not tested.
  543. (* could be used for additional ZINDENT function.
  544. (* wrote one similar to this in ZFIND5.PAS.
  545. (* line length maintained.
  546. (**)
  547.  
  548. var
  549. I : integer;
  550. WLine : THEstr;
  551. WLineLen : nbr;
  552.  
  553. begin (* procedure *)
  554.    Wline    := HIBITline ;
  555.    Wlinelen := length(Wline);
  556.    FOR I := 1 to Wlinelen do
  557.    begin
  558.       IF ord(Wline[I]) > 127 then
  559.       begin
  560.          Wline[I] := chr(ord(Wline[I])-128);
  561.       end;
  562.    end;
  563.    (* return this parameter *)
  564.    HIBITline := Wline ;
  565. end; (* procedure *)
  566.  
  567.  
  568. (********************************************************************)
  569.  
  570. procedure P_NOCTRL(var Cline:string255);
  571.  
  572. (* author's develpmental note to himself :
  573. (*
  574. (* purpose :
  575. (*    delete control characters.
  576. (*
  577. (* not tested , needs development.
  578. (* could be used for additional ZINDENT function.
  579. (* wrote one similar to this in ZFIND5.PAS.
  580. (* line length maintained.
  581. (**)
  582.  
  583. var
  584. I           : integer;
  585. str1, str2  : string255;
  586. Clinelen    : integer;
  587. Wline       : string255;
  588.  
  589. begin (* proc *)
  590.    Wline    := Cline ;
  591.    Clinelen := length(Cline);
  592.    FOR I := 1 to Clinelen do
  593.    begin
  594.       (* trap control character *)
  595.       IF ord(Wline[I]) < ord(' ') then
  596.       begin
  597.          (* delete control character *)
  598.          str1 := copy(Cline,1,I-1);
  599.          str2 := copy(Cline,I+1,Clinelen-I);
  600.          (* generate revised workline *)
  601.          Wline := str1 + str2 ;
  602.          i := i-1;
  603.       end;
  604.    end;
  605.    (* return this parameter *)
  606.    Cline := Wline ;
  607. end; (* proc *)
  608.  
  609. (*******************************************************************)
  610.  
  611. (*<<<>>>*)