home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 3 Comm / 03-Comm.zip / rexxtag2.zip / Tag.Cmd < prev    next >
OS/2 REXX Batch file  |  1996-01-23  |  8KB  |  228 lines

  1. /* * *
  2.  *
  3.  *    Simple Tagline Adder      v1.32\S - 1996-01-23
  4.       Standalone version (ask me for the FleetStreet version)
  5.  
  6.       (c) Copyright 1995, Peter Karlsson, 2:204/137.5 @ Fidonet       *
  7.                                                                       *
  8.                                                                   * * */
  9.  
  10. /* Versions: 1.0 (1995-06-15)
  11.              1.1 (1995-06-28) Added "Q" option, uses #0 for brag
  12.              1.2 (1995-07-17) Removed bug when searching for tagline
  13.              1.3 (1995-10-29) Now the bug when searching should be gone :)
  14.              1.31(1995-11-02) Well, it should now.......
  15.              1.32(1996-01-23) I hate the searching bug                     */
  16.  
  17. version = '1.32'
  18.  
  19. /*TRACE ?I*/
  20.  
  21. ARG Textfile Taglinefile Indexfile
  22.  
  23. CALL RxFuncAdd 'SysFileDelete', 'RexxUtil', 'SysFileDelete'
  24. CALL RxFuncAdd 'SysCls', 'RexxUtil', 'SysCls'
  25.  
  26. IF Indexfile = '' THEN
  27. DO
  28.   SAY 'TAG [drive:][path]textfile [drive:][path]tagfile [drive:][path]index'
  29.   SAY
  30.   SAY '  [drive:][path]textfile   The textfile to which the tagline should be added'
  31.   SAY '  [drive:][path]tagfile    The file containing the taglines'
  32.   SAY '  [drive:][path]index      The file containing the tagline index'
  33.   EXIT 1
  34. END
  35.  
  36. IF STREAM(Taglinefile, 'C', 'QUERY EXISTS') = '' THEN
  37. DO
  38.   SAY 'The specified tagline file ' || Taglinefile || ' could not be found.'
  39.   SAY
  40.   SAY 'Please check the setup'
  41.   EXIT 1
  42. END
  43.  
  44. TagfSize = STREAM(Taglinefile, 'C', 'QUERY SIZE')
  45.  
  46. IF STREAM(Indexfile, 'C', 'QUERY EXISTS') = '' THEN
  47. DO
  48.   SAY 'The specified tagline index file ' || Indexfile || ' could not be found.'
  49.   SupposedSize = -1
  50. END
  51. ELSE
  52. DO
  53.   IF STREAM(Indexfile, 'C', 'OPEN READ') \= 'READY:' THEN
  54.   DO
  55.     SAY 'ERROR: Could not open index file ' || Indexfile || '.'
  56.     SAY
  57.     SAY 'File may be locked by another process.'
  58.     EXIT 2
  59.   END
  60.   SupposedSize = LINEIN(Indexfile)        /* Read supposed size of tagfile */
  61. END
  62.  
  63. If SupposedSize \= TagfSize THEN        /* Supposed size do not match actual */
  64. DO
  65.   SAY 'Tagline index is not accurate, building new index'
  66.   CALL STREAM Indexfile, 'C', 'CLOSE'
  67.   IF SupposedSize \= -1 THEN
  68.     IF SysFileDelete(Indexfile) \= 0 THEN
  69.     DO
  70.       SAY 'ERROR: Could not delete the index file ' || Indexfile || '.'
  71.       SAY
  72.       SAY 'File may be locked by another process.'
  73.       EXIT 2
  74.     END
  75.   IF STREAM(Taglinefile, 'C', 'OPEN READ') \= 'READY:' THEN
  76.   DO
  77.     SAY 'ERROR: Could not open tagline file ' || Taglinefile || '.'
  78.     SAY
  79.     SAY 'File may be locked by another process.'
  80.     EXIT 2
  81.   END
  82.   Lines = 1
  83.   SAY 'Building index, please wait. This may take a while.'
  84.   DO WHILE LINES(Taglinefile)
  85.     Index.Lines = TagfSize - CHARS(Taglinefile) /* Index to line */
  86.     Data = LINEIN(Taglinefile)
  87.     IF LEFT(Data, 1) \= ';' & STRIP(Data) \= '' THEN Lines = Lines + 1
  88.   END
  89.   CALL STREAM Taglinefile, 'C', 'CLOSE'
  90.   IF STREAM(Indexfile, 'C', 'OPEN WRITE') \= 'READY:' THEN
  91.   DO
  92.     SAY 'ERROR: Could not create new index file ' || Indexfile || '.'
  93.     SAY
  94.     SAY 'File may be locked by another process.'
  95.     EXIT 2
  96.   END
  97.   CALL LINEOUT Indexfile, TagfSize      /* Save current filesize */
  98.   CALL LINEOUT Indexfile, Lines         /* Save number of lines */
  99.   DO print = 1 TO Lines-1
  100.     CALL LINEOUT Indexfile, Index.print  /* Save indexes */
  101.   END
  102.   CALL STREAM Indexfile, 'C', 'CLOSE'
  103. END
  104. ELSE
  105. DO                                      /* Read the correct index */
  106.   SAY 'Reading index, please wait. This may take a while.'
  107.   Lines = LINEIN(Indexfile)
  108.   DO read = 1 TO Lines-1
  109.     Index.read = LINEIN(Indexfile)
  110.   END read
  111.   CALL STREAM Indexfile, 'C', 'CLOSE'
  112. END
  113. Index.Lines = TagfSize
  114. SAY
  115. SAY 'Tagline file has ' || Lines-1 || ' entries.'
  116. IF STREAM(Taglinefile, 'C', 'OPEN READ') \= 'READY:' THEN
  117. DO
  118.   SAY 'ERROR: Could not open tagline file ' || Taglinefile || '.'
  119.   SAY
  120.   SAY 'File may be locked by another process.'
  121.   EXIT 2
  122. END
  123. satisfied = 'N'
  124. number = RANDOM(Lines-1)                /* Starting random tagline */
  125. CALL SysCls                             /* Clear screen */
  126. SAY 'Tagline selector. Press [Y] when you are satisfied, [N] if not. [-] will get'
  127. SAY 'the previous tagline, [+] the next. [#] Lets you enter a number and [O] seeks'
  128. SAY 'for a word after the current tagline. [Q] to not add a tagline.'
  129. SAY
  130. SAY '==============================================================================='
  131. DO UNTIL (satisfied = 'Y' | satisfied = 'Q')
  132.   IF number = 0 THEN
  133.   DO
  134.     Tagline = 'Brought to you by TAG.CMD v' || version || ' (c) 1995 Peter Karlsson'
  135.     CALL STREAM Taglinefile, 'C', 'SEEK =' || (Index.1)+1       /* Position */
  136.   END
  137.   ELSE
  138.   DO
  139.     CALL STREAM Taglinefile, 'C', 'SEEK =' || (Index.number)+1  /* Position */
  140.     Tagline = LINEIN(Taglinefile)       /* Read the tagline */
  141.   END
  142.   SAY number || ': ' || Tagline
  143.   satisfied = ''
  144.   DO UNTIL satisfied = 'Y' | satisfied = 'N' | satisfied = '-' | satisfied = '+' | satisfied = 'O' | satisfied = '#' | satisfied = 'Q'
  145.     PULL satisfied
  146.   END
  147.   IF satisfied = 'N' THEN number = RANDOM(Lines-1)      /* New random select */
  148.   IF satisfied = '+' THEN number = number + 1           /* Previous tagline */
  149.   IF satisfied = '-' THEN number = number - 1           /* Next tagline */
  150.   IF satisfied = '#' THEN
  151.   DO
  152.     SAY '[#] Jump to which tagline? ( 0-' || Lines-1 ')'
  153.     PULL number                         /* Select tagline by number */
  154.   END
  155.   IF satisfied = 'O' THEN
  156.   DO
  157.     SAY '[O] Search for what word?'
  158.     PULL word
  159.     nextnumb = number+1
  160.     CALL STREAM Taglinefile, 'C', 'SEEK =' || (Index.nextnumb)+1 /* Position */
  161.     DO search = number+1 TO Lines-1     /* Search subsequent lines for tagline */
  162.       Tagline = TRANSLATE(LINEIN(Taglinefile))
  163.       IF POS(word, Tagline) \= 0 THEN LEAVE
  164.     END search
  165.     IF search = Lines THEN              /* The loop did run until end */
  166.       SAY 'Could not find the word "' || word || '"!'
  167.     ELSE
  168.       number = search                   /* Select the found tagline */
  169.   END
  170. END
  171. CALL STREAM Taglinefile, 'C', 'CLOSE'   /* Close it, we have read the tagline */
  172. If satisfied = 'Q' THEN EXIT
  173. DROP Index.                             /* We do not need the indexes anymore */
  174. IF STREAM(Textfile, 'C', 'QUERY EXISTS') = '' THEN
  175. DO                                      /* Create new text file */
  176.   IF STREAM(Textfile, 'C', 'OPEN WRITE') \= 'READY:' THEN
  177.   DO
  178.     SAY 'ERROR: Could not create new text file ' || Textfile || '.'
  179.     SAY
  180.     SAY 'File may be locked by another process.'
  181.     EXIT 2
  182.   END
  183.   CALL LINEOUT Textfile, '... ' || Tagline
  184.   CALL STREAM Textfile, 'C', 'CLOSE'
  185. END
  186. ELSE
  187. DO
  188.   IF STREAM(Textfile, 'C', 'OPEN READ') \= 'READY:' THEN
  189.   DO
  190.     SAY 'ERROR: Could not open text file ' || Textfile || '.'
  191.     SAY
  192.     SAY 'File may be locked by another process.'
  193.     EXIT 2
  194.   END
  195.   Lines = 0
  196.   DO WHILE LINES(Textfile)
  197.     Data.Lines = LINEIN(Textfile)
  198.     Lines = Lines + 1
  199.   END
  200.   CALL STREAM Textfile, 'C', 'CLOSE'
  201.   IF SysFileDelete(Textfile) \= 0 THEN
  202.   DO
  203.     SAY 'ERROR: Could not delete the old text file ' || Textfile || '.'
  204.     SAY
  205.     SAY 'File may be locked by another process.'
  206.     EXIT 2
  207.   END
  208.   IF STREAM(Textfile, 'C', 'OPEN WRITE') \= 'READY:' THEN
  209.   DO
  210.     SAY 'ERROR: Could not create new text file ' || Textfile || '.'
  211.     SAY
  212.     SAY 'File may be locked by another process.'
  213.     EXIT 2
  214.   END
  215.   Wrotetagline = 0
  216.   DO write = 0 TO Lines-1
  217.     IF LEFT(Data.write, 4) = '--- ' THEN        /* Add before tearline */
  218.     DO
  219.       CALL LINEOUT Textfile, '... ' || Tagline
  220.       Wrotetagline = 1
  221.     END
  222.     CALL LINEOUT Textfile, Data.write
  223.   END write
  224.   IF \ Wrotetagline THEN                /* No tearline, add last */
  225.     CALL LINEOUT Textfile, '... ' || Tagline
  226.   CALL STREAM Textfile, 'C', 'CLOSE'
  227. END
  228.