vdf 0.1.0
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 +7 -0
- data/.gitignore +8 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +18 -0
- data/LICENSE.txt +21 -0
- data/README.md +56 -0
- data/lib/vdf.rb +8 -0
- data/lib/vdf/generate.rb +32 -0
- data/lib/vdf/parse.rb +96 -0
- data/lib/vdf/version.rb +3 -0
- data/test.rb +5 -0
- data/vdf.gemspec +26 -0
- metadata +70 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 32e8e30f4e6373a5dc4ccebbcad8b772ecb61ee33c5569fea8fe847be14554a9
|
4
|
+
data.tar.gz: 178a3988d3a942959aa020a7ae3b03a954173b07ccd366927e661af4608744c3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8074d9085c25209f16f9d6e483282a2d7c775170b3f099666bc34ef1dfe69025dbbbafc2743619853b1293252ec059898a24deedcb8fa5dc73410742317833af
|
7
|
+
data.tar.gz: d4096efae25a923160aa2ddfb99bee79cebd363bfaee906d44be367a4ceec53bed72ff8157c46038ba926ef0f2196297ffe77a48933b5c750882004f6f634574
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Phil
|
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,56 @@
|
|
1
|
+
# Vdf
|
2
|
+
|
3
|
+
VDF is a gem to convert Valve's KeyValue format to ruby hashes and back, based on the excellent https://github.com/node-steam/vdf
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'vdf'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install vdf
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Parsing a VDF is simple:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require "vdf"
|
27
|
+
|
28
|
+
# Load VDF file into a string
|
29
|
+
vdf_contents = File.read("filename.vdf")
|
30
|
+
|
31
|
+
# Parse it
|
32
|
+
parsed = VDF.parse(vdf_contents)
|
33
|
+
|
34
|
+
# Pretty-print the result
|
35
|
+
p parsed
|
36
|
+
|
37
|
+
```
|
38
|
+
|
39
|
+
## Performance comparison
|
40
|
+
|
41
|
+
Small VDF File
|
42
|
+
```
|
43
|
+
user system total real
|
44
|
+
vdf4r 0.391000 0.000000 0.391000 ( 0.383975)
|
45
|
+
vdf 0.016000 0.000000 0.016000 ( 0.012664)
|
46
|
+
```
|
47
|
+
|
48
|
+
Large VDF File (CS:GO's items_game.txt)
|
49
|
+
|
50
|
+
## Contributing
|
51
|
+
|
52
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/vdf.
|
53
|
+
|
54
|
+
## License
|
55
|
+
|
56
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/lib/vdf.rb
ADDED
data/lib/vdf/generate.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
module VDF
|
2
|
+
class Generator
|
3
|
+
class << self
|
4
|
+
|
5
|
+
def generate(object)
|
6
|
+
generate_impl(object, 0)
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def generate_impl(object, level)
|
12
|
+
result = ""
|
13
|
+
indent = -"\t"*level
|
14
|
+
|
15
|
+
object.each do |key, value|
|
16
|
+
if value.respond_to? :each
|
17
|
+
result << [indent, -'"', key, -"\"\n", indent, -"{\n", generate_impl(value, level + 1), indent, -"}\n"].join
|
18
|
+
else
|
19
|
+
result << [indent, -'"', key, -'"', indent, indent, -'"', value.to_s, -"\"\n"].join
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
result
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def generate(object)
|
29
|
+
Generator.generate(object)
|
30
|
+
end
|
31
|
+
module_function :generate
|
32
|
+
end
|
data/lib/vdf/parse.rb
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
module VDF
|
2
|
+
class Parser
|
3
|
+
class << self
|
4
|
+
REGEX = Regexp.new(
|
5
|
+
'^("((?:\\\\.|[^\\\\"])+)"|([a-z0-9\\-\\_]+))' +
|
6
|
+
'([ \t]*(' +
|
7
|
+
'"((?:\\\\.|[^\\\\"])*)(")?' +
|
8
|
+
'|([a-z0-9\\-\\_]+)' +
|
9
|
+
'))?',
|
10
|
+
Regexp::MULTILINE
|
11
|
+
)
|
12
|
+
|
13
|
+
def parse(text)
|
14
|
+
lines = text.lines
|
15
|
+
object = {}
|
16
|
+
stack = [object]
|
17
|
+
expect = false
|
18
|
+
skip_lines = 0
|
19
|
+
|
20
|
+
lines.each_with_index do |line, i|
|
21
|
+
if skip_lines > 0
|
22
|
+
skip_lines -= 1
|
23
|
+
next
|
24
|
+
end
|
25
|
+
|
26
|
+
line.strip!
|
27
|
+
next if line == -'' || line[0] == -'/'
|
28
|
+
|
29
|
+
if line.start_with?(-'{')
|
30
|
+
expect = false
|
31
|
+
next
|
32
|
+
elsif expect
|
33
|
+
raise ParserError, "Invalid syntax on line #{i+1} (1)"
|
34
|
+
end
|
35
|
+
|
36
|
+
if line.start_with?(-'}')
|
37
|
+
stack.pop
|
38
|
+
next
|
39
|
+
end
|
40
|
+
|
41
|
+
loop do
|
42
|
+
m = REGEX.match(line)
|
43
|
+
if m.nil?
|
44
|
+
raise ParserError, "Invalid syntax on line #{i+1} (2)"
|
45
|
+
end
|
46
|
+
|
47
|
+
key = m[2] || m[3]
|
48
|
+
val = m[6] || m[8]
|
49
|
+
|
50
|
+
if val.nil?
|
51
|
+
if stack[-1][key].nil?
|
52
|
+
stack[-1][key] = {}
|
53
|
+
end
|
54
|
+
stack.push(stack[-1][key])
|
55
|
+
expect = true
|
56
|
+
else
|
57
|
+
if m[7].nil? && m[8].nil?
|
58
|
+
line << -"\n" << lines[i+skip_lines+1].chomp
|
59
|
+
skip_lines += 1
|
60
|
+
next
|
61
|
+
end
|
62
|
+
|
63
|
+
stack[stack.length - 1][key] = begin
|
64
|
+
begin
|
65
|
+
Integer(val)
|
66
|
+
rescue ArgumentError
|
67
|
+
Float(val)
|
68
|
+
end
|
69
|
+
rescue ArgumentError
|
70
|
+
case val.downcase
|
71
|
+
when -"true"
|
72
|
+
true
|
73
|
+
when -"false"
|
74
|
+
false
|
75
|
+
when -"null"
|
76
|
+
nil
|
77
|
+
else
|
78
|
+
val
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
break
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
return object
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def parse(text)
|
93
|
+
Parser.parse(text)
|
94
|
+
end
|
95
|
+
module_function :parse
|
96
|
+
end
|
data/lib/vdf/version.rb
ADDED
data/test.rb
ADDED
data/vdf.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
lib = File.expand_path("lib", __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "vdf/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "vdf"
|
7
|
+
spec.version = VDF::VERSION
|
8
|
+
spec.authors = ["Phil"]
|
9
|
+
spec.email = ["phil5686@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = %q{Parses Valve's KeyValue format to Ruby Hashes and back}
|
12
|
+
spec.homepage = "https://github.com/sapphyrus/vdf"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
16
|
+
spec.metadata["source_code_uri"] = "https://github.com/sapphyrus/vdf"
|
17
|
+
|
18
|
+
# Specify which files should be added to the gem when it is released.
|
19
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
20
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
21
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
|
+
end
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: vdf
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Phil
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-08-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
description:
|
28
|
+
email:
|
29
|
+
- phil5686@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".gitignore"
|
35
|
+
- Gemfile
|
36
|
+
- Gemfile.lock
|
37
|
+
- LICENSE.txt
|
38
|
+
- README.md
|
39
|
+
- lib/vdf.rb
|
40
|
+
- lib/vdf/generate.rb
|
41
|
+
- lib/vdf/parse.rb
|
42
|
+
- lib/vdf/version.rb
|
43
|
+
- test.rb
|
44
|
+
- vdf.gemspec
|
45
|
+
homepage: https://github.com/sapphyrus/vdf
|
46
|
+
licenses:
|
47
|
+
- MIT
|
48
|
+
metadata:
|
49
|
+
homepage_uri: https://github.com/sapphyrus/vdf
|
50
|
+
source_code_uri: https://github.com/sapphyrus/vdf
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options: []
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
requirements: []
|
66
|
+
rubygems_version: 3.0.4
|
67
|
+
signing_key:
|
68
|
+
specification_version: 4
|
69
|
+
summary: Parses Valve's KeyValue format to Ruby Hashes and back
|
70
|
+
test_files: []
|