blob: 25f1fcc312285a3d6ab081eb82a9d2a4b5f47f19 [file] [log] [blame]
developer782bc7f2024-08-23 15:39:22 +08001#!/bin/sh
2
3# Copyright (C) 2024 MediaTek Inc. All rights reserved.
4# Author: Weijie Gao <weijie.gao@mediatek.com>
5# Helpers for simple list operations
6
7# Check if an element exists in a list
8# $1: list name
9# $2: element name to be checked
10# return 0 if exists, 1 otherwise
11list_find() {
12 local found=
13
14 eval "local list=\"\$${1}\""
15
16 for ele in ${list}; do
17 if test "${ele}" == "${2}"; then
18 found=1
19 fi
20 done
21
22 [ "${found}" = "1" ] && return 0
23
24 return 1
25}
26
27# Delete element(s) from a list
28# $1: list name
29# $2: element name
30list_del() {
31 local tmp_list=
32
33 eval "local list=\"\$${1}\""
34
35 for ele in ${list}; do
36 if test "${ele}" != "${2}"; then
37 tmp_list="${tmp_list} ${ele}"
38 fi
39 done
40
41 eval "${1}=\"\${tmp_list}\""
42}
43
44# Add an element to the end of a list
45# Elements with the same name are allowed
46# $1: list name
47# $2: new element name
48list_append_forced() {
49 eval "${1}=\"\$${1} ${2}\""
50}
51
52# Add an element to the end of a list
53# If the element to be appended already exists, this function does nothing
54# $1: list name
55# $2: new element name
56list_append() {
57 list_find "${1}" "${2}" || list_append_forced "${1}" "${2}"
58}
59
60# Add an element to the end of a list
61# If the element to be appended already exists, this function will remove previous existed element(s)
62# $1: list name
63# $2: new element name
64list_append_unique() {
65 list_find "${1}" "${2}" && list_del "${1}" "${2}"
66 list_append_forced "${1}" "${2}"
67}
68
69# Add an element to the start of a list
70# Elements with the same name are allowed
71# $1: list name
72# $2: new element name
73list_prepend_forced() {
74 eval "${1}=\"${2} \$${1}\""
75}
76
77# Add an element to the start of a list
78# If the element to be prepended already exists, this function does nothing
79# $1: list name
80# $2: new element name
81list_prepend() {
82 list_find "${1}" "${2}" || list_prepend_forced "${1}" "${2}"
83}
84
85# Add an element to the start of a list
86# If the element to be prepended already exists, this function will remove previous existed element(s)
87# $1: list name
88# $2: new element name
89list_prepend_unique() {
90 list_find "${1}" "${2}" && list_del "${1}" "${2}"
91 list_prepend_forced "${1}" "${2}"
92}
93
94# Add an element ahead of an existed element of a list
95# If the target element doesn't exist, this function behaves identical to list_append
96# $1: list name
97# $2: target element name
98# $3: new element name
99list_add_before() {
100 local tmp_list=
101 local added=
102
103 eval "local list=\"\$${1}\""
104
105 for ele in ${list}; do
106 if test -z "${added}" -a "${ele}" == "${2}"; then
107 tmp_list="${tmp_list} ${3}"
108 added=1
109 fi
110
111 tmp_list="${tmp_list} ${ele}"
112 done
113
114 if test "${added}" != "1"; then
115 tmp_list="${tmp_list} ${3}"
116 fi
117
118 eval "${1}=\"\${tmp_list}\""
119}
120
121# Add an element ahead of an existed element of a list
122# If the target element doesn't exist, this function behaves identical to list_append
123# If the target element already exists, this function will remove previous existed element(s)
124# $1: list name
125# $2: target element name
126# $3: new element name
127list_add_before_unique() {
128 list_find "${1}" "${3}" && list_del "${1}" "${3}"
129 list_add_before "${1}" "${2}" "${3}"
130}
131
132# Add an element next to an existed element of a list
133# If the target element doesn't exist, this function behaves identical to list_append
134# $1: list name
135# $2: target element name
136# $3: new element name
137list_add_after() {
138 local tmp_list=
139 local added=
140
141 eval "local list=\"\$${1}\""
142
143 for ele in ${list}; do
144 tmp_list="${tmp_list} ${ele}"
145
146 if test -z "${added}" -a "${ele}" == "${2}"; then
147 tmp_list="${tmp_list} ${3}"
148 added=1
149 fi
150 done
151
152 if test "${added}" != "1"; then
153 tmp_list="${tmp_list} ${3}"
154 fi
155
156 eval "${1}=\"\${tmp_list}\""
157}
158
159# Add an element next to an existed element of a list
160# If the target element doesn't exist, this function behaves identical to list_append
161# If the target element already exists, this function will remove previous existed element(s)
162# $1: list name
163# $2: target element name
164# $3: new element name
165list_add_after_unique() {
166 list_find "${1}" "${3}" && list_del "${1}" "${3}"
167 list_add_after "${1}" "${2}" "${3}"
168}
169
170# Replace element(s) with new one
171# $1: list name
172# $2: target element name
173# $3: new element name
174list_replace() {
175 local tmp_list=
176
177 eval "local list=\"\$${1}\""
178
179 for ele in ${list}; do
180 if test "${ele}" == "${2}"; then
181 tmp_list="${tmp_list} ${3}"
182 else
183 tmp_list="${tmp_list} ${ele}"
184 fi
185 done
186
187 eval "${1}=\"\${tmp_list}\""
188}