tmpfile 1.0.2

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 15226a21d3b0ae626d223890f7782b2b571195ca2dd16e7bab71f370f1f22352
4
+ data.tar.gz: 220b9bbd9a674cbbba77422ee14de71eba7c27e2d2dc26de2b8a3f372d9e6c34
5
+ SHA512:
6
+ metadata.gz: 73800baf241b4a3066a25ccb28cd41d7736b4868b3b5409398184fd8e7b28ed2e932d5e4aebc16d3576759cbdce46aa622fc12e127c9a8adf851127b9cca06b0
7
+ data.tar.gz: 587279e18a27bab3db6cf2a7888f90fbd2db8919c0ac869b9bfa934b31d47eac571913c2a37276ff5bf1fe37685e8029e05820b101c4a37e87d275fc8ccdf5af
data/.rubocop.yml ADDED
@@ -0,0 +1,200 @@
1
+ AllCops:
2
+ NewCops: enable
3
+ TargetRubyVersion: 3.1
4
+
5
+ Layout/LineLength:
6
+ Max: 1024
7
+
8
+ Layout/SpaceInsideParens:
9
+ Enabled: false
10
+
11
+ Layout/SpaceInsideArrayLiteralBrackets:
12
+ EnforcedStyle: space
13
+
14
+ Layout/MultilineMethodDefinitionBraceLayout:
15
+ EnforcedStyle: new_line
16
+
17
+ Layout/MultilineMethodCallBraceLayout:
18
+ EnforcedStyle: new_line
19
+
20
+ Layout/MultilineHashBraceLayout:
21
+ EnforcedStyle: new_line
22
+
23
+ Layout/MultilineArrayBraceLayout:
24
+ EnforcedStyle: new_line
25
+
26
+ Layout/SpaceAfterNot:
27
+ Enabled: false
28
+
29
+ Layout/SpaceBeforeBlockBraces:
30
+ EnforcedStyle: space
31
+
32
+ Layout/ExtraSpacing:
33
+ AllowForAlignment: true
34
+ AllowBeforeTrailingComments: true
35
+
36
+ Layout/MultilineArrayBraceLayout:
37
+ Enabled: false
38
+
39
+ Lint/AmbiguousOperatorPrecedence:
40
+ Enabled: false
41
+
42
+ Lint/RedundantCopDisableDirective:
43
+ Enabled: false
44
+
45
+ Lint/NonLocalExitFromIterator:
46
+ Enabled: false
47
+
48
+ Naming/ClassAndModuleCamelCase:
49
+ Enabled: false
50
+
51
+ Naming/AccessorMethodName:
52
+ Enabled: false
53
+
54
+ Layout/ArgumentAlignment:
55
+ EnforcedStyle: with_first_argument
56
+
57
+ Lint/ScriptPermission:
58
+ Enabled: false
59
+
60
+ Lint/Loop:
61
+ Enabled: false
62
+
63
+ Style/BitwisePredicate:
64
+ Enabled: false
65
+
66
+ Style/SlicingWithRange:
67
+ Enabled: false
68
+
69
+ Style/FloatDivision:
70
+ Enabled: false
71
+
72
+ Style/FrozenStringLiteralComment:
73
+ Enabled: false
74
+
75
+ Style/NonNilCheck:
76
+ Enabled: false
77
+
78
+ Style/MultipleComparison:
79
+ Enabled: false
80
+
81
+ Style/OptionalBooleanParameter:
82
+ Enabled: false
83
+
84
+ Style/NumericPredicate:
85
+ Enabled: false
86
+
87
+ Style/NumericLiterals:
88
+ Enabled: false
89
+
90
+ Style/IfUnlessModifier:
91
+ Enabled: false
92
+
93
+ Style/RedundantBegin:
94
+ Enabled: false
95
+
96
+ Style/RedundantReturn:
97
+ Enabled: false
98
+
99
+ Style/AccessorGrouping:
100
+ Enabled: false
101
+
102
+ Style/AccessModifierDeclarations:
103
+ EnforcedStyle: inline
104
+
105
+ Style/NegatedIf:
106
+ EnforcedStyle: postfix
107
+
108
+ Style/RegexpLiteral:
109
+ EnforcedStyle: slashes
110
+ AllowInnerSlashes: true
111
+
112
+ Style/ZeroLengthPredicate:
113
+ Enabled: false
114
+
115
+ Style/NestedTernaryOperator:
116
+ Enabled: false
117
+
118
+ Style/RedundantSelf:
119
+ Enabled: false
120
+
121
+ Style/StringConcatenation:
122
+ Enabled: false
123
+
124
+ Style/StderrPuts:
125
+ Enabled: false
126
+
127
+ Style/GuardClause:
128
+ Enabled: false
129
+
130
+ Style/RedundantInterpolation:
131
+ Enabled: false
132
+
133
+ Style/RedundantFileExtensionInRequire:
134
+ Enabled: false
135
+
136
+ Style/MutableConstant:
137
+ Enabled: false
138
+
139
+ Style/EmptyMethod:
140
+ Enabled: false
141
+
142
+ Style/FormatString:
143
+ Enabled: false
144
+
145
+ Style/CommentedKeyword:
146
+ Enabled: false
147
+
148
+ Style/SafeNavigation:
149
+ Enabled: false
150
+
151
+ Style/WordArray:
152
+ EnforcedStyle: brackets
153
+
154
+ Style/SymbolArray:
155
+ EnforcedStyle: brackets
156
+
157
+ Metrics/MethodLength:
158
+ Enabled: false
159
+
160
+ Metrics/BlockLength:
161
+ Enabled: false
162
+
163
+ Metrics/PerceivedComplexity:
164
+ Enabled: false
165
+
166
+ Metrics/ModuleLength:
167
+ Enabled: false
168
+
169
+ Metrics/ClassLength:
170
+ Enabled: false
171
+
172
+ Metrics/AbcSize:
173
+ Enabled: false
174
+
175
+ Metrics/CyclomaticComplexity:
176
+ Enabled: false
177
+
178
+ Metrics/ParameterLists:
179
+ Enabled: false
180
+
181
+ Metrics/BlockNesting:
182
+ Enabled: false
183
+
184
+ Naming/HeredocDelimiterNaming:
185
+ Enabled: false
186
+
187
+ Naming/MethodParameterName:
188
+ Enabled: false
189
+
190
+ Naming/VariableNumber:
191
+ Enabled: false
192
+
193
+ Naming/VariableName:
194
+ Enabled: false
195
+
196
+ Naming/MethodName:
197
+ Enabled: false
198
+
199
+ Naming/FileName:
200
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,16 @@
1
+ ## [1.0.2] - 2026-12-05
2
+ - Added ftell, seek, eof?, putc, getc
3
+ - Moved to bundler layout
4
+ - replaced manual test with minitest
5
+
6
+ ## [0.2.2] - 2016-06-18
7
+ - Code format tidy up
8
+
9
+ ## [0.2.1] - 2016-05-22
10
+ - Better exception messages
11
+
12
+ ## [0.2.0] - 2013-01-10
13
+ - Added in unlink and flush methods
14
+
15
+ ## [0.1.0] - 2012-09-29
16
+ - Initial local working version
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 robertburrowes
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # Tmpfile
2
+
3
+ ca. 2012 learning exercise for writing a C extension for Ruby.
4
+ Created a better temporary file gem than the then Ruby tempfile gem.
5
+
6
+ The newer version of tempfile has improved a lot, so this gem hasn't been needed for sometime. It is still a good reference and test gem for me.
7
+
8
+ ## Installation
9
+
10
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
11
+
12
+ Install the gem and add to the application's Gemfile by executing:
13
+
14
+ ```bash
15
+ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
16
+ ```
17
+
18
+ If bundler is not being used to manage dependencies, install the gem by executing:
19
+
20
+ ```bash
21
+ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
22
+ ```
23
+
24
+ ## Usage
25
+ See test/test_tmpfile.rb
26
+ ```
27
+ require 'tmpfile'
28
+ include
29
+
30
+ # Create a named temporary file, that gets deleted at the end of the block
31
+ TmpFileMod::TmpFile.open('/tmp/xxx','w+') do |fd|
32
+ ...
33
+ end
34
+
35
+ # For debugging, the delete on close can be disabled.
36
+ TmpFileMod::TmpFile.open('/tmp/xxx','w+') do |fd|
37
+ #fd.no_unlink; #For testing, so we can see what was produced.
38
+ ...
39
+ end
40
+ ```
41
+
42
+ ## Development
43
+
44
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
45
+
46
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
47
+
48
+ I have been trying out rv, so a build test cycle becomes:
49
+ ```
50
+ rv run bundle exec rake compile
51
+ rv run rake test
52
+ ```
53
+
54
+ Building and installing local gem with rv:
55
+ ```
56
+ rv run gem build tmpfile.gemspec
57
+ rv run bundle exec rake install
58
+ ```
59
+
60
+ ## License
61
+
62
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ require "rake/extensiontask"
13
+
14
+ task build: :compile
15
+
16
+ GEMSPEC = Gem::Specification.load("tmpfile.gemspec")
17
+
18
+ Rake::ExtensionTask.new("tmpfile", GEMSPEC) do |ext|
19
+ ext.lib_dir = "lib/tmpfile"
20
+ end
21
+
22
+ task default: %i[clobber compile test rubocop]
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "mkmf"
4
+
5
+ dir_config('tmpfile') #optional include-path, optional lib-path
6
+
7
+ # Makes all symbols private by default to avoid unintended conflict
8
+ # with other gems. To explicitly export symbols you can use RUBY_FUNC_EXPORTED
9
+ # selectively, or entirely remove this flag.
10
+ append_cflags("-fvisibility=hidden")
11
+
12
+ abort "Missing tmpfile.h" unless have_header("tmpfile.h")
13
+ #abort "Missing libmy_c_lib" unless have_library("my_c_lib", "some_c_function")
14
+
15
+ create_makefile("tmpfile/tmpfile")
@@ -0,0 +1,537 @@
1
+ #include <sys/types.h>
2
+ #include <sys/wait.h>
3
+
4
+ #include <errno.h>
5
+ #include <string.h>
6
+ #include <stdlib.h>
7
+ #include <stdio.h>
8
+ #include <unistd.h>
9
+
10
+ #include "tmpfile.h"
11
+
12
+
13
+ static VALUE myModule;
14
+ static VALUE myClass;
15
+ static VALUE tmp_unlink(VALUE obj);
16
+
17
+ static size_t tmp_memsize(const void *ptr)
18
+ {
19
+ return sizeof(Tmpfile);
20
+ }
21
+
22
+ // Mark our structure for freeing
23
+ static void tmp_mark(void *tf)
24
+ {
25
+ //don't need to do anything.
26
+ }
27
+
28
+ // Free a returned structure.
29
+ static void tmp_free(void *tf)
30
+ {
31
+ if(tf)
32
+ {
33
+ if(((Tmpfile *)tf)->fp)
34
+ fclose(((Tmpfile *)tf)->fp);
35
+ free(tf);
36
+ }
37
+ }
38
+
39
+ // Now need this wrapper to update
40
+ // Data_Wrap_Struct to TypedData_Wrap_Struct
41
+ // & Data_Get_Struct to TypedData_Get_Struct
42
+ static const rb_data_type_t tmp_type = {
43
+ .wrap_struct_name = "Tmpfile",
44
+ .function =
45
+ {
46
+ .dmark = tmp_mark,
47
+ .dfree = tmp_free,
48
+ .dsize = tmp_memsize,
49
+ },
50
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
51
+ };
52
+
53
+ // Dynamically allocate our Tmpfile structure
54
+ static VALUE tmp_alloc(VALUE klass)
55
+ {
56
+ Tmpfile *tf = calloc(1, sizeof(Tmpfile));
57
+
58
+ // return Data_Wrap_Struct(klass, tmp_mark, tmp_free, tf); //wraps a c struct in
59
+ return TypedData_Wrap_Struct(klass, &tmp_type, tf); // Newer form of Data_Wrap_Struct
60
+ // recover with TypedData_Get_Struct()
61
+ }
62
+
63
+ static VALUE tmp_initialize(int argc, VALUE *argv, VALUE obj)
64
+ {
65
+ VALUE filename, flags;
66
+ Tmpfile *tf;
67
+ FILE *fp;
68
+ char *filename_p, *flags_p;
69
+
70
+ rb_scan_args(argc, argv, "2", &filename, &flags);
71
+ if(TYPE(filename) != T_STRING)
72
+ rb_raise(rb_eTypeError, "tmp_initialize: Filename should be a String");
73
+
74
+ if(TYPE(flags) != T_STRING)
75
+ rb_raise(rb_eTypeError, "tmp_initialize: Flags should be a String");
76
+
77
+ // Data_Get_Struct(obj, Tmpfile, tf);
78
+ TypedData_Get_Struct(obj, Tmpfile, &tmp_type, tf); // Newer form of Data_Get_Struct
79
+
80
+ filename_p = StringValuePtr(filename);
81
+ flags_p = StringValuePtr(flags);
82
+ if( ( fp = fopen(filename_p, flags_p)) == NULL)
83
+ rb_raise(rb_eArgError, "tmp_initialize: %s", strerror(errno));
84
+
85
+ tf->fp = fp;
86
+ strcpy(tf->filename, filename_p);
87
+ strcpy(tf->flags, flags_p);
88
+
89
+ return obj;
90
+ }
91
+
92
+ static VALUE tmp_close(VALUE obj)
93
+ {
94
+ Tmpfile *tf;
95
+
96
+ // Data_Get_Struct(obj, Tmpfile, tf);
97
+ TypedData_Get_Struct(obj, Tmpfile, &tmp_type, tf); // Newer form of Data_Get_Struct
98
+ if(tf->fp != NULL)
99
+ {
100
+ fclose(tf->fp); //close the file
101
+ tf->fp = NULL;
102
+ if(tf->no_unlink == 0)
103
+ tmp_unlink(obj); //remove the file from disk
104
+ }
105
+ return obj;
106
+ }
107
+
108
+ //Class level method, alternate to new.
109
+ static VALUE tmp_open(int argc, VALUE *argv, VALUE obj)
110
+ {
111
+ VALUE nobj = rb_class_new_instance( argc, argv, obj); //new instance of this class.
112
+
113
+ if(rb_block_given_p()) //if we have a block.
114
+ { rb_yield(nobj);
115
+ tmp_close(nobj);
116
+ }
117
+ return nobj;
118
+ }
119
+
120
+ static VALUE tmp_each_line(VALUE obj)
121
+ {
122
+ Tmpfile *tf;
123
+ char buffer[8196];
124
+
125
+ // Data_Get_Struct(obj, Tmpfile, tf);
126
+ TypedData_Get_Struct(obj, Tmpfile, &tmp_type, tf); // Newer form of Data_Get_Struct
127
+
128
+
129
+ if(tf->fp == NULL)
130
+ rb_raise(rb_eRuntimeError, "tmp_each_line: File not open!"); //need to change error type.
131
+
132
+ if(rb_block_given_p()) //if we have a block.
133
+ {
134
+ while(fgets(buffer,8192,tf->fp) != NULL) //read the next line.
135
+ rb_yield(rb_str_new2(buffer)); //pass that line to the block.
136
+ //we should have EOF.
137
+ if(feof(tf->fp) == 0) //Then we had an io error, rather than eof.
138
+ rb_raise(rb_eRuntimeError, "tmp_each_line: %s", strerror(errno)); //need to change error type.
139
+ }
140
+ else
141
+ rb_raise(rb_eRuntimeError, "tmp_each_line: no block given"); //need to change error type.
142
+
143
+ return obj; //return ourselves.
144
+ }
145
+
146
+ //Read a line. Max line size is 8192.
147
+ static VALUE tmp_gets(VALUE obj)
148
+ {
149
+ Tmpfile *tf;
150
+ char buffer[8194];
151
+
152
+ // Data_Get_Struct(obj, Tmpfile, tf);
153
+ TypedData_Get_Struct(obj, Tmpfile, &tmp_type, tf); // Newer form of Data_Get_Struct
154
+
155
+ if(tf->fp == NULL)
156
+ rb_raise(rb_eRuntimeError, "tmp_gets: File not open!"); //need to change error type.
157
+
158
+ if(fgets(buffer,8192,tf->fp) == NULL) //read the next line.
159
+ {
160
+ if(feof(tf->fp) == 0) //Then we had an io error, rather than eof.
161
+ rb_raise(rb_eRuntimeError, "tmp_gets: %s", strerror(errno)); //need to change error type.
162
+ else
163
+ return Qnil; //nothing more to read.
164
+ }
165
+ return rb_str_new2(buffer); //return the string we just read.
166
+ }
167
+
168
+ static VALUE tmp_getc(VALUE obj)
169
+ {
170
+ Tmpfile *tf;
171
+ int c; // Read into int, so we can tell -1 from 0xFF
172
+
173
+ // Data_Get_Struct(obj, Tmpfile, tf);
174
+ TypedData_Get_Struct(obj, Tmpfile, &tmp_type, tf); // Newer form of Data_Get_Struct
175
+
176
+ if(tf->fp == NULL)
177
+ rb_raise(rb_eRuntimeError, "tmp_getc: File not open!"); //need to change error type.
178
+
179
+ if ((c = fgetc(tf->fp)) == EOF) // read the next byte.
180
+ {
181
+ if(feof(tf->fp) == 0) //Then we had an io error, rather than eof.
182
+ rb_raise(rb_eRuntimeError, "tmp_getc: %s", strerror(errno)); //need to change error type.
183
+ else
184
+ return Qnil; //nothing more to read.
185
+ }
186
+ return INT2FIX((uint8_t) c); // Returns Ruby Integer 255
187
+ }
188
+
189
+ //Mimic Ruby puts, adding a '\n' if the strings don't have one.
190
+ static VALUE tmp_puts(int argc, VALUE *argv, VALUE obj)
191
+ {
192
+ Tmpfile *tf;
193
+ int i;
194
+ VALUE tmp;
195
+
196
+ // Data_Get_Struct(obj, Tmpfile, tf);
197
+ TypedData_Get_Struct(obj, Tmpfile, &tmp_type, tf); // Newer form of Data_Get_Struct
198
+
199
+ if(tf->fp == NULL)
200
+ rb_raise(rb_eRuntimeError, "tmp_puts: File not open!"); //need to change error type.
201
+
202
+ for(i = 0; i < argc; i++)
203
+ {
204
+ char *s;
205
+ int l;
206
+
207
+ if(TYPE(argv[i]) == T_STRING)
208
+ s = StringValuePtr(argv[i]);
209
+ else
210
+ { tmp = rb_funcall( argv[i], rb_intern("to_s"), 0 );
211
+ s = StringValuePtr(tmp);
212
+ }
213
+
214
+ l = (int)strlen(s);
215
+ //only output if the string isn't empty.
216
+ if(l && fputs(s, tf->fp) == EOF)
217
+ rb_raise(rb_eRuntimeError, "tmp_puts: %s", strerror(errno)); //need to change error type.
218
+ //Add a new line char if the string didn't end in one.
219
+ if(l && s[l-1] != '\n' && fputs("\n", tf->fp) == EOF)
220
+ rb_raise(rb_eRuntimeError, "tmp_puts: %s", strerror(errno)); //need to change error type.
221
+ }
222
+
223
+ return obj;
224
+ }
225
+
226
+ // Write a single byte the open File.
227
+ // @param obj [String|Numeric] Byte is first char of String or Numeric & 0xFF
228
+ // @return [Numeric] The Byte we wrote
229
+ static VALUE tmp_putc(VALUE obj, VALUE arg)
230
+ {
231
+ Tmpfile *tf;
232
+ uint8_t first_byte;
233
+
234
+ // Data_Get_Struct(obj, Tmpfile, tf);
235
+ TypedData_Get_Struct(obj, Tmpfile, &tmp_type, tf); // Newer form of Data_Get_Struct
236
+
237
+ if(tf->fp == NULL)
238
+ rb_raise(rb_eRuntimeError, "tmp_putc: File not open!"); //need to change error type.
239
+
240
+ if (RB_TYPE_P(arg, T_STRING)) {
241
+ if (RSTRING_LEN(arg) == 0) {
242
+ rb_raise(rb_eArgError, "tmp_putc: string cannot be empty");
243
+ }
244
+ first_byte = (uint8_t)RSTRING_PTR(arg)[0];
245
+ } else if (rb_obj_is_kind_of(arg, rb_cNumeric)) {
246
+ // Converts Integer/Float/Numeric to an integer and takes the least significant byte
247
+ first_byte = (uint8_t)(NUM2ULONG(arg) & 0xFF);
248
+ } else {
249
+ rb_raise(rb_eTypeError, "tmp_putc: expected String or Numeric");
250
+ }
251
+
252
+ if (putc(first_byte, tf->fp) == EOF)
253
+ rb_raise(rb_eRuntimeError, "tmp_putc: %s", strerror(errno)); //need to change error type.
254
+
255
+ return INT2FIX(first_byte);
256
+ }
257
+
258
+ static VALUE tmp_print(int argc, VALUE *argv, VALUE obj)
259
+ {
260
+ Tmpfile *tf;
261
+ int i;
262
+ VALUE tmp;
263
+
264
+ // Data_Get_Struct(obj, Tmpfile, tf);
265
+ TypedData_Get_Struct(obj, Tmpfile, &tmp_type, tf); // Newer form of Data_Get_Struct
266
+
267
+ if(tf->fp == NULL)
268
+ rb_raise(rb_eRuntimeError, "tmp_print: File not open!"); //need to change error type.
269
+
270
+ for(i = 0; i < argc; i++)
271
+ {
272
+ char *s;
273
+
274
+ if(TYPE(argv[i]) == T_STRING)
275
+ s = StringValuePtr(argv[i]);
276
+ else
277
+ { tmp = rb_funcall( argv[i], rb_intern("to_s"), 0 );
278
+ s = StringValuePtr(tmp);
279
+ }
280
+
281
+ //only output if the string isn't empty.
282
+ if(s[0] != '\0' && fputs(s, tf->fp) == EOF)
283
+ rb_raise(rb_eRuntimeError, "tmp_print: %s", strerror(errno)); //need to change error type.
284
+ }
285
+
286
+ return obj;
287
+ }
288
+
289
+ static VALUE tmp_seek(int argc, VALUE *argv, VALUE obj)
290
+ {
291
+ Tmpfile *tf;
292
+ VALUE byte_arg;
293
+ VALUE whence_arg;
294
+ int whence;
295
+
296
+ // Data_Get_Struct(obj, Tmpfile, tf);
297
+ TypedData_Get_Struct(obj, Tmpfile, &tmp_type, tf); // Newer form of Data_Get_Struct
298
+ if(tf->fp == NULL)
299
+ rb_raise(rb_eRuntimeError, "tmp_seek: File not open!"); //need to change error type.
300
+
301
+ if(tf->fp != NULL){
302
+ if(argc == 0) {
303
+ if(fseek(tf->fp, 0, SEEK_SET) == -1) //go back to the start of the file.
304
+ rb_raise(rb_eRuntimeError, "tmp_seek %s", strerror(errno));
305
+ }
306
+ else {
307
+ //rewind n bytes in the input stream.
308
+ rb_scan_args(argc, argv, "11", &byte_arg, &whence_arg); // 10 is 1 mandatory and 0 optional arguments
309
+ whence = argc == 1 ? SEEK_SET : NUM2INT(whence_arg); // default to seeking to absolute position.
310
+ if(fseek(tf->fp, NUM2INT(byte_arg), whence) == -1)
311
+ rb_raise(rb_eRuntimeError, "tmp_seek %s", strerror(errno));
312
+ }
313
+ }
314
+ return obj;
315
+ }
316
+
317
+ static VALUE tmp_seek_end(VALUE obj)
318
+ {
319
+ Tmpfile *tf;
320
+
321
+ //Data_Get_Struct(obj, Tmpfile, tf);
322
+ TypedData_Get_Struct(obj, Tmpfile, &tmp_type, tf); // Newer form of Data_Get_Struct
323
+ if(tf->fp == NULL)
324
+ rb_raise(rb_eRuntimeError, "tmp_seek_end: File not open!"); //need to change error type.
325
+
326
+ if(fseek(tf->fp,0,SEEK_END) == -1) //go to the end of the file.
327
+ rb_raise(rb_eRuntimeError, "tmp_seek_end.fseek %s", strerror(errno));
328
+ return obj;
329
+ }
330
+
331
+ //rewind to start of file, or if arg, back arg bytes.
332
+ static VALUE tmp_rewind(int argc, VALUE *argv, VALUE obj)
333
+ {
334
+ Tmpfile *tf;
335
+ VALUE nbytes;
336
+
337
+ // Data_Get_Struct(obj, Tmpfile, tf);
338
+ TypedData_Get_Struct(obj, Tmpfile, &tmp_type, tf); // Newer form of Data_Get_Struct
339
+ if(tf->fp == NULL)
340
+ rb_raise(rb_eRuntimeError, "tmp_rewind: File not open!"); //need to change error type.
341
+
342
+ if(tf->fp != NULL){
343
+ if(argc == 0) {
344
+ if(fseek(tf->fp, 0, SEEK_SET) == -1) //go back to the start of the file.
345
+ rb_raise(rb_eRuntimeError, "tmp_rewind.rewind %s", strerror(errno));
346
+ }
347
+ else {
348
+ //rewind n bytes in the input stream.
349
+ rb_scan_args(argc, argv, "10", &nbytes); // 10 is 1 mandatory and 0 optional arguments
350
+ if(fseek(tf->fp, -NUM2INT(nbytes), SEEK_CUR) == -1)
351
+ rb_raise(rb_eRuntimeError, "tmp_rewind.rewind %s", strerror(errno));
352
+ }
353
+ }
354
+ return obj;
355
+ }
356
+
357
+ static VALUE tmp_ftell(VALUE obj)
358
+ {
359
+ Tmpfile *tf;
360
+ long pos;
361
+
362
+ //Data_Get_Struct(obj, Tmpfile, tf);
363
+ TypedData_Get_Struct(obj, Tmpfile, &tmp_type, tf); // Newer form of Data_Get_Struct
364
+ if(tf->fp == NULL)
365
+ rb_raise(rb_eRuntimeError, "tmp_ftell: File not open!"); //need to change error type.
366
+
367
+ if((pos = ftell(tf->fp)) == -1) //go to the end of the file.
368
+ rb_raise(rb_eRuntimeError, "tmp_ftell %s", strerror(errno));
369
+ return INT2FIX(pos);
370
+ }
371
+
372
+ static VALUE tmp_flush(VALUE obj)
373
+ {
374
+ Tmpfile *tf;
375
+
376
+ // Data_Get_Struct(obj, Tmpfile, tf);
377
+ TypedData_Get_Struct(obj, Tmpfile, &tmp_type, tf); // Newer form of Data_Get_Struct
378
+ if(tf->fp == NULL)
379
+ rb_raise(rb_eRuntimeError, "tmp_flush: File not open!"); //need to change error type.
380
+
381
+ if(fflush(tf->fp) == -1)
382
+ rb_raise(rb_eRuntimeError, "tmp_flush.flush: %s", strerror(errno));
383
+ return obj;
384
+ }
385
+
386
+ static VALUE tmp_eof(VALUE obj)
387
+ {
388
+ Tmpfile *tf;
389
+
390
+ // Data_Get_Struct(obj, Tmpfile, tf);
391
+ TypedData_Get_Struct(obj, Tmpfile, &tmp_type, tf); // Newer form of Data_Get_Struct
392
+ if(tf->fp == NULL)
393
+ rb_raise(rb_eRuntimeError, "tmp_flush: File not open!"); //need to change error type.
394
+
395
+ return feof(tf->fp) == 0 ? Qfalse : Qtrue ;
396
+ }
397
+
398
+ static VALUE tmp_set_no_unlink(VALUE obj)
399
+ {
400
+ Tmpfile *tf;
401
+
402
+ // Data_Get_Struct(obj, Tmpfile, tf);
403
+ TypedData_Get_Struct(obj, Tmpfile, &tmp_type, tf); // Newer form of Data_Get_Struct
404
+ tf->no_unlink = 1;
405
+ return obj;
406
+ }
407
+
408
+ static VALUE tmp_set_unlink(VALUE obj)
409
+ {
410
+ Tmpfile *tf;
411
+
412
+ // Data_Get_Struct(obj, Tmpfile, tf);
413
+ TypedData_Get_Struct(obj, Tmpfile, &tmp_type, tf); // Newer form of Data_Get_Struct
414
+ tf->no_unlink = 0;
415
+ return obj;
416
+ }
417
+
418
+ static VALUE tmp_do_unlink(VALUE obj)
419
+ {
420
+ Tmpfile *tf;
421
+
422
+ // Data_Get_Struct(obj, Tmpfile, tf);
423
+ TypedData_Get_Struct(obj, Tmpfile, &tmp_type, tf); // Newer form of Data_Get_Struct
424
+ return tf->no_unlink == 0 ? Qtrue:Qfalse ;
425
+ }
426
+
427
+ static VALUE tmp_unlink(VALUE obj)
428
+ {
429
+ Tmpfile *tf;
430
+
431
+ // Data_Get_Struct(obj, Tmpfile, tf);
432
+ TypedData_Get_Struct(obj, Tmpfile, &tmp_type, tf); // Newer form of Data_Get_Struct
433
+ if(tf->filename[0] != '\0')
434
+ {
435
+ unlink(tf->filename); //remove the file from disk
436
+
437
+ //Forget the name, as the file is now unnamed.
438
+ //i.e. it can only be accessed via the file pointer, not by name.
439
+ // and it will vanish from the disk once the fp is closed.
440
+ tf->filename[0] = '\0';
441
+ tf->flags[0] = '\0';
442
+ }
443
+ return obj;
444
+ }
445
+
446
+ static VALUE tmp_to_s(VALUE obj)
447
+ {
448
+ char *buffer;
449
+ Tmpfile *tf;
450
+ VALUE rb_string;
451
+
452
+ // Data_Get_Struct(obj, Tmpfile, tf);
453
+ TypedData_Get_Struct(obj, Tmpfile, &tmp_type, tf); // Newer form of Data_Get_Struct
454
+
455
+ if((buffer = malloc(strlen(tf->filename) + strlen(tf->flags) + 30)) == NULL)
456
+ rb_raise(rb_eRuntimeError, "Malloc failed");
457
+
458
+ sprintf(buffer, "filename = \'%s\' flags = \'%s\'", tf->filename, tf->flags);
459
+ rb_string = rb_str_new2(buffer); // return the string we just read.
460
+ free(buffer); // rb_string is a copy, so we don't need the original.
461
+ return rb_string;
462
+ }
463
+
464
+ static VALUE tmp_exec(int argc, VALUE *argv, VALUE obj)
465
+ {
466
+ char **exec_argv;
467
+ int i;
468
+ int status = 0;
469
+ int pid;
470
+ int error;
471
+ VALUE tmp;
472
+
473
+ if(argc == 0)
474
+ rb_raise(rb_eRuntimeError, "tmp_exec: %s", "Need arguments!");
475
+
476
+ exec_argv = calloc(argc + 1, sizeof(char *));
477
+ for(i = 0; i < argc; i++)
478
+ { if(TYPE(argv[i]) == T_STRING)
479
+ exec_argv[i] = StringValuePtr(argv[i]);
480
+ else
481
+ { tmp = rb_funcall( argv[i], rb_intern("to_s"), 0 ) ;
482
+ exec_argv[i] = StringValuePtr(tmp);
483
+ }
484
+ }
485
+ exec_argv[i] = NULL;
486
+
487
+ if((pid = fork()) == 0) //child
488
+ execv(exec_argv[0], exec_argv);
489
+ else if(pid == -1) //failed
490
+ rb_raise(rb_eRuntimeError, "tmp_exec.exec %s", strerror(errno)); //need to change error type.
491
+ else //Parent
492
+ { int count = 0;
493
+ while((error = wait(&status)) == -1 && errno == EINTR && count < 4) count++ ;
494
+ if(error == -1)
495
+ rb_raise(rb_eRuntimeError, "tmp_exec: %s", strerror(errno)); //need to change error type.
496
+ }
497
+ if(exec_argv != NULL)
498
+ free(exec_argv);
499
+
500
+ return INT2FIX(status);
501
+ }
502
+
503
+ RUBY_FUNC_EXPORTED void Init_tmpfile(void)
504
+ {
505
+ // Create a Ruby class in this module.
506
+ // rb_cObject is defined in ruby.h
507
+
508
+ myModule = rb_define_module("TmpFileMod");
509
+ myClass = rb_define_class_under(myModule, "TmpFile", rb_cObject);
510
+ rb_define_alloc_func(myClass, tmp_alloc);
511
+
512
+ rb_define_method(myClass, "initialize", tmp_initialize, -1); //2 args
513
+
514
+ //Class methods
515
+ rb_define_module_function(myClass, "open", tmp_open, -1); //2 args, but easier to code as optional.
516
+ rb_define_module_function(myClass, "exec", tmp_exec, -1);
517
+
518
+ //methods
519
+ rb_define_method(myClass, "each_line", tmp_each_line, 0);
520
+ rb_define_method(myClass, "gets", tmp_gets, 0);
521
+ rb_define_method(myClass, "getc", tmp_getc, 0);
522
+ rb_define_method(myClass, "puts", tmp_puts, -1);
523
+ rb_define_method(myClass, "putc", tmp_putc, 1);
524
+ rb_define_method(myClass, "print", tmp_print, -1);
525
+ rb_define_method(myClass, "close", tmp_close, 0);
526
+ rb_define_method(myClass, "unlink?", tmp_do_unlink, 0);
527
+ rb_define_method(myClass, "no_unlink", tmp_set_no_unlink, 0);
528
+ rb_define_method(myClass, "auto_unlink", tmp_set_unlink, 0);
529
+ rb_define_method(myClass, "unlink", tmp_unlink, 0);
530
+ rb_define_method(myClass, "seek", tmp_seek, -1); //optional arguments for byte and whence
531
+ rb_define_method(myClass, "seek_end", tmp_seek_end, 0);
532
+ rb_define_method(myClass, "rewind", tmp_rewind, -1); //optional argument for how far to rewind.
533
+ rb_define_method(myClass, "ftell", tmp_ftell, 0);
534
+ rb_define_method(myClass, "flush", tmp_flush, 0);
535
+ rb_define_method(myClass, "eof?", tmp_eof, 0);
536
+ rb_define_method(myClass, "to_s", tmp_to_s, 0);
537
+ }
@@ -0,0 +1,15 @@
1
+ #ifndef TMPFILE
2
+ #define TMPFILE
3
+
4
+ #include <ruby.h>
5
+ #include <stdio.h>
6
+
7
+ typedef struct tmpfile
8
+ {
9
+ FILE *fp;
10
+ char filename[256];
11
+ char flags[16];
12
+ int no_unlink;
13
+ } Tmpfile;
14
+
15
+ #endif //TMPFILE
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tmpfile
4
+ PROJECT="tmpfile"
5
+ VERSION = "1.0.2"
6
+ end
data/lib/tmpfile.rb ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "tmpfile/version"
4
+ require_relative "tmpfile/tmpfile"
5
+
6
+ module Tmpfile
7
+ class Error < StandardError; end
8
+ # Your code goes here...
9
+ end
data/sig/tmpfile.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Tmpfile
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
data/version ADDED
@@ -0,0 +1,2 @@
1
+ PROJECT="tmpfile"
2
+ VERSION=1.0.2
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tmpfile
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2
5
+ platform: ruby
6
+ authors:
7
+ - robertburrowes
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: Early ruby c experiment. Building a better tempfile gem
13
+ email:
14
+ - r.burrrowes@burrowes.org
15
+ executables: []
16
+ extensions:
17
+ - ext/tmpfile/extconf.rb
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rubocop.yml"
21
+ - CHANGELOG.md
22
+ - LICENSE.txt
23
+ - README.md
24
+ - Rakefile
25
+ - ext/tmpfile/extconf.rb
26
+ - ext/tmpfile/tmpfile.c
27
+ - ext/tmpfile/tmpfile.h
28
+ - lib/tmpfile.rb
29
+ - lib/tmpfile/version.rb
30
+ - sig/tmpfile.rbs
31
+ - version
32
+ homepage: https://github.com/rbur004/tmpfile
33
+ licenses:
34
+ - MIT
35
+ metadata:
36
+ allowed_push_host: https://rubygems.org/
37
+ homepage_uri: https://github.com/rbur004/tmpfile
38
+ source_code_uri: https://github.com/rbur004/tmpfile
39
+ changelog_uri: https://github.com/rbur004/tmpfile/CHANGELOG.md
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 3.1.0
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubygems_version: 4.0.20
55
+ specification_version: 4
56
+ summary: Early ruby c experiment. Building a better tempfile gem
57
+ test_files: []