Difference between revisions of "Manual:DIL Manual/savestr()"
(XML import) |
(checked against the compiler and the interpreter; example compiles) |
||
| (One intermediate revision by one other user not shown) | |||
| Line 1: | Line 1: | ||
| + | = savestr() = | ||
| + | '''Function''' (integer). Writes a string to a file, to read back later with loadstr(). | ||
| + | savestr(filename : string, s : string, mode : string) | ||
| − | < | + | Saves <code>s</code> in the file <code>strings/filename</code> under the zone's DIL file |
| + | directory: the last column of the zone's line in <code>vme/etc/zonelist</code>, or | ||
| + | <code>dil_file_dir</code> from <code>server.cfg</code> (<code>../lib/file/</code>) when the zone | ||
| + | has none. The <code>strings</code> directory is made when it is missing. Such files outlive | ||
| + | reboots; boards.zon keeps its messages in them. | ||
| − | + | == Parameters == | |
| + | {| class="wikitable" | ||
| + | ! Name !! Type !! Meaning | ||
| + | |- | ||
| + | | filename || string || The file's name inside the <code>strings</code> directory. | ||
| + | |- | ||
| + | | s || string || The text to save, as it is (no newline is added). | ||
| + | |- | ||
| + | | mode || string || <code>"w"</code> to write from the start of the file, <code>"a"</code> to add to its end. Only the first letter counts, and it must be lower case. | ||
| + | |} | ||
| − | + | == Returns == | |
| − | + | Integer, constants from <code>vme/include/vme.h</code>: | |
| − | + | * <code>FILE_SAVED</code> (1): written. | |
| − | + | * <code>FILE_ILEGAL_OPP</code> (-1): the mode is neither <code>w</code> nor <code>a</code>. | |
| − | + | The header also has <code>FILE_NOT_CREATED</code> (0) and <code>FILE_NOT_SAVED</code> (-2), but | |
| − | + | savestr() doesn't give them: a file that can't be opened still reports <code>FILE_SAVED</code>. | |
| − | |||
| − | |||
| − | + | It fails when any of the three strings is empty, so an empty text can't be saved: remove the | |
| − | + | file with [[Manual:DIL Manual/delstr()|delstr()]] instead. | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | == Notes == | |
| − | + | * Only zones with access level 10 or lower (the fourth column of <code>vme/etc/zonelist</code>) | |
| − | + | may use it. Elsewhere it writes "attempt to violate system access security (savestr)" to the | |
| − | + | zone's log and the program stops. | |
| − | + | * '''<code>"w"</code> does not empty the file first.''' It writes over the start of the old | |
| − | + | text, and when the new text is shorter, the end of the old one stays behind. To replace a | |
| − | + | file whose text may get shorter, call [[Manual:DIL Manual/delstr()|delstr()]] first. | |
| − | + | * Unlike loadstr() and delstr(), the file name is not checked: keep it to letters, digits, | |
| − | + | <code>_</code> and <code>.</code>, and never build it from what a player typed. | |
| − | + | * Disk access is slow; keep the files small and don't save on every heartbeat. | |
| − | |||
| − | + | == Example == | |
| − | + | <pre> | |
| − | + | dilbegin note_winner(pc : unitptr); | |
| − | + | var | |
| + | err : integer; | ||
| + | code | ||
| + | { | ||
| + | err := savestr("lottery.win", pc.name + " " + asctime(realtime) + "<br/>", "a"); | ||
| + | if (err != FILE_SAVED) | ||
| + | log("lottery: could not save the winner " + pc.name); | ||
| + | quit; | ||
| + | } | ||
| + | dilend | ||
| + | </pre> | ||
| + | Adds a line naming a lottery winner to a file that keeps growing, the way boards.zon appends to | ||
| + | a board's index. | ||
| − | + | == See also == | |
| − | + | * [[Manual:DIL Manual/loadstr()|loadstr()]] | |
| − | + | * [[Manual:DIL Manual/delstr()|delstr()]] | |
| − | + | * [[Manual:DIL Manual/flog()|flog()]] | |
| − | + | * [[Manual:DIL Manual/store()|store()]] | |
| − | |||
Latest revision as of 23:20, 14 November 2026
savestr()
Function (integer). Writes a string to a file, to read back later with loadstr().
savestr(filename : string, s : string, mode : string)
Saves s in the file strings/filename under the zone's DIL file
directory: the last column of the zone's line in vme/etc/zonelist, or
dil_file_dir from server.cfg (../lib/file/) when the zone
has none. The strings directory is made when it is missing. Such files outlive
reboots; boards.zon keeps its messages in them.
Parameters
| Name | Type | Meaning |
|---|---|---|
| filename | string | The file's name inside the strings directory.
|
| s | string | The text to save, as it is (no newline is added). |
| mode | string | "w" to write from the start of the file, "a" to add to its end. Only the first letter counts, and it must be lower case.
|
Returns
Integer, constants from vme/include/vme.h:
FILE_SAVED(1): written.FILE_ILEGAL_OPP(-1): the mode is neitherwnora.
The header also has FILE_NOT_CREATED (0) and FILE_NOT_SAVED (-2), but
savestr() doesn't give them: a file that can't be opened still reports FILE_SAVED.
It fails when any of the three strings is empty, so an empty text can't be saved: remove the file with delstr() instead.
Notes
- Only zones with access level 10 or lower (the fourth column of
vme/etc/zonelist)
may use it. Elsewhere it writes "attempt to violate system access security (savestr)" to the zone's log and the program stops.
"w"does not empty the file first. It writes over the start of the old
text, and when the new text is shorter, the end of the old one stays behind. To replace a file whose text may get shorter, call delstr() first.
- Unlike loadstr() and delstr(), the file name is not checked: keep it to letters, digits,
_and., and never build it from what a player typed.
- Disk access is slow; keep the files small and don't save on every heartbeat.
Example
dilbegin note_winner(pc : unitptr);
var
err : integer;
code
{
err := savestr("lottery.win", pc.name + " " + asctime(realtime) + "<br/>", "a");
if (err != FILE_SAVED)
log("lottery: could not save the winner " + pc.name);
quit;
}
dilend
Adds a line naming a lottery winner to a file that keeps growing, the way boards.zon appends to a board's index.