rb_import 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/CHANGELOG.md +9 -1
- data/Rakefile +0 -2
- data/ext/rb_import/rb_import.c +3 -25
- data/ext/rb_import/rb_import_utils.c +36 -0
- data/ext/rb_import/rb_import_utils.h +9 -0
- data/rb_import.gemspec +1 -1
- data/test/basic_test.rb +12 -0
- metadata +3 -1
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f1e9c432a8f32cac0839e42bdf6055415b2aea1d
|
4
|
+
data.tar.gz: ed2a36e286c606b83e061c524f76b31331dcb5f4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 442c1ba01a463cdddcf2b99a2702409cc7a3800e2f1880f08b4b84eec57b5bbb5938a2bfddb977782a42d2aa1e0815427cc7423c9e732286ed1bc8486e2aa48f
|
7
|
+
data.tar.gz: e71d49660215101c0cb031ad40e61b25bf578ebe252536256c6643d0a82c5ed1741c9ddfc4d03604469e3434094ecf624917c28da5fbf75c61c2f10ffc21b81c
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data.tar.gz.sig
CHANGED
Binary file
|
data/CHANGELOG.md
CHANGED
data/Rakefile
CHANGED
data/ext/rb_import/rb_import.c
CHANGED
@@ -1,28 +1,5 @@
|
|
1
1
|
#include <ruby.h>
|
2
|
-
|
3
|
-
char *load_file_in_str(const char *in) {
|
4
|
-
FILE *f = fopen(in, "r");
|
5
|
-
|
6
|
-
if(!f) {
|
7
|
-
return 0;
|
8
|
-
} else {
|
9
|
-
fseek(f, 0, SEEK_END);
|
10
|
-
long fsize = ftell(f);
|
11
|
-
fseek(f, 0, SEEK_SET);
|
12
|
-
|
13
|
-
char * out = malloc(fsize + 1);
|
14
|
-
int res = fread(out, fsize, 1, f);
|
15
|
-
|
16
|
-
if(res <= 0) {
|
17
|
-
return 0;
|
18
|
-
} else {
|
19
|
-
fclose(f);
|
20
|
-
|
21
|
-
out[fsize] = 0;
|
22
|
-
return out;
|
23
|
-
}
|
24
|
-
}
|
25
|
-
}
|
2
|
+
#include "rb_import_utils.h"
|
26
3
|
|
27
4
|
VALUE rb_import(VALUE self, VALUE file_name) {
|
28
5
|
/* try finding the file the same way Ruby's `load`
|
@@ -31,10 +8,11 @@ VALUE rb_import(VALUE self, VALUE file_name) {
|
|
31
8
|
|
32
9
|
if(RTEST(file_location)) {
|
33
10
|
char *file_path = RSTRING_PTR(file_location);
|
34
|
-
char *file_content =
|
11
|
+
char *file_content = rb_import_load_file_in_str(file_path);
|
35
12
|
int result;
|
36
13
|
|
37
14
|
VALUE obj = rb_eval_string_protect(file_content, &result);
|
15
|
+
free(file_content);
|
38
16
|
if (result) {
|
39
17
|
rb_raise(rb_eLoadError, "can't eval imported file %s", RSTRING_PTR(file_location));
|
40
18
|
} else {
|
@@ -0,0 +1,36 @@
|
|
1
|
+
#include "rb_import_utils.h"
|
2
|
+
|
3
|
+
long rb_import_get_file_size(FILE *f) {
|
4
|
+
/* preserve the current position */
|
5
|
+
long current_position = ftell(f);
|
6
|
+
|
7
|
+
fseek(f, 0, SEEK_END);
|
8
|
+
long file_size = ftell(f);
|
9
|
+
|
10
|
+
/* restore the previous position */
|
11
|
+
fseek(f, 0, current_position);
|
12
|
+
|
13
|
+
return file_size;
|
14
|
+
}
|
15
|
+
|
16
|
+
char *rb_import_read_file(FILE *f) {
|
17
|
+
long file_size = rb_import_get_file_size(f);
|
18
|
+
char * file_content = malloc(file_size + 1);
|
19
|
+
int res = fread(file_content, file_size, 1, f);
|
20
|
+
|
21
|
+
fclose(f);
|
22
|
+
file_content[file_size] = 0;
|
23
|
+
|
24
|
+
if(res <= 0) {
|
25
|
+
free(file_content);
|
26
|
+
file_content = 0;
|
27
|
+
}
|
28
|
+
|
29
|
+
return file_content;
|
30
|
+
}
|
31
|
+
|
32
|
+
char *rb_import_load_file_in_str(const char *in) {
|
33
|
+
FILE *f = fopen(in, "r");
|
34
|
+
|
35
|
+
return f ? rb_import_read_file(f) : 0;
|
36
|
+
}
|
data/rb_import.gemspec
CHANGED
data/test/basic_test.rb
CHANGED
@@ -6,4 +6,16 @@ class BasicTest < Minitest::Test
|
|
6
6
|
f = import("./test/fixtures/foo.rb")
|
7
7
|
assert_equal "foo bar", f.new.to_s
|
8
8
|
end
|
9
|
+
|
10
|
+
def test_missing_file
|
11
|
+
error_occured = false
|
12
|
+
|
13
|
+
begin
|
14
|
+
import("unknown_file.rb")
|
15
|
+
rescue LoadError => error
|
16
|
+
error_occured = true
|
17
|
+
end
|
18
|
+
|
19
|
+
assert error_occured
|
20
|
+
end
|
9
21
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rb_import
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Franck Verrot
|
@@ -121,6 +121,8 @@ files:
|
|
121
121
|
- checksums/0.1.0.sha512
|
122
122
|
- ext/rb_import/extconf.rb
|
123
123
|
- ext/rb_import/rb_import.c
|
124
|
+
- ext/rb_import/rb_import_utils.c
|
125
|
+
- ext/rb_import/rb_import_utils.h
|
124
126
|
- lib/.gitkeep
|
125
127
|
- rb_import.gemspec
|
126
128
|
- test/basic_test.rb
|
metadata.gz.sig
CHANGED
Binary file
|