home *** CD-ROM | disk | FTP | other *** search
- *****************************************************************
- FUNCTION ARRESTORE (arr_name, arr_file)
- *****************************************************************
-
- * Restores an array from a disk file created by ARSAVE
-
- * Copyright(c) 1991 -- James Occhiogrosso
-
- LOCAL arr_string, counter, data_end, dec_pos, elem_delim, ;
- elem_type, num_dec, num_len, str_num
-
- * Define element delimiter as a null byte
- elem_delim = CHR(255)
-
- * Return error if any parameter is incorrect.
- IF PCOUNT() != 2 .OR. VALTYPE(arr_file) != 'C' .OR. ;
- VALTYPE(arr_name) != 'A'
- RETURN .F.
- ENDIF
-
- * If extension not passed, default to .ARR
- IF AT('.', arr_file) = 0
- arr_file = arr_file + '.ARR'
- ENDIF
-
- IF FILE(arr_file)
-
- * Load the file into a string
- arr_string = MEMOREAD(arr_file)
-
- * Test first five bytes. If file was created by ARSAVE,
- * bytes 1-4 are array length and VAL will return
- * a number. Byte five must equal the element delimiter.
-
- IF .NOT. VAL(SUBSTR(arr_string,1,4)) > 0 .AND. ;
- elem_delim = SUBSTR(arr_string,5,1)
- * Return false if text file not created by ARSAVE
- RETURN(.F.)
- ENDIF
- ELSE
- * Return false if text file does not exist
- RETURN(.F.)
- ENDIF
-
- * Set size of array to match stored element count
- ASIZE(arr_name, VAL(SUBSTR(arr_string,1,4)) )
-
- * Each iteration of the FOR/NEXT loop below gets an
- * element from the string and places it in the array.
-
- arr_string = SUBSTR(arr_string, 6)
-
- FOR counter = 1 TO LEN(arr_name)
-
- * Get element type (first byte) and null position
- elem_type = LEFT(arr_string,1)
- data_end = AT(elem_delim, arr_string)
-
- IF elem_type = 'C'
- * Character element. No conversion required.
- arr_name[counter] = SUBSTR(arr_string, 2, data_end-2)
-
- ELSEIF elem_type = 'D'
- * Date element. Convert to a date value.
- arr_name[counter] = CTOD(SUBSTR(arr_string,2,data_end-2))
-
- ELSEIF elem_type = 'L'
- * Logic element. Convert to a logical value.
- arr_name[counter] = IF(SUBSTR(arr_string, 2, 1) = 'T', ;
- .T., .F.)
- ELSEIF elem_type = 'N'
- * Numeric element. Initialize length and decimal places.
- num_len = VAL(SUBSTR(arr_string, 2, 3))
- num_dec = VAL(SUBSTR(arr_string, 5, 3))
- arr_name[counter] = REPLICATE('0', (num_len-1)-num_dec) ;
- + '.' + REPLICATE('0', num_dec)
- arr_name[counter] = VAL(SUBSTR(arr_string,8,data_end-8))
-
- ENDIF
- arr_string = SUBSTR(arr_string, data_end+1)
- NEXT
-
- * Return true if array length is greater than zero,
- * and the original string is empty
-
- RETURN ( LEN(arr_name) > 0 .AND. EMPTY(arr_string) )
-
-
-