rb_cfg_parser 0.1.4 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e14ff372afbe8faee2d1a06e9032624372102fcf4c66c8577c4329766d9575c3
4
- data.tar.gz: 6c0c647bc7ee0de4ff8a66bd4f2b1e1e997006b9b626d84c7275c37c0c65a0c5
3
+ metadata.gz: c257a0100090068ccddd91e39ad4d7bdaec8967c7c189e8e98389f156c93142b
4
+ data.tar.gz: f87674b4731d40f87dab6a66e7480be37a3a39347dc8097ce942ed30bd9c256c
5
5
  SHA512:
6
- metadata.gz: 9d8e00529d4c631c7eb7582e4b70f08def9a72927ac9ae8b455be37d0b3a01a66e9515d543bc71fa6c52b1610b91ea8fbd7dfc61f48bae4dcefd7656832a2d5e
7
- data.tar.gz: '08109656cc3dbc6a473a7354b33401c18c4ecf0d9077008745af3b21aa1dbcb0db4c62988f2cbe501c516f31d908a7d0310e0771b63fb55510abce414275c9af'
6
+ metadata.gz: 68af5ba52f48f5b2c28cf48d5f856d31ada97b8cfd46a0ac5929a8f872f19818700e9003af8a33b8a751db42a203629417e32156dc8167392c0308baacca7c03
7
+ data.tar.gz: 686bcdd920f1a917cfd5c424a3d9387af583c057f0a063506346699c7a3e73c43bf8cc1e2bd6bab63f21a35f4f189c3517e5e0bb645e95b4c1ebdc13cba1f220
data/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) since 2025: rb-cfg-parser Ruby Gem
3
+ Copyright (c) since 2025: rb-cfg-parser Ruby Library
4
4
  description: ruby gem to parse and load .cfg files
5
5
  author: J. Arrizza email: cppgent0@gmail.com
