CAD-Lisp: configuration file syntax

There are plenty of ways to create configuration files. They all have pros and cons. What are the possibilities for CAD-Lisp?

Choosing a configuration file syntax

Win.ini is nice because of its simple structure. The downside is that it is limited, lacking array functionality for example. On the other side of the spectrum there is XML. Way to complex for simple tasks.

How about JSON?

For CAD-Lisp I often used Unix style configuration, and doing arrays in semicolon format. Nowadays JSON is popular and that looks a bit like structured lists. The example on https://en.wikipedia.org/wiki/JSON:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
{
  "firstName": "John",
  "lastName": "Smith",
  "isAlive": true,
  "age": 27,
  "address": {
    "streetAddress": "21 2nd Street",
    "city": "New York",
    "state": "NY",
    "postalCode": "10021-3100"
  },
  "phoneNumbers": [
    {
      "type": "home",
      "number": "212 555-1234"
    },
    {
      "type": "office",
      "number": "646 555-4567"
    },
    {
      "type": "mobile",
      "number": "123 456-7890"
    }
  ],
  "children": [],
  "spouse": null
}

Or simply… Lisp!

Nobody can deny the similarities found in Lisp. So it is very tempting to go back to basic Lisp format, simply because it supports all that Lisp supports, because, well, now we get to the core, it is Lisp in itself!

This is how the file looks, translated to Lisp format:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
(
   (firstName "John")
   (lastName "Smith")
   (isAlive T)
   (age 27)
   (address
      (
         (streetAddress "21 2nd Street")
         (city "New York")
         (state "NY")
         (postalCode "10021-3100")
      )
   )
   (phoneNumbers
      (
         (
            (type "home")
            (number "212 555-1234")
         )
         (
            (type "office")
            (number "646 555-4567")
         )
         (
            (type "mobile")
            (number "123 456-7890")
         )
      )
   (children ())
   (spouse nil)
)

Just like with JSON, the structure and nesting is immediately clear. Pros: simplest syntax. Cons: bracket matching is the only separation (that is a pro too).

And that can be consolidated to, for example, only main level entries without loosing information:

1
2
3
4
5
6
7
8
9
10
(
   (firstName "John")
   (lastName "Smith")
   (isAlive T)
   (age 27)
   (address ((streetAddress "21 2nd Street")(city "New York")(state "NY")(postalCode "10021-3100")))
   (phoneNumbers (((type "home")(number "212 555-1234"))((type "office")(number "646 555-4567"))((type "mobile")(number "123 456-7890")))
   (children ())
   (spouse nil)
)

Needless to say, a proper parenthesis aware editor, such as Notepad++ and Geany are of great help here.

As a one liner, the configuration becomes this:

1
((firstName "John")(lastName "Smith")(isAlive T)(age 27)(address ((streetAddress "21 2nd Street")(city "New York")(state "NY")(postalCode "10021-3100")))(phoneNumbers (((type "home")(number "212 555-1234"))((type "office")(number "646 555-4567"))((type "mobile")(number "123 456-7890")))(children ())(spouse nil))

Parsing it

Disregarding comments, simply opening a file (open file "r"), reading all lines (read-line file-desc) and concatenating them (strcat collection line) returns an ugly result, one configuration list, polluted with spaces and tabs. Probably not something to be proud of, but… It works!

Leave a Reply

Your email address will not be published. Required fields are marked *