encodage des valeurs vsep

This commit is contained in:
Jephté Clain 2015-02-09 12:16:54 +04:00
parent c926eb229c
commit 7f426a74b0
3 changed files with 42 additions and 8 deletions

View File

@ -1 +1 @@
007019000
007020000

View File

@ -1834,6 +1834,17 @@ function num(s) {
if (s ~ /^[0-9]+$/) return int(s)
else return s
}
function ord(s, i) {
s = substr(s, 1, 1)
i = index(" !\"#$%&'\''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~", s)
if (i != 0) i += 32 - 1
return i
}
function hex(i, s) {
s = sprintf("%x", i)
if (length(s) < 2) s = "0" s
return s
}
function quote_html(s) {
gsub(/&/, "\\&amp;", s)
gsub(/"/, "\\&quot;", s)
@ -1860,6 +1871,10 @@ function quoted_values( i, line) {
}
return line
}
function quote_regexp(s) {
gsub(/[[\\.^$*+?()|{]/, "\\\\&", s)
return s
}
function quote_subrepl(s) {
gsub(/\\/, "\\\\", s)
gsub(/&/, "\\\\&", s)

View File

@ -559,6 +559,25 @@ function quote(text) {
'
}
__AWK_VSEP_FUNCTIONS='
function quote_vsep(value, vsep, hvsep) {
gsub(/\\/, "\\5c", value)
if (vsep != "\\") {
hvsep = "\\" hex(ord(vsep))
gsub(quote_regexp(vsep), hvsep, value)
}
return value
}
function unquote_vsep(value, vsep, hvsep) {
if (vsep != "\\") {
hvsep = "\\" hex(ord(vsep))
gsub(quote_regexp(hvsep), vsep, value)
}
gsub(quote_regexp("\\5c"), "\\", value)
return value
}
'
function tl_format() {
local args show_headers asep vsep qsep escape shell cmd begincmd endcmd localvars
show_headers=
@ -596,7 +615,7 @@ function tl_format() {
awkrun -f show_headers:int="$show_headers" asep="$asep" vsep="$vsep" qsep="$qsep" escape="$escape" \
shell:int="$shell" cmd="$cmd" begincmd="$begincmd" endcmd="$endcmd" localvars:int="$localvars" \
attrs[@] mapattrs[@] '
attrs[@] mapattrs[@] "$__AWK_VSEP_FUNCTIONS"'
function reset_values() {
for (i = 1; i <= attrs_count; i++) {
attr = norm_attr(attrs[i])
@ -705,9 +724,9 @@ $0 != "" && match_attr($1) {
}
} else {
if (values[attr] == "") {
values[attr] = value
values[attr] = quote_vsep(value, vsep)
} else {
values[attr] = values[attr] vsep value
values[attr] = values[attr] vsep quote_vsep(value, vsep)
}
}
}
@ -770,7 +789,7 @@ function tl_parsecsv() {
awkrun -f \
basedn="$basedn" rdnattr="$rdnattr" \
skip_lines:int="$skip_lines" parse_headers:int="$parse_headers" headers[@] \
vsep="$vsep" '
vsep="$vsep" "$__AWK_VSEP_FUNCTIONS"'
NR <= skip_lines { next }
parse_headers {
array_parsecsv(headers, $0)
@ -796,7 +815,7 @@ parse_headers {
split(attrvalue, values, vsep)
values_count = array_len(values)
for (j = 1; j <= values_count; j++) {
print attr ": " values[j]
print attr ": " unquote_vsep(values[j], vsep)
}
}
print ""
@ -830,7 +849,7 @@ function tl_parsecsvmod() {
awkrun -f \
basedn="$basedn" rdnattr="$rdnattr" \
skip_lines:int="$skip_lines" parse_headers:int="$parse_headers" headers[@] \
vsep="$vsep" '
vsep="$vsep" "$__AWK_VSEP_FUNCTIONS"'
NR <= skip_lines { next }
parse_headers {
array_parsecsv(headers, $0)
@ -878,7 +897,7 @@ parse_headers {
split(attrvalue, values, vsep)
values_count = array_len(values)
for (j = 1; j <= values_count; j++) {
print attr ": " values[j]
print attr ": " unquote_vsep(values[j], vsep)
}
print "-"
}