2018-04-26 23:19:17 +04:00
|
|
|
# -*- coding: utf-8 mode: awk -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
|
|
|
|
|
|
|
|
function mkindices(values, indices, i, j) {
|
2018-09-25 15:05:41 +04:00
|
|
|
array_new(indices);
|
|
|
|
j = 1;
|
2018-04-26 23:19:17 +04:00
|
|
|
for (i in values) {
|
2018-09-25 15:05:41 +04:00
|
|
|
indices[j++] = int(i);
|
2018-04-26 23:19:17 +04:00
|
|
|
}
|
2018-09-25 15:05:41 +04:00
|
|
|
return asort(indices);
|
2018-04-26 23:19:17 +04:00
|
|
|
}
|
|
|
|
function array_new(dest) {
|
2018-09-25 15:05:41 +04:00
|
|
|
dest[0] = 0; # forcer awk à considérer dest comme un tableau
|
|
|
|
delete dest;
|
2018-04-26 23:19:17 +04:00
|
|
|
}
|
|
|
|
function array_newsize(dest, size, i) {
|
2018-09-25 15:05:41 +04:00
|
|
|
dest[0] = 0; # forcer awk à considérer dest comme un tableau
|
|
|
|
delete dest;
|
|
|
|
size = int(size);
|
2018-04-26 23:19:17 +04:00
|
|
|
for (i = 1; i <= size; i++) {
|
2018-09-25 15:05:41 +04:00
|
|
|
dest[i] = "";
|
2018-04-26 23:19:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
function array_len(values, count, i) {
|
|
|
|
# length(array) a un bug sur awk 3.1.5
|
|
|
|
# cette version est plus lente mais fonctionne toujours
|
2018-09-25 15:05:41 +04:00
|
|
|
count = 0;
|
2018-04-26 23:19:17 +04:00
|
|
|
for (i in values) {
|
2018-09-25 15:05:41 +04:00
|
|
|
count++;
|
2018-04-26 23:19:17 +04:00
|
|
|
}
|
2018-09-25 15:05:41 +04:00
|
|
|
return count;
|
2018-04-26 23:19:17 +04:00
|
|
|
}
|
|
|
|
function array_copy(dest, src, count, indices, i) {
|
2018-09-25 15:05:41 +04:00
|
|
|
array_new(dest);
|
|
|
|
count = mkindices(src, indices);
|
2018-04-26 23:19:17 +04:00
|
|
|
for (i = 1; i <= count; i++) {
|
2018-09-25 15:05:41 +04:00
|
|
|
dest[indices[i]] = src[indices[i]];
|
2018-04-26 23:19:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
function array_getlastindex(src, count, indices) {
|
2018-09-25 15:05:41 +04:00
|
|
|
count = mkindices(src, indices);
|
|
|
|
if (count == 0) return 0;
|
|
|
|
return indices[count];
|
2018-04-26 23:19:17 +04:00
|
|
|
}
|
|
|
|
function array_add(dest, value, lastindex) {
|
2018-09-25 15:05:41 +04:00
|
|
|
lastindex = array_getlastindex(dest);
|
|
|
|
dest[lastindex + 1] = value;
|
2018-04-26 23:19:17 +04:00
|
|
|
}
|
|
|
|
function array_deli(dest, i, l) {
|
2018-09-25 15:05:41 +04:00
|
|
|
i = int(i);
|
|
|
|
if (i == 0) return;
|
|
|
|
l = array_len(dest);
|
2018-04-26 23:19:17 +04:00
|
|
|
while (i < l) {
|
2018-09-25 15:05:41 +04:00
|
|
|
dest[i] = dest[i + 1];
|
|
|
|
i++;
|
2018-04-26 23:19:17 +04:00
|
|
|
}
|
2018-09-25 15:05:41 +04:00
|
|
|
delete dest[l];
|
2018-04-26 23:19:17 +04:00
|
|
|
}
|
|
|
|
function array_del(dest, value, ignoreCase, i) {
|
|
|
|
do {
|
2018-09-25 15:05:41 +04:00
|
|
|
i = key_index(value, dest, ignoreCase);
|
|
|
|
if (i != 0) array_deli(dest, i);
|
|
|
|
} while (i != 0);
|
2018-04-26 23:19:17 +04:00
|
|
|
}
|
|
|
|
function array_extend(dest, src, count, lastindex, indices, i) {
|
2018-09-25 15:05:41 +04:00
|
|
|
lastindex = array_getlastindex(dest);
|
|
|
|
count = mkindices(src, indices);
|
2018-04-26 23:19:17 +04:00
|
|
|
for (i = 1; i <= count; i++) {
|
2018-09-25 15:05:41 +04:00
|
|
|
dest[lastindex + i] = src[indices[i]];
|
2018-04-26 23:19:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
function array_fill(dest, i) {
|
2018-09-25 15:05:41 +04:00
|
|
|
array_new(dest);
|
2018-04-26 23:19:17 +04:00
|
|
|
for (i = 1; i <= NF; i++) {
|
2018-09-25 15:05:41 +04:00
|
|
|
dest[i] = $i;
|
2018-04-26 23:19:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
function array_getline(src, count, indices, i, j) {
|
2018-09-25 15:05:41 +04:00
|
|
|
$0 = "";
|
|
|
|
count = mkindices(src, indices);
|
2018-04-26 23:19:17 +04:00
|
|
|
for (i = 1; i <= count; i++) {
|
2018-09-25 15:05:41 +04:00
|
|
|
j = indices[i];
|
|
|
|
$j = src[j];
|
2018-04-26 23:19:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
function array_appendline(src, count, indices, i, nf, j) {
|
2018-09-25 15:05:41 +04:00
|
|
|
count = mkindices(src, indices);
|
|
|
|
nf = NF;
|
2018-04-26 23:19:17 +04:00
|
|
|
for (i = 1; i <= count; i++) {
|
2018-09-25 15:05:41 +04:00
|
|
|
j = nf + indices[i];
|
|
|
|
$j = src[indices[i]];
|
2018-04-26 23:19:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
function in_array(value, values, ignoreCase, i) {
|
|
|
|
if (ignoreCase) {
|
2018-09-25 15:05:41 +04:00
|
|
|
value = tolower(value);
|
2018-04-26 23:19:17 +04:00
|
|
|
for (i in values) {
|
2018-09-25 15:05:41 +04:00
|
|
|
if (tolower(values[i]) == value) return 1;
|
2018-04-26 23:19:17 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (i in values) {
|
2018-09-25 15:05:41 +04:00
|
|
|
if (values[i] == value) return 1;
|
2018-04-26 23:19:17 +04:00
|
|
|
}
|
|
|
|
}
|
2018-09-25 15:05:41 +04:00
|
|
|
return 0;
|
2018-04-26 23:19:17 +04:00
|
|
|
}
|
|
|
|
function key_index(value, values, ignoreCase, i) {
|
|
|
|
if (ignoreCase) {
|
2018-09-25 15:05:41 +04:00
|
|
|
value = tolower(value);
|
2018-04-26 23:19:17 +04:00
|
|
|
for (i in values) {
|
2018-09-25 15:05:41 +04:00
|
|
|
if (tolower(values[i]) == value) return int(i);
|
2018-04-26 23:19:17 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (i in values) {
|
2018-09-25 15:05:41 +04:00
|
|
|
if (values[i] == value) return int(i);
|
2018-04-26 23:19:17 +04:00
|
|
|
}
|
|
|
|
}
|
2018-09-25 15:05:41 +04:00
|
|
|
return 0;
|
2018-04-26 23:19:17 +04:00
|
|
|
}
|
|
|
|
function array2s(values, prefix, sep, suffix, noindices, first, i, s) {
|
2018-09-25 15:05:41 +04:00
|
|
|
if (!prefix) prefix = "[";
|
|
|
|
if (!sep) sep = ", ";
|
|
|
|
if (!suffix) suffix = "]";
|
|
|
|
s = prefix;
|
|
|
|
first = 1;
|
2018-04-26 23:19:17 +04:00
|
|
|
for (i in values) {
|
2018-09-25 15:05:41 +04:00
|
|
|
if (first) first = 0;
|
|
|
|
else s = s sep;
|
|
|
|
if (!noindices) s = s "[" i "]=";
|
|
|
|
s = s values[i];
|
2018-04-26 23:19:17 +04:00
|
|
|
}
|
2018-09-25 15:05:41 +04:00
|
|
|
s = s suffix;
|
|
|
|
return s;
|
2018-04-26 23:19:17 +04:00
|
|
|
}
|
|
|
|
function array2so(values, prefix, sep, suffix, noindices, count, indices, i, s) {
|
2018-09-25 15:05:41 +04:00
|
|
|
if (!prefix) prefix = "[";
|
|
|
|
if (!sep) sep = ", ";
|
|
|
|
if (!suffix) suffix = "]";
|
|
|
|
s = prefix;
|
|
|
|
count = mkindices(values, indices);
|
2018-04-26 23:19:17 +04:00
|
|
|
for (i = 1; i <= count; i++) {
|
2018-09-25 15:05:41 +04:00
|
|
|
if (i > 1) s = s sep;
|
|
|
|
if (!noindices) s = s "[" indices[i] "]=";
|
|
|
|
s = s values[indices[i]];
|
2018-04-26 23:19:17 +04:00
|
|
|
}
|
2018-09-25 15:05:41 +04:00
|
|
|
s = s suffix;
|
|
|
|
return s;
|
2018-04-26 23:19:17 +04:00
|
|
|
}
|
|
|
|
function array_join(values, sep, prefix, suffix, count, indices, i, s) {
|
2018-09-25 15:05:41 +04:00
|
|
|
s = prefix;
|
|
|
|
count = mkindices(values, indices);
|
2018-04-26 23:19:17 +04:00
|
|
|
for (i = 1; i <= count; i++) {
|
2018-09-25 15:05:41 +04:00
|
|
|
if (i > 1) s = s sep;
|
|
|
|
s = s values[indices[i]];
|
2018-04-26 23:19:17 +04:00
|
|
|
}
|
2018-09-25 15:05:41 +04:00
|
|
|
s = s suffix;
|
|
|
|
return s;
|
2018-04-26 23:19:17 +04:00
|
|
|
}
|