#REFACTORING by cg
authorClaus Gittinger <cg@exept.de>
Fri, 04 May 2018 01:26:51 +0200
changeset 4645 6e70eaa6130f
parent 4644 a55695d86845
child 4646 b4c4cac968f5
#REFACTORING by cg class: MacPlistBinaryDecoder changed: #decodedObject class: MacPlistBinaryDecoder class comment/format in: #examples
MacPlistBinaryDecoder.st
--- a/MacPlistBinaryDecoder.st	Wed May 02 16:07:43 2018 +0200
+++ b/MacPlistBinaryDecoder.st	Fri May 04 01:26:51 2018 +0200
@@ -1,3 +1,5 @@
+"{ Encoding: utf8 }"
+
 "
  COPYRIGHT (c) 2015 by Claus Gittinger
               All Rights Reserved
@@ -65,7 +67,7 @@
 "
    restore:
                                                                 [exBegin]
-     |inFile d decodedObject|
+     |inFile decodedObject|
 
      inFile := '/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/German.lproj/InfoPlist.strings'.
      inFile := '/Applications/TextEdit.app/Contents/Resources/German.lproj/Localizable.strings'.
@@ -73,6 +75,14 @@
      decodedObject := (MacPlistBinaryDecoder on:inFile asFilename binaryContentsOfEntireFile) decodedObject.
      decodedObject inspect.
                                                                 [exEnd]
+    on my system, this is definitely a binary file:
+                                                                [exBegin]
+     |inFile decodedObject|
+
+     inFile := '/Library/Preferences/com.apple.Bluetooth.plist' asFilename readStream.
+     decodedObject := (MacPlistBinaryDecoder on:inFile asFilename binaryContentsOfEntireFile) decodedObject.
+     decodedObject inspect.
+                                                                [exEnd]
 
 "
 ! !
@@ -95,17 +105,25 @@
     tmpFile := Filename newTemporary.
     [
         tmpFile contents:inputStream upToEnd.
-        tmpXMLFile := Filename newTemporary.
-        OperatingSystem executeCommand:('plutil -convert xml1 -o %2 %1' bindWith:tmpFile pathName with:tmpXMLFile pathName).
-        tmpXMLFile readingFileDo:[:s |            
-            "/ to avoid package dependency from libbasic2 to goodies/xml
-            "/ see package loading on instance-side
-            decoder := (Smalltalk at:#MacPlistXMLDecoder) on:s.
-            object := decoder decodedObject.
-        ].    
+        (OperatingSystem isOSXlike and:[OperatingSystem canExecuteCommand:'plutil']) ifTrue:[
+            "/ use the plutil command to convert from binary to XML,
+            "/ then read the XML file    
+            tmpXMLFile := Filename newTemporary.
+            OperatingSystem executeCommand:('plutil -convert xml1 -o %2 %1' bindWith:tmpFile pathName with:tmpXMLFile pathName).
+            tmpXMLFile readingFileDo:[:s |            
+                "/ to avoid package dependency from libbasic2 to goodies/xml
+                "/ see package loading on instance-side
+                decoder := (Smalltalk at:#MacPlistXMLDecoder) on:s.
+                object := decoder decodedObject.
+            ].    
+        ] ifFalse:[
+            "/ use the 100% Smalltalk binary plist reader 
+            "/ (which may or may not work 100%, as it is written after reverse engineering the file structure)
+            object := MacBinaryPListFileReader readFrom:(tmpFile readStream).
+        ].        
     ] ensure:[
         tmpFile remove.
-        tmpXMLFile remove.
+        tmpXMLFile notNil ifTrue:[tmpXMLFile remove].
     ].    
     ^ object.