stdlib: Implement read() and readlines() functions.
authorRichard W.M. Jones <rjones@redhat.com>
Sun, 12 Jan 2020 20:30:40 +0000 (20:30 +0000)
committerRichard W.M. Jones <rjones@redhat.com>
Sun, 12 Jan 2020 20:30:40 +0000 (20:30 +0000)
docs/Goalfile.pod
stdlib/prelude.gl
tests/10-function-read.data [new file with mode: 0644]
tests/10-function-read.gl [new file with mode: 0644]
tests/10-function-read.sh [new file with mode: 0755]
tests/10-function-readlines.data1 [new file with mode: 0644]
tests/10-function-readlines.data2 [new file with mode: 0644]
tests/10-function-readlines.expected [new file with mode: 0644]
tests/10-function-readlines.gl [new file with mode: 0644]
tests/10-function-readlines.sh [new file with mode: 0755]

index 37f3c07..46fbe1d 100644 (file)
@@ -56,6 +56,23 @@ Concatenate C<list1> and C<list2>.  It's not usually necessary to use
 this function since goals automatically flattens lists within lists
 into simple lists in many cases.
 
+=head3 read (filename)
+
+For example:
+
+ read ("filename") => "this is the content of filename"
+
+Read the contents of C<filename> and return it as a single string.
+If there is a trailing C<\n> in the file it is truncated.
+
+=head3 readlines (filename)
+
+For example:
+
+ readlines ("filename") => ["line1", "line2", "line3"]
+
+Read the lines in C<filename> returning a list of strings.
+
 =head3 realpath (filename)
 
 For example:
index e2d0400..5d072ea 100644 (file)
@@ -107,6 +107,16 @@ pure function extension (name) returning string = @{
     echo "${name##*.}"
 }
 
+# Read a file.
+function read (filename) returning string = @{
+    cat %filename
+}
+
+# Read a file as a list of lines.
+function readlines (filename) returning strings = @{
+    cat %filename
+}
+
 # Real path.
 function realpath (filename) returning string = @{
     realpath -- %filename
diff --git a/tests/10-function-read.data b/tests/10-function-read.data
new file mode 100644 (file)
index 0000000..7898192
--- /dev/null
@@ -0,0 +1 @@
+a
diff --git a/tests/10-function-read.gl b/tests/10-function-read.gl
new file mode 100644 (file)
index 0000000..47bc818
--- /dev/null
@@ -0,0 +1,23 @@
+# Goals test.
+# Copyright (C) 2020 Richard W.M. Jones
+# Copyright (C) 2020 Red Hat Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+# Test the read function.
+
+let x = read ("10-function-read.data")
+
+goal all = { echo %x }
diff --git a/tests/10-function-read.sh b/tests/10-function-read.sh
new file mode 100755 (executable)
index 0000000..3601a7d
--- /dev/null
@@ -0,0 +1,24 @@
+#!/usr/bin/env bash
+# Goals test.
+# Copyright (C) 2020 Richard W.M. Jones
+# Copyright (C) 2020 Red Hat Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+set -e
+
+goals -f 10-function-read.gl > 10-function-read.out
+test "$(cat 10-function-read.out)" = "a"
+rm 10-function-read.out
diff --git a/tests/10-function-readlines.data1 b/tests/10-function-readlines.data1
new file mode 100644 (file)
index 0000000..de98044
--- /dev/null
@@ -0,0 +1,3 @@
+a
+b
+c
diff --git a/tests/10-function-readlines.data2 b/tests/10-function-readlines.data2
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/10-function-readlines.expected b/tests/10-function-readlines.expected
new file mode 100644 (file)
index 0000000..de98044
--- /dev/null
@@ -0,0 +1,3 @@
+a
+b
+c
diff --git a/tests/10-function-readlines.gl b/tests/10-function-readlines.gl
new file mode 100644 (file)
index 0000000..f4359e2
--- /dev/null
@@ -0,0 +1,25 @@
+# Goals test.
+# Copyright (C) 2020 Richard W.M. Jones
+# Copyright (C) 2020 Red Hat Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+# Test the readlines function.
+
+let xs = readlines ("10-function-readlines.data1")
+let ys = readlines ("10-function-readlines.data2")
+let r = join (xs, ys)
+
+goal all = { for f in %r; do echo "$f"; done }
diff --git a/tests/10-function-readlines.sh b/tests/10-function-readlines.sh
new file mode 100755 (executable)
index 0000000..c1f8145
--- /dev/null
@@ -0,0 +1,24 @@
+#!/usr/bin/env bash
+# Goals test.
+# Copyright (C) 2020 Richard W.M. Jones
+# Copyright (C) 2020 Red Hat Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+set -e
+
+goals -f 10-function-readlines.gl > 10-function-readlines.out
+diff -u 10-function-readlines.out 10-function-readlines.expected
+rm 10-function-readlines.out