Home › Forums › General › News and releases › 1231 – replaceFileContentWithData missing
Tagged: save to file
- This topic has 8 replies, 3 voices, and was last updated 11 years, 3 months ago by Hecticcc.
-
AuthorPosts
-
January 26, 2013 at 10:10 pm #5852
I am trying to save some data to disk using an example from elsewhere on the forum, and when Lua wants to call replaceFileContentWithData it returns
<code>attempt to call ‘replaceFileContentWithData’ (a nil value)
</code>
I tried using the new what() on the file object and the functions returned do not list replaceFileContentWithData:
<code>
__init: function
getFileName: function
hasFileExtension: function
getFileExtension: function
isOnHardDisk: function
hasWriteAccess: function
getNonexistentChildFile: function
replaceWithData: function
getRelativePathFrom: function
getNonexistentSibling: function
setReadOnly: function
getVolumeTotalSize: function
getSize: function
getSiblingFile: function
getCreationTime: function
hashCode64: function
hashCode: function
getVolumeLabel: function
getParentDirectory: function
moveToTrash: function
exists: function
findChildFiles: function
loadFileAsString: function
getFileNameWithoutExtension: function
withFileExtension: function
create: function
getNumberOfChildFiles: function
getVersion: function
hasIdenticalContentTo: function
setLastAccessTime: function
deleteRecursively: function
replaceWithText: function
isOnRemovableDrive: function
createDirectory: function
getLinkedTarget: function
getVolumeSerialNumber: function
isAChildOf: function
getChildFile: function
isDirectory: function
deleteFile: function
getLastModificationTime: function
getLastAccessTime: function
isHidden: function
setCreationTime: function
moveFileTo: function
copyFileTo: function
existsAsFile: function
getFullPathName: function
startAsProcess: function
setAsCurrentWorkingDirectory: function
loadFileAsData: function
isOnCDRomDrive: function
setLastModificationTime: function
containsSubDirectories: function
getBytesFreeOnVolume: function
revealToUser: function
appendData: function
appendText: function</code>
January 26, 2013 at 10:46 pm #5853Now it’s replaceWithData
January 26, 2013 at 11:00 pm #5854lol i just noticed 🙂
But i am having trouble with it, i cannot seem to be bale to save the contents of a memoryblock, if it try i get “no matching overload found”
Example:
In a script:
data = somevalue
mb = CtrlrLuaMemoryBlock
mb:append(data)
saveToFile(mb)
in the saveToFile script:
function saveToFile(dataToSave)-- By default the dialog window will ask for confirmation on overwrites, so we don't need to check
-- You can replace the File("").getSpecialLocation(File.userDesktopDirectory) with some other file location
-- that can be remembered across calls so that you hint the user his last browsed dir or some other location--x = string.format(dataToSave)
file = utils.saveFileWindow ("Save file", File(""), "*.emu", true)
if not file:hasFileExtension(String ("emu")) then
-- this is stupid the File() should not be necessary but luabind does not handle type conversions wellfile = File (file:withFileExtension(String ("emu")))
endif file:hasWriteAccess() then
-- We can write to that locationfile:replaceWithData(mb)
end
debug (what(file))
end
If i do not use a memoryblock and save the data as textstring it works…
January 26, 2013 at 11:14 pm #5855Try this:
data1 = 10
data2 = 20
mb = MemoryBlock()
mb:insert(mb,data1)
mb:insert(mb,data2)
console (mb:toHexString(1))
if configFile:existsAsFile() then -- your file here
configFile:replaceWithData(mb)
endJanuary 27, 2013 at 12:00 am #5856Check what(mb) i can’t see it initialized in the saveToFile() function
January 27, 2013 at 8:32 am #5860Thanks guys, gonna try it later today when my son is taking his nap 🙂
Will update with results.January 27, 2013 at 3:20 pm #5868Got it working, thanks guys 🙂
I was declaring the memoryblock in the script that holds the values to be saved, and tried to pass it as an argument to saveToFile instead of making it in the saveToFile script itself.
I did have to do a little trickery to get it working as it looks like passing userdata as argument for a function is not possible.I want to save the result of a midi:getRange() but if i try i get errors. Worked around by making it a table and then it works as expected.
Now it looks like this:
originating script:
l = midi:getSize()if l == 255 then --programdump
-- make table from midimessage
t = {}
for i =1, l-1 do -- -1 remove header
byte = midi:getLuaData():getByte(i)
table.insert(t,i,byte)
endsaveToFile(t)
endIn saveToFile:
function saveToFile(data)mb = MemoryBlock()
mb:createFromTable(data)
file = utils.saveFileWindow ("Save file", File("test"), "*.emu", true)if not file:hasFileExtension(String ("emu")) then
-- this is stupid the File() should not be necessary but luabind does not handle type conversions wellfile = File (file:withFileExtension(String ("emu")))
endif file:hasWriteAccess() then
-- We can write to that location
file:replaceWithData(mb)
end
end- This reply was modified 11 years, 3 months ago by Hecticcc. Reason: tags
- This reply was modified 11 years, 3 months ago by Hecticcc. Reason: code tags
- This reply was modified 11 years, 3 months ago by Hecticcc. Reason: trying to edit tags for code to be correct
- This reply was modified 11 years, 3 months ago by Hecticcc.
January 27, 2013 at 4:01 pm #5873I did have to do a little trickery to get it working as it looks like passing userdata as argument for a function is not possible.
That’s impossible passing ANYTHING as a parameter to any function is valid and will work.
January 27, 2013 at 5:25 pm #5874If i try this in saveToFile
mb=MemoryBlock()
mb:append(data) -- getrange result passed as parameter
<\code>It gives an error. Same if i try mb:insert(mb,data)
-
AuthorPosts
- The forum ‘News and releases’ is closed to new topics and replies.