ooo-build r11425 - in trunk: . scratch/sc-xlsutil/src



Author: kyoshida
Date: Mon Jan 28 04:40:38 2008
New Revision: 11425
URL: http://svn.gnome.org/viewvc/ooo-build?rev=11425&view=rev

Log:
2008-01-27  Kohei Yoshida  <kyoshida novell com>

	* scratch/sc-xlsutil/src/formula.py:
	* scratch/sc-xlsutil/src/globals.py:
	* scratch/sc-xlsutil/src/record.py:
	* scratch/sc-xlsutil/src/stream.py: added more record handlers (RK, ROW
	and NUMBER).


Modified:
   trunk/ChangeLog
   trunk/scratch/sc-xlsutil/src/formula.py
   trunk/scratch/sc-xlsutil/src/globals.py
   trunk/scratch/sc-xlsutil/src/record.py
   trunk/scratch/sc-xlsutil/src/stream.py

Modified: trunk/scratch/sc-xlsutil/src/formula.py
==============================================================================
--- trunk/scratch/sc-xlsutil/src/formula.py	(original)
+++ trunk/scratch/sc-xlsutil/src/formula.py	Mon Jan 28 04:40:38 2008
@@ -3,6 +3,17 @@
 
 
 class TokenBase(object):
+    """base class for token handler
+
+Derive a class from this base class to create a token handler for a formula
+token.  
+
+The parse method takes the token array position that points to the first 
+token to be processed, and returns the position of the laste token that has 
+been processed.  So, if the handler processes only one token, it should 
+return the same value it receives without incrementing it.  
+
+"""
     def __init__ (self, tokens):
         self.tokens = tokens
 
@@ -80,6 +91,12 @@
 }
 
 class FormulaParser(object):
+    """formula parser for token bytes
+
+This class receives a series of bytes that represent formula tokens through
+the constructor.  That series of bytes must also include the formula length
+which is usually the first 2 bytes.
+"""
     def __init__ (self, tokens):
         self.tokens = tokens
         self.text = ''
@@ -94,13 +111,17 @@
         i = 0
         while i < length:
             tk = ftokens[i]
+
             if type(tk) == type('c'):
                 # get the ordinal of the character.
                 tk = ord(tk)
+
             if not tokenMap.has_key(tk):
+                # no token handler
                 i += 1
                 continue
 
+            # token handler exists.
             o = tokenMap[tk](ftokens)
             i = o.parse(i)
             self.text += o.getText() + ' '

Modified: trunk/scratch/sc-xlsutil/src/globals.py
==============================================================================
--- trunk/scratch/sc-xlsutil/src/globals.py	(original)
+++ trunk/scratch/sc-xlsutil/src/globals.py	Mon Jan 28 04:40:38 2008
@@ -73,21 +73,30 @@
         return struct.unpack('b', text)[0]
     elif n == 2:
         # short - 2 bytes
-        return struct.unpack('h', text)[0]
+        return struct.unpack('<h', text)[0]
     elif n == 4:
         # int, long - 4 bytes
-        return struct.unpack('l', text)[0]
+        return struct.unpack('<l', text)[0]
 
     raise ByteConvertError
 
 
+def getFloat (bytes):
+    n = len(bytes)
+    if n == 0:
+        return 0.0
+
+    text = toTextBytes(bytes)
+    return struct.unpack('<f', text)[0]
+
+
 def getDouble (bytes):
     n = len(bytes)
     if n == 0:
         return 0.0
 
     text = toTextBytes(bytes)
-    return struct.unpack('d', text)[0]
+    return struct.unpack('<d', text)[0]
 
 
 def getUTF8FromUTF16 (bytes):

Modified: trunk/scratch/sc-xlsutil/src/record.py
==============================================================================
--- trunk/scratch/sc-xlsutil/src/record.py	(original)
+++ trunk/scratch/sc-xlsutil/src/record.py	Mon Jan 28 04:40:38 2008
@@ -1,4 +1,5 @@
 
+import struct
 import globals, formula
 
 # -------------------------------------------------------------------
@@ -97,6 +98,73 @@
         self.appendLine("XF record ID: %d"%xf)
         self.appendLine("value: %g"%fval)
 