6
6
 
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
- * website: <https://arrizza.com/rb-cfg-parser.html>
2
- * installation: see <https://arrizza.com/setup-common.html>
1
+ * website: [Website](https://arrizza.com/rb-cfg-parser.html)
2
+ * installation: [Common Setup](https://arrizza.com/setup-common.html)
3
3
 
4
4
  ## Summary
5
5
 
@@ -92,7 +92,7 @@ Line comments are allowed. Lines starting with ';' or '#' are considered comment
92
92
  Trailing comments are not recommended.
93
93
  They may work okay but may also cause problems:
94
94
 
95
- ```
95
+ ```text
96
96
  [sect1] # comment 1 <== will be ignored
97
97
  vrbl1 = val1
98
98
  val2 ; comment2 <== will be part of the values in vrbl1
@@ -292,9 +292,11 @@ class RbCfgParser
292
292
  # print a debug line
293
293
  # @param msg line to print
294
294
  # @return None
295
+ # :nocov:
295
296
  def print_dbg(msg)
296
297
  @log_cb.call(format('DBG line: %3d] %s', @lineno, msg)) if @verbose > 1
297
298
  end
299
+ # :nocov:
298
300
 
299
301
  ###
300
302
  # print a line
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  # frozen_string_literal: true
2
2
  ## current version
3
- VERSION = '0.1.4'
3
+ VERSION = '0.3.0'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rb_cfg_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - J. Arrizza
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-07-13 00:00:00.000000000 Z
11
+ date: 2026-03-25 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: ruby gem to parse and load .cfg files
14
14
  email: cppgent0@gmail.com
@@ -20,7 +20,6 @@ files:
20
20
  - README.md
21
21
  - lib/parser/cfg_lexer.rex.rb
22
22
  - lib/parser/cfg_parser.rb
23
- - lib/parser/pf_config_parser.rb
24
23
  - lib/rb_cfg_parser.rb
25
24
  - lib/version.rb
26
25
  homepage: https://rubygems.org/gems/rb_cfg_parser
@@ -1,253 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- ###
4
- # Configuration parser
5
- class RbCfgParser
6
- ###
7
- # constructor
8
- def initialize
9
- _reset_all
10
- end
11
-
12
- ###
13
- # parse the given file
14
- #
15
- # @param path the path to the file
16
- # @return (hash) the content
17
- def parse_file(path)
18
- file_content = File.read(path)
19
- parse_str(file_content.lines)
20
- end
21
-
22
- ###
23
- # parse the given array of lines
24
- #
25
- # @param lines (array) the lines to parse
26
- # @return (hash) the content
27
- def parse_str(lines)
28
- # uncomment to debug
29
- # puts("\nlines: >>>")
30
- # lineno = 0
31
- # lines.each_line do |line|
32
- # lineno += 1
33
- # line = line.delete!("\n")
34
- # puts("#{lineno}] '#{line}'")
35
- # end
36
- # puts("<<< lines: #{lineno}")
37
-
38
- _reset_all
39
-
40
- lineno = 0
41
- lines.each do |line|
42
- lineno += 1
43
- line = line.strip
44
-
45
- # empty line
46
- next if line == ''
47
-
48
- # comment
49
- next if %w[; #].include?(line[0])
50
-
51
- if line[0] == '['
52
- _parse_section(line)
53
- else
54
- _parse_single_line(line)
55
- end
56
- end
57
-
58
- # save previous section
59
- @content[@curr_section_name] = @curr_section if @curr_section_name == :unnamed && @curr_section != {}
60
-
61
- @content
62
- end
63
-
64
- ###
65
- # reset content info
66
- #
67
- # @return None
68
- def _reset_all
69
- @content = {}
70
- @curr_param_name = :unnamed
71
- @curr_param = nil
72
- @curr_section_name = :unnamed
73
- @curr_section = {}
74
- end
75
-
76
- ###
77
- # parse a line containing a section name
78
- #
79
- # @param line the line to parse
80
- # @return None
81
- def _parse_section(line)
82
- # save previous section if not already
83
- if !@content.key?(@curr_section_name) && (@curr_section_name != :unnamed || @curr_section != {})
84
- @content[@curr_section_name] = @curr_section
85
- end
86
-
87
- # TODO: check section_name; what chars are allowed?
88
- m = line.match(/\[\s*(.*)\s*\]/)
89
- @curr_section_name = m[1].strip
90
- @curr_section = {}
91
- @content[@curr_section_name] = @curr_section
92
- @curr_param = nil
93
- @curr_param_name = :unnamed
94
- end
95
-
96
- ###
97
- # parse the given line.
98
- # If it is an id or an assigment it is handled
99
- # otherwise it is ignored.
100
- #
101
- # @param line the line to parse
102
- # @return None
103
- def _parse_single_line(line)
104
- # TODO: alphanum+; check if line has only alphanum
105
- unless line.include?('=')
106
- id = _clean_it(line)
107
- _parse_raw_id(id)
108
- return
109
- end
110
-
111
- tokens = line.split('=')
112
- if tokens.length == 1
113
- _parse_assignment(tokens[0], '')
114
- elsif tokens.length == 2
115
- _parse_assignment(tokens[0], tokens[1])
116
- else
117
- # TODO: raise excp?
118
- puts("BUG unknown format, tokens: #{tokens}")
119
- end
120
- end
121
-
122
- ###
123
- # handle an assigment given the name and values found.
124
- #
125
- # @param name the name found
126
- # @param val the value found
127
- # @return None
128
- def _parse_assignment(name, val)
129
- @curr_section[@curr_param_name] = @curr_param if @curr_param_name != :unnamed
130
-
131
- @curr_param_name = name.strip
132
- @curr_param = nil
133
-
134
- val = val.strip
135
- if val == ''
136
- @curr_param = val
137
- @curr_section[@curr_param_name] = @curr_param
138
- return
139
- end
140
-
141
- # handle single quoted values
142
- m = val.scan(/'([^']*)'/)
143
- if m.length == 1
144
- _clean_and_parse1(val)
145
- return
146
- end
147
-
148
- if m.length > 1
149
- _clean_and_parse_scan_many(m)
150
- return
151
- end
152
-
153
- # handle double quoted values
154
- m = val.scan(/"([^"]*)"/)
155
- if m.length == 1
156
- _clean_and_parse1(val)
157
- return
158
- end
159
-
160
- if m.length > 1
161
- _clean_and_parse_scan_many(m)
162
- return
163
- end
164
-
165
- # handle unquoted values
166
- vals = val.split
167
- if vals.length == 1
168
- _clean_and_parse1(val)
169
- else
170
- _clean_and_parse_many(vals)
171
- end
172
- end
173
-
174
- ###
175
- # clean quotes etc. and then submit the single value as the current parameter
176
- #
177
- # @param val a single value
178
- # @return None
179
- def _clean_and_parse1(val)
180
- id = _clean_it(val)
181
- _parse_raw_id(id)
182
- end
183
-
184
- ###
185
- # clean quotes etc. and then submit the resulting values as the current parameter
186
- #
187
- # @param vals a result from a .scan()
188
- # @return None
189
- def _clean_and_parse_scan_many(vals)
190
- results = []
191
- vals.each_with_index do |v, i|
192
- results[i] = _clean_it(v[0])
193
- end
194
- @curr_param = results
195
- @curr_section[@curr_param_name] = @curr_param
196
- end
197
-
198
- ###
199
- # clean quotes etc. and then submit the resulting values as the current parameter
200
- #
201
- # @param vals a multiple value
202
- # @return None
203
- def _clean_and_parse_many(vals)
204
- results = []
205
- vals.each do |v|
206
- id = _clean_it(v)
207
- results.append(id)
208
- end
209
- @curr_param = results
210
- @curr_section[@curr_param_name] = @curr_param
211
- end
212
-
213
- ###
214
- # clean quotes or commas from the given value.
215
- #
216
- # @param val (string) the value to clean up
217
- # @return None
218
- def _clean_it(val)
219
- if val[0] == '\''
220
- m = val.match(/'(.*)'/)
221
- val = m[1].strip
222
- elsif val[0] == '"'
223
- m = val.match(/"(.*)"/)
224
- val = m[1].strip
225
- elsif val.end_with?(',')
226
- val = val.delete_suffix(',')
227
- end
228
-
229
- val = '' if val == '[]'
230
-
231
- val
232
- end
233
-
234
- ###
235
- # check and store the given value
236
- #
237
- # @param id the entity to store
238
- # @return None
239
- def _parse_raw_id(id)
240
- if @curr_param.nil?
241
- @curr_param = id
242
- elsif @curr_param.instance_of?(Array)
243
- @curr_param.append(id)
244
- elsif @curr_param.instance_of?(String)
245
- @curr_param = [@curr_param, id]
246
- else
247
- # :nocov: coverage: unknown how to cause
248
- puts("BUG parse_raw_id: unknown format '#{id}'")
249
- # :nocov:
250
- end
251
- @curr_section[@curr_param_name] = @curr_param
252
- end
253
- end