[comp.sys.mac.programmer] Allegro Lisp question

gf0c+@andrew.cmu.edu (Gregory S. Fox) (09/25/89)

     I have a question regarding Allegro Common Lisp and how it
implements certain features of the Mac's OS.  Specifically, I
need to know how perform certain operations with strings, and my
Lisp programming skills are somewhat weak.

     There's a special function (%get-string pointer), which takes
a variable of type 'pointer and returns  [our beloved]  PASCAL string.
I want it to return a Lisp string  (of type 'string or 'simple-string);
ie- a string that's longer than 255 bytes.  This is complicated by the
fact that I don't know how Lisp strings are stored-  I know they are
allocated arrays, but are they null terminated?

     There's a function, something like (with-pointer ((var pointer)+) form))
that binds a variable to a pointer, but its application to my problem
escapes me.  There must be a simple way to do this, though...

     If you can clue me in, bless you...

-Greg
gf0c@andrew.cmu.edu

alms@cambridge.apple.com (Andrew L. M. Shalit) (09/25/89)

In article <sZ7P5O_00WB90Du0RC@andrew.cmu.edu> gf0c+@andrew.cmu.edu (Gregory S. Fox) writes:


	There's a special function (%get-string pointer), which takes
   a variable of type 'pointer and returns  [our beloved]  PASCAL string.
   I want it to return a Lisp string  (of type 'string or 'simple-string);
   ie- a string that's longer than 255 bytes.  This is complicated by the
   fact that I don't know how Lisp strings are stored-  I know they are
   allocated arrays, but are they null terminated?

%GET-STRING takes a pointer to a Pascal string as an argument, and
returns a Lisp string as the result.  The Lisp string will never
be longer than 255 characters, because the Pascal string can never
be longer than that.

If you have a longer string, it's probably a CString, that is, a zero-
terminated string.  To convert that to a Lisp string, you could
try something like the following:

(defun get-c-string (pointer)
  "puts bytes from pointer into string until it reaches a zero byte"
  (let* ((new-string (make-array 10
                                 :element-type 'string-char
                                 :fill-pointer 0
                                 :adjustable t))
         (one-char nil))
   (loop
     (setq one-char (%get-byte pointer))
     (when (= one-char 0)
      (return-from get-c-string new-string))
     (vector-push-extend new-string (code-char one-char))
     (setq pointer (%inc-pointer pointer 1)))))

You are basically just going through a loop, getting one character at
a time and adding it into a string.  Make sense?

   -andrew
    alms@cambridge.apple.com