home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a013 / 1.ddi / SOURCE.EXE / F_ARREST.PRG < prev    next >
Encoding:
Text File  |  1991-01-25  |  2.7 KB  |  89 lines

  1. *****************************************************************
  2. FUNCTION ARRESTORE  (arr_name, arr_file)
  3. *****************************************************************
  4.  
  5. * Restores an array from a disk file created by ARSAVE
  6.  
  7. * Copyright(c) 1991 -- James Occhiogrosso
  8.  
  9. LOCAL arr_string, counter, data_end, dec_pos, elem_delim, ;
  10.       elem_type, num_dec,  num_len, str_num
  11.  
  12. * Define element delimiter as a null byte
  13. elem_delim = CHR(255)
  14.  
  15. * Return error if any parameter is incorrect.
  16. IF PCOUNT() != 2 .OR. VALTYPE(arr_file) != 'C' .OR. ;
  17.                       VALTYPE(arr_name) != 'A'
  18.     RETURN .F.
  19. ENDIF
  20.  
  21. * If extension not passed, default to .ARR
  22. IF AT('.', arr_file) = 0
  23.     arr_file = arr_file + '.ARR'
  24. ENDIF
  25.  
  26. IF FILE(arr_file)
  27.  
  28.     * Load the file into a string
  29.     arr_string = MEMOREAD(arr_file)
  30.  
  31.     * Test first five bytes. If file was created by ARSAVE,
  32.     * bytes 1-4 are array length and VAL will return
  33.     * a number. Byte five must equal the element delimiter.
  34.  
  35.     IF .NOT. VAL(SUBSTR(arr_string,1,4)) > 0 .AND. ;
  36.              elem_delim = SUBSTR(arr_string,5,1)
  37.         * Return false if text file not created by ARSAVE
  38.         RETURN(.F.)
  39.     ENDIF
  40. ELSE
  41.     * Return false if text file does not exist
  42.     RETURN(.F.)
  43. ENDIF
  44.  
  45. * Set size of array to match stored element count
  46. ASIZE(arr_name, VAL(SUBSTR(arr_string,1,4)) )
  47.  
  48. * Each iteration of the FOR/NEXT loop below gets an
  49. * element from the string and places it in the array.
  50.  
  51. arr_string = SUBSTR(arr_string, 6)
  52.  
  53. FOR counter = 1 TO LEN(arr_name)
  54.  
  55.     * Get element type (first byte) and null position
  56.     elem_type = LEFT(arr_string,1)
  57.     data_end = AT(elem_delim, arr_string)
  58.  
  59.     IF elem_type = 'C'
  60.         * Character element. No conversion required.
  61.         arr_name[counter] = SUBSTR(arr_string, 2, data_end-2)
  62.  
  63.     ELSEIF elem_type = 'D'
  64.         * Date element. Convert to a date value.
  65.         arr_name[counter] = CTOD(SUBSTR(arr_string,2,data_end-2))
  66.  
  67.     ELSEIF elem_type = 'L'
  68.         * Logic element. Convert to a logical value.
  69.         arr_name[counter] = IF(SUBSTR(arr_string, 2, 1) = 'T', ;
  70.                                .T., .F.)
  71.     ELSEIF elem_type = 'N'
  72.         * Numeric element. Initialize length and decimal places.
  73.         num_len =  VAL(SUBSTR(arr_string, 2, 3))
  74.         num_dec =  VAL(SUBSTR(arr_string, 5, 3))
  75.         arr_name[counter] = REPLICATE('0', (num_len-1)-num_dec) ; 
  76.                             + '.' + REPLICATE('0', num_dec)
  77.         arr_name[counter] = VAL(SUBSTR(arr_string,8,data_end-8))
  78.  
  79.     ENDIF
  80.     arr_string = SUBSTR(arr_string, data_end+1)
  81. NEXT
  82.  
  83. * Return true if array length is greater than zero,
  84. * and the original string is empty
  85.  
  86. RETURN ( LEN(arr_name) > 0 .AND. EMPTY(arr_string) )
  87.  
  88.  
  89.