Eyeon:Script/Tutorials/Get Loader Filenames/Further Explorations of IO

From VFXPedia

< Eyeon:Script | Tutorials | Get Loader Filenames
Revision as of 04:16, 4 November 2008 by Daniel (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Lesson 2.4 : Further Exploration of IO

As stated in the previous exercise, the user may not want to delete the contents of the original file. Before writing to the file, we should see if it even exists first. To do that, we can use the fileexists function from the IO function set.

if fileexists("c:\\LoaderList.txt") == true then
else
    fh, errormessage =io.output("c:\\LoaderList.txt")
end

If it does exist, what we want to do is use the io.open method instead. When calling the io.open method, two pieces of information need to be passed on to the function. The first is the location of the file; the second is the write mode that should be employed.

There are six different modes:

  • "r" -- Read Mode
  • "w" -- Write Mode
  • "a" -- Append Mode
  • "r+" -- Update Mode; All previous data is preserved
  • "w+" -- Update Mode; All previous data is destroyed
  • "a+" -- Append Update Mode; All previous data preserved, writing only permitted at the end of the file.

The appropriate option in this case is "Append Update Mode" as it will preserve all the previous data in the text file. The code would then look like this:

if fileexists("c:\\LoaderList.txt") == true then
    fh, errormessage = io.open("c:\\LoaderList.txt","a+")
else
    fh, errormessage = io.output("c:\\LoaderList.txt")
end

The user might also want to know the original contents of the text document before the new text dump. To do that, we would use the fh:read method. The read method needs to know what kind of read mode is preferred.

The various read modes are as follows:

  • "*n" -- reads a number - this is the only format which returns a number instead of a string.
  • "*l" -- reads an entire line, (skipping the end of the line) and returns nil at the end of the file.
  • "*a" -- reads the entire file, starting from the current position. At the end of the file, it will return an empty string.
  • number -- Reads a string with the specified number of characters, or if the file is binary, with the specified number of bytes..

In this case, the entire file should be read and dumped to the console.

if fileexists("c:\\LoaderList.txt") == true then
    fh, errormessage = io.open("c:\\LoaderList.txt","a+")
    print("TEXT FROM FILE:\n"..fh:read("*a").."\n\nLOADER ClipS:")
else
    fh, errormessage = io.output("c:\\LoaderList.txt")
end

For the fun of it, try using io.open to open the file in question in the console. Now attempt to read it with the "*l" option. Try it a couple of times. Notice how each time the method is called with this option, it steps to a new line in the text file? This can be handy if ever the information that is being gathered from a file needs to be iterated through for storage in a table.


Below is the complete code from this lesson.

print("\n\n----------------------------------------------\nLIST
LOADER FOOTAGE:\n----------------------------------------------\n\n")
 
-- Define the file to write to.  Check to see if it exists.
if fileexists("c:\\LoaderList.txt") == true then
    -- If it does exist, append new information to the end of the file.
    fh, errormessage = io.open("c:\\LoaderList.txt","a+")
 
    -- Print all the information in the text file.
    print("TEXT FROM FILE:\n"..fh:read("*a").."\n\nLOADER ClipS:")
else
    fh, errormessage = io.output("c:\\LoaderList.txt")
end
 
-- check to see if it returned a value for fh
if not fh then -- if fh is equal to nil
    print(errormessage) 
    return
end
 
-- Get a list of all loaders
loaderlist = eyeon.GetLoaders(composition)
 
-- Cycle through all the loaders
for i, tool in loaderlist do
    -- Get the tool's Clip List attribute
    attrs = tool:GetAttrs()
 
    -- check to see if there's a clip list
    if attrs["TOOLST_Clip_Name"] then
        -- print the loader's name
        print(attrs.TOOLS_Name)
 
        -- cycle through that table  
        for j = 1, table.getn(attrs["TOOLST_Clip_Name"]) do
            -- print each entry to the console.
            print("-- "..attrs["TOOLST_Clip_Name"][j])
 
            -- Write our data.
            fh:write(attrs["TOOLST_Clip_Name"][j].."\n")
        end
    end
end
 
fh:close()
 
print("\nSCRIPT
COMPLETE:\n----------------------------------------------\n")


Tips for Further Explorations of IO (edit)

EyeonTips:Script/Tutorials/Get Loader Filenames/Further Explorations of IO