home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / arexx / ole1v10a.lha / OLE_System / ole / Parse_PCF.rexx < prev    next >
Encoding:
OS/2 REXX Batch file  |  1995-02-10  |  4.2 KB  |  245 lines

  1. /*
  2.  * Parse_PCF.rexx
  3.  *
  4.  * USAGE: Parse_PCF(OleObj,SpreadFile,Range)
  5.  *
  6.  * This script take the hard job of parsing a PCF file format to extract
  7.  * all usefull information to help programs that need to manage ProCalc
  8.  * worksheet. In future this module of my OLE system will be written in C
  9.  * to speed-up the operation.
  10.  *
  11.  * $(C): (1994, Rocco Coluccelli, Bologna)
  12.  * $VER: Parse_PCF.rexx 1.24 (17.Oct.1994)
  13.  */
  14.  
  15. LF = '0A'X
  16.  
  17. PARSE ARG oleobj,spreadfile,range
  18.  
  19. NUMERIC DIGITS 4
  20.  
  21. IF ~OPEN(pcf,spreadfile,'R') THEN EXIT 20
  22.  
  23. /*
  24.  *    Set default values for all cells
  25.  */
  26. row.  = 0
  27. col.  = 0
  28. cell. = 0
  29. data. = 0
  30. exts. = 'þTOPAZÞ' 8 0 0 0 0
  31.  
  32. /*
  33.  *    Set alignements left right center
  34.  */
  35. def.0.001 = 100
  36. def.1.001 = 010
  37. def.0.000 = 010
  38. def.1.000 = 100
  39. def.0.010 = 010
  40. def.1.010 = 010
  41. def.1.100 = 001
  42. def.0.100 = 001
  43.  
  44.  
  45. IF ~OPEN(pip,'PIPE:' || oleobj,'W') THEN DO
  46.     CALL CLOSE(pcf)
  47.     EXIT 20
  48.     END
  49.  
  50. CALL WRITELN(pip,spreadfile)
  51. CALL WRITELN(pip,range)
  52.  
  53. minrow = 10000
  54. maxrow = 1
  55. mincol = 10000
  56. maxcol = 1
  57.  
  58. DO UNTIL range = ''
  59.     PARSE VAR range firstcell':'lastcell range
  60.  
  61.     pos = POS('-',firstcell)
  62.     head = (pos = 0)
  63.     firstcell = SUBSTR(firstcell,pos + 1)
  64.  
  65.     PARSE VALUE To_coords(firstcell) To_coords(lastcell) WITH startrow startcol endrow endcol .
  66.  
  67.     DO i = startrow TO endrow
  68.         row.i = 1
  69.  
  70.         DO j = startcol TO endcol
  71.             cell.i.j = head
  72.         END
  73.     END
  74.  
  75.     DO j = startcol TO endcol
  76.         col.j = 1
  77.     END
  78.  
  79.     minrow = MIN(minrow,startrow)
  80.     maxrow = MAX(maxrow,endrow)
  81.     mincol = MIN(mincol,startcol)
  82.     maxcol = MAX(maxcol,endcol)
  83. END
  84.  
  85. /*
  86.  *    Read all chunks in the input file
  87.  */
  88. DO FOREVER
  89.  
  90.     PARSE VALUE READCH(pcf,3) WITH head +1 len +2
  91.     head = C2D(head)
  92.     len = C2D(len)
  93.  
  94.     SELECT
  95.  
  96.         /*
  97.          *    Number cell
  98.          *    Label cell
  99.          */
  100.         WHEN head = 110 | head = 120 | head = 100 THEN DO
  101.             PARSE VALUE READCH(pcf,len) WITH row +2 col +2 rest
  102.  
  103.             row = C2D(row)
  104.             col = C2D(col)
  105.             IF ~cell.row.col THEN ITERATE
  106.  
  107.             IF head = 100 THEN
  108.                 PARSE VAR rest bitset +4 color +1 len +1 rest
  109.             ELSE
  110.                 PARSE VAR rest bitset +4 color +1 +3 ieee +8 len +1 rest
  111.  
  112.             PARSE VALUE C2B(bitset) WITH +17 italic +1 bold +1 underline +1 align +3 .
  113.  
  114.             len = C2D(len)
  115.             PARSE VAR rest +len len +1 text '00'x
  116.             len = C2D(len) - 1
  117.  
  118.             IF len = -1 THEN DO
  119.                 text = C2F(ieee)
  120.                 len = LENGTH(text)
  121.             END
  122.  
  123.             num = DATATYPE(text,'N')
  124.  
  125.             data.row.col = def.num.align bold italic underline C2D(color) len 'þ'text'Þ'
  126.         END
  127.  
  128.         /*
  129.          *    Extended cell informations
  130.          */
  131.         WHEN head = 125 & cell.row.col THEN DO
  132.  
  133.             PARSE VALUE READCH(pcf,len) WITH font'.font' +1 size +2 bits +2
  134.             PARSE VALUE C2B(bits) WITH +1 ldown +1 lup +1 lright +1 lleft +1 .
  135.  
  136.             exts.row.col = 'þ'UPPER(font)'Þ' C2D(size) lleft lright lup ldown
  137.  
  138.         END
  139.  
  140.         /*
  141.          *    Default format for cells and rows
  142.          *    under construction
  143.          */
  144.         WHEN head = 16 | head = 17 THEN DO
  145.             CALL SEEK(pcf,len)
  146.         END
  147.  
  148.         /*
  149.          *    Reached end of file
  150.          */
  151.         WHEN head = 0 THEN DO
  152.             CALL CLOSE(pcf)
  153.             LEAVE
  154.         END
  155.  
  156.         /*
  157.          *    Skip forward unknown chunks
  158.          */
  159.         OTHERWISE CALL SEEK(pcf,len)
  160.  
  161.     END    /* end SELECT */
  162.  
  163. END    /* end DO */
  164.  
  165.  
  166. /*
  167.  *    Reformat the output table
  168.  */
  169. row = 0
  170. DO i = minrow TO maxrow
  171.  
  172.     IF row.i THEN DO
  173.         row = row + 1
  174.         row.i = row
  175.         END
  176. END
  177.  
  178. col = 0
  179. DO j = mincol TO maxcol
  180.  
  181.     IF col.j THEN DO
  182.         col = col + 1
  183.         col.j = col
  184.         END
  185. END
  186.  
  187.  
  188. /*
  189.  * When the parse process has finished write out the result.
  190.  *
  191.  * HEADER:
  192.  * spreadfile
  193.  * ranges
  194.  * rows cols
  195.  *
  196.  * DATAS:
  197.  * row col align bold italic underline color len 'þ'text'Þ' 'þ'font'Þ' size lleft lright lup ldown
  198.  *
  199.  * (row col) of the cell, (len) length in chars of (text)
  200.  * (align) aligned with (bold italic underline) style
  201.  * typed in (color) with (font) of (size)
  202.  * with borders (lineleft lineright lineup linedown)
  203.  */
  204.  
  205. CALL WRITELN(pip,row col)
  206.  
  207. DO i = minrow TO maxrow
  208.     DO j = mincol TO maxcol
  209.  
  210.         IF data.i.j ~= 0 THEN
  211.             CALL WRITELN(pip,row.i col.j data.i.j exts.i.j)
  212.     END
  213. END
  214.  
  215. CALL CLOSE(pip)
  216.  
  217. EXIT 0
  218.  
  219.  
  220. /*
  221.  * convert cell coordinates into numbers
  222.  */
  223. To_coords: PROCEDURE
  224. ARG cell .
  225.  
  226.     pos = VERIFY(cell,'0123456789','m')
  227.     row = SUBSTR(cell,pos)
  228.  
  229.     cell = RIGHT(LEFT(cell,pos - 1),2,'40'X)
  230.     col = (C2D(LEFT(cell,1)) - 64) * 26 + (C2D(RIGHT(cell,1)) - 64)
  231.  
  232. RETURN row col
  233.  
  234.  
  235. C2F: PROCEDURE
  236. PARSE ARG ieee
  237.  
  238.     PARSE VALUE C2B(ieee) WITH s +1 e +11 fl +32 fh +20
  239.  
  240.     s = -1 ** s
  241.     e = C2D(B2C(e)) - 1023
  242.     f = '1.' || C2D(B2C(fl))
  243.  
  244. RETURN s * f * (2 ** e)
  245.