home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / games / patterns.zip / GDT.DOC < prev    next >
Text File  |  1988-04-01  |  11KB  |  276 lines

  1.  
  2.                              GDT.COM
  3.        Horizontally Aligns Material on a GIven Marker Byte
  4.  
  5.                                by
  6.                        Robert A. Magnuson
  7.                          DMB, DCRT, NIH
  8.                        Bethesda, MD 20892
  9.                             Mar 1988
  10.                         Revised Apr 1988
  11.  
  12.  
  13.  
  14. GDT (for Generalized Decimal Tab) is a filter that reads lines
  15. from a set of files--or from stdin if no files are specified.
  16. Each line is written to stdout, shifted such that the first
  17. occurrence of a marker byte is in a specified column.  A line
  18. with no marker byte is shifted as if a marker byte were just
  19. beyond the end.  An option permits truncation.  Without that
  20. option the shifting stops just short of truncation.  Such lines
  21. can optionally be written to stderr.
  22.  
  23.     [This document is intended to be read from the screen.
  24.     It contains some characters which will probably not print
  25.     correctly on a printer.]
  26.  
  27. A DOS command line that invokes GDT contains a number of
  28. arguments.  The syntax is expressed by the following diagram:
  29.  
  30.      ┌──────────────────────────────┐
  31.      │     ┌─ d ─┐ delete marker     │                     ┌──────────┐
  32. GDT ─┴ / ┬─┤     ├────────────────┬┴─ <marker> ── <col> ─┴┬ <file> ┬┴──
  33.          │ ├─ e ─┤ write truncated │                        └───────┘
  34.          │ │     │ lines to stderr │
  35.          │ └─ t ─┘ truncation OK   │                        3/27/88
  36.          └────────────────────────┘
  37.  
  38. First there may be optional arguments specifying various GDT
  39. options.  Then there is the required marker byte, the first
  40. occurrence of which in each line read determines the horizontal
  41. shifting of that line.  Next, there is the required column
  42. specification.  Then come the filenames which can be wildcarded.
  43. If no filenames appear, GDT gets its input from stdin.
  44.  
  45.     The arguments can optionally be enclosed in double
  46.     quotes.  The enclosing quotes are stripped off and not
  47.     seen by GDT.  Should you need to have a double quote
  48.     within an argument, the argument must be double quoted
  49.     and the internal double quote must be escaped by
  50.     preceding it with a backslash.  This treatment of the
  51.     double quotes is done by the argc/argv mechanism of the C
  52.     compiler.  [GDT is implemented in Borland TurboC.]
  53.  
  54.  
  55. GDT exits with ERRORLEVEL set to one if some lines needed to be
  56. truncated--but weren't because there was no /t option.  In that
  57. case the produced alignment is incorrect for those lines.  The
  58. ERRORLEVEL is set to zero when no truncation problems arose, and
  59. to two for syntax problems.
  60.  
  61. OPTION ARGUMENTS:
  62.  
  63. GDT options are specified by the presence or absence of various
  64. option letters in option arguments.  Any option argument must
  65. begin with a slash and must appear in front of the other kinds of
  66. arguments.  Any number of legal option letters can appear in an
  67. option argument.  Thus, you can have multiple option arguments,
  68. perhaps each with a single option letter (and each beginning with
  69. a slash), or just one option argument containing all of the
  70. option letters desired.  CURRENTLY, ALL LEGAL GDT OPTION LETTERS
  71. ARE lower case.
  72.  
  73.     For the sake of readability, the above syntax diagram
  74.     shows only the case where all option letters appear in
  75.     one option argument.
  76.  
  77. A GDT syntax error occurs when illegal option letters appear, and
  78. when required arguments are missing or invalid.  Both the
  79. <marker> and <col> arguments are required.  The marker must be
  80. entered as a single byte, as \d..d, or as \xh..h--where the d's
  81. are decimal digits, and the h's are hexadecimal digits.
  82.  
  83. When a syntax error occurs GDT prints a boxed syntax diagram
  84. containing terse instructions on how to use GDT.  This mechanism
  85. can be deliberately tripped in order to get on-screen help.  The
  86. suggested way is to invoke GDT with no arguments--thus causing
  87. the no-<marker> syntax error.
  88.  
  89. The /d option specifiess that the markers are to be deleted from
  90. the shifted lines.  The marker is searched for from left to
  91. right.  The first one found is used for alignment.  Any further
  92. markers are ignored and remain as text.
  93.  
  94. The /e option is used for error reporting.  Lines that could not
  95. be aligned without truncation are copied to stderr.
  96.  
  97. The /t option freely permits truncation.  No errors are reported,
  98. and the ERRORLEVEL will not be one.
  99.  
  100.  
  101. EXAMPLES:
  102.  
  103. Let the file AMOUNTS contain the following lines:
  104.  
  105.         47.23
  106.         1923.50
  107.         .57
  108.         32768
  109.         1.27
  110.         4.12
  111.         588.90
  112.  
  113. The indentation shown is to distinguish the dislayed contents
  114. from the expanatory text. I.e., each line in AMOUNTS starts in
  115. column one without any leading blanks.  A copy of this data with
  116. the decimal points aligned in, say, column ten is obtained via
  117.  
  118.         gdt . 10 AMOUNTS
  119.  
  120. Here the marker is a period, the alignment column is 10.  The
  121. results will come out on the screen, i.e., stdout, as follows:
  122.  
  123.                47.23
  124.              1923.50
  125.                  .57
  126.             32768
  127.                 1.27
  128.                 4.12
  129.               588.90
  130.  
  131. The following material
  132.  
  133.        Julius @Caesar
  134.        Charles @De Gaulle
  135.        Hernando @de Soto
  136.        Ferdinand Victor Eugene @Delacroix
  137.        Office of the @Emperor
  138.        Kublai @Khan
  139.        @Office of the President
  140.        Aleksandr Sergeyevich @Pushkin
  141.        Baron von @Richthofen
  142.        @Royal Canadian Air Force
  143.        Bachelor of @Theology
  144.        Vincent @van Gogh
  145.  
  146. when filtered through GDT via
  147.  
  148.         gdt/d @ 30
  149.  
  150. comes out as
  151.  
  152.                               Julius Caesar
  153.                              Charles De Gaulle
  154.                             Hernando de Soto
  155.              Ferdinand Victor Eugene Delacroix
  156.                        Office of the Emperor
  157.                               Kublai Khan
  158.                                      Office of the President
  159.                Aleksandr Sergeyevich Pushkin
  160.                            Baron von Richthofen
  161.                                      Royal Canadian Air Force
  162.                          Bachelor of Theology
  163.                              Vincent van Gogh
  164.  
  165. Now you may wonder how the at-sign markers got into the file, how
  166. the material got sorted, etc.  Much of this can be done by FP.EXE
  167. and CHP.EXE (which are pattern utilities).
  168.  
  169. Suppose we start with NAMES, an unsorted file of the material to
  170. be massaged:
  171.  
  172.         ---------- NAMES
  173.         [1]Charles @De Gaulle
  174.         [2]Kublai Khan
  175.         [3]Bachelor of Theology
  176.         [4]Aleksandr Sergeyevich Pushkin
  177.         [5]@Office of the President
  178.         [6]Office of the Emperor
  179.         [7]Julius Caesar
  180.         [8]Vincent @van Gogh
  181.         [9]@Royal Canadian Air Force
  182.         [10]Ferdinand Victor Eugene Delacroix
  183.         [11]Hernando @de Soto
  184.         [12]Baron von Richthofen
  185.  
  186. We assume here that most names are to be sorted by the last word
  187. first.  Therefore, at-sign markers appear only where needed by
  188. exception (in lines 1, 5, 8, 9 and 11).  GDT is used at the end
  189. of the processing.  The required processing is:
  190.  
  191.   ■ Separate out the lines with the at-signs,
  192.  
  193.   ■ For the other lines precede the last word with the
  194.     at-sign mark,
  195.  
  196.   ■ Recombine lines just automatically marked with others,
  197.  
  198.   ■ For each line, put the mark and following bytes in front
  199.     followed by a new temporary mark (here a smiley face),
  200.     followed by stuff before mark,
  201.  
  202.   ■ sort,
  203.  
  204.   ■ rearrange each line in former order (removing smiley
  205.     faces),
  206.  
  207.   ■ Finally via GDT, align mark on chosen column.
  208.  
  209. This can be done automatically via the following batch file:
  210.  
  211.         ---------- Z.BAT
  212.         [1]:split off already marked names to temp file,
  213.         [2]: and, at the same time,
  214.         [3]:take unmarked names, precede last word with mark,
  215.         [4]:then append to temp file
  216.         [5]fp/vou (tmp) @ names|chp "\( ?\)\([a-z]+\)$" \1@\2>>(tmp)
  217.         [6]:put the mark and following stuff in front,
  218.         [7]:followed by a smiley face, followed by stuff before mark,
  219.         [8]:then sort, then rearrange,
  220.         [9]:then align at-sign mark on column 30
  221.         [10]chp \(.*\)\(@.*\) \2\1 (tmp) | sort
  222.             | chp \(.*\)\(.*\) \2\1 |gdt/d @ 30
  223.  
  224. Comments and explanations follow:
  225.  
  226.     Note that the /u option of FP is used below.  You will
  227.     need a version of FP.EXE with a date of at least 4/01/88
  228.     for this option to be in FP.
  229.  
  230.         [5]fp/vou (tmp) @ names|chp "\( ?\)\([a-z]+\)$" \1@\2>>(tmp)
  231.  
  232.     Here FP (for Find Pattern) first writes to the file (TMP)
  233.     those lines in the file NAMES that contain an at-sign.
  234.     These lines are the ones that are not selected, and are
  235.     written because of the /u option.  At the same time FP
  236.     selects those lines that do NOT have an at_sign.  (The /v
  237.     option inverts the selection, and the /o option omits
  238.     including the filename--which would be ---------- NAMES.)
  239.     Those lines are piped to CHP (CHange via Pattern).  The
  240.     pattern consists mainly of two backslash-parenthesized
  241.     patterns.  The first matches zero or one blank.  The
  242.     second matches one or more letters (irrespective of
  243.     case).  The ending dollar sign matches end of line.  Thus
  244.     \2 is the last word, and \1 is the preceding space.
  245.     (There would be no space if a line had just one word on
  246.     it.) The result consists of the possibly empty blank, the
  247.     at_sign, and the last word.  What about the material to
  248.     the left of the rightmost word?  CHP writes out intact
  249.     all unmatched material.  This is then appended (because
  250.     of the >> redirection) to the temp file that already
  251.     contains the lines that had had at-signs.  Now every line
  252.     has an at-sign in front of the major-sort part.  The
  253.     [a-z] character set would have to be enlarged if any of
  254.     the names were hyphenated or contained apostrophes.
  255.  
  256.         [10]chp \(.*\)\(@.*\) \2\1 (tmp) | sort
  257.             | chp \(.*\)\(.*\) \2\1 |gdt/d @ 30
  258.  
  259.     Now, before sorting, the major-sort part needs to be put
  260.     in front.  Therefore, the first CHP--reading from the
  261.     temp file--puts all the material preceding the at-sign
  262.     into \1, and puts the at-sign and all following bytes
  263.     into \2.  The result is to be the two parts interchanged
  264.     with a smiley face in between.  The smiley face is
  265.     inserted so that the end of the last name (in case of
  266.     multiple words there) can be found later.  This is piped
  267.     to SORT whose stdout is, in turn, piped to the second
  268.     CHP.  There, the two parts are interchanged again and the
  269.     smiley face is removed.  Finally GDT does its thing.  You
  270.     may wonder about the smiley face used temporarily to mark
  271.     the end of the major-sort part.  Other characters could
  272.     have been used but this temporary marker should be
  273.     lexicographically below a blank in order to sort
  274.     correctly.
  275.  
  276.