Skip to content
Browse files

File.open

  • Loading branch information...
1 parent 0334a2e commit 7e4c5f36aa18c0b8252cbb6b48fd7677ad1ed2d8 @tokuhirom committed Mar 19, 2012
Showing with 56 additions and 26 deletions.
  1. +1 −19 src/builtin.cc
  2. +2 −0 src/object.h
  3. +39 −0 src/object/file.cc
  4. +1 −1 src/value.cc
  5. +13 −6 t/tra/file.tra
View
20 src/builtin.cc
@@ -57,25 +57,7 @@ static SharedPtr<Value> builtin_open(VM *vm, const std::vector<SharedPtr<Value>>
if (args.size() != 1 && args.size() != 2) {
return new ExceptionValue("Invalid argument count for open(): %zd. open() function requires 1 or 2 argument.", args.size());
}
-
- SharedPtr<Value> filename(args.at(0));
- std::string mode;
- if (args.size() == 2) {
- mode = args.at(1)->upcast<StrValue>()->str_value.c_str();
- } else {
- mode = "rb";
- }
-
- // TODO: check \0
- SharedPtr<FileValue> file = new FileValue();
- if (file->open(
- filename->upcast<StrValue>()->str_value,
- mode
- )) {
- return file;
- } else {
- return new ExceptionValue("Cannot open file: %s: %s", filename->upcast<StrValue>()->str_value.c_str(), strerror(errno));
- }
+ return File_open(vm, args.at(0).get(), args.size() == 2 ? args.at(1).get() : NULL);
}
static SharedPtr<Value> builtin_print(VM *vm, const std::vector<SharedPtr<Value>> & args) {
View
2 src/object.h
@@ -2,6 +2,7 @@
#define TORA_OBJECT_H_
#include "shared_ptr.h"
+#include <vector>
namespace tora {
@@ -29,6 +30,7 @@ void Init_Bytes(VM *vm);
SharedPtr<Value> Symbol_bless(VM * vm, Value* self, Value *data);
ObjectValue* Dir_new(VM *vm, StrValue *dirname);
+SharedPtr<Value> File_open(VM *vm, Value *fname, Value *mode);
void load_builtin_objects(VM *vm);
View
39 src/object/file.cc
@@ -10,6 +10,44 @@ inline static FILE *FP(Value* self) {
return self->upcast<FileValue>()->fp;
}
+/*
+ * You can pass the mode_v as NULL value.
+ */
+SharedPtr<Value> tora::File_open(VM *vm, Value *fname, Value *mode_v) {
+ assert(fname);
+
+ std::string mode;
+ if (mode_v) {
+ SharedPtr<StrValue> mode_s = mode_v->to_s();
+ mode = mode_s->str_value.c_str();
+ } else {
+ mode = "rb";
+ }
+
+ // TODO: check \0
+ SharedPtr<FileValue> file = new FileValue();
+ if (file->open(
+ fname->to_s()->str_value,
+ mode
+ )) {
+ return file;
+ } else {
+ return new ExceptionValue("Cannot open file: %s: %s", fname->upcast<StrValue>()->str_value.c_str(), strerror(errno));
+ }
+}
+
+/**
+ * File.open(Str $fname) : FileHandle
+ * File.open(Str $fname, Str $mode) : FileHandle
+ */
+static SharedPtr<Value> file_open_method(VM *vm, const std::vector<SharedPtr<Value>> & args) {
+ if (args.size() != 2 && args.size() != 3) {
+ return new ExceptionValue("Invalid argument count for open(): %zd. open() function requires 1 or 2 argument.", args.size());
+ }
+
+ return File_open(vm, args.at(1).get(), args.size()==3 ? args.at(2).get() : NULL);
+}
+
/**
* $file.slurp()
*
@@ -56,6 +94,7 @@ void tora::Init_File(VM* vm) {
// isatty, __iter__, __next__, read(), readline(), readlines(), seek()
// tell(), truncate(), write($str), fdopen
SharedPtr<Package> pkg = vm->find_package("File");
+ pkg->add_method(vm->symbol_table->get_id("open"), new CallbackFunction(file_open_method));
pkg->add_method(vm->symbol_table->get_id("slurp"), new CallbackFunction(file_slurp));
pkg->add_method(vm->symbol_table->get_id("close"), new CallbackFunction(file_close));
pkg->add_method(vm->symbol_table->get_id("flush"), new CallbackFunction(file_flush));
View
2 src/value.cc
@@ -84,7 +84,7 @@ double Value::to_double() {
SharedPtr<StrValue> Value::to_s() {
switch (value_type) {
case VALUE_TYPE_STR:
- return this->upcast<StrValue>();
+ return static_cast<StrValue*>(this);
case VALUE_TYPE_INT: {
SharedPtr<StrValue> v = new StrValue();
std::ostringstream os;
View
19 t/tra/file.tra
@@ -1,12 +1,19 @@
use Test::More *;
-my $fh = open("t/dat/file/f1");
-ok($fh);
-is($fh.slurp(), "Hello, John.\n");
-is(typeof($fh.fileno()), "Int");
-ok($fh.fileno() > 2);
+{
+ my $fh = open("t/dat/file/f1");
+ ok($fh);
+ is($fh.slurp(), "Hello, John.\n");
+ is(typeof($fh.fileno()), "Int");
+ ok($fh.fileno() > 2);
-$fh.close();
+ $fh.close();
+}
+
+{
+ my $fh = File.open('t/dat/file/f1');
+ is($fh.slurp(), "Hello, John.\n");
+}
done_testing();

0 comments on commit 7e4c5f3

Please sign in to comment.
Something went wrong with that request. Please try again.