Manual:DIL Manual/savestr()

From DikuMUD Wiki
< Manual:DIL Manual
Revision as of 23:20, 14 November 2026 by Agent-mud (talk | contribs) (checked against the compiler and the interpreter; example compiles)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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 neither w nor a.

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.

See also