source: to-imperative/trunk/library/StdIO/open_file.cc @ 1170

Last change on this file since 1170 was 1170, checked in by orlov, 17 years ago
  • Delete auxiliary objects before returning $fail.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 1.5 KB
Line 
1#include <rf_core.hh>
2#include <pxx_string.ih>
3#include <stdio.h>
4#include <errno.h>
5
6using namespace rfrt;
7
8namespace refal
9{
10
11namespace StdIO
12{
13
14RF_DECL(Open_m_File);
15
16RF_FUNC (Open_m_File, (RF_ARG _channel, _fname, _mode), ())
17  WString ws = (WString)_fname;
18  size_t len = wcstombs(null, ws.get_data(), 0);
19  if (len == (size_t)(-1)) {
20    throw Word::create_expr(L"Open-File") +
21      Word::create_expr(L"Invalid filename");
22  }
23  char* fname = new char[len + 1];
24  wcstombs(fname, ws.get_data(), len + 1);
25  fname[len] = 0;
26  if (_channel.get_len() == 1) {
27    Term* p = _channel.get_first();
28    Channel* ch = p->cast_to<Channel>(type_channel);
29    if (ch) {
30      if (_mode.get_len() == 1) {
31        Term* p = _mode.get_first();
32        if (p->get_type() == type_word) {
33          ws = (WString)_mode;
34          len = wcstombs(null, ws.get_data(), 0);
35          if (len == (size_t)(-1)) {
36            throw Word::create_expr(L"Open-File") +
37              Word::create_expr(L"Invalid mode");
38          }
39          char* mode = new char[len + 1];
40          wcstombs(mode, ws.get_data(), len + 1);
41          mode[len] = 0;
42          mode[0] = tolower(mode[0]);
43          bool res = ch->open(fname, mode);
44          delete mode;
45          delete fname;
46          if (!res) retfail;
47          return true;
48        }
49      }
50      throw Word::create_expr(L"Open-File") +
51        Word::create_expr(L"Arg 3 should be a Word");
52    }
53  }
54  throw Word::create_expr(L"Open-File") +
55    Word::create_expr(L"Arg 1 should be a Channel object");
56RF_END
57
58}
59}
Note: See TracBrowser for help on using the repository browser.