caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Setting the EOL character....
@ 2002-09-13  0:07 Richard Lyman
  0 siblings, 0 replies; 6+ messages in thread
From: Richard Lyman @ 2002-09-13  0:07 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 1395 bytes --]

To start - I really don't know that much about programming (3 years self-taught in scripted web languages - Perl/PHP/Ruby), and I'm trying to learn OCaml...

I've mostly dealt with persistent socket servers in other programming languages, and I'd like to write one in OCaml.

I've read most of the book - Developing Applications with Objective Caml (English PDF), so I understand a little - but I'm still very unsure of myself.

Here's the problem I'm trying to solve...

I have a proprietary format I'm trying to read in through a socket - when I say proprietary I mean that the EOL character is the ASCII null character (\000) and not the '\n' character.

I've looked at the examples in the OReilly book, but I'm not sure how to change the default End Of Line character (\n) to \000.

Will I have to do some sort of pattern match on the stream so that I can deal with 'each line' using \000 as the EOL character?

Do the input, input_line, read, or read_line methods accept a different EOL character as a parameter - or can I define the method differently?

Since I can read in a certain amount of characters with methods like read, can I find the location of the ASCII null character and read from the stream until right after it?

I hope I've asked good enough questions...

Thanks for any help you guys can give!

Let me know if anything's not clear enough...

-Rich

