public inbox for kawa@sourceware.org
 help / color / mirror / Atom feed
* how to read a file in a string
@ 2015-12-11 10:07 Damien MATTEI
  2015-12-11 10:17 ` Dirk Huesken
  2015-12-11 10:28 ` Per Bothner
  0 siblings, 2 replies; 7+ messages in thread
From: Damien MATTEI @ 2015-12-11 10:07 UTC (permalink / raw)
  To: kawa

Hi,

i have a method in a class to read a file (html page) in a string,method 
defined below:

((getHtmlCounterPage) ::java.lang.String
     (gnu.lists.FString:toString (read-string 65535 (open-input-file 
htmlCounterPage))))

is there a simplest way, that would be independent of 65535 (max size of 
file) bytes to read a file ?

Regards,

Damien

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: how to read a file in a string
  2015-12-11 10:07 how to read a file in a string Damien MATTEI
@ 2015-12-11 10:17 ` Dirk Huesken
  2015-12-15  1:12   ` Damien MATTEI
  2015-12-11 10:28 ` Per Bothner
  1 sibling, 1 reply; 7+ messages in thread
From: Dirk Huesken @ 2015-12-11 10:17 UTC (permalink / raw)
  To: kawa

Theres a section in the docs:

   http://www.gnu.org/software/kawa/Reading-and-writing-whole-files.html

Regards,
Dirk

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: how to read a file in a string
  2015-12-11 10:07 how to read a file in a string Damien MATTEI
  2015-12-11 10:17 ` Dirk Huesken
@ 2015-12-11 10:28 ` Per Bothner
  2015-12-15  3:11   ` Damien MATTEI
  1 sibling, 1 reply; 7+ messages in thread
From: Per Bothner @ 2015-12-11 10:28 UTC (permalink / raw)
  To: Damien MATTEI, kawa



On 12/11/2015 02:07 AM, Damien MATTEI wrote:
> i have a method in a class to read a file (html page) in a string,method defined below:
>
> ((getHtmlCounterPage) ::java.lang.String
>      (gnu.lists.FString:toString (read-string 65535 (open-input-file htmlCounterPage))))
>
> is there a simplest way, that would be independent of 65535 (max size of file) bytes to read a file ?

You might find the path-data function convenient:
http://www.gnu.org/software/kawa/Reading-and-writing-whole-files.html

I haven't tested the following, but try it:

((getHtmlCounterPage) ::java.lang.String
    (path-data htmlCounterPage))

Where does 65535 come from?  True, read-string does allocate a buffer of the requested size,
but the max size of an array (or a string) is 2147483647.  So read-string is not a good
choice for reading an entire file of unknown size :-)
-- 
	--Per Bothner
per@bothner.com   http://per.bothner.com/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: how to read a file in a string
  2015-12-11 10:17 ` Dirk Huesken
@ 2015-12-15  1:12   ` Damien MATTEI
  2015-12-15  1:59     ` Per Bothner
  0 siblings, 1 reply; 7+ messages in thread
From: Damien MATTEI @ 2015-12-15  1:12 UTC (permalink / raw)
  To: kawa

thanks
i finally wrote one code version doing it:
(define (read-counter-value fn)
    (define cf &<{&[fn]})
    (define toStringCF (gnu.lists.Blob:toString (->string cf)))
    (display "------------------------------------")
    (newline)
    (display "counter value string:")
    (display  toStringCF)
    (display "|")
    (newline)
    ;;(define toIntegerCF (->int cf))
    (if (string? toStringCF)
        (display "toStringCF is a string !")
        (display "toStringCF is NOT a string !"))
    (newline)
    (define toFStringCF (gnu.lists.FString toStringCF))
    (display "------------------------------------")
    (newline)
    (display "last char string:")
    (define lastCS  (string-ref toFStringCF (- (string-length 
toFStringCF) 1)))
    (display (char->integer lastCS))
    (display "|")
    (newline)

    ;; (string-set! toFStringCF (- (string-length  toFStringCF) 1) 
#\space) ;; replace the trailing CR by a space
    ;; (display "counter value Fstring:")
    ;; (display  toFStringCF)
    ;; (display "|")
    ;; (newline)
    (define strCount (regex-split (string lastCS) toFStringCF))
    (display "counter value strCount:")
    (display  strCount)
    (display "|")
    (newline)
    (define rv (string->number (car strCount)))
    (display "------------------------------------")
    (newline)
    (display "counter value:")
    (newline)
    (display rv)
    (newline)
    rv)

Damien

Le 11/12/2015 11:17, Dirk Huesken a écrit :
> Theres a section in the docs:
>
> http://www.gnu.org/software/kawa/Reading-and-writing-whole-files.html
>
> Regards,
> Dirk
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: how to read a file in a string
  2015-12-15  1:12   ` Damien MATTEI
@ 2015-12-15  1:59     ` Per Bothner
  2015-12-15  2:29       ` Damien MATTEI
  0 siblings, 1 reply; 7+ messages in thread
