From 915b9bff547705bf9df287ffb2a057eeaec4ca02 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Sun, 12 Jan 2020 20:30:40 +0000 Subject: [PATCH] stdlib: Implement read() and readlines() functions. --- docs/Goalfile.pod | 17 +++++++++++++++++ stdlib/prelude.gl | 10 ++++++++++ tests/10-function-read.data | 1 + tests/10-function-read.gl | 23 +++++++++++++++++++++++ tests/10-function-read.sh | 24 ++++++++++++++++++++++++ tests/10-function-readlines.data1 | 3 +++ tests/10-function-readlines.data2 | 0 tests/10-function-readlines.expected | 3 +++ tests/10-function-readlines.gl | 25 +++++++++++++++++++++++++ tests/10-function-readlines.sh | 24 ++++++++++++++++++++++++ 10 files changed, 130 insertions(+) create mode 100644 tests/10-function-read.data create mode 100644 tests/10-function-read.gl create mode 100755 tests/10-function-read.sh create mode 100644 tests/10-function-readlines.data1 create mode 100644 tests/10-function-readlines.data2 create mode 100644 tests/10-function-readlines.expected create mode 100644 tests/10-function-readlines.gl create mode 100755 tests/10-function-readlines.sh diff --git a/docs/Goalfile.pod b/docs/Goalfile.pod index 37f3c07..46fbe1d 100644 --- a/docs/Goalfile.pod +++ b/docs/Goalfile.pod @@ -56,6 +56,23 @@ Concatenate C and C. 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 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 returning a list of strings. + =head3 realpath (filename) For example: diff --git a/stdlib/prelude.gl b/stdlib/prelude.gl index e2d0400..5d072ea 100644 --- a/stdlib/prelude.gl +++ b/stdlib/prelude.gl @@ -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 index 0000000..7898192 --- /dev/null +++ b/tests/10-function-read.data @@ -0,0 +1 @@ +a diff --git a/tests/10-function-read.gl b/tests/10-function-read.gl new file mode 100644 index 0000000..47bc818 --- /dev/null +++ b/tests/10-function-read.gl @@ -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 index 0000000..3601a7d --- /dev/null +++ b/tests/10-function-read.sh @@ -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 index 0000000..de98044 --- /dev/null +++ b/tests/10-function-readlines.data1 @@ -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 index 0000000..e69de29 diff --git a/tests/10-function-readlines.expected b/tests/10-function-readlines.expected new file mode 100644 index 0000000..de98044 --- /dev/null +++ b/tests/10-function-readlines.expected @@ -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 index 0000000..f4359e2 --- /dev/null +++ b/tests/10-function-readlines.gl @@ -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 index 0000000..c1f8145 --- /dev/null +++ b/tests/10-function-readlines.sh @@ -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 -- 1.8.3.1