Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # NOTE: See http://pastebin.com/q7RpEp5y for an updated version
- BEGIN { keyword = "pkg" }
- function trim_match() {
- match_text = substr($0, RSTART, RLENGTH)
- $0 = substr($0, RSTART + RLENGTH)
- return match_text
- }
- function trim_keyword() {
- keyword = substr(keyword, 0, length(keyword) - length(key[i--]) - 1)
- }
- function print_value(var, value) {
- print ++L, var "=[" value "]"
- }
- {
- while ($0) {
- #print ++L, $0
- if (building_string) {
- while (match($0, /^[^"]*\\"/))
- value = value trim_match()
- if (!match($0, /^[^"]*"/)) {
- value = value $0
- next
- }
- building_string = 0
- value = value substr($0, RSTART, RLENGTH - 1)
- trim_match()
- sub(/^[[:space:]]*,[[:space:]]*/, "")
- print_value(keyword, value)
- trim_keyword()
- }
- else if (match($0, /^[[:space:]]*{[[:space:]]*/))
- trim_match()
- else if (match($0, \
- /^[[:space:]]*"[^"]+"[[:space:]]*:[[:space:]]*/ \
- )) {
- key[++i] = trim_match()
- sub(/^[[:space:]]*"/, "", key[i])
- sub(/"[[:space:]]*:[[:space:]]*$/, "", key[i])
- keyword = keyword ( keyword ? "." : "" ) key[i]
- }
- else if (keyword && match($0, /^[[:space:]]*\[/)) {
- array_value = ""
- building_array = 1
- trim_match()
- }
- else if (keyword && match($0, /^[[:space:]]*"/)) {
- value = ""
- trim_match()
- while (match($0, /^[^"]*\\"/))
- value = value trim_match()
- if (!match($0, /^[^"]*"/)) {
- building_string = 1
- value = $0
- next
- }
- value = value substr($0, RSTART, RLENGTH - 1)
- trim_match()
- sub(/^[[:space:]]*,[[:space:]]*/, "")
- if (building_array) array_value = array_value \
- ( array_value ? "," : "" ) value
- else {
- print_value(keyword, value)
- trim_keyword()
- }
- }
- else if (keyword && match($0, \
- /^[[:space:]]*[^[:space:],}\]]+[[:space:]]*/ \
- )) {
- value = trim_match()
- sub(/^[[:space:]]*/, "", value)
- sub(/[[:space:]]*$/, "", value)
- sub(/^[[:space:]]*,[[:space:]]*/, "")
- print_value(keyword, value)
- trim_keyword()
- }
- else if (match($0, /^[[:space:]]*\][[:space:]]*/)) {
- print_value(keyword, array_value)
- building_array = 0
- trim_keyword()
- trim_match()
- sub(/^[[:space:]]*,[[:space:]]*/, "")
- }
- else if (match($0, /^[[:space:]]*}[[:space:]]*/)) {
- building_array = 0
- trim_keyword()
- trim_match()
- sub(/^[[:space:]]*,[[:space:]]*/, "")
- }
- }
- }
Add Comment
Please, Sign In to add comment