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)
 
Line 1: Line 1:
−
<span id="bfsavestr"></span>
+
= savestr() =
 +
'''Function''' (integer). Writes a string to a file, to read back later with loadstr().
  
−
  '''Function:'''  <i><!--CODE-->integer savestr( filename : string , buff : string , wa :string);</i><!--ENDCODE-->
+
  savestr(filename : string, s : string, mode : string)
  
−
<!--TERM--> '''filename'''
+
Saves <code>s</code> in the file <code>strings/filename</code> under the zone's DIL file
−
<!--DEFINITION-->         The name of the String file to save the String to
+
directory: the last column of the zone's line in <code>vme/etc/zonelist</code>, or
−
<!--TERM--> '''buff'''
+
<code>dil_file_dir</code> from <code>server.cfg</code> (<code>../lib/file/</code>) when the zone
−
<!--DEFINITION-->         The String you wish to save into the file
+
has none. The <code>strings</code> directory is made when it is missing. Such files outlive
−
<!--TERM--> '''wa'''
+
reboots; boards.zon keeps its messages in them.
−
<!--DEFINITION-->         Write or append
 
−
<!--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.
+
== Parameters ==
−
The 'savestr' and 'Loadstr' is perfect for operations such as
+
{| class="wikitable"
−
on-line edited newspaper, a lottery where the tickets are sold to players,
+
! Name !! Type !! Meaning
−
creating smarter NPC's that can remember through reboots who they are hunting,
+
|-
−
Dil based teachers, message boards, mail system, news command., zone or
+
| filename || string || The file's name inside the <code>strings</code> directory.
−
room based help, competition boards, and much much more.
+
|-
−
'''Note:'''The append/write argument must be in lower case and can only be a
+
| s || string || The text to save, as it is (no newline is added).
−
'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
+
| 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.
−
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*/);
+
== Returns ==
−
var
+
Integer, constants from <code>vme/include/vme.h</code>:
−
        ret:integer;/*to hold the return value if saved or not*/
+
* <code>FILE_SAVED</code> (1): written.
−
code
+
* <code>FILE_ILEGAL_OPP</code> (-1): the mode is neither <code>w</code> nor <code>a</code>.
−
dilend
+
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.
  
−
</i><!--ENDCODE-->
+
== Notes ==
−
'''See Also''' <i><!--CODE-->
+
* Only zones with access level 10 or lower (the fourth column of <code>vme/etc/zonelist</code>)
−
[[#bfdelstr|Delete a String file]] and
+
  may use it. Elsewhere it writes "attempt to violate system access security (savestr)" to the
−
[[#bfloadstr|Load a String file]]</i><!--ENDCODE-->
+
  zone's log and the program stops.
−
<span id="bfsavestr"></span>
+
* '''<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.
  
−
'''Function:'''  <i><!--CODE-->integer savestr( filename : string , buff : string , wa :string);</i><!--ENDCODE-->
+
== Example ==
−
 
+
<pre>
−
<!--TERM-->  '''filename'''
+
dilbegin note_winner(pc : unitptr);
−
<!--DEFINITION-->        The name of the String file to save the String to
+
var
−
<!--TERM-->  '''buff'''
+
  err : integer;
−
<!--DEFINITION-->        The String you wish to save into the file
+
code
−
<!--TERM-->  '''wa'''
+
{
−
<!--DEFINITION-->        Write or append
+
  err := savestr("lottery.win", pc.name + " " + asctime(realtime) + "<br/>", "a");
−
<!--TERM-->  '''Return'''
+
  if (err != FILE_SAVED)
−
<!--DEFINITION-->        <i><!--CODE-->FILE_SAVED, FILE_NOT_SAVED, FILE_NOT_CREATED, or FILE_ILEGAL_OPP</i><!--ENDCODE-->
+
      log("lottery: could not save the winner " + pc.name);
−
 
+
  quit;
−
Savestr is used to save strings to disk to be loaded later by the 'load' function.
+
}
−
The 'savestr' and 'Loadstr' is perfect for operations such as
 
−
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*/);
 
−
var
 
−
        ret:integer;/*to hold the return value if saved or not*/
 
−
code
 
 
dilend
 
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