supplement 1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,35 @@
1
+ /*
2
+ * supplement/filesys.h -- File system tools
3
+ */
4
+
5
+ #ifndef __SUPPLEMENT_FILESYS_H__
6
+ #define __SUPPLEMENT_FILESYS_H__
7
+
8
+ #include <ruby.h>
9
+
10
+
11
+ extern VALUE rb_fsstat_s_alloc( VALUE);
12
+ extern VALUE rb_fsstat_init( VALUE, VALUE);
13
+ extern VALUE rb_fsstat_init_copy( VALUE, VALUE);
14
+
15
+ extern VALUE rb_fsstat_type( VALUE);
16
+ extern VALUE rb_fsstat_bsize( VALUE);
17
+ extern VALUE rb_fsstat_blocks( VALUE);
18
+ extern VALUE rb_fsstat_bfree( VALUE);
19
+ extern VALUE rb_fsstat_bavail( VALUE);
20
+ extern VALUE rb_fsstat_files( VALUE);
21
+ extern VALUE rb_fsstat_ffree( VALUE);
22
+ extern VALUE rb_fsstat_fsid( VALUE);
23
+
24
+ extern VALUE rb_fsstat_bytes( VALUE);
25
+ extern VALUE rb_fsstat_free( VALUE);
26
+ extern VALUE rb_fsstat_avail( VALUE);
27
+ extern VALUE rb_fsstat_pfree( VALUE);
28
+ extern VALUE rb_fsstat_pavail( VALUE);
29
+
30
+ extern VALUE rb_fsstat_inspect( VALUE);
31
+
32
+ extern void Init_filesys( void);
33
+
34
+ #endif
35
+
@@ -0,0 +1,126 @@
1
+ /*
2
+ * supplement/itimer.c -- Interval timer
3
+ */
4
+
5
+
6
+ #include "itimer.h"
7
+
8
+ #include <math.h>
9
+
10
+ static void suppelement_sec_timeval( VALUE, struct timeval *t);
11
+ static VALUE suppelement_timeval_sec( struct timeval *);
12
+
13
+
14
+ /*
15
+ * call-seq:
16
+ * Process.setitimer( interval = nil, value = nil) -> nil
17
+ *
18
+ * Set alarm timer. If <code>value</code> is nonzero, it is the
19
+ * expiration time to the first alarm. If it is zero, the timer is disabled.
20
+ * If <code>value</code> is nonzero and <code>interval</code> is zero,
21
+ * the alarm is triggered once.
22
+ *
23
+ */
24
+
25
+ VALUE
26
+ rb_process_setitimer( int argc, VALUE *argv, VALUE obj)
27
+ {
28
+ VALUE isec;
29
+ VALUE vsec;
30
+ struct itimerval it;
31
+
32
+ rb_scan_args( argc, argv, "02", &isec, &vsec);
33
+
34
+ suppelement_sec_timeval( isec, &it.it_interval);
35
+ if (NIL_P(vsec) && !NIL_P(isec))
36
+ it.it_value = it.it_interval;
37
+ else
38
+ suppelement_sec_timeval( vsec, &it.it_value);
39
+
40
+ if (setitimer( ITIMER_REAL, &it, NULL) < 0)
41
+ rb_raise( rb_eSystemCallError, "setitimer failed.");
42
+ return Qnil;
43
+ }
44
+
45
+ void
46
+ suppelement_sec_timeval( VALUE secs, struct timeval *t)
47
+ {
48
+ switch (TYPE(secs)) {
49
+ case T_FIXNUM:
50
+ t->tv_sec = FIX2LONG(secs), t->tv_usec = 0;
51
+ if (t->tv_sec < 0)
52
+ rb_raise( rb_eArgError, "time interval must be positive");
53
+ break;
54
+
55
+ case T_FLOAT:
56
+ if (RFLOAT_VALUE(secs) < 0.0)
57
+ rb_raise( rb_eArgError, "time interval must be positive");
58
+ else {
59
+ double f, d;
60
+
61
+ d = modf( RFLOAT_VALUE(secs), &f);
62
+ t->tv_sec = (time_t) f, t->tv_usec = (time_t) (d*1e6+0.5);
63
+ if (f != t->tv_sec)
64
+ rb_raise( rb_eRangeError, "time interval out of Time range",
65
+ RFLOAT_VALUE(secs));
66
+ }
67
+ break;
68
+
69
+ case T_BIGNUM:
70
+ t->tv_sec = NUM2LONG(secs), t->tv_usec = 0;
71
+ if (t->tv_sec < 0)
72
+ rb_raise(rb_eArgError, "time interval must be positive");
73
+ break;
74
+
75
+ case T_NIL:
76
+ t->tv_sec = 0, t->tv_usec = 0;
77
+ break;
78
+
79
+ default:
80
+ rb_raise( rb_eTypeError, "can't convert %s into time interval",
81
+ rb_obj_classname( secs));
82
+ break;
83
+ }
84
+ }
85
+
86
+
87
+ /*
88
+ * call-seq:
89
+ * Process.getitimer -> [interval, value]
90
+ *
91
+ * Returns the interval and the remaining seconds to next alarm.
92
+ */
93
+
94
+ VALUE
95
+ rb_process_getitimer( VALUE obj)
96
+ {
97
+ struct itimerval it;
98
+ VALUE r;
99
+
100
+ if (getitimer( ITIMER_REAL, &it) < 0)
101
+ rb_raise( rb_eSystemCallError, "getitimer failed.");
102
+
103
+ r = rb_ary_new3( 2,
104
+ suppelement_timeval_sec( &it.it_interval),
105
+ suppelement_timeval_sec( &it.it_value));
106
+ return r;
107
+ }
108
+
109
+ VALUE
110
+ suppelement_timeval_sec( struct timeval *t)
111
+ {
112
+ VALUE r;
113
+ if (t->tv_usec)
114
+ r = rb_float_new( t->tv_sec + (1e-6 * t->tv_usec));
115
+ else
116
+ r = INT2NUM(t->tv_sec);
117
+ return r;
118
+ }
119
+
120
+
121
+ void Init_itimer( void)
122
+ {
123
+ rb_define_singleton_method( rb_mProcess, "setitimer", rb_process_setitimer, -1);
124
+ rb_define_singleton_method( rb_mProcess, "getitimer", rb_process_getitimer, 0);
125
+ }
126
+
@@ -0,0 +1,17 @@
1
+ /*
2
+ * supplement/itimer.h -- Interval timer
3
+ */
4
+
5
+ #ifndef __SUPPLEMENT_ITIMER_H__
6
+ #define __SUPPLEMENT_ITIMER_H__
7
+
8
+ #include <ruby.h>
9
+
10
+
11
+ extern VALUE rb_process_setitimer( int, VALUE *, VALUE);
12
+ extern VALUE rb_process_getitimer( VALUE obj);
13
+
14
+ extern void Init_itimer( void);
15
+
16
+ #endif
17
+
@@ -0,0 +1,144 @@
1
+ /*
2
+ * supplement/terminal.c -- Terminal methods
3
+ */
4
+
5
+ #include "terminal.h"
6
+
7
+ #if HAVE_HEADER_RUBYIO_H
8
+ #include <rubyio.h>
9
+ #elif HAVE_HEADER_RUBY_IO_H
10
+ #include <ruby/io.h>
11
+ #endif
12
+
13
+ #include <sys/ioctl.h>
14
+ #include <termios.h>
15
+
16
+ static VALUE io_unget( VALUE);
17
+ static VALUE io_reset( VALUE);
18
+
19
+
20
+ #ifndef RUBY_VM
21
+ #define RB_SYS_FAIL( fptr) rb_sys_fail( ((OpenFile *) fptr)->path);
22
+ #else
23
+ #define RB_SYS_FAIL( fptr) rb_sys_fail_str( ((rb_io_t *) fptr)->pathv);
24
+ #endif
25
+
26
+ /*
27
+ * call-seq:
28
+ * io.unget(str, ...) -> nil
29
+ *
30
+ * Feed <em>str</em> into the TTY's input queue.
31
+ *
32
+ * $stdin.unget "hello\n" #=> nil
33
+ */
34
+
35
+ VALUE
36
+ rb_io_unget( int argc, VALUE *argv, VALUE io)
37
+ {
38
+ #ifndef RUBY_VM
39
+ OpenFile *fptr;
40
+ #else
41
+ rb_io_t *fptr;
42
+ #endif
43
+ int fd;
44
+ struct termios oldtio, newtio;
45
+ void *v[5];
46
+
47
+ GetOpenFile( io, fptr);
48
+ #ifndef RUBY_VM
49
+ fd = fileno( fptr->f);
50
+ #else
51
+ fd = fptr->fd;
52
+ #endif
53
+
54
+ if (tcgetattr( fd, &oldtio) < 0)
55
+ RB_SYS_FAIL( fptr);
56
+ newtio = oldtio;
57
+ newtio.c_iflag &= ~ICRNL;
58
+ if (tcsetattr( fd, TCSANOW, &newtio) < 0)
59
+ RB_SYS_FAIL( fptr);
60
+
61
+ v[0] = &fd, v[1] = fptr, v[2] = &oldtio,
62
+ v[3] = &argc, v[4] = (void *) argv;
63
+ return rb_ensure( io_unget, (VALUE) v, io_reset, (VALUE) v);
64
+ }
65
+
66
+ VALUE
67
+ io_unget( VALUE v)
68
+ {
69
+ void **vp = (void **) v;
70
+ VALUE str;
71
+ long i;
72
+ int j;
73
+ char *p;
74
+ int *argc;
75
+ VALUE *argv;
76
+
77
+ argc = (int *)vp[3], argv = (VALUE *) vp[4];
78
+ for (j = 0 ; j < *argc; ++j) {
79
+ str = argv[ j];
80
+ p = RSTRING_PTR(str);
81
+ for (i = RSTRING_LEN(str); i; --i, ++p)
82
+ if (ioctl( *(int *) vp[0], TIOCSTI, p) < 0)
83
+ RB_SYS_FAIL( vp[1]);
84
+ }
85
+ return Qnil;
86
+ }
87
+
88
+ VALUE
89
+ io_reset( VALUE v)
90
+ {
91
+ void **vp = (void **) v;
92
+
93
+ if (tcsetattr( *(int *) vp[0], TCSANOW, (struct termios *) vp[2]) < 0)
94
+ RB_SYS_FAIL( vp[1]);
95
+
96
+ return Qnil;
97
+ }
98
+
99
+
100
+ /*
101
+ * call-seq:
102
+ * io.winsize() -> ary
103
+ *
104
+ * Get the available window space.
105
+ *
106
+ * cols, rows, xpixel, ypixel = $stdout.winsize
107
+ */
108
+
109
+ VALUE
110
+ rb_io_winsize( VALUE self)
111
+ {
112
+ #ifndef RUBY_VM
113
+ OpenFile *fptr;
114
+ #else
115
+ rb_io_t *fptr;
116
+ #endif
117
+ int fd;
118
+ struct winsize w;
119
+ VALUE r;
120
+
121
+ GetOpenFile( self, fptr);
122
+ #ifndef RUBY_VM
123
+ fd = fileno( fptr->f);
124
+ #else
125
+ fd = fptr->fd;
126
+ #endif
127
+
128
+ if (ioctl( fd, TIOCGWINSZ, &w) < 0)
129
+ RB_SYS_FAIL( fptr);
130
+ r = rb_ary_new2( 4);
131
+ rb_ary_store( r, 0, INT2NUM( w.ws_col));
132
+ rb_ary_store( r, 1, INT2NUM( w.ws_row));
133
+ rb_ary_store( r, 2, INT2NUM( w.ws_xpixel));
134
+ rb_ary_store( r, 3, INT2NUM( w.ws_ypixel));
135
+ return r;
136
+ }
137
+
138
+
139
+ void Init_terminal( void)
140
+ {
141
+ rb_define_method( rb_cIO, "unget", rb_io_unget, -1);
142
+ rb_define_method( rb_cIO, "winsize", rb_io_winsize, 0);
143
+ }
144
+
@@ -0,0 +1,21 @@
1
+ /*
2
+ * supplement/terminal.h -- Terminal methods
3
+ */
4
+
5
+ #ifndef __SUPPLEMENT_TERMINAL_H__
6
+ #define __SUPPLEMENT_TERMINAL_H__
7
+
8
+ #if HAVE_HEADER_RUBY_H
9
+ #include <ruby.h>
10
+ #elif HAVE_HEADER_RUBY_RUBY_H
11
+ #include <ruby/ruby.h>
12
+ #endif
13
+
14
+
15
+ extern VALUE rb_io_unget( int, VALUE *, VALUE);
16
+ extern VALUE rb_io_winsize( VALUE);
17
+
18
+ extern void Init_terminal( void);
19
+
20
+ #endif
21
+
data/lib/sync.c ADDED
@@ -0,0 +1,34 @@
1
+ /*
2
+ * sync.c -- Sync all
3
+ */
4
+
5
+
6
+ #include "sync.h"
7
+
8
+ #include <ruby.h>
9
+ #include <unistd.h>
10
+
11
+
12
+ static VALUE rb_process_sync( VALUE obj);
13
+
14
+
15
+ /*
16
+ * call-seq:
17
+ * Process.sync -> nil
18
+ *
19
+ * Force completion of pending disk writes (flush cache). See sync(8).
20
+ */
21
+
22
+ VALUE
23
+ rb_process_sync( VALUE obj)
24
+ {
25
+ sync();
26
+ return Qnil;
27
+ }
28
+
29
+
30
+ void Init_supplement_sync( void)
31
+ {
32
+ rb_define_singleton_method( rb_mProcess, "sync", rb_process_sync, 0);
33
+ }
34
+
data/lib/sync.h ADDED
@@ -0,0 +1,13 @@
1
+ /*
2
+ * sync.h -- Sync all
3
+ */
4
+
5
+
6
+ #ifndef __SYNC_H__
7
+ #define __SYNC_H__
8
+
9
+
10
+ extern void Init_supplement_sync( void);
11
+
12
+ #endif
13
+
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: supplement
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.6'
5
+ platform: ruby
6
+ authors:
7
+ - Bertram Scharpf
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: autorake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ description: ! 'Simple methods that didn''t manage to become part of standard Ruby.
28
+
29
+ '
30
+ email: <software@bertram-scharpf.de>
31
+ executables: []
32
+ extensions:
33
+ - lib/mkrf_conf
34
+ extra_rdoc_files:
35
+ - README
36
+ - LICENSE
37
+ files:
38
+ - lib/mkrf_conf
39
+ - lib/Rakefile
40
+ - lib/supplement.c
41
+ - lib/supplement.h
42
+ - lib/sync.c
43
+ - lib/sync.h
44
+ - lib/supplement/filesys.c
45
+ - lib/supplement/filesys.h
46
+ - lib/supplement/itimer.c
47
+ - lib/supplement/itimer.h
48
+ - lib/supplement/terminal.c
49
+ - lib/supplement/terminal.h
50
+ - lib/supplement/date.rb
51
+ - examples/teatimer
52
+ - README
53
+ - LICENSE
54
+ homepage: http://www.bertram-scharpf.de
55
+ licenses: []
56
+ metadata: {}
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements:
72
+ - Ruby and the autorake gem
73
+ rubyforge_project: NONE
74
+ rubygems_version: 2.0.7
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: Simple Ruby extensions
78
+ test_files: []