2 # ==============================================================================
3 # portsreinstall library script
4 # - Editing operations on files -
5 # Copyright (C) 2013-2018 Mamoru Sakaue, MwGhennndo, All Rights Reserved.
6 # This software is distributed under the 2-Clause BSD License.
7 # ==============================================================================
9 # ============= Remove lines exactly matching a string from a file =============
12 local item dstpath tmpfile
15 [ -n "$item" ] || return 0
16 [ -e "$dstpath" ] || return 0
17 tmpfile=$dstpath.fileedit_rm_a_line.tmp
18 grep -v -Fx "$item" "$dstpath" 2> /dev/null > $tmpfile || :
19 mv "$tmpfile" "$dstpath"
22 # ============= Add a line to a file if no matching one exists in the file =============
23 fileedit_add_a_line_if_new ()
25 local item dstpath tmpfile dstdir
28 [ -n "$item" ] || return 0
29 [ -n "$dstpath" ] || return 0
30 tmpfile=$dstpath.fileedit_add_a_line_if_new.tmp
33 cp "$dstpath" "$tmpfile"
34 grep -q -Fx "$item" "$dstpath" \
35 || echo "$item" >> $tmpfile
37 dstdir=`dirname "$dstpath"`
39 echo "$item" > $tmpfile
41 mv "$tmpfile" "$dstpath"
44 # ============= Remove lines exactly matching a string from multiple files =============
45 fileedit_rm_a_line_from_files ()
49 [ -n "$item" ] || return 0
52 fileedit_rm_a_line "$item" "$filepath"
56 # ============= Add a line to each of multiple files if no matching one exists in the file =============
57 fileedit_add_a_line_to_files_if_new ()
61 [ -n "$item" ] || return 0
64 fileedit_add_a_line_if_new "$item" "$filepath"
68 # ============= Add multiple lines to a file for what don't match any existing ones in the file =============
69 fileedit_add_lines_if_new ()
71 local dstpath advance tmpfile tmpclip origin dstdir
74 tmpfile=$dstpath.fileedit_add_lines_if_new.tmp
75 tmpclip=${TMPDIR}/fileedit_add_lines_if_new:add
80 grep -q -Fx "$origin" "$dstpath" \
83 if [ "@$advance" = @advance ]
85 cat "$tmpclip" "$dstpath"
87 cat "$dstpath" "$tmpclip"
90 dstdir=`dirname "$dstpath"`
94 mv "$tmpfile" "$dstpath"
97 # ============= Combine (possibly missing) lists into one =============
98 fileedit_combine_lists ()
100 cat "$@" 2> /dev/null | sort -u || :
103 # ============= Manipulate old and new lines from an old and a new versions of a file =============
104 fileedit_manipulate_old_new_lines ()
106 local oldsrc newsrc olddiff newdiff tmpdiff
111 tmpdiff=${TMPDIR}/fileedit_manipulate_old_new_lines::diff
112 [ -e "$oldsrc" ] || oldsrc=/dev/null
113 [ -e "$newsrc" ] || newsrc=/dev/null
114 diff "$oldsrc" "$newsrc" > $tmpdiff && return 1
115 grep '^<' "$tmpdiff" | sed 's/^< //' > $olddiff || :
116 grep '^>' "$tmpdiff" | sed 's/^> //' > $newdiff || :
119 # ============= Manipulate old lines from an old and a new versions of a file =============
120 fileedit_manipulate_old_lines ()
122 local oldsrc newsrc tmpdiff
125 tmpdiff=${TMPDIR}/fileedit_manipulate_old_lines::diff
126 [ -e "$oldsrc" ] || oldsrc=/dev/null
127 [ -e "$newsrc" ] || newsrc=/dev/null
128 diff "$oldsrc" "$newsrc" > $tmpdiff && return 1
129 grep '^<' "$tmpdiff" | sed 's/^< //'
132 # ============= Manipulate new lines from an old and a new versions of a file =============
133 fileedit_manipulate_new_lines ()
135 local oldsrc newsrc tmpdiff
138 tmpdiff=${TMPDIR}/fileedit_manipulate_new_lines::diff
139 [ -e "$oldsrc" ] || oldsrc=/dev/null
140 [ -e "$newsrc" ] || newsrc=/dev/null
141 diff "$oldsrc" "$newsrc" > $tmpdiff && return 1
142 grep '^>' "$tmpdiff" | sed 's/^> //'
145 # ============= Check whether removed lines exists in updating from an old and a new versions of a file =============
146 fileedit_exists_old_lines ()
148 local oldsrc newsrc tmpdiff
151 tmpdiff=${TMPDIR}/fileedit_exists_old_lines::diff
152 [ -e "$oldsrc" ] || oldsrc=/dev/null
153 [ -e "$newsrc" ] || newsrc=/dev/null
154 diff "$oldsrc" "$newsrc" > $tmpdiff && return 1
155 grep -q '^<' "$tmpdiff"