-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Roberto Reale
committed
Nov 10, 2016
1 parent
50354ec
commit 7f6bbf9
Showing
1 changed file
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
################################################################################ | ||
# | ||
# | | | | | ||
# |---.,---.,---.|---.| ,---.|--- ,---. | ||
# | |,---|`---.| || |---'| `---. | ||
# `---'`---^`---'` '`---'`---'`---'`---' | ||
# | ||
# | ||
# Bashlets -- A modular extensible toolbox for Bash | ||
# | ||
# Copyright (c) 2016 Roberto Reale | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in | ||
# all copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
# | ||
################################################################################ | ||
|
||
|
||
source bashlet charstream/append | ||
source bashlet charstream/filter | ||
source bashlet marshal/pairs | ||
source bashlet os/tempfile | ||
|
||
|
||
## WARNING: some versions of the zipnote(1) utilities might be buggy! | ||
BASHLETS_FILE_ZIP_ZIPNOTE=zipnote | ||
|
||
#@method | ||
function bashlets_file_zip_get_comments() | ||
{ | ||
local filename="$1" | ||
local startmarker="@.*(zip file comment below this line)" | ||
|
||
$BASHLETS_FILE_ZIP_ZIPNOTE "$filename" \ | ||
| filter after_pattern "$startmarker" | ||
} | ||
|
||
#@method | ||
function bashlets_file_zip_set_comments() | ||
{ | ||
local filename="$1" | ||
local comments_filename="$2" | ||
local startmarker="@.*(zip file comment below this line)" | ||
local tempfile | ||
|
||
[ -e "$filename" ] || return 1 | ||
[ -e "$comments_filename" ] || return 1 | ||
|
||
tempfile="$(tempfile create)" | ||
|
||
# extract header | ||
$BASHLETS_FILE_ZIP_ZIPNOTE "$filename" \ | ||
| filter till_pattern "$startmarker" \ | ||
> "$tempfile" | ||
|
||
# append comments | ||
cat "$tempfile" \ | ||
| append after_pattern "$comments_filename" "$startmarker" \ | ||
| $BASHLETS_FILE_ZIP_ZIPNOTE -w "$filename" | ||
|
||
tempfile destroy "$tempfile" | ||
} | ||
|
||
#@method | ||
function bashlets_file_zip_get_comment_by_key() | ||
{ | ||
local filename="$1" | ||
local key="$2" | ||
|
||
bashlets_file_zip_get_comments $filename \ | ||
| pairs get_by_key "$key" | ||
} | ||
|
||
#@method | ||
function bashlets_file_zip_set_comment_by_key() | ||
{ | ||
local filename="$1" | ||
local key="$2" | ||
shift 2 | ||
local value="$@" | ||
local comments_tempfile | ||
local retval | ||
|
||
[ -e "$filename" ] || return 1 | ||
|
||
comments_tempfile="$(tempfile create)" | ||
|
||
bashlets_file_zip_get_comments $filename \ | ||
| pairs set_by_key "$key" "$value" \ | ||
> "$comments_tempfile" | ||
|
||
bashlets_file_zip_set_comments "$filename" "$comments_tempfile" | ||
retval=$? | ||
|
||
tempfile destroy "$comments_tempfile" | ||
|
||
return $retval | ||
} | ||
|
||
|
||
# ex: ts=4 sw=4 et filetype=sh noexpandtab |