+
+class RK(BaseRecordHandler):
+    """Cell with encoded integer or floating-point value"""
+
+    def parseBytes (self):
+        row = globals.getSignedInt(self.bytes[0:2])
+        col = globals.getSignedInt(self.bytes[2:4])
+        xf  = globals.getSignedInt(self.bytes[4:6])
+
+        rkval = globals.getSignedInt(self.bytes[6:10])
+        multi100  = ((rkval & 0x00000001) != 0)
+        signedInt = ((rkval & 0x00000002) != 0)
+        realVal   = (rkval & 0xFFFFFFFC)
+
+        if signedInt:
+            # for integer, perform right-shift by 2 bits.
+            realVal = realVal/4
+        else:
+            # for floating-point, convert the value back to the bytes,
+            # pad the bytes to make it 8-byte long, and convert it back
+            # to the numeric value.
+            tmpBytes = struct.pack('<L', realVal)
+            tmpBytes = struct.pack('xxxx') + tmpBytes
+            realVal = struct.unpack('<d', tmpBytes)[0]
+
+        if multi100:
+            realVal /= 100
+
+        self.appendLine("cell position: (col: %d; row: %d)"%(col, row))
+        self.appendLine("XF record ID: %d"%xf)
+        self.appendLine("multiplied by 100: %d"%multi100)
+        if signedInt:
+            self.appendLine("type: signed integer")
+        else:
+            self.appendLine("type: floating point")
+        self.appendLine("value: %g"%realVal)
+
+
+class Blank(BaseRecordHandler):
+
+    def parseBytes (self):
+        row = globals.getSignedInt(self.bytes[0:2])
+        col = globals.getSignedInt(self.bytes[2:4])
+        xf  = globals.getSignedInt(self.bytes[4:6])
+        self.appendLine("cell position: (col: %d; row: %d)"%(col, row))
+        self.appendLine("XF record ID: %d"%xf)
+
+
+class Row(BaseRecordHandler):
+
+    def parseBytes (self):
+        row  = globals.getSignedInt(self.bytes[0:2])
+        col1 = globals.getSignedInt(self.bytes[2:4])
+        col2 = globals.getSignedInt(self.bytes[4:6])
+
+        rowHeightBits = globals.getSignedInt(self.bytes[6:8])
+        rowHeight     = (rowHeightBits & 0x7FFF)
+        defaultHeight = ((rowHeightBits & 0x8000) == 1)
+
+        self.appendLine("row: %d; col: %d - %d"%(row, col1, col2))
+        self.appendLine("row height (twips): %d"%rowHeight)
+        if defaultHeight:
+            self.appendLine("row height type: default")
+        else:
+            self.appendLine("row height type: custom")
+
+
 # -------------------------------------------------------------------
 # CT - Change Tracking
 

Modified: trunk/scratch/sc-xlsutil/src/stream.py
==============================================================================
--- trunk/scratch/sc-xlsutil/src/stream.py	(original)
+++ trunk/scratch/sc-xlsutil/src/stream.py	Mon Jan 28 04:40:38 2008
@@ -56,7 +56,6 @@
     0x0060: ["TEMPLATE", "Workbook Is a Template"],
     0x0063: ["OBJPROTECT", "Objects Are Protected"],
     0x007D: ["COLINFO", "Column Formatting Information"],
-    0x007E: ["RK", "Cell Value"],
     0x007F: ["IMDATA", "Image Data"],
     0x0080: ["GUTS", "Size of Row and Column Gutters"],
     0x0081: ["WSBOOL", "Additional Workspace Information"],
@@ -161,12 +160,12 @@
     0x01BC: ["PROT4REVPASS", "Shared Workbook Protection Password"],
     0x01BE: ["DV", "Data Validation Criteria"],
     0x0200: ["DIMENSIONS", "Cell Table Size"],
-    0x0201: ["BLANK", "Cell Value"],
+    0x0201: ["BLANK", "Blank Cell", record.Blank],
     0x0203: ["NUMBER", "Floating-Point Cell Value", record.Number],
     0x0204: ["LABEL", "Cell Value"],
     0x0205: ["BOOLERR", "Cell Value"],
     0x0207: ["STRING", "String Value of a Formula"],
-    0x0208: ["ROW", "Describes a Row"],
+    0x0208: ["ROW", "Describes a Row", record.Row],
     0x020B: ["INDEX", "Index Record"],
     0x0218: ["NAME", "Defined Name"],
     0x0221: ["ARRAY", "Array-Entered Formula"],
@@ -175,6 +174,7 @@
     0x0231: ["FONT", "Font Description"],
     0x0236: ["TABLE", "Data Table"],
     0x023E: ["WINDOW2", "Sheet Window Information"],
+    0x027E: ["RK", "Cell with Encoded Integer or Floating-Point", record.RK],
     0x0293: ["STYLE", "Style Information"],
     0x041E: ["FORMAT", "Number Format"],
     0x0809: ["BOF", "Beginning of File", record.BOF],



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]