data -> extempore

54 views
Skip to first unread message

Jason Levine

unread,
Jul 1, 2016, 11:47:11 AM7/1/16
to extemp...@googlegroups.com
Hey all,

I know how to load .xtm files, audiofiles, and images but now I'm looking to load/save data and settings files(xml, json, csv) with Extempore. I can't find any evidence of data being loaded or saved in the extempore examples. I looked though the jansson.xtm library, but wasn't sure of the api. I guess I could translate a jansson c example into extempore?   But what about something more basic like loading a csv and string splitting it by '\n' and then by ','.  Where should I look for functionality like that?

Thanks,
--
Jason Levine
new media performer + creative coder

Emmanuel Oga

unread,
Jul 1, 2016, 5:51:00 PM7/1/16
to Extempore

Toby Gifford

unread,
Jul 1, 2016, 6:23:35 PM7/1/16
to extemp...@googlegroups.com
Hi Jason, I wanted to load csv files in as matrix data, so developed some library code for this. Attached is a basic filesystem library which wraps the apache runtime library (in order to be cross-platform), and an example of reading in a csv file.

Caveat: i've designed it to be cross-platform, but haven't actually tested it on Windows

--
You received this message because you are subscribed to the Google Groups "Extempore" group.
To unsubscribe from this group and stop receiving emails from it, send an email to extemporelan...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ybot_filesystem.xtm.zip
csv_example.xtm.zip

Andrew Sorensen

unread,
Jul 1, 2016, 7:53:04 PM7/1/16
to extemp...@googlegroups.com
Assuming xtlang here, and not Scheme (which has all the usual file I/O stuff available: http://www.scheme.com/tspl3/io.html)

For 'simple' jobs on smallish files slurp and regex are usually the easiest option.

sys_slurp_file simply reads the entire file and returns an i8*, which you'll usually just convert into a String* as the examples below demonstrate.

Just about all String function definitions can be found in either libs/base/base.xtm or libs/core/adt.xtm.

There are a bunch of regex functions in libs/core/adt for working on Strings (and Lists).

Here are a couple of basic examples.

(sys:load "libs/core/adt.xtm")

(bind-func csv_dump
  (lambda (file)
    (letz ((data (sys_slurp_file file))
           (l1 (regex_split ",|\n" (Str data)))
           (l2 (map (lambda (e) (trim e)) l1)))
      (println l2)
      void)))

($ (csv_dump "/tmp/test.csv"))

(sys:load "libs/core/math.xtm")

(bind-func csv_4x4matrix_dump
  (lambda (file)
    (letz ((data (sys_slurp_file file))
           (l1 (regex_split ",|\n" (Str data)))
           (l2 (map (lambda (e) (atof (cstring (trim e)))) l1))
           (num_mats (/ (length l2) 16))
           (matrix:double* (alloc 16))
           (i 0) (k 0))
      (if (<> (% (length l2) 16) 0)
          (println "Poorly formed matrix CSV file - must be modulo 16 (i.e. 4x4):" (Str file))
          (begin
            (dotimes (i num_mats)
              (dotimes (k 16)
                (pset! matrix k (nth l2 (+ k (* i 16)))))
              (println "mat:" i)
              (mprint matrix 4 4)
              (println))
            void)))))

($ (csv_4x4matrix_dump "/tmp/matrix_test.csv"))

Ultimately for more complex parsing jobs you either (a) want to look for a c library that already does the job or (b) drop down to extempore's clib wrapper (i.e. fopen, strtok etc.).

Cheers,
Andrew.

Reply all
Reply to author
Forward
0 new messages