[-- Attachment #2: Type: text/html, Size: 2836 bytes --]

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

* Re: [Caml-list] Setting the EOL character....
  2002-09-13 15:33   ` Bruno.Verlyck
@ 2002-09-13 15:52     ` Matt Boyd
  0 siblings, 0 replies; 6+ messages in thread
From: Matt Boyd @ 2002-09-13 15:52 UTC (permalink / raw)
  To: caml-list

Bruno.Verlyck@inria.fr wrote:
>    From: "Richard Lyman" <rich@lithinos.com>
>    Date: Thu, 12 Sep 2002 20:36:45 -0600
> 
>    ... so then - there's no OCaml concept of a global EOL variable, or
>    method that deals with reading in from a stream until the EOL??
> 

I make no promises of efficiency or thread safety, but this should do 
what you want:

let read_until n =
   let buf = Buffer.create 100 in
   fun st ->
     let rec aux = function
       | n' when n' = n ->
           let str = Buffer.contents buf in
           Buffer.reset buf;
           str
       | n' ->
           Buffer.add_char buf n';
           aux (Stream.next st);
     in
     aux (Stream.next st)

let read_until_null = read_until '\000'



> 
> Bruno.
> -------------------
> To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
> Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> 
> 
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Setting the EOL character....
  2002-09-13  2:36 ` Richard Lyman
  2002-09-13  9:10   ` Alessandro Baretta
@ 2002-09-13 15:33   ` Bruno.Verlyck
  2002-09-13 15:52     ` Matt Boyd
  1 sibling, 1 reply; 6+ messages in thread
From: Bruno.Verlyck @ 2002-09-13 15:33 UTC (permalink / raw)
  To: rich; +Cc: caml-list

   From: "Richard Lyman" <rich@lithinos.com>
   Date: Thu, 12 Sep 2002 20:36:45 -0600

   ... so then - there's no OCaml concept of a global EOL variable, or
   method that deals with reading in from a stream until the EOL??

   I was hoping more for something along the lines of -
    myStream = Unix.accepted_socket_stream
    while(true){
	a_line = myStream.read_until("\000")
	deal_with_line(a_line)
    }
FWIW, Cash supports reading a line terminated by any char from a set
of chars.  If that's your only need, Cash may be overkill, except if
you have a performance bottleneck here: its read_line should be at
least as fast as Pervasives.read_line.  Anyway,
  http://pauillac.inria.fr/cash/

Bruno.
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Setting the EOL character....
  2002-09-13  2:36 ` Richard Lyman
@ 2002-09-13  9:10   ` Alessandro Baretta
  2002-09-13 15:33   ` Bruno.Verlyck
  1 sibling, 0 replies; 6+ messages in thread
From: Alessandro Baretta @ 2002-09-13  9:10 UTC (permalink / raw)
  To: Richard Lyman, Ocaml



Richard Lyman wrote:
> Ok...
> 
> That doesn't look too bad - and I think I almost understand it. : )
> 
> ... so then - there's no OCaml concept of a global EOL variable, or method
> that deals with reading in from a stream until the EOL??
> 
> I was hoping more for something along the lines of -
> 
> (start pseudocode)
> 
> myStream = Unix.accepted_socket_stream
> while(true){
>     a_line = myStream.read_until("\000")
>     deal_with_line(a_line)
> }
> 
> (end pseudocode)
> 
> Thanks for your thoughts!
> 
> -Rich

How about the following:

let process_line out_channel line = .... in
let read_until_null in_channel out_channel =
    Scanf.fscanf in_channel "[^\000]\000"
      (process_line out_channel) in
let process_request in_channel out_channel =
    try while true do
       read_until_null in_channel out_channel
    done with End_of_file -> ()
let sockaddr = <server_socket_specification> in
Unix.establish_server process_request sockaddr


Nonetheless, let me suggest that you look into ocamllex and 
ocamlyacc--lexer and parser generators respectively. They 
are probably your best choice for lexing and parsing the 
input stream, no matter how weird its structure.

Alex

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Setting the EOL character....
  2002-09-13  2:24 Arturo Borquez
@ 2002-09-13  2:36 ` Richard Lyman
  2002-09-13  9:10   ` Alessandro Baretta
  2002-09-13 15:33   ` Bruno.Verlyck
  0 siblings, 2 replies; 6+ messages in thread
From: Richard Lyman @ 2002-09-13  2:36 UTC (permalink / raw)
  To: caml-list

Ok...

That doesn't look too bad - and I think I almost understand it. : )

... so then - there's no OCaml concept of a global EOL variable, or method
that deals with reading in from a stream until the EOL??

I was hoping more for something along the lines of -

(start pseudocode)

myStream = Unix.accepted_socket_stream
while(true){
    a_line = myStream.read_until("\000")
    deal_with_line(a_line)
}

(end pseudocode)

Thanks for your thoughts!

-Rich


----- Original Message -----
From: "Arturo Borquez" <artboreb@netscape.net>
To: ""Richard Lyman"" <rich@lithinos.com>
Cc: <caml-list@inria.fr>
Sent: Thursday, September 12, 2002 8:24 PM
Subject: RE: [Caml-list] Setting the EOL character....


> "Richard Lyman" <rich@lithinos.com> wrote:
>
> >To start - I really don't know that much about programming (3 years
self-taught in scripted web languages - Perl/PHP/Ruby), and I'm trying to
learn OCaml...
> >
> >I've mostly dealt with persistent socket servers in other programming
languages, and I'd like to write one in OCaml.
> >
> >I've read most of the book - Developing Applications with Objective Caml
(English PDF), so I understand a little - but I'm still very unsure of
myself.
> >
> >Here's the problem I'm trying to solve...
> >
> >I have a proprietary format I'm trying to read in through a socket - when
I say proprietary I mean that the EOL character is the ASCII null character
(\000) and not the '\n' character.
> >
> >I've looked at the examples in the OReilly book, but I'm not sure how to
change the default End Of Line character (\n) to \000.
> >
> >Will I have to do some sort of pattern match on the stream so that I can
deal with 'each line' using \000 as the EOL character?
> >
> >Do the input, input_line, read, or read_line methods accept a different
EOL character as a parameter - or can I define the method differently?
> >
> >Since I can read in a certain amount of characters with methods like
read, can I find the location of the ASCII null character and read from the
stream until right after it?
> >
> >I hope I've asked good enough questions...
> >
> >Thanks for any help you guys can give!
> >
> >Let me know if anything's not clear enough...
> >
> >-Rich
> >
>
> Hi,
>
> you could try something like this
>
> let read_eol inchan =
>   let rec f buf =
>     let c = input_char inchan in
>     if c = '\000' then Buffer.contents buf
>     else begin Buffer.add_char buf c; f buf end
>   in f (Buffer.create 64);;
>
> or in 'revised syntax which I prefer)
>
> value read_eol inchan =
>   f (Buffer.create 64) where rec f buf =
>     let c = input_char inchan in
>     if c = '\000' then Buffer.contents buf
>     else do { Buffer.add_char buf c; f buf }
> ;
>
> (Note there is no exception handling in examples)
> Regards
> --
> Arturo Borquez
>
>
> __________________________________________________________________
> The NEW Netscape 7.0 browser is now available. Upgrade now!
http://channels.netscape.com/ns/browsers/download.jsp
>
> Get your own FREE, personal Netscape Mail account today at
http://webmail.netscape.com/
>

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* RE: [Caml-list] Setting the EOL character....
@ 2002-09-13  2:24 Arturo Borquez
  2002-09-13  2:36 ` Richard Lyman
  0 siblings, 1 reply; 6+ messages in thread
From: Arturo Borquez @ 2002-09-13  2:24 UTC (permalink / raw)
  To: "Richard Lyman"; +Cc: caml-list

"Richard Lyman" <rich@lithinos.com> wrote:

>To start - I really don't know that much about programming (3 years self-taught in scripted web languages - Perl/PHP/Ruby), and I'm trying to learn OCaml...
>
>I've mostly dealt with persistent socket servers in other programming languages, and I'd like to write one in OCaml.
>
>I've read most of the book - Developing Applications with Objective Caml (English PDF), so I understand a little - but I'm still very unsure of myself.
>
>Here's the problem I'm trying to solve...
>
>I have a proprietary format I'm trying to read in through a socket - when I say proprietary I mean that the EOL character is the ASCII null character (\000) and not the '\n' character.
>
>I've looked at the examples in the OReilly book, but I'm not sure how to change the default End Of Line character (\n) to \000.
>
>Will I have to do some sort of pattern match on the stream so that I can deal with 'each line' using \000 as the EOL character?
>
>Do the input, input_line, read, or read_line methods accept a different EOL character as a parameter - or can I define the method differently?
>
>Since I can read in a certain amount of characters with methods like read, can I find the location of the ASCII null character and read from the stream until right after it?
>
>I hope I've asked good enough questions...
>
>Thanks for any help you guys can give!
>
>Let me know if anything's not clear enough...
>
>-Rich
>

Hi,

you could try something like this

let read_eol inchan =
  let rec f buf =
    let c = input_char inchan in
    if c = '\000' then Buffer.contents buf
    else begin Buffer.add_char buf c; f buf end
  in f (Buffer.create 64);;   

or in 'revised syntax which I prefer)

value read_eol inchan =
  f (Buffer.create 64) where rec f buf =
    let c = input_char inchan in
    if c = '\000' then Buffer.contents buf
    else do { Buffer.add_char buf c; f buf }
;

(Note there is no exception handling in examples)
Regards
-- 
Arturo Borquez


__________________________________________________________________
The NEW Netscape 7.0 browser is now available. Upgrade now! http://channels.netscape.com/ns/browsers/download.jsp 

Get your own FREE, personal Netscape Mail account today at http://webmail.netscape.com/
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

end of thread, other threads:[~2002-09-16 10:07 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-09-13  0:07 [Caml-list] Setting the EOL character Richard Lyman
2002-09-13  2:24 Arturo Borquez
2002-09-13  2:36 ` Richard Lyman
2002-09-13  9:10   ` Alessandro Baretta
2002-09-13 15:33   ` Bruno.Verlyck
2002-09-13 15:52     ` Matt Boyd

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 NNTP newsgroup(s).