Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* [Caml-list] input_line for zero-terminated strings
@ 2012-06-07 17:37 Joel Reymont
  2012-06-07 19:54 ` AW: " Gerd Stolpmann
  0 siblings, 1 reply; 2+ messages in thread
From: Joel Reymont @ 2012-06-07 17:37 UTC (permalink / raw)
  To: caml-list

I'm trying to read C-style strings terminated with \0.

Does Pervasives.input_line provide the optimal implementation for me to clone?

    Thanks, Joel

---
let input_line ic =
  let buf = ref (string_create 128) in
  let pos = ref 0 in
  begin try
    while true do
      if !pos = string_length !buf then begin
        let newbuf = string_create (2 * !pos) in
        string_blit !buf 0 newbuf 0 !pos;
        buf := newbuf
      end;
      let c = input_char ic in
      if c = '\n' then raise Exit;
      !buf.[!pos] <- c;
      incr pos
    done
  with Exit -> ()
     | End_of_file -> if !pos = 0 then raise End_of_file
  end;
  let res = string_create !pos in
  string_blit !buf 0 res 0 !pos;
  res

--------------------------------------------------------------------------
AlgoKit: EasyLanguage trading strategies, on the server, w/ Rithmic R|API
---------------------+------------+---------------------------------------
http://wagerlabs.com | @wagerlabs | http://www.linkedin.com/in/joelreymont
---------------------+------------+---------------------------------------

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

end of thread, other threads:[~2012-06-07 19:54 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-06-07 17:37 [Caml-list] input_line for zero-terminated strings Joel Reymont
2012-06-07 19:54 ` AW: " Gerd Stolpmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox