Adds file-create-temp and file-name, updates readme

master
sloum 2 years ago
parent 9f9a838aed
commit a848d649b0

@ -283,9 +283,11 @@ Not implemented, but on my radar:
<li><code>(read-all-lines [[IOHandle]])</code>: <code>list</code></li>
<li><code>(close [IOHandle])</code>: <code>()</code></li>
<li><code>(file-create [filepath: string])</code>: <code>IOHandle</code></li>
<li><code>(file-create-temp [filename-pattern: string])</code>: <code>IOHandle</code></li>
<li><code>(file-open-read [filepath: string])</code>: <code>IOHandle</code></li>
<li><code>(file-open-write [filepath: string])</code>: <code>IOHandle</code></li>
<li><code>(file-append-to [filepath: string] [string...])</code>: <code>()</code></li>
<li><code>(file-name [IOHandle: file])</code>: <code>string</code></li>
<li><code>(file-stat [filepath: string])</code>: <code>assoc list</code> The associative list will have the following keys: <code>name</code>, <code>size</code>, <code>mode</code>, <code>mod-time</code>, <code>is-dir?</code>. <code>mode</code> will be in decimal as a number and can be converted to an octal string with <code>number->string</code>. <code>size</code> is in bytes.</li>
</ul>
</details>

@ -1374,8 +1374,39 @@ var stdLibrary = vars{
}
return strconv.FormatFloat(float64(n), 'f', -1, 64)
},
"file-create-temp": func(a ...expression) expression {
if len(a) == 0 {
return exception("'file-create-temp' expects a filename pattern as a string, no value was given")
}
fp, ok := a[0].(string)
if !ok {
return exception("'file-create-temp' expects a string representing a filename pattern, a non-string value was given")
}
f, err := os.CreateTemp("", fp)
if err != nil {
return exception("'file-create-temp' was unable to create the requested temporary file: " + err.Error())
}
obj := &IOHandle{f, true}
openFiles = append(openFiles, obj)
return obj
},
"file-name": func(a ...expression) expression {
if len(a) == 0 {
return exception("'file-path' expects an IOHandle, no value was given")
}
ioh, ok := a[0].(*IOHandle)
if !ok {
return exception("'file-path' expects an IOHandle, a non-IOHandle value was given")
}
switch f := ioh.Obj.(type) {
case *os.File:
return f.Name()
default:
return exception("'file-path' encountered an IOHandle type that it cannot process")
}
},
"file-stat": func(a ...expression) expression {
if len(a) < 1 {
if len(a) == 0 {
return exception("'file-stat' expects a filepath, no filepath was given")
}
fp, ok := a[0].(string)

Loading…
Cancel
Save