Difference between revisions of "Manual:DIL Manual/savestr()"

From DikuMUD Wiki
Jump to navigation Jump to search
(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)
  
−
<span id="bfsavestr"></span>
+
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.
  
−
'''Function:'''  <i><!--CODE-->integer savestr( filename : string , buff : string , wa :string);</i><!--ENDCODE-->
+
== 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.
 +
|}
  
−
<!--TERM--> '''filename'''
+
== Returns ==
−
<!--DEFINITION-->         The name of the String file to save the String to
+
Integer, constants from <code>vme/include/vme.h</code>:
−
<!--TERM--> '''buff'''
+
* <code>FILE_SAVED</code> (1): written.
−
<!--DEFINITION-->         The String you wish to save into the file
+
* <code>FILE_ILEGAL_OPP</code> (-1): the mode is neither <code>w</code> nor <code>a</code>.
−
<!--TERM--> '''wa'''
+
The header also has <code>FILE_NOT_CREATED</code> (0) and <code>FILE_NOT_SAVED</code> (-2), but
−
<!--DEFINITION-->         Write or append
+
savestr() doesn't give them: a file that can't be opened still reports <code>FILE_SAVED</code>.
−
<!--TERM--> '''Return'''
 
−
<!--DEFINITION-->         <i><!--CODE-->FILE_SAVED, FILE_NOT_SAVED, FILE_NOT_CREATED, or FILE_ILEGAL_OPP</i><!--ENDCODE-->
 
  
−
Savestr is used to save strings to disk to be loaded later by the 'load' function.
+
It fails when any of the three strings is empty, so an empty text can't be saved: remove the
−
The 'savestr' and 'Loadstr' is perfect for operations such as
+
file with [[Manual:DIL Manual/delstr()|delstr()]] instead.
−
on-line edited newspaper, a lottery where the tickets are sold to players,
 
−
creating smarter NPC's that can remember through reboots who they are hunting,
 
−
Dil based teachers, message boards, mail system, news command., zone or
 
−
room based help,  competition boards, and much much more.
 
−
'''Note:'''The append/write argument must be in lower case and can only be a
 
−
'w' or a 'a' surrounded by '"'.  If the argument is a 'w' it will over write
 
−
any string file by that name.  If the argument is 'a' it will append to the
 
−
file by that name.
 
−
'''Disk access is always slow'''.
 
−
If you use loadstr on a continuous basis always
 
−
attempt to keep file sizes to a minimum for quick loading.  Otherwise you
 
−
might cause serious delays on the server.
 
−
'''Example:'''
 
−
<i><!--CODE-->
 
−
---~---~---~---~---~---~---~---~---
 
  
−
dilbegin news_save (arg:string  /*for saving*/);
+
== Notes ==
−
var
+
* Only zones with access level 10 or lower (the fourth column of <code>vme/etc/zonelist</code>)
−
        ret:integer;/*to hold the return value if saved or not*/
+
  may use it. Elsewhere it writes "attempt to violate system access security (savestr)" to the
−
code
+
  zone's log and the program stops.
−
{
+
* '''<code>"w"</code> does not empty the file first.''' It writes over the start of the old
−
ret:= savestr("news.txt",arg,"w");
+
  text, and when the new text is shorter, the end of the old one stays behind. To replace a
−
if (!ret)
+
  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,
−
        log ("File not wrote");
+
  <code>_</code> and <code>.</code>, and never build it from what a player typed.
−
        quit;
+
* Disk access is slow; keep the files small and don't save on every heartbeat.
−
        }
 
  
−
sendtext("New news file wrote.[&amp;]n",self);
+
== Example ==
−
quit;/*dil save routine done destroy self.*/
+
<pre>
−
}
+
dilbegin note_winner(pc : unitptr);
−
dilend
+
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()]]
−
</i><!--ENDCODE-->
+
* [[Manual:DIL Manual/delstr()|delstr()]]
−
'''See Also''' <i><!--CODE-->
+
* [[Manual:DIL Manual/flog()|flog()]]
−
[[#bfdelstr|Delete a String file]] and
+
* [[Manual:DIL Manual/store()|store()]]
−
[[#bfloadstr|Load a String file]]</i><!--ENDCODE-->
 

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 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