home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1989 / 09 / dunteman.lst < prev    next >
File List  |  1989-07-27  |  17KB  |  584 lines

  1. _STRUCTURED PROGRAMMING COLUMN_
  2. by Jeff Duntemann
  3.  
  4. [LISTING ONE]
  5.  
  6. {--------------------------------------------------------------}
  7. {                          PrinByte                            }
  8. {                                                              }
  9. {    Byte-value print object for object extendability demo     }
  10. {                                                              }
  11. {                                    by Jeff Duntemann         }
  12. {                                    Turbo Pascal V5.5         }
  13. {                                    Last modified 5/11/89     }
  14. {--------------------------------------------------------------}
  15.  
  16.  
  17. UNIT PrinByte;
  18.  
  19. INTERFACE
  20.  
  21. USES Printer;
  22.  
  23.  
  24. {--------------------------------------------------------------}
  25. { The PrintByte object "knows" how to print its byte value in  }
  26. { four different formats:  As decimal quantity, hex quantity,  }
  27. { binary quantity, and extended ASCII symbol.  It takes the    }
  28. { cautious path in printing symbols, as prints spaces for all  }
  29. { of the "control" characters 0-31, "rubout" ($7F), and char   }
  30. { $FF.  If your printer has some means of printing smiley      }
  31. { faces and all that other folderol in the low 32 characters,  }
  32. { override the PrintSymbol method with a new method that uses  }
  33. { whatever mechanism your printers offers to print those       }
  34. { characters "spaced out" by this implementation of the        }
  35. { PrintSymbol method.                                          }
  36. {--------------------------------------------------------------}
  37.  
  38. TYPE
  39.   PrintByte   = OBJECT
  40.                   BV : Byte;     { Byte value }
  41.                   FUNCTION  DontPrint(ShouldI : Byte) : Boolean;
  42.                   PROCEDURE PrintDecimal;
  43.                   PROCEDURE PrintHex;
  44.                   PROCEDURE PrintBinary;
  45.                   PROCEDURE PrintSymbol;
  46.                 END;
  47.  
  48.  
  49. IMPLEMENTATION
  50.  
  51.  
  52. { Returns True for any code that CANNOT be printed verbatim }
  53.  
  54. FUNCTION PrintByte.DontPrint(ShouldI : Byte) : Boolean;
  55.  
  56. CONST
  57.   NotThese = [0..31,127,255];  { Unprintable codes }
  58.  
  59. BEGIN
  60.   IF ShouldI IN NotThese THEN DontPrint := True
  61.     ELSE DontPrint := False;
  62. END;
  63.  
  64.  
  65.  
  66. PROCEDURE PrintByte.PrintDecimal;
  67.  
  68. BEGIN
  69.   Write(LST,BV:3);
  70. END;
  71.  
  72.  
  73.  
  74. PROCEDURE PrintByte.PrintHex;
  75.  
  76. CONST
  77.   HexDigits : ARRAY[0..15] OF Char = '0123456789ABCDEF';
  78.  
  79. BEGIN
  80.   Write(LST,HexDigits[BV SHR 4],HexDigits[BV AND $0F]);
  81. END;
  82.  
  83.  
  84.  
  85. PROCEDURE PrintByte.PrintBinary;
  86.  
  87. VAR
  88.   Index : Integer;
  89.  
  90. BEGIN
  91.   FOR Index := 7 DOWNTO 0 DO
  92.     IF Odd(BV SHR Index) THEN Write(LST,'1')
  93.       ELSE Write(LST,'0');
  94. END;
  95.  
  96.  
  97.  
  98. PROCEDURE PrintByte.PrintSymbol;
  99.  
  100. BEGIN
  101.   IF DontPrint(BV) THEN
  102.     Write(LST,' ')
  103.   ELSE
  104.     Write(LST,Chr(BV));
  105. END;
  106.  
  107.  
  108. END.
  109.  
  110.  
  111. [LISTING TWO]
  112.  
  113. {--------------------------------------------------------------}
  114. {                          ASCIIChart                          }
  115. {                                                              }
  116. {   Object Extendability demo program: Prints an ASCII chart   }
  117. {                                                              }
  118. {   Note: This program contains control codes specific to      }
  119. {         the HP Laserjet Series II Printer.  Use with         }
  120. {         other printers may be hazardous to your aesthetic    }
  121. {         sensibilities.                                       }
  122. {                                                              }
  123. {                                    Jeff Duntemann            }
  124. {                                    Turbo Pascal V5.5         }
  125. {                                    Last modified 6/8/89      }
  126. {--------------------------------------------------------------}
  127.  
  128.  
  129.  
  130. {--------------------------------------------------------------}
  131. { Run this on an HP Laserjet Series II, and you'll get an      }
  132. { ASCII chart including all the weird characters for which the }
  133. { Series II has characters in its built-in fonts.  If you have }
  134. { some other printer, you need only modify the PrintSymbol     }
  135. { method to use whatever mechanism your printer has to print   }
  136. { the weird characters.  By modifying PrintSymbol you are      }
  137. { extending the object PrintByte defined in unit PRINBYTE.PAS, }
  138. { WITHOUT needing full source code to PRINBYTE.PAS.            }
  139. {--------------------------------------------------------------}
  140.  
  141.  
  142.  
  143. PROGRAM ASCIIChart;
  144.  
  145.  
  146. USES Printer,    { Standard Borland unit }
  147.      PrinByte;   { PRINBYTE.PAS from DDJ for September 1989 }
  148.  
  149.  
  150. CONST
  151.   Title    = 'THE EXTENDED ASCII CODE AND SYMBOL SET';
  152.   Header   = '   Dec  Hex    Binary  Symbol     Dec  Hex    Binary  Symbol';
  153.   BarChar  = Chr(205);
  154.   ColChar  = Chr(177);
  155.   FormFeed = Chr(12);
  156.  
  157.  
  158. TYPE
  159.  
  160. {---------------------------------------------------------------}
  161. { This is a child object of PrintByte, defined in separate file }
  162. { PRINBYTE.PAS.  Remember:  PrintByteHP inherits EVERYTHING     }
  163. { defined in PrintByte.  EVERYTHING!  The only difference is    }
  164. { that the PrintSymbol method is overridden by an HP Series II  }
  165. { specific symbol-print method.  Everything else is EXACTLY as  }
  166. { it is defined in PrintByte.                                   }
  167. {---------------------------------------------------------------}
  168.  
  169.   PrintByteHP = OBJECT(PrintByte)
  170.                   PROCEDURE PrintSymbol
  171.                 END;
  172.  
  173.  
  174. VAR
  175.   I,J,K       : Integer;
  176.   Char1,Char2 : PrintByteHP;  { Instances of PrintByteHP object type }
  177.  
  178.  
  179. {--------------------------------------------------------------}
  180. { This method overrides the PrintSymbol method defined in the  }
  181. { PrintByte parent object defined in PRINCHAR.PAS:             }
  182. {--------------------------------------------------------------}
  183.  
  184. PROCEDURE PrintByteHP.PrintSymbol;
  185.  
  186. BEGIN
  187.   IF DontPrint(BV) THEN                { If DontPrint method says so, }
  188.     Write(LST,Chr(27),'&p1X',Chr(BV))  { use "transparent data print" }
  189.   ELSE                                 { feature to print weird chars }
  190.     Write(LST,Chr(BV));                { Otherwise, just print 'em... }
  191. END;
  192.  
  193.  
  194.  
  195. PROCEDURE Spaces(NumberOfSpaces : Integer);
  196.  
  197. VAR
  198.   Index : Integer;
  199.  
  200. BEGIN
  201.   FOR Index := 1 TO NumberOfSpaces DO
  202.     Write(LST,' ')
  203. END;
  204.  
  205.  
  206.  
  207. PROCEDURE NewPage;
  208.  
  209. VAR
  210.   Index : Integer;
  211.  
  212. BEGIN
  213.   Write(LST,FormFeed);
  214. END;
  215.  
  216.  
  217.  
  218. PROCEDURE PrintHeader;
  219.  
  220. VAR
  221.   Index : Integer;
  222.  
  223. BEGIN
  224.   Spaces(10);
  225.   Writeln(LST,Header);
  226.   Spaces(10);
  227.   FOR Index := 1 TO 60 DO
  228.     Write(LST,BarChar);
  229.   Writeln(LST);
  230. END;
  231.  
  232.  
  233.  
  234. {--------------------------------------------------------------}
  235. { Nothing more elaborate here than printing the byte out as a  }
  236. { decimal number, a hex number, a binary number, and a symbol. }
  237. { The four "Target." calls are method calls to the object      }
  238. { passed to PrintChar in the Target parameter.                 }
  239. {--------------------------------------------------------------}
  240.  
  241. PROCEDURE PrintChar(Target : PrintByteHP);
  242.  
  243. BEGIN
  244.   Spaces(3);
  245.   Target.PrintDecimal;
  246.   Spaces(3);
  247.   Target.PrintHex;
  248.   Spaces(3);
  249.   Target.PrintBinary;
  250.   Spaces(3);
  251.   Target.PrintSymbol;
  252.   Spaces(4);
  253. END;
  254.  
  255.  
  256. {--------------------------------------------------------------}
  257. { This simply sets the HP Series II to its PC symbol set.      }
  258. {--------------------------------------------------------------}
  259.  
  260. PROCEDURE InitHP;
  261.  
  262. BEGIN
  263.   Write(LST,Chr(27),'(10U');        { Select symbol set 10U }
  264. END;
  265.  
  266.  
  267.  
  268.  
  269. BEGIN
  270.   InitHP;                           { Select the PC symbol set }
  271.   Spaces(10);                       { Output 10 spaces }
  272.   Writeln(LST,Title);               { Print out the title string }
  273.   Writeln(LST);                     { Space down one line }
  274.   FOR I := 0 TO 3 DO                { 256 characters takes 4 pages }
  275.     BEGIN
  276.       FOR K := 1 TO 3 DO Writeln(LST);  { Space down 3 lines }
  277.       PrintHeader;                      { Print the chart header }
  278.       FOR J := 0 TO 31 DO               { Do 2 columns of 32 chars. }
  279.         BEGIN
  280.           Char1.BV := (I*64)+J; Char2.BV := (I*64)+J+32;
  281.           Spaces(10);
  282.           PrintChar(Char1);         { Print the left column character }
  283.           Write(LST,ColChar);       { Print the column separator      }
  284.           PrintChar(Char2);         { Print the right column character }
  285.           Writeln(LST);
  286.         END;
  287.       NewPage;                      { Issue a form feed to printer }
  288.     END;
  289. END.
  290.  
  291.  
  292. [LISTING THREE]
  293.  
  294. {--------------------------------------------------------------}
  295. {                          PrinByte                            }
  296. {                                                              }
  297. {    Byte-value print object for object extendibility demo     }
  298. {                                                              }
  299. {                                    by Jeff Duntemann         }
  300. {                                    QuickPascal V1.0          }
  301. {                                    Last modified 6/6/89      }
  302. {--------------------------------------------------------------}
  303.  
  304.  
  305. UNIT PrinByte;
  306.  
  307. INTERFACE
  308.  
  309. USES Printer;
  310.  
  311.  
  312. {--------------------------------------------------------------}
  313. { The PrintByte object "knows" how to print its byte value in  }
  314. { four different formats:  As decimal quantity, hex quantity,  }
  315. { binary quantity, and extended ASCII symbol.  It takes the    }
  316. { cautious path in printing symbols, as prints spaces for all  }
  317. { of the "control" characters 0-31, "rubout" ($7F), and char   }
  318. { $FF.  If your printer has some means of printing smiley      }
  319. { faces and all that other folderol in the low 32 characters,  }
  320. { override the PrintSymbol method with a new method that uses  }
  321. { whatever mechanism your printers offers to print those       }
  322. { characters "spaced out" by this implementation of the        }
  323. { PrintSymbol method.                                          }
  324. {--------------------------------------------------------------}
  325.  
  326. TYPE
  327.   PrintByte   = OBJECT
  328.           BV : Byte;     { Byte value }
  329.           FUNCTION  DontPrint(ShouldI : Byte) : Boolean;
  330.           PROCEDURE PrintDecimal;
  331.           PROCEDURE PrintHex;
  332.           PROCEDURE PrintBinary;
  333.           PROCEDURE PrintSymbol;
  334.         END;
  335.  
  336.  
  337. IMPLEMENTATION
  338.  
  339.  
  340. { Returns True for any code that CANNOT be printed verbatim }
  341.  
  342. FUNCTION PrintByte.DontPrint(ShouldI : Byte) : Boolean;
  343.  
  344. CONST
  345.   NotThese = [0..31,127,255];  { Unprintable codes }
  346.  
  347. BEGIN
  348.   IF ShouldI IN NotThese THEN DontPrint := True
  349.     ELSE DontPrint := False;
  350. END;
  351.  
  352.  
  353.  
  354. PROCEDURE PrintByte.PrintDecimal;
  355.  
  356. BEGIN
  357.   Write(LST,Self.BV:3);
  358. END;
  359.  
  360.  
  361.  
  362. PROCEDURE PrintByte.PrintHex;
  363.  
  364. CONST
  365.   HexDigits : ARRAY[0..15] OF Char = '0123456789ABCDEF';
  366.  
  367. BEGIN
  368.   WITH Self DO
  369.     Write(LST,HexDigits[BV SHR 4],HexDigits[BV AND $0F]);
  370. END;
  371.  
  372.  
  373.  
  374. PROCEDURE PrintByte.PrintBinary;
  375.  
  376. VAR
  377.   Index : Integer;
  378.  
  379. BEGIN
  380.   WITH Self DO
  381.     FOR Index := 7 DOWNTO 0 DO
  382.       IF Odd(BV SHR Index) THEN Write(LST,'1')
  383.     ELSE Write(LST,'0');
  384. END;
  385.  
  386.  
  387.  
  388. PROCEDURE PrintByte.PrintSymbol;
  389.  
  390. BEGIN
  391.   WITH Self DO
  392.     IF DontPrint(BV) THEN
  393.       Write(LST,' ')
  394.     ELSE
  395.       Write(LST,Chr(BV));
  396. END;
  397.  
  398.  
  399. END.
  400.  
  401.  
  402. [LISTING FOUR]
  403.  
  404. {--------------------------------------------------------------}
  405. {                          ASCIIChart                          }
  406. {                                                              }
  407. {   Object Extendability demo program: Prints an ASCII chart   }
  408. {                                                              }
  409. {   Note: This program contains control codes specific to      }
  410. {         the HP Laserjet Series II Printer.  Use with         }
  411. {         other printers may be hazardous to your aesthetic    }
  412. {         sensibilities.                                       }
  413. {                                                              }
  414. {                                    Jeff Duntemann            }
  415. {                                    QuickPascal V1.0          }
  416. {                                    Last modified 6/6/89      }
  417. {--------------------------------------------------------------}
  418.  
  419.  
  420.  
  421. {--------------------------------------------------------------}
  422. { Run this on an HP Laserjet Series II, and you'll get an      }
  423. { ASCII chart including all the weird characters for which the }
  424. { Series II has characters in its built-in fonts.  If you have }
  425. { some other printer, you need only modify the PrintSymbol     }
  426. { method to use whatever mechanism your printer has to print   }
  427. { the weird characters.  By modifying PrintSymbol you are      }
  428. { extending the object PrintByte defined in unit PRINBYTE.PAS, }
  429. { WITHOUT needing full source code to PRINBYTE.PAS.            }
  430. {--------------------------------------------------------------}
  431.  
  432.  
  433.  
  434. PROGRAM ASCIIChart;
  435.  
  436.  
  437. USES Printer,    { Standard Borland unit }
  438.      PrinByte;   { PRINBYTE.PAS from DDJ for September 1989 }
  439.  
  440.  
  441. CONST
  442.   Title    = 'THE EXTENDED ASCII CODE AND SYMBOL SET';
  443.   Header   = '   Dec  Hex    Binary  Symbol     Dec  Hex    Binary  Symbol';
  444.   BarChar  = Chr(205);
  445.   ColChar  = Chr(177);
  446.   FormFeed = Chr(12);
  447.  
  448.  
  449. TYPE
  450.  
  451. {---------------------------------------------------------------}
  452. { This is a child object of PrintByte, defined in separate file }
  453. { PRINBYTE.PAS.  Remember:  PrintByteHP inherits EVERYTHING     }
  454. { defined in PrintByte.  EVERYTHING!  The only difference is    }
  455. { that the PrintSymbol method is overridden by an HP Series II  }
  456. { specific symbol-print method.  Everything else is EXACTLY as  }
  457. { it is defined in PrintByte.                                   }
  458. {---------------------------------------------------------------}
  459.  
  460.   PrintByteHP = OBJECT(PrintByte)
  461.           PROCEDURE PrintSymbol; OVERRIDE
  462.         END;
  463.  
  464.  
  465. VAR
  466.   I,J,K       : Integer;
  467.   Char1,Char2 : PrintByteHP;  { Instances of PrintByteHP object type }
  468.  
  469.  
  470. {--------------------------------------------------------------}
  471. { This method overrides the PrintSymbol method defined in the  }
  472. { PrintByte parent object defined in PRINCHAR.PAS:             }
  473. {--------------------------------------------------------------}
  474.  
  475. PROCEDURE PrintByteHP.PrintSymbol;
  476.  
  477. BEGIN
  478.   WITH Self DO
  479.     IF Self.DontPrint(BV) THEN           { If DontPrint method says so, }
  480.       Write(LST,Chr(27),'&p1X',Chr(BV))  { use "transparent data print" }
  481.     ELSE                                 { feature to print weird chars }
  482.       Write(LST,Chr(BV));                { Otherwise, just print 'em... }
  483. END;
  484.  
  485.  
  486.  
  487. PROCEDURE Spaces(NumberOfSpaces : Integer);
  488.  
  489. VAR
  490.   Index : Integer;
  491.  
  492. BEGIN
  493.   FOR Index := 1 TO NumberOfSpaces DO
  494.     Write(LST,' ')
  495. END;
  496.  
  497.  
  498.  
  499. PROCEDURE NewPage;
  500.  
  501. VAR
  502.   Index : Integer;
  503.  
  504. BEGIN
  505.   Write(LST,FormFeed);
  506. END;
  507.  
  508.  
  509.  
  510. PROCEDURE PrintHeader;
  511.  
  512. VAR
  513.   Index : Integer;
  514.  
  515. BEGIN
  516.   Spaces(10);
  517.   Writeln(LST,Header);
  518.   Spaces(10);
  519.   FOR Index := 1 TO 60 DO
  520.     Write(LST,BarChar);
  521.   Writeln(LST);
  522. END;
  523.  
  524.  
  525.  
  526. {--------------------------------------------------------------}
  527. { Nothing more elaborate here than printing the byte out as a  }
  528. { decimal number, a hex number, a binary number, and a symbol. }
  529. { The four "Target." calls are method calls to the object      }
  530. { passed to PrintChar in the Target parameter.                 }
  531. {--------------------------------------------------------------}
  532.  
  533. PROCEDURE PrintChar(Target : PrintByteHP);
  534.  
  535. BEGIN
  536.   Spaces(3);
  537.   Target.PrintDecimal;
  538.   Spaces(3);
  539.   Target.PrintHex;
  540.   Spaces(3);
  541.   Target.PrintBinary;
  542.   Spaces(3);
  543.   Target.PrintSymbol;
  544.   Spaces(4);
  545. END;
  546.  
  547.  
  548. {--------------------------------------------------------------}
  549. { This simply sets the HP Series II to its PC symbol set.      }
  550. {--------------------------------------------------------------}
  551.  
  552. PROCEDURE InitHP;
  553.  
  554. BEGIN
  555.   Write(LST,Chr(27),'(10U');        { Select symbol set 10U }
  556. END;
  557.  
  558.  
  559.  
  560.  
  561. BEGIN
  562.   InitHP;                           { Select the PC symbol set }
  563.   New(Char1); New(Char2);           { Create objects on the heap }
  564.   Spaces(10);                       { Output 10 spaces }
  565.   Writeln(LST,Title);               { Print out the title string }
  566.   Writeln(LST);                     { Space down one line }
  567.   FOR I := 0 TO 3 DO                { 256 characters takes 4 pages }
  568.     BEGIN
  569.       FOR K := 1 TO 3 DO Writeln(LST);  { Space down 3 lines }
  570.       PrintHeader;                      { Print the chart header }
  571.       FOR J := 0 TO 31 DO               { Do 2 columns of 32 chars. }
  572.     BEGIN
  573.       Char1.BV := (I*64)+J; Char2.BV := (I*64)+J+32;
  574.       Spaces(10);
  575.       PrintChar(Char1);         { Print the left column character }
  576.       Write(LST,ColChar);       { Print the column separator      }
  577.       PrintChar(Char2);         { Print the right column character }
  578.       Writeln(LST);
  579.     END;
  580.       NewPage;                      { Issue a form feed to printer }
  581.     END;
  582. END.
  583.  
  584.