get-string question

Started by maq, January 17, 2005, 10:28:02 PM

Previous topic - Next topic

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

HPW

#1
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.
Hans-Peter

maq

#2
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

HPW

#3
Quote
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:



> (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)
Hans-Peter

Lutz

#4
'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:

newLISP v.8.3.6 on Win32 MinGW.

> (set 'str "abc00def")
"abc00def"
> (length str)
7
> (length (get-string (address str)))
3
> (get-string (address str))
"abc"
>


The 00 insertes a null byte



Lutz