Page 1 of 1

get-string question

Posted: Tue Jan 18, 2005 6:28 am
by maq
Why is it that I get different values from the following two length calls:

(set 'ifile (read-file "foo2.png"))
(println "length of ifile " (length ifile))
(println "length through a get-string " (length (get-string (address ifile))))
(exit)

--OUTPUT--
maq:maq $ ./test.lsp
length of ifile 90
length through a get-string 8

It appears that I'm truncating the string in ifile when I try to extract from a pointer with get-string. Clearly I'm missing something very basic here. Any insights would be greatly appreaciated.
thanks in advance,

--maq

Posted: Tue Jan 18, 2005 6:46 am
by HPW
I do not think read-file is for binary data.
You should use 'read-buffer'.

>It appears that I'm truncating the string in ifile when I try to extract from a pointer with get-string. Clearly I'm missing something very basic here.

Maybe your data has a null inside and the get-string truncates it there.

Posted: Tue Jan 18, 2005 7:03 am
by maq
Thanks for the prompt reply. However, a read-buffer does not fix it. With:

(set 'in (open "foo2.png" "read"))
;;(set 'ifile (read-file "foo2.png"))
(read-buffer in 'ifile 100)
(close in)
(println "length of ifile " (length ifile))
(println "length through a get-string " (length (get-string (address ifile))))

I still get:

maq:maq $ ./test.lsp
length of ifile 90
length through a get-string 8

Interestingly though, when I use a text file I get the same length value for both. Any other thoughts?
--maq

Posted: Tue Jan 18, 2005 10:00 am
by HPW
Maybe your data has a null inside and the get-string truncates it there.
You should open your foo2.png with a hex-editor amd watch the content. Maybe at offset 8 is a null-byte.
Then the get-string would truncate there and you get the wrong count.

But why do you need this. You get the length as the return of the read-buffer function:

Code: Select all

> (set 'in (open "foo2.png" "read"))
3
> (set 'flength (read-buffer in 'ifile 100))
51
> (close in)
true
> (println "length of ifile " (length ifile))
length of ifile 51
51
> (println "length through a get-string " (length (get-string (address ifile))))
length through a get-string 51
51
>flength
51
(I fake the file with a text-file)

Posted: Tue Jan 18, 2005 12:48 pm
by Lutz
'read-file' will read the whole file including binary null data, but 'get-string' will only get the string up to the first null byte it encounters. You can try the following to see this:

Code: Select all

newLISP v.8.3.6 on Win32 MinGW.

> (set 'str "abc\000def")
"abc\000def"
> (length str)
7
> (length (get-string (address str)))
3
> (get-string (address str))
"abc"
>
The \000 insertes a null byte

Lutz