summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Friesel <derf@derf.homelinux.org>2009-05-29 18:18:01 +0200
committerDaniel Friesel <derf@derf.homelinux.org>2009-05-29 18:18:01 +0200
commitb7fd6861218840e1bd037e0c76c4ae699e9ee612 (patch)
tree2bab2ee649074c362813e5f94484d4a3ceedff50
Initial commit
-rwxr-xr-xlib/Simplestore.pm45
1 files changed, 45 insertions, 0 deletions
diff --git a/lib/Simplestore.pm b/lib/Simplestore.pm
new file mode 100755
index 0000000..aea8b2b
--- /dev/null
+++ b/lib/Simplestore.pm
@@ -0,0 +1,45 @@
+#!/usr/bin/env perl
+## Library for the Simplestore format
+## Copyright © 2009 by Daniel Friesel <derf@derf.homelinux.org>
+## License: WTFPL <http://sam.zoy.org/wtfpl>
+use strict;
+use warnings;
+
+our (@ISA, @EXPORT, $VERSION);
+require Exporter;
+@ISA = ('Exporter');
+@EXPORT = ('load', 'save');
+$VERSION = '1.0';
+
+sub load {
+ my $file = shift;
+ my ($store, $key, $value);
+ $store = shift if @_;
+ open(my $handle, '<', $file) or die("Cannot read $file: $!");
+ while (<$handle>) {
+ chomp;
+ /^(\S+)\s+(.*)$/ or next;
+ ($key, $value) = ($1, $2);
+ if (exists($store->{$key})) {
+ $store->{$key} .= "\n$value";
+ } else {
+ $store->{$key} = $value;
+ }
+ }
+ close($handle);
+ return($store);
+}
+
+sub save {
+ my ($file, $store) = @_;
+ my $key;
+ open(my $handle, '>', $file) or die("Cannot open $file: $!");
+ foreach $key (keys(%$store)) {
+ foreach (split(/\n/, $store->{$key})) {
+ print $handle "$key\t$_\n";
+ }
+ }
+ close($handle);
+}
+
+1;