aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-09-09 15:02:47 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-09-09 15:02:47 -0400
commite61096d6eb426747376cf8d8c6f46c62b583eb8e (patch)
treede3f98a30772c47bf1a7024edd23ccb5af842e1a
parent013fbe22e043320c3154d2b21bfeab733128ed1d (diff)
Fix up ini example
-rw-r--r--environment.c1
-rw-r--r--examples/ini.tm18
2 files changed, 10 insertions, 9 deletions
diff --git a/environment.c b/environment.c
index c65edac1..97beaa5a 100644
--- a/environment.c
+++ b/environment.c
@@ -259,6 +259,7 @@ env_t *new_compilation_unit(CORD *libname)
{"files", "Path$children", "func(path:Path, include_hidden=no)->[Path]"},
{"is_directory", "Path$is_directory", "func(path:Path, follow_symlinks=yes)->Bool"},
{"is_file", "Path$is_file", "func(path:Path, follow_symlinks=yes)->Bool"},
+ {"is_pipe", "Path$is_pipe", "func(path:Path, follow_symlinks=yes)->Bool"},
{"is_socket", "Path$is_socket", "func(path:Path, follow_symlinks=yes)->Bool"},
{"is_symlink", "Path$is_symlink", "func(path:Path)->Bool"},
{"parent", "Path$parent", "func(path:Path)->Path"},
diff --git a/examples/ini.tm b/examples/ini.tm
index 5e6f438e..b2e59986 100644
--- a/examples/ini.tm
+++ b/examples/ini.tm
@@ -6,12 +6,8 @@ _HELP := "
$_USAGE
"
-file := use ./file.tm
-
-func parse_ini(filename:Text)->{Text:{Text:Text}}:
- text := when file.read(filename) is Failure(err): fail(err)
- is Success(text): text
-
+func parse_ini(path:Path)->{Text:{Text:Text}}:
+ text := path:read()
sections := {:Text:@{Text:Text}}
current_section := @{:Text:Text}
sections:set("", current_section)
@@ -21,8 +17,9 @@ func parse_ini(filename:Text)->{Text:{Text:Text}}:
for line in text:lines():
line = line:trim()
+ skip if line:starts_with(";") or line:starts_with("#")
if line:matches($/{0+space}[{..}]/):
- section_name := line:replace($/{0+space}[{..}]{..}/, "\2"):trim():lower()
+ section_name := line:replace($/[?]/, "\1"):trim():lower()
current_section = @{:Text:Text}
sections:set(section_name, current_section)
else if line:matches($/{..}={..}/):
@@ -32,7 +29,7 @@ func parse_ini(filename:Text)->{Text:{Text:Text}}:
return {k:v[] for k,v in sections}
-func main(filename:Text, key:Text):
+func main(path:Path, key:Text):
keys := key:split($Pattern"/")
if keys.length > 2:
exit(1, message="
@@ -40,7 +37,10 @@ func main(filename:Text, key:Text):
$_USAGE
")
- data := parse_ini(filename)
+ if not path:is_file() or path:is_pipe():
+ exit(code=1, "Could not read file: $(path.text_content)")
+
+ data := parse_ini(path)
if keys.length < 1 or keys[1] == '*':
!! $data
return