Today, Edi Weitz edi@agharta.de wrote:
Hi!
Hi there!
On Wed, 08 Mar 2006 00:43:12 +0100, Andreas Fuchs asf@boinkor.net wrote:
So, once I've read an irc command (as a "line" of latin-1 characters), I'd like to convert it to text that looks the least insane, which is why my code tries several external-formats in a row and returns as soon as it found one that doesn't throw an error:
If LINE is a string you want this:
(defun try-decode-line (line external-formats) (loop for external-format in external-formats for decoded = nil for error = nil do (multiple-value-setq (decoded error) (ignore-errors (with-input-from-string (in line) (let ((flexi (flexi-streams:make-flexi-stream in :external-format external-format))) (read-line flexi))))) do (format t "~&tried ~s: ~S~% error: ~A~%" external-format decoded error) if decoded do (return decoded)))
This didn't exactly work; I get errors stating that the in-memory stream isn't a binary stream.
But actually I think you want LINE to be a sequence of octets, so this is what you want:
And this does work perfectly. Thanks so much! (:
My current scheme is (horrible and) as follows: I read a line in the latin-1 external format, then create a vector from the char-codes, and decode that with try-decode-line.
Why latin-1, you ask? Because it has a 1:1 code point mapping to bytes, as far as irc is concerned, and I don't have to implement my own buffering, which would really suck - IRC operates on lines, whereas all binary streams need fixed-width buffers.
54It seems like the easiest way to interoperate with broken protocols is to break a little inside, just like their authors once did. (:
But you'll need version 0.5.3 to see that because there was a typo in the code which generated the error messages.
Heh. I just upgraded.
Hrmpf! Am I abusing flexi-streams too much or is that a bug? How should one read externally-formatted data from an in-memory stream, anyway?
You forgot that you have to create a flexi stream first - in-memory streams happen to be provided by the same library but they're something different. Use MAKE-FLEXI-STREAM to turn them into flexi streams.
Oh. I misread the phrase 'These streams can obviously be used as the underlying streams for flexi streams.' as 'They are the base class of flexi-streams, and they work just like flexi-streams do.' -- I blame late-night hacking. (:
And are string-backed in-memory streams even allowed?
No. CL already has WITH-INPUT-FROM-STRING... :)
Thanks for your time and for developing flexi-streams. In return, I hope to be able to buy you a beverage of your choice in Hamburg (-:
Nice. Looking forward to seeing you there!
Me too (:
Thanks again,