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

Last change on this file since 1186 was 1186, checked in by pooh, 17 years ago
  • Open-File() fixes.
  • 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#include <ctype.h>
6
7using namespace rfrt;
8
9namespace refal
10{
11
12namespace StdIO
13{
14
15RF_DECL(Open_m_File);
16
17RF_FUNC (Open_m_File, (RF_ARG _channel, _fname, _mode), ())
18  WString ws = (WString)_fname;
19  size_t len = wcstombs(null, ws.get_data(), 0);
20  if (len == (size_t)(-1)) {
21    throw Word::create_expr(L"Open-File") +
22      Word::create_expr(L"Invalid filename");
23  }
24  char* fname = static_cast<char*>(alloca(len + 1));
25  wcstombs(fname, ws.get_data(), len + 1);
26  fname[len] = 0;
27  if (_channel.get_len() == 1) {
28    Term* p = _channel.get_first();
29    Channel* ch = p->cast_to<Channel>(type_channel);
30    if (ch) {
31      if (_mode.get_len() == 1) {
32        Term* p = _mode.get_first();
33        if (p->get_type() == type_word) {
34          ws = (WString)_mode;
35          len = wcstombs(null, ws.get_data(), 0);
36          if (len == (size_t)(-1)) {
37            throw Word::create_expr(L"Open-File") +
38              Word::create_expr(L"Invalid mode");
39          }
40          char* mode = static_cast<char*>(alloca(len + 1));
41          wcstombs(mode, ws.get_data(), len + 1);
42          mode[len] = 0;
43          mode[0] = tolower(mode[0]);
44          bool res = ch->open(fname, mode);
45          if (!res) retfail;
46          return true;
47        }
48      }
49      throw Word::create_expr(L"Open-File") +
50        Word::create_expr(L"Arg 3 should be a Word");
51    }
52  }
53  throw Word::create_expr(L"Open-File") +
54    Word::create_expr(L"Arg 1 should be a Channel object");
55RF_END
56
57}
58}
Note: See TracBrowser for help on using the repository browser.