Next Previous Up Top Contents Index

5.3.1 Handling files in the task list manager

5.3.1.4 The load-task-list function

The code for load-task-list is shown below. Because this function does not use any DUIM code, it is described only briefly.

define function load-task-list
   (filename :: <string>) => (task-list :: false-or(<task-list>))
  let tasks = make(<stretchy-vector>);
  block (return)
    with-open-file (stream = filename, direction: #"input")
      while (#t)
        let name = read-line(stream, on-end-of-stream: #f);
        unless (name) return() end;
        let priority = read-line(stream, on-end-of-stream: #f);
        unless (priority) 
          error("Unexpectedly missing priority!") 
        end;
        let task = make(<task>, name: name, 
                        priority: as(<symbol>, priority));
        add!(tasks, task)
      end
    end
  end;
  make(<task-list>, tasks: tasks, filename: filename)
end function load-task-list;

Add this code to task-list.dylan.

The function load-task-list reads a file from disk and attempts to convert its contents into an instance of <task-list>, which itself contains any number of instances of <task>. It takes one argument, the filename, and returns one value, the instance of <task-list>.

This function uses a generic function and a macro from the Streams library to read information from the file. For full information about this library, please refer to the I/O and Networks Library Reference.

The file format used by the task list manager is very simple, with each element of a task occupying a single line in the file. Suppose load-task-list is called on a file containing the following information:

Wash the dog
medium
Video Men Behaving Badly
high

This would create an instance of <task-list> whose task-list-tasks slot was a sequence of two instances of <task>.

The task-list-filename slot of the <task-list> is the filename itself. Note that the task-list-modified? slot of the <task-list> is set to #f, reflecting the fact that the task list is loaded, but unchanged. This does not have to be done explicitly by load-task-list, since #f is the default value of this slot, as you can see from its definition in Section 5.1 on page 42.

The file is opened for reading using the with-open-file macro. It is then read a line at a time, setting the local variables name and priority with each alternate line. After successfully setting both name and priority, an instance of <task> is created, and added to the stretchy vector tasks using add!. When the end of the file is reached, #f is returned and an instance of <task-list> is created from tasks and returned by the function.

Note how the as method is used to convert a string value such as "medium" into a symbol such as #"medium". This is a useful technique to use when you wish to save and load symbol information in an application.


Building Applications Using DUIM - 26 May 1999

Next Previous Up Top Contents Index