random_file 0.0.1

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.
Files changed (2) hide show
  1. data/lib/random_file.rb +107 -0
  2. metadata +46 -0
@@ -0,0 +1,107 @@
1
+ # Simple Random File Access
2
+ # Example Code
3
+ #
4
+ # def update_attack(ip)
5
+ # rtf = RandomFile.new
6
+ #
7
+ # found = false
8
+ # counter = 1
9
+ # while !rtf.eof do
10
+ # puts rtf.pos
11
+ # res = rtf.read
12
+ # m = res.split(9.chr)
13
+ # if m[0] == ip
14
+ # counter = m[1].to_i + 1
15
+ # rtf.seek(rtf.pos-1)
16
+ # rtf.write(ip+9.chr+counter.to_s)
17
+ # puts ip+9.chr+counter.to_s
18
+ # found = true
19
+ # break
20
+ # end
21
+ # end
22
+ # if !found
23
+ # rtf.append
24
+ # rtf.write(ip+9.chr+counter.to_s)
25
+ # end
26
+ # rtf.close
27
+ # end
28
+ #
29
+ # def clear_attack(ip)
30
+ # rtf = RandomFile.new
31
+ #
32
+ # found = false
33
+ # counter = 0
34
+ # while !rtf.eof do
35
+ # res = rtf.read
36
+ # m = res.split(9.chr)
37
+ # if m[0] == ip
38
+ # rtf.seek(rtf.pos-1)
39
+ # rtf.write(ip+9.chr+counter.to_s)
40
+ # puts ip+9.chr+counter.to_s
41
+ # found = true
42
+ # break
43
+ # end
44
+ # end
45
+ # if !found
46
+ # rtf.append
47
+ # rtf.write(ip+9.chr+counter.to_s)
48
+ # end
49
+ # rtf.close
50
+ # end
51
+ #
52
+ # Arguments:
53
+ # filename = name of file
54
+ # recordsize (default = 80)
55
+
56
+
57
+ class RandomFile
58
+ def initialize(filename = nil,recordsize = 80)
59
+ if filename == nil
60
+ filename = File.join(Rails.root, 'log', 'NoNameR-'+Time.now.localtime.strftime("%Y-%m-%d")+".log")
61
+ end
62
+ @recordsize = recordsize
63
+ if FileTest::exist?(filename)
64
+ @file = File.new(filename, 'r+')
65
+ else
66
+ @file = File.new(filename, 'w+')
67
+ end
68
+ end
69
+
70
+ def eof
71
+ return @file.eof?
72
+ end
73
+
74
+ def pos
75
+ return @file.pos / @recordsize
76
+ end
77
+
78
+ def write(val)
79
+ while val.length < @recordsize
80
+ val = val + 0.chr
81
+ end
82
+ @file.write val
83
+ end
84
+
85
+ def seek(posn)
86
+ @file.seek(@recordsize * posn)
87
+ end
88
+
89
+ def append
90
+ @file.seek(0,IO::SEEK_END)
91
+ end
92
+
93
+ def close
94
+ @file.close
95
+ end
96
+
97
+ def read
98
+ res = @file.read(@recordsize)
99
+ posn = res.index(0.chr)
100
+ if posn != nil
101
+ res = res[0,posn]
102
+ end
103
+ return res
104
+ end
105
+
106
+
107
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: random_file
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Stephen Foley
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-08-24 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Simple Reading and Writing of Random Access Files
15
+ email: stephen@talaw.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/random_file.rb
21
+ homepage: http://rubygems.org/gems/random_file
22
+ licenses:
23
+ - MIT
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 1.8.25
43
+ signing_key:
44
+ specification_version: 3
45
+ summary: Read/Write Random Files
46
+ test_files: []