source: to-imperative/trunk/library/StdIO/read_m_line.cc @ 1172

Last change on this file since 1172 was 1172, checked in by orlov, 17 years ago
  • Read-Line calls Read-Line! through TAILCALL for the correct handling of $fail-s.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 1.0 KB
Line 
1#include <rf_core.hh>
2#include <stdio.h>
3
4using namespace rfrt;
5
6namespace refal
7{
8
9namespace StdIO
10{
11
12RF_DECL(Read_m_Line_e_);
13RF_DECL(Read_m_Line);
14
15extern Expr StdIn ;
16
17RF_FUNC (Read_m_Line_e_, (RF_ARG _channel), (RF_RES _line))
18  if (_channel.get_len() == 1) {
19    Term* p = _channel.get_first();
20    Channel* ch = p->cast_to<Channel>(type_channel);
21    if (ch) {
22      FILE* fp = ch->get_fp();
23      if (fp == NULL) {
24        throw Word::create_expr(L"Read-Line!") +
25          Word::create_expr(L"Channel is not open");
26      }
27      char* p = null;
28      size_t cnt = 0;
29      ssize_t n ;
30      n = getline(&p, &cnt, fp);
31      if (n != -1) {
32        if (p[n - 1] == '\n') p[n - 1] = 0;
33        _line = Char::create_expr(p);
34        free(p);
35        return true;
36      } else {
37        if (p != null) free(p);
38        retfail;
39      }
40    }
41  }
42  throw Word::create_expr(L"Read-Line!") +
43    Word::create_expr(L"Arg 1 should be a Channel object");
44RF_END
45
46RF_FUNC (Read_m_Line, (), (RF_RES _line))
47  RF_TAILCALL(Read_m_Line_e_, (StdIn), (_line));
48RF_END
49
50}
51}
Note: See TracBrowser for help on using the repository browser.