home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a012 / 1.ddi / CHAP09.EXE / CHP0918.PRG < prev    next >
Encoding:
Text File  |  1991-06-01  |  3.1 KB  |  107 lines

  1. /*
  2.    Listing 9.18. Loads an array from a disk file.
  3.    Listing 9.19. Handles loading of a single array dimension.
  4.    Author: Greg Lief / Craig Yellick
  5.    Excerpted from "Clipper 5: A Developer's Guide"
  6.    Copyright (c) 1991 M&T Books
  7.                       501 Galveston Drive
  8.                       Redwood City, CA 94063-4728
  9.                       (415) 366-3600
  10. */
  11.  
  12. function RestArray(fileName)
  13. /*
  14.    General-purpose function for restoring an array from a disk
  15.    file. This is only the public interface. The real work is done
  16.    by recursive calls to the ElementIn() function.
  17.  
  18.    Given the name of a file created by the SaveArray() function,
  19.    read the file construct an array to return.
  20.  
  21.      n_ := RestArray("NUMS.ARY")
  22.  
  23.    This function returns a reference to the array it creates.
  24. */
  25.  
  26. local handle, a_ := {}
  27.  
  28.   if (handle := fopen(fileName)) != -1
  29.      ElementIn(handle, a_)
  30.   endif
  31.   fclose(handle)
  32.  
  33. return a_
  34.  
  35.  
  36. static function ElementIn(handle, a_)
  37. /*
  38.   Given a file handle and a reference to an array, read elements
  39.   from the file and add them to the array.
  40.  
  41.   The file must be created by the SaveArray() function and be
  42.   structured as described in the SaveArray() comments.
  43.  
  44.   Nested arrays are detected and sub-arrays stored in the main
  45.   array as needed. The file reading for a sub-array is accomplished
  46.   through another call to ElementIn(). This is known as recursion.
  47.  
  48.   This is a static function and therefore not visible to any
  49.   functions outside of the source code file containing it. All
  50.   calls from the outside must be made to RestArray().
  51. */
  52.  
  53. local buffer, i, cnt, iLen, iType
  54.  
  55. //  Read the overall array size
  56. buffer := space(2)
  57. if fread(handle, @buffer, 2) = 2
  58.  
  59.    //  Process each array element stored in the file.
  60.    cnt := bin2w(buffer)
  61.    for i = 1 to cnt
  62.  
  63.       //  Read the element's data type.
  64.       //  If element is a nested array-- recursion time!
  65.       //
  66.       if (iType := freadstr(handle,1)) = "A"
  67.         aadd(a_, {})
  68.         ElementIn( handle, a_[ len(a_) ] )
  69.  
  70.       else
  71.         //  Read the length of the element.
  72.         buffer := space(2)
  73.         if fread(handle, @buffer, 2) = 2
  74.           iLen := bin2w(buffer)
  75.  
  76.           //  Read the actual element.
  77.           buffer := space(iLen)
  78.           if fread(handle, @buffer, iLen) = iLen
  79.  
  80.             //  Convert from string to specified data type.
  81.             do case
  82.              /*
  83.                 Note that we can't save code blocks. If you
  84.                 attempted to save one from an array, we will have
  85.                 empty space and thus must add a NIL to serve
  86.                 as a placeholder.
  87.              */
  88.              case (iType = "B") .or. (iType = "Z")
  89.                 aadd(a_, nil)
  90.              case iType = "C"
  91.                 aadd(a_, buffer)
  92.              case iType = "D"
  93.                 aadd(a_, ctod(buffer))
  94.              case iType = "L"
  95.                 aadd(a_, (buffer == "T"))
  96.              case iType = "N"
  97.                 aadd(a_, val(buffer))
  98.              endcase
  99.           endif
  100.         endif
  101.       endif
  102.    next i
  103. endif
  104. return nil
  105.  
  106. // end of file CHP0918.PRG
  107.