home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / xwplascr.zip / XWPL0208.ZIP / tools / sortdlg.cmd < prev    next >
OS/2 REXX Batch file  |  2001-04-17  |  3KB  |  124 lines

  1. /* REXX script to sort OS/2 .DLG files */
  2. /* (C) 2000 Paul Ratcliffe */
  3. parse upper source . . me
  4. call RxFuncAdd 'SysLoadFuncs', 'REXXUTIL', 'SysLoadFuncs'
  5. call SysLoadFuncs
  6. arg opt
  7. if opt = '' then do
  8.   me = filespec('N', me)
  9.   say
  10.   say 'Usage:' left(me, lastpos('.', me) - 1) '<file.dlg> [file.dlg] [file.dlg] ...'
  11.   return
  12. end
  13. if translate(right(opt, 4)) \= '.DLG' then
  14.   opt = opt'.dlg'
  15. call SysFileTree opt, 'stem', 'O'
  16. do i = 1 to stem.0
  17.   if i \= 1 then
  18.     say
  19.   say 'Processing "'stem.i'"'
  20.   call processdlg stem.i
  21. end
  22. return
  23.  
  24. processdlg: procedure expose os
  25.   arg fileout
  26.   fileout = translate(fileout, "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  27.   say fileout
  28.   filein = left(fileout, length(fileout)-1)'U'
  29.   call SysFileDelete filein
  30.   '@ren' fileout '*.dlu'
  31.   dialog.0 = 0
  32.   do while lines(filein) \= 0
  33.     line = linein(filein)
  34.     sline = striptab(line)
  35.     parse upper var sline key1 key2 key3 .
  36.     if key1 = 'DLGTEMPLATE' then do
  37.       dialog.0 = dialog.0 + 1;
  38.       i = dialog.0
  39.       dialog.i.num = key2
  40.       j = 1
  41.       dialog.i.j = line
  42.       do while lines(filein) \= 0
  43.     line = linein(filein)
  44.     sline = striptab(line)
  45.     j = j + 1
  46.     dialog.i.j = line
  47.     parse upper var sline key1 .
  48.     if length(sline) = 3 & sline = 'END' then
  49.       leave
  50.       end
  51.       dialog.i.0 = j
  52.     end; else if dialog.0 = 0 then
  53.       call lineout fileout, line
  54.   end
  55.   say '  Dialogs:' dialog.0
  56.   if dialog.0 \= 0 then do
  57.     stem.0 = dialog.0
  58.     do i = 1 to stem.0
  59.       stem.i.index = i
  60.       stem.i = dialog.i.num
  61.     end
  62.     call heapsort
  63.     do i = 1 to stem.0
  64.       k = stem.i.index
  65.       say '   ' dialog.k.num
  66.       do j = 1 to dialog.k.0
  67.     call lineout fileout, dialog.k.j
  68.       end
  69.       call lineout fileout, ''
  70.     end
  71.   end
  72.   call stream filein, 'C', 'CLOSE'
  73.   call stream fileout, 'C', 'CLOSE'
  74.   return
  75.  
  76. striptab: procedure
  77.   arg line
  78.   do forever
  79.     i = pos(d2c(9), line)
  80.     if i = 0 then
  81.       leave
  82.     line = overlay(' ', line, i)
  83.   end
  84.   return line
  85.  
  86. heapsort: procedure expose stem.
  87.   n = stem.0
  88.   do i = n % 2 to 1 by -1
  89.     call downheap i n
  90.   end
  91.   do while n > 1
  92.     tmp = stem.1.index
  93.     stem.1.index = stem.n.index
  94.     stem.n.index = tmp
  95.     tmp = stem.1
  96.     stem.1 = stem.n
  97.     stem.n = tmp
  98.     n = n - 1
  99.     call downheap 1 n
  100.   end
  101.   return
  102.  
  103. downheap: procedure expose stem.
  104.   parse arg k n
  105.   v = stem.k
  106.   w = stem.k.index
  107.   do while k <= n % 2
  108.     j = k + k
  109.     if j < n then do
  110.       i = j + 1
  111.       if stem.j < stem.i then
  112.         j = j + 1
  113.     end
  114.     if v >= stem.j then
  115.       signal label
  116.     stem.k = stem.j
  117.     stem.k.index = stem.j.index
  118.     k = j
  119.   end
  120. label:
  121.   stem.k = v
  122.   stem.k.index = w
  123.   return
  124.