Problems with arrayencode/decode

Phil Davis revdev at pdslabs.net
Wed Jan 2 23:33:51 EST 2013


Hi Pete,

Here's what I do on a regular basis (with names changed to protect the 
innocent - Sgt. Friday would be pleased). I use "get url" instead of 
open/read/close" but it seems to me either should work. I don't know if 
the 'waits' in my code make much difference:


put arrayFromFile(tMyFilePath) into tMyArray
put "new stuff" into tMyArray["buckets"][15]
SaveArrayToFile tMyArray, tMyFilePath



command SaveArrayToFile pArray, pFilePath
     if (the keys of pArray = empty) or (pFilePath = empty) then
        reportError "Bad param"
        exit to top
     end if

     put base64Encode(arrayEncode(pArray)) into tFileData
     put tFileData into url ("file:" & pFilePath)
     if the result <> empty then -- an error occurred
        reportError "Error while saving file"
        exit to top
     end if
end SaveArrayToFile



function ArrayFromFile pFilePath
     if there is a file pFilePath
     then -- unpack it and return it

         get url ("file:" & pFilePath)
         if the result = empty -- we got the file
         then -- decode & return the array
             put base64Decode(it) into tArrayData
             if the result <> empty then
                 reportError "Could not base64Decode"
                 exit to top
             end if

             wait 0 seconds -- just for fun
             put arrayDecode(tArrayData) into tArrayA
             if the result <> empty then
                 reportError "Could not arrayDecode"
                 exit to top
             end if

             wait 0 seconds -- just for fun
             return tArrayA
         else -- exit with error
             reportError kMsgCouldNotReadArrayFile
             exit to top
         end if

     else -- no file was found

         reportError "file not found:" && pFilePath
         return empty

     end if
end ArrayFromFile


HTH -
Phil Davis



On 1/2/13 6:55 PM, Peter Haworth wrote:
> I'm writing an arrayencoded array to a disk file using write to file.  When
> I read the file back into memory with read from file until EOF and then try
> to arraydecode it, I get a runtime error "arraydecode:failue".  In debug, I
> can see data appear in it right after the read.
>
> I've tried all possible combinations of reading/writing binary and also
> base64encoding the arrayencoded array. Same result every time.
>
> The only thing I think might be a problem is that after reading the file,
> the result is set to "eof".  It's a little hard to tell from the
> dictionary whether I should expect that when I tell the read to read until
> eof.  If it is an indication that the read encountered eof before
> completing, I can see that the arraydecode would fail but I'm not sure why
> that would happen.
>
> Any ideas?
>
> Pete
> lcSQL Software <http://www.lcsql.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
>

-- 
Phil Davis





More information about the Use-livecode mailing list