LC7 arrayEncode/Decode

Peter M. Brigham pmbrig at gmail.com
Sat May 2 22:27:15 EDT 2015


I have this in my library, s\collected at sone point from this list from Phil Davis. Don't know if this will still work on more recent versions of LC.

-- Peter

Peter M. Brigham
pmbrig at gmail.com
http://home.comcast.net/~pmbrig

---------

on arrayToFile pArray, pSaveToWhere
   -- saves an array to disk, base64encoded
   -- the array can be retrieved by arrayFromFile(), which is the inverse function
   -- if char 1 of pSaveToWhere = "/" then treats pSaveToWhere as a valid filePath,
   --    otherwise treats pSaveToWhere as a prompt for an ask file dialog
   --    if pSaveToWhere is empty then will use a generic prompt
   -- if the save is successful the filepath will be returned in the result,
   --    in case you need to store a user-chosen filepath
   --    if an error occurs, the result will contain the error
   --       check: if char 1 of the result <> "/" then...
   -- from Phil Davis, LC-use list,
   --    revised by Peter M. Brigham, pmbrig at gmail.com
   -- requires arrayFromFile()
   
   if (the keys of pArray = empty) then
      return "arrayToFile(): this is not an array"
   end if
   if char 1 of pSaveToWhere <> "/" then
      if pSaveToWhere = empty then
         put "Name your file:" into tPrompt
      else
         if char -1 of pSaveToWhere <> ":" then put ":" after pSaveToWhere
         put pSaveToWhere into tPrompt
      end if
      ask file tPrompt
      if it = empty then exit to top
      put it into pSaveToWhere
   end if
   put base64encode(arrayEncode(pArray)) into tFileData
   put tFileData into url ("file:" & pSaveToWhere)
   if the result <> empty then -- an error occurred
      return "error while saving file:" & cr & the result & cr & pSaveToWhere
   else
      return pSaveToWhere
   end if
end arrayToFile

function arrayFromFile pFilePath
   -- rebuilds an array from a file saved with arrayToFile
   -- if first char of pFilePath = "/" then treats pFilePath as a valid filepath
   --    otherwise treats pFilePath as a prompt for an answer file dialog
   --    if pFilePath is empty then will use a generic prompt
   -- from Phil Davis, LC-use list
   --    revised by Peter M. Brigham, pmbrig at gmail.com
   -- check that the returned value is an array,
   --    if not then it will contain an error message
   -- requires arrayToFile
   
   if char 1 of pFilePath <> "/" then
      if pFilePath = empty then
         put "Choose your array file:" into tPrompt
      else
         if char -1 of pFilePath <> ":" then put ":" after pFilePath
         put pFilePath into tPrompt
      end if
      answer file tPrompt
      if it = empty then exit arrayFromFile
      put it into pFilePath
   end if
   if not (there is a file pFilePath) then
      return "file not found:" & cr & pFilePath
      exit to top
   end if
   put url ("file:" & pFilePath) into tData
   put the result into tResult
   if tResult <> empty then
      return "error in importing array file:" & cr & tResult & cr & pFilePath
      exit to top
   end if
   -- we got the file, unpack, decode, and return it
   put base64decode(tData) into tArrayData
   put the result into tResult
   if tResult <> empty then
      return "could not base64decode:" & cr & tResult & cr & pFilePath
      exit to top
   end if
   wait 0 seconds
   put arrayDecode(tArrayData) into tArrayA
   put the result into tResult
   if tResult <> empty then
      return "could not arrayDecode:" & cr & tResult & cr & pFilePath
      exit to top
   end if
   wait 0 seconds
   return tArrayA
end arrayFromFile

On May 2, 2015, at 8:48 PM, Peter Haworth wrote:

> I haven't come across that yet but probably because I base64 encode the
> array as well as array encode it and don't write to the file in binary mode.
> 
> Which raises another question.  I'm converting the app to 7.0 to be unicode
> compliant.  The data in the array will have been textDecoded format. Do I
> need to textEncode it before base64/arrayencoding it?
> 
> Pete
> lcSQL Software <http://www.lcsql.com>
> Home of lcStackBrowser <http://www.lcsql.com/lcstackbrowser.html> and
> SQLiteAdmin <http://www.lcsql.com/sqliteadmin.html>
> 
> On Sat, May 2, 2015 at 5:06 PM, William Prothero <prothero at earthednet.org>
> wrote:
> 
>> I found an oddity with arrayEncode and arrayDecode, When I saved an array
>> to a file, first using arrayEncode(myArray), wrote it out as
>> 
>> put arrayEncode(myArray) into theEncodedArray
>> put theEncodedArray into URL(“binfile:”&myfile)
>> 
>> and then read it back in using
>> put URL(“binfile:”&myFile) into theEncodedArray
>> put arrayDecode(theEncodedArray) into myArray
>> 
>> myArray has the first element as a blank entry. It’s not the same array as
>> I started with.
>> 
>> My array looks like myArray[1][“name”], myArray[2][“name”], etc
>> 
>> This seems like a bug to me. I didn’t get any satisfaction when I set the
>> storage method as text, but then again, I couldn’t find a lesson where that
>> format was specified. But, it seems like whether it’s binary or not, it
>> should still read back in and decode correctly.
>> 
>> Regards,
>> Bill
>> 
>> 
>> 
>>> On May 2, 2015, at 2:11 PM, Mark Talluto <userev at canelasoftware.com>
>> wrote:
>>> 
>>> On May 2, 2015, at 10:48 AM, Peter Haworth <pete at lcsql.com> wrote:
>>> 
>>>> However, there is no version parameter for the arrayDecode function.
>> Does
>>>> this mean that LC7 can detect the encoding of the array and decode it
>>>> appropriately?
>>> 
>>> 
>>> Yes indeed!
>>> 
>>> 
>>> Best regards,
>>> 
>>> Mark Talluto
>>> livecloud.io
>>> canelasoftware.com
>>> 
>>> _______________________________________________
>>> use-livecode mailing list
>>> use-livecode at lists.runrev.com
>>> Please visit this url to subscribe, unsubscribe and manage your
>> subscription preferences:
>>> http://lists.runrev.com/mailman/listinfo/use-livecode
>> 
>> 
>> _______________________________________________
>> use-livecode mailing list
>> use-livecode at lists.runrev.com
>> Please visit this url to subscribe, unsubscribe and manage your
>> subscription preferences:
>> http://lists.runrev.com/mailman/listinfo/use-livecode
>> 
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode





More information about the use-livecode mailing list