--------------------------------------------------------------------- -- Andy Drummond 1 September 2004 -- -- This routine can be used in place of Euphoria's print routine -- to print arbitrary Euphoria objects in a single invocation while -- remaining able to read the object back in using a single get() call -- of Euphoria. -- The pretty_print() routine does not allow the use of get() to reload -- an arbitrary Euphoria object due to the addition of formatting newlines. -- The result is far more readable than a standard Euphoria print while -- remaining in the most basic Euphoria format as is required by get(). -- It also saves a lot of disk space when large ASCII-oriented objects -- such as databases are saved. global procedure tidy_print(int fp, object data) int fh, IsFirst, IsASCII -- Initial call to tidy_print has a positive file pointer. -- This routine calls itself recursively with a negative value. -- Thus we know on entry if we were the initial call or a sub-call. -- If it was the initial call then we add a newline to the end. if fp > 0 then IsFirst = True -- this is the first-level call fh = fp -- so access file with the handle as supplied fp = -fp -- but negate for future calls else IsFirst = False -- this is a subsequent call fh = -fp -- access the file with the positive of the argument fp end if -- If the object is an atom, print it as normal if atom(data) then print(fh, data) -- If a sequence then call this routine for every element of it ... -- but first either enclose the data in braces or quotes depending ... elsif sequence(data) then IsASCII = True -- Look to see if this is a basic ASCII string first of all -- (The test could be widened if you wish to allow larger integers as being ASCII) for i= 1 to length(data) do if integer(data[i]) then if data[i] < ' ' or data[i] > 126 then IsASCII = False exit end if else IsASCII = False exit end if end for -- If ASCII then print as a string in quotes.. if IsASCII then puts(fh,"\"") puts(fh, data) puts(fh,"\"") -- Else print as a sequence of objects in braces, using tidy_print recursively else puts(fh, "{") for i=1 to length(data) do tidy_print(fp, data[i]) if i < length(data) then puts(fh,",") end if end for puts(fh,"}") end if end if -- Having done that, if this was the initial call to the routine we add newline if IsFirst then puts(fh, "\n") end if end procedure