string stream::read_line(value sep_) { local_root_scope scope; if (sep_.is_undefined_or_null()) sep_ = string("\n"); string sep = sep_.to_string(); if (sep.length() != 1) throw exception("Line separators with size other than 1 are not supported"); int sepc = sep.data()[0]; if (sepc >= 128) throw exception("Non-ASCII line separators are not supported"); std::string line; for (;;) { int ch = streambuf_->sbumpc(); if (ch == std::char_traits<char>::eof()) break; line += ch; if (ch == sepc) break; } return line; }
object fs_base::raw_open(char const* name, value mode, value perms) { // TODO: Deal with permissions somewhere :) if (!perms.is_undefined_or_null()) throw exception("rawOpen: permissions not yet supported"); return create_native_object<io::file>(object(), name, mode); }
object sqlite3::compile(flusspferd::string sql_in, value bind ) { local_root_scope scope; size_t n_bytes = sql_in.length() * 2; sqlite3_stmt * sth = 0; js_char16_t * tail = 0; // uncompiled part of the sql (when multiple stmts) if (sqlite3_prepare16_v2(db, sql_in.data(), n_bytes, &sth, (const void**)&tail) != SQLITE_OK) { raise_sqlite_error(db); } object cursor = create<sqlite3_cursor>(fusion::make_vector(sth)); string tail_str; if (tail) { tail_str = string(tail); } string sql = sql_in.substr( 0, sql_in.size() - tail_str.size() ); cursor.define_property("sql", sql); cursor.define_property("tail", tail_str); if ( !bind.is_undefined_or_null() ) { cursor.call("bind", bind ); } return cursor; }
void file::open(char const *name, value options) { security &sec = security::get(); if (boost::filesystem::is_directory(std::string(name))) { throw exception( std::string("Could not open file: it is a directory (")+ name + ")" ); } std::ios::openmode open_mode = std::ios::openmode(); bool exclusive = false, create = false; if (options.is_string()) { // String modes always set create std::string mode = options.to_std_string(); if (mode == "r") open_mode = std::ios::in; else if (mode == "r+") open_mode = std::ios::in | std::ios::out; else if (mode == "r+x") { open_mode = std::ios::in | std::ios::out; exclusive = create = true; } else if (mode == "w") { open_mode = std::ios::out; create = true; } else if (mode == "wx") { open_mode = std::ios::out; exclusive = create = true; } else if (mode == "w+x") { open_mode = std::ios::out | std::ios::in | std::ios::trunc; exclusive = create = true; } else { throw exception(format("File.open: mode '%s' not supported (yet?)") % mode); } }else if (options.is_object()) { object obj = options.get_object(); create = obj.get_property("create").to_boolean(); if (obj.get_property("read").to_boolean()) open_mode |= std::ios::in; if (obj.get_property("write").to_boolean()) open_mode |= std::ios::out; if (obj.get_property("truncate").to_boolean()) open_mode |= std::ios::trunc; if (obj.get_property("append").to_boolean()) { if (!(open_mode & std::ios::out)) { throw exception("File.open: append mode can only be used with write"); } open_mode |= std::ios::app | std::ios::out; } if (obj.get_property("exclusive").to_boolean()) { if (!create) throw exception("File.open: exclusive mode can only be used with create"); exclusive = create = true; } }else if (options.is_undefined_or_null()) { open_mode = std::ios::in; }else { throw exception("File.open: Invalid options argument", "TypeError"); } unsigned sec_mode = 0; if (open_mode & std::ios::in) sec_mode |= security::READ; if (open_mode & std::ios::out) sec_mode |= security::WRITE; if (create) sec_mode |= security::CREATE; if (!sec.check_path(name, sec_mode)) { throw exception( format("File.open: could not open file: 'denied by security' (%s)") % name ); } if (create) { // C++ streams don't support O_EXCL|O_CREAT modes. Fall back to open unsigned o_mode = exclusive ? O_CREAT|O_EXCL : O_CREAT; int fd = ::open(name, o_mode, 0666); if (fd == -1) throw exception(compose_error_message("File.open: couldn't create file", name)); // Done - got the file (exclusively) created. ::close(fd); } p->stream.open(name, open_mode); if (!p->stream) throw exception(compose_error_message("Could not open file", name)); define_property("fileName", string(name), permanent_property | read_only_property ); }