home *** CD-ROM | disk | FTP | other *** search
- /*
- Listing 9.18. Loads an array from a disk file.
- Listing 9.19. Handles loading of a single array dimension.
- Author: Greg Lief / Craig Yellick
- Excerpted from "Clipper 5: A Developer's Guide"
- Copyright (c) 1991 M&T Books
- 501 Galveston Drive
- Redwood City, CA 94063-4728
- (415) 366-3600
- */
-
- function RestArray(fileName)
- /*
- General-purpose function for restoring an array from a disk
- file. This is only the public interface. The real work is done
- by recursive calls to the ElementIn() function.
-
- Given the name of a file created by the SaveArray() function,
- read the file construct an array to return.
-
- n_ := RestArray("NUMS.ARY")
-
- This function returns a reference to the array it creates.
- */
-
- local handle, a_ := {}
-
- if (handle := fopen(fileName)) != -1
- ElementIn(handle, a_)
- endif
- fclose(handle)
-
- return a_
-
-
- static function ElementIn(handle, a_)
- /*
- Given a file handle and a reference to an array, read elements
- from the file and add them to the array.
-
- The file must be created by the SaveArray() function and be
- structured as described in the SaveArray() comments.
-
- Nested arrays are detected and sub-arrays stored in the main
- array as needed. The file reading for a sub-array is accomplished
- through another call to ElementIn(). This is known as recursion.
-
- This is a static function and therefore not visible to any
- functions outside of the source code file containing it. All
- calls from the outside must be made to RestArray().
- */
-
- local buffer, i, cnt, iLen, iType
-
- // Read the overall array size
- buffer := space(2)
- if fread(handle, @buffer, 2) = 2
-
- // Process each array element stored in the file.
- cnt := bin2w(buffer)
- for i = 1 to cnt
-
- // Read the element's data type.
- // If element is a nested array-- recursion time!
- //
- if (iType := freadstr(handle,1)) = "A"
- aadd(a_, {})
- ElementIn( handle, a_[ len(a_) ] )
-
- else
- // Read the length of the element.
- buffer := space(2)
- if fread(handle, @buffer, 2) = 2
- iLen := bin2w(buffer)
-
- // Read the actual element.
- buffer := space(iLen)
- if fread(handle, @buffer, iLen) = iLen
-
- // Convert from string to specified data type.
- do case
- /*
- Note that we can't save code blocks. If you
- attempted to save one from an array, we will have
- empty space and thus must add a NIL to serve
- as a placeholder.
- */
- case (iType = "B") .or. (iType = "Z")
- aadd(a_, nil)
- case iType = "C"
- aadd(a_, buffer)
- case iType = "D"
- aadd(a_, ctod(buffer))
- case iType = "L"
- aadd(a_, (buffer == "T"))
- case iType = "N"
- aadd(a_, val(buffer))
- endcase
- endif
- endif
- endif
- next i
- endif
- return nil
-
- // end of file CHP0918.PRG
-