2 # ==============================================================================
3 # portsreinstall library script
4 # - Editing operations on files -
5 # Copyright (C) 2013 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 pattern
15 [ -n "$item" ] || return 0
16 [ -e "$dstpath" ] || return 0
17 tmpfile=$dstpath.fileedit_rm_a_line.tmp
18 pattern=`str_escape_regexp "$item"`
19 grep -v -E "^$pattern$" "$dstpath" 2> /dev/null > $tmpfile || :
20 mv "$tmpfile" "$dstpath"
23 # ============= Add a line to a file if no matching one exists in the file =============
24 fileedit_add_a_line_if_new ()
26 local item dstpath tmpfile pattern dstdir
29 [ -n "$item" ] || return 0
30 [ -n "$dstpath" ] || return 0
31 tmpfile=$dstpath.fileedit_add_a_line_if_new.tmp
32 pattern=`str_escape_regexp "$item"`
35 cp "$dstpath" "$tmpfile"
36 grep -q -E "^$pattern$" "$dstpath" \
37 || echo "$item" >> $tmpfile
39 dstdir=`dirname "$dstpath"`
40 [ -d "$dstdir" ] || mkdir -p "$dstdir"
41 echo "$item" > $tmpfile
43 mv "$tmpfile" "$dstpath"
46 # ============= Remove lines exactly matching a string from multiple files =============
47 fileedit_rm_a_line_from_files ()
51 [ -n "$item" ] || return 0
54 fileedit_rm_a_line "$item" "$filepath"
58 # ============= Add a line to each of multiple files if no matching one exists in the file =============
59 fileedit_add_a_line_to_files_if_new ()
63 [ -n "$item" ] || return 0
66 fileedit_add_a_line_if_new "$item" "$filepath"
70 # ============= Add multiple lines to a file for what don't match any existing ones in the file =============
71 fileedit_add_lines_if_new ()
73 local dstpath advance tmpfile tmpclip origin dstdir
76 tmpfile=$dstpath.fileedit_add_lines_if_new.tmp
77 tmpclip=${TMPDIR}/fileedit_add_lines_if_new:add
82 grep -q -E "^`str_escape_regexp \"$origin\"`$" "$dstpath" \
85 if [ "@$advance" = @advance ]
87 cat "$tmpclip" "$dstpath"
89 cat "$dstpath" "$tmpclip"
92 dstdir=`dirname "$dstpath"`
93 [ -d "$dstdir" ] || mkdir -p "$dstdir"
96 mv "$tmpfile" "$dstpath"
99 # ============= Combine (possibly missing) lists into one =============
100 fileedit_combine_lists ()
102 cat "$@" 2> /dev/null | sort -u || :
105 # ============= Manipulate old and new lines from an old and a new versions of a file =============
106 fileedit_manipulate_old_new_lines ()
108 local oldsrc newsrc olddiff newdiff
113 tmpdiff=${TMPDIR}/fileedit_manipulate_old_new_lines::diff
114 [ -e "$oldsrc" ] || oldsrc=/dev/null
115 [ -e "$newsrc" ] || newsrc=/dev/null
116 diff "$oldsrc" "$newsrc" > $tmpdiff && return 1
117 grep '^<' "$tmpdiff" | sed 's/^< //' > $olddiff || :
118 grep '^>' "$tmpdiff" | sed 's/^> //' > $newdiff || :
121 # ============= Manipulate old lines from an old and a new versions of a file =============
122 fileedit_manipulate_old_lines ()
127 tmpdiff=${TMPDIR}/fileedit_manipulate_old_lines::diff
128 [ -e "$oldsrc" ] || oldsrc=/dev/null
129 [ -e "$newsrc" ] || newsrc=/dev/null
130 diff "$oldsrc" "$newsrc" > $tmpdiff && return 1
131 grep '^<' "$tmpdiff" | sed 's/^< //'
134 # ============= Manipulate new lines from an old and a new versions of a file =============
135 fileedit_manipulate_new_lines ()
140 tmpdiff=${TMPDIR}/fileedit_manipulate_new_lines::diff
141 [ -e "$oldsrc" ] || oldsrc=/dev/null
142 [ -e "$newsrc" ] || newsrc=/dev/null
143 diff "$oldsrc" "$newsrc" > $tmpdiff && return 1
144 grep '^>' "$tmpdiff" | sed 's/^> //'
147 # ============= Check whether removed lines exists in updating from an old and a new versions of a file =============
148 fileedit_exists_old_lines ()
153 tmpdiff=${TMPDIR}/fileedit_exists_old_lines::diff
154 [ -e "$oldsrc" ] || oldsrc=/dev/null
155 [ -e "$newsrc" ] || newsrc=/dev/null
156 diff "$oldsrc" "$newsrc" > $tmpdiff && return 1
157 grep -q '^<' "$tmpdiff"