-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibnvl.sh
165 lines (143 loc) · 2.53 KB
/
libnvl.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#
# NVL CODE START
# NVL version 0, lib version 1.0
# NAME strict level 0 excluding \0 and newline
# VAL strict level 0 excluding \0
# Dependencies: wc
#
nvl_reset()
{
NVL_NAME=
NVL_LEN=
NVL_VAL=
NVL_LINENO=1
NVL_ERRMSG=
}
nvl_reset
nvl_header_read()
{
local line
nvl_reset
if ! IFS= read -r line; then
if [ -z "$line" ]; then
return 1
fi
fi
NVL_LINENO=$((NVL_LINENO + 1))
if [ "$line" = "NVL0" ]; then
return 0
fi
NVL_ERRMSG="NVL: there is no nvl formatted data"
return 1
}
nvl_header_write()
{
echo "NVL0"
}
nvl_rec_read()
{
local line state len total_len fname
state=0
NVL_NAME=
NVL_LEN=
NVL_VAL=
while IFS= read -r line; do
NVL_LINENO=$((NVL_LINENO + 1))
case $state in
0)
NVL_NAME=${line%%=*}
if [ "$NVL_NAME" = "$line" ]; then
NVL_ERRMSG="NVL: can't find name"
return 1
fi
line="${line#*=}"
NVL_LEN=${line%%:*}
if [ "$NVL_LEN" = "$line" ]; then
NVL_ERRMSG="NVL: $NVL_NAME: can't find length"
return 1
fi
NVL_VAL="${line#*:}"
if [ -z "$NVL_LEN" ]; then
fname=`_nvl_get_keyval "$NVL_NAME" "$@"`
if [ "$fname" ]; then
echo -n "$NVL_VAL" > "$fname"
NVL_VAL=""
fi
return 0
fi
if ! [ "$NVL_LEN" -ge 0 ]; then
NVL_ERRMSG="NVL: $NVL_NAME: length should be positive integer"
return 1
fi
len=`echo -n "$NVL_VAL" | wc -c`
if [ "$len" -eq "$NVL_LEN" ]; then
return 0
fi
total_len=$len
state=1
;;
1)
NVL_VAL="$NVL_VAL
$line"
len=`echo -n "$line" | wc -c`
total_len=$((total_len + len + 1))
if [ "$total_len" -eq "$NVL_LEN" ]; then
fname=`_nvl_get_keyval "$NVL_NAME" "$@"`
if [ "$fname" ]; then
echo -n "$NVL_VAL" > "$fname"
NVL_VAL=""
fi
return 0
fi
;;
esac
done
if [ "$NVL_LEN" ]; then
NVL_ERRMSG="NVL: data is end before value is read(need $NVL_LEN, read $total_len)"
fi
return 1
}
_nvl_get_keyval()
{
local kv key want
want="$1"
shift
for kv in "$@"; do
key="${kv%%=*}"
if [ "$key" = "$want" ] || [ "$key" = "*" ]; then
echo "${kv#*=}"
return 0
fi
done
echo ""
return 1
}
nvl_rec_write()
{
local N=$1
local V=$2
if [ "${N#*=}" != $N ]; then
NVL_ERRMSG="NVL: name shouldn't contain '=': '$N'"
return 1
fi
echo "$N=`echo $V | wc -c`:$V"
return 0
}
nvl_rec_write_from_file()
{
local N=$1
local V=$2
if [ "${N#*=}" != $N ]; then
NVL_ERRMSG="NVL: name shouldn't contain '=': '$N'"
return 1
fi
echo -n "$N="`wc -c "$V"`:
if ! cat "$V"; then
NVL_ERRMSG="NVL: can't write a value from a file '$V'"
return 1
fi
return 0
}
#
# NVL CODE END
#