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

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:
 +
= loadstr() =
 +
'''Function''' (integer). Reads a whole file into a string variable.
  
 +
loadstr(filename : string, s : string)
  
−
<span id="bfloadstr"></span>
+
Reads 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> when it has none) and puts its text in the variable <code>s</code>. The
 +
files are usually written by [[Manual:DIL Manual/savestr()|savestr()]], but any text file in
 +
that directory will do: basis.zon reads <code>motd</code> and <code>welcome</code> this way.
  
−
'''Function:'''
+
== Parameters ==
−
<i><!--CODE-->integer loadstr( filename : string , buff : string );</i><!--ENDCODE-->
+
{| class="wikitable"
 +
! Name !! Type !! Meaning
 +
|-
 +
| filename || string || The file's name inside the <code>strings</code> directory: letters, digits, <code>_</code> and <code>.</code> only, at most 512 characters.
 +
|-
 +
| s || string || A string variable of the program. It receives the text.
 +
|}
  
−
<!--TERM--> '''filename'''
+
== Returns ==
−
<!--DEFINITION-->         The name of the string file to be loaded
+
Integer, constants from <code>vme/include/vme.h</code>:
−
<!--TERM--> '''buff'''
+
* <code>FILE_LOADED</code> (1): read.
−
<!--DEFINITION-->         The string that you wish to read the file contents into
+
* <code>FILE_NOT_FOUND</code> (0): no such file, or the name has other characters (also
−
<!--TERM--> '''Return'''
+
  written to the zone's log).
−
<!--DEFINITION-->         <i><!--CODE-->FILE_LOADED, FILE_NOT_FOUND, FILE_OUT_OF_MEMORY,or FILE_TO_LARGE</i><!--ENDCODE-->
+
* <code>FILE_TO_LARGE</code> (-1): the file is over 1,000,000 bytes.
 +
* <code>FILE_OUT_OF_MEMORY</code> (2).
 +
It fails when either argument is a failed value.
  
−
Loadstr is used to load strings from disk that were saved either by savestr
+
== Notes ==
−
or any text editor.
+
* Test the result. When the file is missing or empty, <code>s</code> keeps the value it had
−
The 'loadstr' is perfect for operations such as
+
  before the call.
−
on-line edited newspaper, a lottery where the tickets are sold to players,
+
* '''The second argument must be a plain string variable.''' vmc also takes a constant or a
−
creating smarter NPC's that can remember through reboots who they are hunting,
+
  field such as <code>u.title</code>, but when the file is read, the server then writes into
−
Dil based teachers, message boards, mail system, news command., zone or
+
  memory that isn't a variable and crashes.
−
room based help,  competition boards, and much much more.
+
* Only zones with access level 10 or lower (the fourth column of <code>vme/etc/zonelist</code>)
−
'''Disk access is always slow'''.
+
  may use it. Elsewhere it writes "attempt to violate system access security (loadstr)" to the
−
attempt to keep file sizes to a minimum for quick loading. Otherwise you
+
  zone's log and the program stops.
−
might cause serious delays on the server.
+
* The text comes as it is in the file. Use [[Manual:DIL Manual/textformat()|textformat()]] to
−
'''Example:'''
+
  turn <code>\n</code> written in it into new lines.
−
<i><!--CODE-->
+
* Disk access is slow; keep the files small and read them only when needed.
−
---~---~---~---~---~---~---~---~---
 
  
−
dilbegin news_load ();
+
== Example ==
−
var
+
<pre>
−
        ret:integer;/*to hold the return value if loaded or not*/
+
dilbegin show_news(pc : unitptr);
−
        buff:string;/*to hold the loaded string*/
+
var
−
code
+
  err : integer;
−
{
+
  buf : string;
−
ret:= loadstr("news.txt",buff);
+
code
−
if (!ret)
+
{
−
        {
+
  err := loadstr("news", buf);
−
        log ("File not read.");
+
  if (err == FILE_LOADED)
−
        quit;
+
      pagestring(buf, pc);
−
        }
+
  else
 +
      sendtext("There is no news.<br/>", pc);
 +
  quit;
 +
}
 +
dilend
 +
</pre>
 +
Shows the news file to a player, page by page.
  
−
sendtext(buff+"[&amp;]n",self);
+
== See also ==
−
quit;/*dil load routine done destroy self.*/
+
* [[Manual:DIL Manual/savestr()|savestr()]]
−
}
+
* [[Manual:DIL Manual/delstr()|delstr()]]
−
dilend
+
* [[Manual:DIL Manual/textformat()|textformat()]]
−
 
+
* [[Manual:DIL Manual/pagestring()|pagestring()]]
−
---~---~---~---~---~---~---~---~---
 
−
 
 
−
</i><!--ENDCODE-->
 
−
'''See Also''' <i><!--CODE-->
 
−
[[#bfdelstr|Delete a String file]] and
 
−
[[#bfsavestr|Save String file]]</i><!--ENDCODE-->
 
−
 
 
−
---~---~---~---~---~---~---~---~---
 

Latest revision as of 23:20, 14 November 2026

loadstr()

Function (integer). Reads a whole file into a string variable.

loadstr(filename : string, s : string)

Reads 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 when it has none) and puts its text in the variable s. The files are usually written by savestr(), but any text file in that directory will do: basis.zon reads motd and welcome this way.

Parameters

Name Type Meaning
filename string The file's name inside the strings directory: letters, digits, _ and . only, at most 512 characters.
s string A string variable of the program. It receives the text.

Returns

Integer, constants from vme/include/vme.h:

  • FILE_LOADED (1): read.
  • FILE_NOT_FOUND (0): no such file, or the name has other characters (also
 written to the zone's log).
  • FILE_TO_LARGE (-1): the file is over 1,000,000 bytes.
  • FILE_OUT_OF_MEMORY (2).

It fails when either argument is a failed value.

Notes

  • Test the result. When the file is missing or empty, s keeps the value it had
 before the call.
  • The second argument must be a plain string variable. vmc also takes a constant or a
 field such as u.title, but when the file is read, the server then writes into
 memory that isn't a variable and crashes.
  • 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 (loadstr)" to the
 zone's log and the program stops.
 turn \n written in it into new lines.
  • Disk access is slow; keep the files small and read them only when needed.

Example

dilbegin show_news(pc : unitptr);
var
   err : integer;
   buf : string;
code
{
   err := loadstr("news", buf);
   if (err == FILE_LOADED)
      pagestring(buf, pc);
   else
      sendtext("There is no news.<br/>", pc);
   quit;
}
dilend

Shows the news file to a player, page by page.

See also