Package tree

Solution to "how to convert a file to a list?"

Author

Damien Diederen  <dd@crosstwine.com>

Overview

Cf. http://tech.groups.yahoo.com/group/skill_school/message/2262 for a (succinct) description of the problem. Function MyTreeFromFile implements the requested functionality:

(println (MyTreeFromFile "./my-file.dat"))
=> (a (b) c (d (f)))

References:

From: *redacted*
Date: Sun, 06 Jan 2013 17:09:50 +0800
Subject: how to convert a file to a list?
List-Id: <skill_school.yahoogroups.com>
Message-ID: <op.wqhfqojhuwf9wv@hamp-sz-0073.hamppcb.com>

Index

Unexported variable chompCharacterBag
Unexported variable indentCharacterBag
Unexported function DepthAndData (line)
Unexported function EmptyTconc
Function MyTreeFromFile (filename)
Function MyTreeFromLiner (liner)
Unexported function PortLiner (port)
Unexported function TconcData (tc)
Unexported function TransformData (data)
Unexported function TrimLeftIndex (characterBag string a b)
Unexported function TrimRightIndex (characterBag string a b)

Package files

tree.ils

Details

Unexported variable chompCharacterBag

(define chompCharacterBag ...)

Characters trimmed on the right.

Unexported variable indentCharacterBag

(define indentCharacterBag ...)

Characters trimmed on the left and determining a particular line's "depth."

Unexported function DepthAndData

(defun DepthAndData (line) ...)

DepthAndData transforms a "raw" line into a list of two values: its integer depth, as indicated by the number of characters from indentCharacterBag, and the rest of the line, right-trimmed with chompCharacterBag.

Unexported function EmptyTconc

(defun EmptyTconc () ...)

EmptyTconc returns a new, empty "tconc structure." Used for code readability; cf. tconc's documentation for a description of tconc structures.

Function MyTreeFromFile (exported)

(defun TreeFromFile (filename) ...)

Builds a tree (list of lists and symbols) by reading filename.

Levels are determined by looking at the number of dots (which must be >= 1) in front of each line. The rest of each line, after the dots and up to the return carriage, if any, is transformed to a symbol and inserted as a leaf at the right position. Example:

.a
..b
.c      =>     (a (b) c (d (f)))
..d
...f

Cf. package commentary for additional details.

Function MyTreeFromLiner (exported)

(defun TreeFromLiner (liner) ...)

Builds a tree from liner, a function which returns "raw" lines, or nil on "EOF." Cf. TreeFromFile and package commentary for details.

Unexported function PortLiner

(defun PortLiner (port) ...)

Builds a "liner" (a function which returns "raw" lines and nil on EOF), from port.

Unexported function TconcData

(defun TconcData (tc) ...)

TconcData retrieves the newly-built list from "tconc structure" tc. Used for code readability; cf. tconc's documentation for a description of tconc structures.

Unexported function TransformData

(defun TransformData (data) ...)

According to the example, tree leaves ought to be symbols.

Unexported function TrimLeftIndex

(defun TrimLeftIndex (characterBag string a b) ...)

TrimLeftIndex indicates how to trim the substring of string delimited by a and b to remove all leading characters contained in characterBag.

Both a and b are inclusive. Use 1 and (strlen string) to trim the whole string.

Returns the starting index in the resulting substring.

Unexported function TrimRightIndex

(defun TrimRightIndex (characterBag string a b) ...)

TrimLeftIndex indicates how to trim the substring of string delimited by a and b to remove all trailing characters contained in characterBag.

Both a and b are inclusive. Use 1 and (strlen string) to trim the whole string.

Returns the last index in the resulting substring.