From: Per Bothner @ 2015-12-15  1:59 UTC (permalink / raw)
  To: Damien MATTEI, kawa



On 12/14/2015 05:12 PM, Damien MATTEI wrote:
> thanks
> i finally wrote one code version doing it:
> (define (read-counter-value fn)
>     (define cf &<{&[fn]})
>     (define toStringCF (gnu.lists.Blob:toString (->string cf)))


That is redundant, as you're probably guessing.  Looks like you've
been experimenting - but now your code can be cleaned up. :-)

The reason ->string is a no-op on cf is because the Blob is
already a string, because Kawa considers any java.lang.CharSequence
to be a string.  Thus java.lang.String, gnu.lists.FString, and
gnu.lists.Blob are all strings (in the Scheme sense) but only
gnu.lists.FString is a mutable (modifiable) string.

For example you can call string-ref and string-length (but not
string-set!) on a Blob.

If you want a java.lang.String, you can just do:

(gnu.lists.Blob:toString cf)

or just:

(cf:toString)

>     (define toFStringCF (gnu.lists.FString toStringCF))

So you only need to convert toStringCF (or for that matter cf)
you actually want an FString - i.e. you want to modify it, which
you usually don't.
-- 
	--Per Bothner
per@bothner.com   http://per.bothner.com/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: how to read a file in a string
  2015-12-15  1:59     ` Per Bothner
@ 2015-12-15  2:29       ` Damien MATTEI
  0 siblings, 0 replies; 7+ messages in thread
From: Damien MATTEI @ 2015-12-15  2:29 UTC (permalink / raw)
  To: Per Bothner, kawa



Le 15/12/2015 02:59, Per Bothner a écrit :
>
>
> On 12/14/2015 05:12 PM, Damien MATTEI wrote:
>> thanks
>> i finally wrote one code version doing it:
>> (define (read-counter-value fn)
>>     (define cf &<{&[fn]})
>>     (define toStringCF (gnu.lists.Blob:toString (->string cf)))
>
>
> That is redundant, as you're probably guessing.  Looks like you've
> been experimenting - but now your code can be cleaned up. :-)

yes i do it, i no more need FString as it was for a previous version 
needing mutable string

Damien
>
> The reason ->string is a no-op on cf is because the Blob is
> already a string, because Kawa considers any java.lang.CharSequence
> to be a string.  Thus java.lang.String, gnu.lists.FString, and
> gnu.lists.Blob are all strings (in the Scheme sense) but only
> gnu.lists.FString is a mutable (modifiable) string.
>
> For example you can call string-ref and string-length (but not
> string-set!) on a Blob.
>
> If you want a java.lang.String, you can just do:
>
> (gnu.lists.Blob:toString cf)
>
> or just:
>
> (cf:toString)
>
>>     (define toFStringCF (gnu.lists.FString toStringCF))
>
> So you only need to convert toStringCF (or for that matter cf)
> you actually want an FString - i.e. you want to modify it, which
> you usually don't.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: how to read a file in a string
  2015-12-11 10:28 ` Per Bothner
@ 2015-12-15  3:11   ` Damien MATTEI
  0 siblings, 0 replies; 7+ messages in thread
From: Damien MATTEI @ 2015-12-15  3:11 UTC (permalink / raw)
  To: Per Bothner, kawa



Le 11/12/2015 11:27, Per Bothner a écrit :
>
>
> On 12/11/2015 02:07 AM, Damien MATTEI wrote:
>> i have a method in a class to read a file (html page) in a 
>> string,method defined below:
>>
>> ((getHtmlCounterPage) ::java.lang.String
>>      (gnu.lists.FString:toString (read-string 65535 (open-input-file 
>> htmlCounterPage))))
>>
>> is there a simplest way, that would be independent of 65535 (max size 
>> of file) bytes to read a file ?
>
> You might find the path-data function convenient:
> http://www.gnu.org/software/kawa/Reading-and-writing-whole-files.html
>
> I haven't tested the following, but try it:
>
> ((getHtmlCounterPage) ::java.lang.String
>    (path-data htmlCounterPage))

need to cast it to java string ;-) :

(gnu.lists.Blob:toString (path-data htmlCounterPage))
>
> Where does 65535 come from?

2^16 - 1 : approximatively the size of the memory of an apple  ][ :-)
> True, read-string does allocate a buffer of the requested size,
> but the max size of an array (or a string) is 2147483647.  So 
> read-string is not a good
> choice for reading an entire file of unknown size :-)

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2015-12-15  3:11 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-12-11 10:07 how to read a file in a string Damien MATTEI
2015-12-11 10:17 ` Dirk Huesken
2015-12-15  1:12   ` Damien MATTEI
2015-12-15  1:59     ` Per Bothner
2015-12-15  2:29       ` Damien MATTEI
2015-12-11 10:28 ` Per Bothner
2015-12-15  3:11   ` Damien MATTEI

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).