supplement 1.6

Sign up to get free protection for your applications and to get access to all the features.
data/lib/supplement.h ADDED
@@ -0,0 +1,83 @@
1
+ /*
2
+ * supplement.h -- Simple Ruby Extensions
3
+ */
4
+
5
+
6
+ #ifndef __SUPPELEMENT_H__
7
+ #define __SUPPELEMENT_H__
8
+
9
+ #if HAVE_HEADER_RUBY_H
10
+ #include <ruby.h>
11
+ #elif HAVE_HEADER_RUBY_RUBY_H
12
+ #include <ruby/ruby.h>
13
+ #endif
14
+
15
+
16
+ extern VALUE rb_obj_new_string( VALUE);
17
+ extern VALUE rb_obj_nil_if( VALUE, VALUE);
18
+ extern VALUE rb_krn_tap( VALUE);
19
+ extern VALUE rb_krn_tap_bang( VALUE);
20
+
21
+ extern VALUE rb_nil_notempty_p( VALUE);
22
+ extern VALUE rb_nil_each_line( VALUE);
23
+
24
+ extern VALUE rb_str_new_string( VALUE);
25
+ extern VALUE rb_str_notempty_p( VALUE);
26
+ extern VALUE rb_str_eat( int, VALUE *, VALUE);
27
+ extern VALUE rb_str_cut_bang( VALUE, VALUE);
28
+ #ifdef FEATURE_STRING_CLEAR
29
+ extern VALUE rb_str_clear( VALUE);
30
+ #endif
31
+ extern VALUE rb_str_head( int, VALUE *, VALUE);
32
+ extern VALUE rb_str_rest( int, VALUE *, VALUE);
33
+ extern VALUE rb_str_tail( int, VALUE *, VALUE);
34
+ #ifdef FEATURE_STRING_START_WITH
35
+ extern VALUE rb_str_start_with_p( VALUE, VALUE);
36
+ extern VALUE rb_str_end_with_p( VALUE, VALUE);
37
+ #endif
38
+ extern VALUE rb_str_starts_with_p( VALUE, VALUE);
39
+ extern VALUE rb_str_ends_with_p( VALUE, VALUE);
40
+ #ifdef FEATURE_STRING_ORD
41
+ extern VALUE rb_str_ord( VALUE);
42
+ #endif
43
+ extern VALUE rb_str_axe( int, VALUE *, VALUE);
44
+
45
+ extern VALUE rb_ary_notempty_p( VALUE);
46
+ extern VALUE rb_ary_indexes( VALUE);
47
+ extern VALUE rb_ary_pick( VALUE);
48
+ extern VALUE rb_ary_rpick( VALUE);
49
+ #ifdef FEATURE_ARRAY_INDEX_WITH_BLOCK
50
+ extern VALUE rb_ary_index( int, VALUE *, VALUE);
51
+ extern VALUE rb_ary_rindex( int, VALUE *, VALUE);
52
+ #endif
53
+ #ifdef FEATURE_ARRAY_SELECT_BANG
54
+ extern VALUE rb_ary_select_bang( VALUE);
55
+ #endif
56
+
57
+ extern VALUE rb_num_pos_p( VALUE);
58
+ extern VALUE rb_num_neg_p( VALUE);
59
+ extern VALUE rb_num_grammatical( VALUE, VALUE, VALUE);
60
+ extern VALUE rb_num_sqrt( VALUE);
61
+ extern VALUE rb_num_cbrt( VALUE);
62
+
63
+ extern VALUE rb_hash_notempty_p( VALUE);
64
+
65
+ extern VALUE rb_file_size( VALUE);
66
+ extern VALUE rb_file_flockb( int, VALUE *, VALUE);
67
+ extern VALUE rb_file_s_umask( int, VALUE *);
68
+ extern VALUE rb_dir_s_current( VALUE);
69
+ extern VALUE rb_dir_s_mkdir_bang( int, VALUE *);
70
+ extern VALUE rb_dir_entries_bang( VALUE);
71
+ extern VALUE rb_dir_chdir( VALUE);
72
+
73
+ extern VALUE rb_match_begin( int, VALUE *, VALUE);
74
+ extern VALUE rb_match_end( int, VALUE *, VALUE);
75
+
76
+ #ifdef FEATURE_THREAD_EXCLUSIVE
77
+ extern VALUE rb_thread_exclusive( void);
78
+ #endif
79
+
80
+ extern void Init_supplement( void);
81
+
82
+ #endif
83
+
@@ -0,0 +1,53 @@
1
+ #
2
+ # supplement/date.rb -- More Date methods
3
+ #
4
+
5
+ require "date"
6
+
7
+ class Date
8
+
9
+ def y ; year ; end
10
+ def m ; month ; end
11
+ def d ; day ; end
12
+
13
+ def to_a ; [ year, month, day ] ; end
14
+
15
+ end
16
+
17
+ unless DateTime.method_defined? :to_time then
18
+
19
+ class Time
20
+ def to_time ; self ; end
21
+ def to_date
22
+ jd = Date.civil_to_jd year, mon, mday, Date::ITALY
23
+ hd = Date.jd_to_ajd jd, 0, 0
24
+ Date.new! hd, 0, Date::ITALY
25
+ end
26
+ def to_datetime
27
+ jd = DateTime.civil_to_jd year, mon, mday, DateTime::ITALY
28
+ fr = DateTime.time_to_day_fraction hour, min, [sec, 59].min
29
+ fr += (Rational usec, 86400*1000000000)
30
+ of = (Rational utc_offset, 86400)
31
+ hd = DateTime.jd_to_ajd jd, fr, of
32
+ DateTime.new! hd, of, DateTime::ITALY
33
+ end
34
+ end
35
+
36
+ class Date
37
+ def to_time ; Time.local year, mon, mday ; end
38
+ def to_date ; self ; end
39
+ def to_datetime ; DateTime.new! Date.jd_to_ajd(jd,0,0), @of, @sg ; end
40
+ end
41
+
42
+ class DateTime < Date
43
+ def to_time
44
+ (new_offset 0).instance_eval do
45
+ Time.utc year, mon, mday, hour, min, sec + sec_fraction
46
+ end.getlocal
47
+ end
48
+ def to_date ; Date.new! Date.jd_to_ajd(jd,0,0), 0, @sg ; end
49
+ def to_datetime ; self ; end
50
+ end
51
+
52
+ end
53
+
@@ -0,0 +1,395 @@
1
+ /*
2
+ * supplement/filesys.c -- File system tools
3
+ */
4
+
5
+ #include "filesys.h"
6
+
7
+ #include <stdlib.h>
8
+ #ifdef __FSID_T_TYPE
9
+ /* Linux */
10
+ #include <sys/vfs.h>
11
+ #else
12
+ #include <sys/param.h>
13
+ #include <sys/mount.h>
14
+ #endif
15
+
16
+
17
+
18
+ static struct statfs *get_statfs( VALUE);
19
+
20
+ static ID id_mul;
21
+
22
+ /*
23
+ * Document-class: Filesys
24
+ *
25
+ * File systems. Mount and umount commands are on the TODO list.
26
+ */
27
+
28
+ /*
29
+ * Document-class: Filesys::Stat
30
+ *
31
+ * Objects of class <code>Filesys::Stat</code> encapsulate common status
32
+ * information for <code>Filesys</code> objects. The information is
33
+ * recorded at the moment the <code>Filesys::Stat</code> object is
34
+ * created; changes made to the file after that point will not be
35
+ * reflected.
36
+ *
37
+ * Many of its attributes contain platform-specific values, and not all
38
+ * values are meaningful on all systems.
39
+ */
40
+
41
+ VALUE
42
+ rb_fsstat_s_alloc( VALUE klass)
43
+ {
44
+ return Data_Wrap_Struct( klass, NULL, &free, (struct statfs *) NULL);
45
+ }
46
+
47
+ /*
48
+ * call-seq:
49
+ *
50
+ * Filesys::Stat.new( dir_name) => stat
51
+ *
52
+ * Create a Filesys::Stat object for the given file system.
53
+ */
54
+
55
+ VALUE
56
+ rb_fsstat_init( VALUE obj, VALUE dname)
57
+ {
58
+ struct statfs st, *nst;
59
+
60
+ SafeStringValue( dname);
61
+
62
+ if (statfs( StringValueCStr( dname), &st) == -1)
63
+ rb_sys_fail( RSTRING_PTR(dname));
64
+ if (DATA_PTR(obj)) {
65
+ free( DATA_PTR(obj));
66
+ DATA_PTR(obj) = NULL;
67
+ }
68
+ nst = ALLOC(struct statfs);
69
+ *nst = st;
70
+ DATA_PTR(obj) = nst;
71
+
72
+ return Qnil;
73
+ }
74
+
75
+ /* :nodoc: */
76
+ VALUE
77
+ rb_fsstat_init_copy( VALUE copy, VALUE orig)
78
+ {
79
+ struct statfs *nst;
80
+
81
+ if (copy == orig)
82
+ return orig;
83
+
84
+ rb_check_frozen( copy);
85
+ if (!rb_obj_is_instance_of( orig, rb_obj_class( copy)))
86
+ rb_raise(rb_eTypeError, "wrong argument class");
87
+ if (DATA_PTR(copy)) {
88
+ free( DATA_PTR(copy));
89
+ DATA_PTR(copy) = NULL;
90
+ }
91
+ if (DATA_PTR(orig)) {
92
+ nst = ALLOC(struct statfs);
93
+ *nst = *(struct statfs *) DATA_PTR(orig);
94
+ DATA_PTR(copy) = nst;
95
+ }
96
+
97
+ return copy;
98
+ }
99
+
100
+ /* :nodoc: */
101
+ struct statfs *
102
+ get_statfs( VALUE self)
103
+ {
104
+ struct statfs *st;
105
+ Data_Get_Struct(self, struct statfs, st);
106
+ if (!st)
107
+ rb_raise( rb_eTypeError, "uninitialized Filesys::Stat");
108
+ return st;
109
+ }
110
+
111
+
112
+
113
+ /*
114
+ * call-seq:
115
+ * stat.type => fixnum
116
+ *
117
+ * Type of filesystem.
118
+ */
119
+
120
+ VALUE
121
+ rb_fsstat_type( VALUE self)
122
+ {
123
+ return INT2NUM(get_statfs(self)->f_type);
124
+ }
125
+
126
+ /*
127
+ * call-seq:
128
+ * stat.bsize => fixnum
129
+ *
130
+ * Filesystem fragment size.
131
+ */
132
+
133
+ VALUE
134
+ rb_fsstat_bsize( VALUE self)
135
+ {
136
+ return INT2NUM(get_statfs(self)->f_bsize);
137
+ }
138
+
139
+ /*
140
+ * call-seq:
141
+ * stat.blocks => fixnum
142
+ *
143
+ * Total data blocks in filesystem.
144
+ */
145
+
146
+ VALUE
147
+ rb_fsstat_blocks( VALUE self)
148
+ {
149
+ return INT2NUM(get_statfs(self)->f_blocks);
150
+ }
151
+
152
+ /*
153
+ * call-seq:
154
+ * stat.bfree => fixnum
155
+ *
156
+ * Free blocks in filesystem.
157
+ */
158
+
159
+ VALUE
160
+ rb_fsstat_bfree( VALUE self)
161
+ {
162
+ return INT2NUM(get_statfs(self)->f_bfree);
163
+ }
164
+
165
+ /*
166
+ * call-seq:
167
+ * stat.bavail => fixnum
168
+ *
169
+ * Free blocks avail to non-superuser.
170
+ */
171
+
172
+ VALUE
173
+ rb_fsstat_bavail( VALUE self)
174
+ {
175
+ return INT2NUM(get_statfs(self)->f_bavail);
176
+ }
177
+
178
+ /*
179
+ * call-seq:
180
+ * stat.files => fixnum
181
+ *
182
+ * Total file nodes in filesystem.
183
+ */
184
+
185
+ VALUE
186
+ rb_fsstat_files( VALUE self)
187
+ {
188
+ return INT2NUM(get_statfs(self)->f_files);
189
+ }
190
+
191
+ /*
192
+ * call-seq:
193
+ * stat.ffree => fixnum
194
+ *
195
+ * Free nodes avail to non-superuser.
196
+ */
197
+
198
+ VALUE
199
+ rb_fsstat_ffree( VALUE self)
200
+ {
201
+ return INT2NUM(get_statfs(self)->f_ffree);
202
+ }
203
+
204
+
205
+ /*
206
+ * call-seq:
207
+ * stat.fsid => ary
208
+ *
209
+ * Filesystem id; array of 2 integers.
210
+ */
211
+
212
+ #ifdef __FSID_T_TYPE
213
+ /* Linux */
214
+ #define FSID_val __val
215
+ #else
216
+ #define FSID_val val
217
+ #endif
218
+
219
+ VALUE
220
+ rb_fsstat_fsid( VALUE self)
221
+ {
222
+ struct statfs *s;
223
+ VALUE r;
224
+
225
+ s = get_statfs( self);
226
+ r = rb_ary_new3( 2,
227
+ INT2NUM(s->f_fsid.FSID_val[0]),
228
+ INT2NUM(s->f_fsid.FSID_val[1]));
229
+ return r;
230
+ }
231
+
232
+
233
+ /*
234
+ * call-seq:
235
+ * stat.bytes => fixnum
236
+ *
237
+ * Total data bytes in filesystem.
238
+ */
239
+
240
+ VALUE
241
+ rb_fsstat_bytes( VALUE self)
242
+ {
243
+ struct statfs *s;
244
+
245
+ s = get_statfs( self);
246
+ return rb_funcall( INT2NUM(s->f_blocks), id_mul, 1, INT2NUM(s->f_bsize));
247
+ }
248
+
249
+ /*
250
+ * call-seq:
251
+ * stat.free => num
252
+ *
253
+ * Free bytes in filesystem.
254
+ */
255
+
256
+ VALUE
257
+ rb_fsstat_free( VALUE self)
258
+ {
259
+ struct statfs *s;
260
+
261
+ s = get_statfs( self);
262
+ return rb_funcall( INT2NUM(s->f_bfree), id_mul, 1, INT2NUM(s->f_bsize));
263
+ }
264
+
265
+ /*
266
+ * call-seq:
267
+ * stat.avail => num
268
+ *
269
+ * Free bytes avail to non-superuser.
270
+ */
271
+
272
+ VALUE
273
+ rb_fsstat_avail( VALUE self)
274
+ {
275
+ struct statfs *s;
276
+
277
+ s = get_statfs( self);
278
+ return rb_funcall( INT2NUM(s->f_bavail), id_mul, 1, INT2NUM(s->f_bsize));
279
+ }
280
+
281
+
282
+ /*
283
+ * call-seq:
284
+ * stat.pfree => fixnum
285
+ *
286
+ * Free percentage in filesystem.
287
+ */
288
+
289
+ VALUE
290
+ rb_fsstat_pfree( VALUE self)
291
+ {
292
+ struct statfs *s;
293
+
294
+ s = get_statfs( self);
295
+ return rb_float_new( 100.0 * s->f_bfree / s->f_blocks);
296
+ }
297
+
298
+ /*
299
+ * call-seq:
300
+ * stat.pavail => fixnum
301
+ *
302
+ * Free percentage avail to non-superuser.
303
+ */
304
+
305
+ VALUE
306
+ rb_fsstat_pavail( VALUE self)
307
+ {
308
+ struct statfs *s;
309
+
310
+ s = get_statfs( self);
311
+ return rb_float_new( 100.0 * s->f_bavail / s->f_blocks);
312
+ }
313
+
314
+
315
+ /*
316
+ * call-seq:
317
+ * stat.inspect => string
318
+ *
319
+ * Produce a nicely formatted description of <i>stat</i> with all available
320
+ * information.
321
+ */
322
+
323
+ VALUE
324
+ rb_fsstat_inspect( VALUE self)
325
+ {
326
+ VALUE str;
327
+ int i, m;
328
+ static const struct {
329
+ const char *name;
330
+ VALUE (*func)(VALUE);
331
+ } member[] = {
332
+ { "type", &rb_fsstat_type },
333
+ { "bsize", &rb_fsstat_bsize },
334
+ { "blocks", &rb_fsstat_blocks},
335
+ { "bfree", &rb_fsstat_bfree },
336
+ { "bavail", &rb_fsstat_bavail},
337
+ { "files", &rb_fsstat_files },
338
+ { "ffree", &rb_fsstat_ffree },
339
+ { "fsid", &rb_fsstat_fsid }
340
+ };
341
+
342
+ str = rb_str_buf_new2("#<");
343
+ rb_str_buf_cat2(str, rb_obj_classname( self));
344
+ rb_str_buf_cat2(str, " ");
345
+
346
+ m = sizeof(member) / sizeof(member[0]);
347
+ for (i = 0; i < m; i++) {
348
+ VALUE v;
349
+
350
+ if (i > 0)
351
+ rb_str_buf_cat2( str, ", ");
352
+ rb_str_buf_cat2( str, member[i].name);
353
+ rb_str_buf_cat2( str, "=");
354
+ rb_str_append( str, rb_inspect( (*member[i].func)( self)));
355
+ }
356
+ rb_str_buf_cat2( str, ">");
357
+ OBJ_INFECT( str, self);
358
+
359
+ return str;
360
+ }
361
+
362
+
363
+
364
+ void Init_filesys( void)
365
+ {
366
+ VALUE rb_cFilesys;
367
+ VALUE rb_cFilesysStat;
368
+
369
+ rb_cFilesys = rb_define_class( "Filesys", rb_cObject);
370
+
371
+ rb_cFilesysStat = rb_define_class_under( rb_cFilesys, "Stat", rb_cObject);
372
+ rb_define_alloc_func( rb_cFilesysStat, rb_fsstat_s_alloc);
373
+ rb_define_method( rb_cFilesysStat, "initialize", rb_fsstat_init, 1);
374
+ rb_define_method( rb_cFilesysStat, "initialize_copy", rb_fsstat_init_copy, 1);
375
+
376
+ rb_define_method( rb_cFilesysStat, "type", rb_fsstat_type , 0);
377
+ rb_define_method( rb_cFilesysStat, "bsize", rb_fsstat_bsize , 0);
378
+ rb_define_method( rb_cFilesysStat, "blocks", rb_fsstat_blocks, 0);
379
+ rb_define_method( rb_cFilesysStat, "bfree", rb_fsstat_bfree , 0);
380
+ rb_define_method( rb_cFilesysStat, "bavail", rb_fsstat_bavail, 0);
381
+ rb_define_method( rb_cFilesysStat, "files", rb_fsstat_files , 0);
382
+ rb_define_method( rb_cFilesysStat, "ffree", rb_fsstat_ffree , 0);
383
+ rb_define_method( rb_cFilesysStat, "fsid", rb_fsstat_fsid , 0);
384
+
385
+ rb_define_method( rb_cFilesysStat, "bytes", rb_fsstat_bytes, 0);
386
+ rb_define_method( rb_cFilesysStat, "free", rb_fsstat_free , 0);
387
+ rb_define_method( rb_cFilesysStat, "avail", rb_fsstat_avail, 0);
388
+ rb_define_method( rb_cFilesysStat, "pfree", rb_fsstat_pfree , 0);
389
+ rb_define_method( rb_cFilesysStat, "pavail", rb_fsstat_pavail, 0);
390
+
391
+ rb_define_method( rb_cFilesysStat, "inspect", rb_fsstat_inspect, 0);
392
+
393
+ id_mul = rb_intern( "*");
394
+ }
395
+