[Libreoffice-commits] .: src/formula.py src/xlsrecord.py

Kohei Yoshida kohei at kemper.freedesktop.org
Mon Aug 8 18:28:18 PDT 2011


 src/formula.py   |   35 +++++++++++++++++++++++++++++++++++
 src/xlsrecord.py |   12 ++++++++++--
 2 files changed, 45 insertions(+), 2 deletions(-)

New commits:
commit 8087ab8ce73df9f77d92dade754f92dee424f74d
Author: Kohei Yoshida <kohei.yoshida at suse.com>
Date:   Mon Aug 8 21:28:42 2011 -0400

    Parse formula tokens in the chart data series.

diff --git a/src/formula.py b/src/formula.py
index f5572b3..530b3b9 100644
--- a/src/formula.py
+++ b/src/formula.py
@@ -125,6 +125,22 @@ class TokenType:
     Area3d = 0
     Unknown = 9999
 
+def getPtgDataType (opcode):
+    return (opcode & 0x60) / (2**5)
+
+class PtgDataType(object):
+
+    Reference = 0x1
+    Value     = 0x2
+    Array     = 0x3
+
+    __names = ['ref', 'value', 'array']
+    @staticmethod
+    def getText (value):
+        if 1 <= value and value <= 3:
+            return PtgDataType.__names[value-1]
+        return 'unknown'
+
 class PtgBase(object):
     def __init__ (self, strm, opcode1, opcode2=None):
         self.opcode1 = opcode1
@@ -166,6 +182,14 @@ class PtgRef(PtgBase):
     def getText (self):
         return "(ref: row=%d, col=%d)"%(self.row, self.col)
 
+class PtgMemFunc(PtgBase):
+    def parseBytes(self):
+        self.dataType = getPtgDataType(self.opcode1)
+        self.length = self.strm.readUnsignedInt(2)
+
+    def getText (self):
+        return "(mem func: type=%s size=%d)"%(PtgDataType.getText(self.dataType), self.length)
+
 class PtgStr(PtgBase):
     def parseBytes (self):
         length = self.strm.readUnsignedInt(1)
@@ -189,6 +213,14 @@ class PtgInt(PtgBase):
     def getText (self):
         return "(int: %d)"%self.value
 
+class PtgParen(PtgBase):
+    def getText (self):
+        return '(paren)'
+
+class PtgUnion(PtgBase):
+    def getText (self):
+        return '(union)'
+
 class _Area3d(PtgBase):
     def parseBytes (self):
         self.xti = self.strm.readUnsignedInt(2)
@@ -607,10 +639,13 @@ class PtgFuncVar(PtgBase):
 
 _tokenMap = {
     0x01: PtgExp,
+    0x10: PtgUnion,
+    0x15: PtgParen,
     0x16: PtgMissArg,
     0x17: PtgStr,
     0x1E: PtgInt,
     0x24: PtgRef,
+    0x29: PtgMemFunc,
     0x3B: _Area3d,
     0x59: PtgNameX,
     0x5B: _Area3d,
diff --git a/src/xlsrecord.py b/src/xlsrecord.py
index d37bd9b..d5117f8 100644
--- a/src/xlsrecord.py
+++ b/src/xlsrecord.py
@@ -3541,8 +3541,8 @@ class Brai(BaseRecordHandler):
         flag = self.readUnsignedInt(2)
         self.unlinkedIFmt = (flag & 0x0001) != 0
         self.iFmt = self.readUnsignedInt(2)
-        tokenCount = self.readUnsignedInt(2)
-        # TODO: parse chart formula tokens here.
+        tokenBytes = self.readUnsignedInt(2)
+        self.formulaBytes = self.readBytes(tokenBytes)
 
     def parseBytes (self):
         self.__parseBytes()
@@ -3556,6 +3556,14 @@ class Brai(BaseRecordHandler):
             self.appendLine(s)
 
         self.appendLine("number format ID: %d"%self.iFmt)
+        self.appendLine("formula size (bytes): %d"%len(self.formulaBytes))
+        if len(self.formulaBytes) > 0:
+            parser = formula.FormulaParser(self.header, self.formulaBytes)
+            try:
+                parser.parse()
+                self.appendLine("formula: %s"%parser.getText())
+            except formula.FormulaParserError as e:
+                self.appendLine("formula parser error: %s"%e.args[0])
 
 class MSODrawing(BaseRecordHandler):
     """Handler for the MSODRAWING record


More information about the Libreoffice-commits mailing list