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)
 
Line 1: Line 1:
−
<span id="bfloadstr"></span>
+
= loadstr() =
 +
'''Function''' (integer). Reads a whole file into a string variable.
  
−
  '''Function:'''
+
  loadstr(filename : string, s : string)
−
<i><!--CODE-->integer loadstr( filename : string , buff : string );</i><!--ENDCODE-->
 
  
−
<!--TERM--> '''filename'''
+
Reads the file <code>strings/filename</code> under the zone's DIL file directory (the last
−
<!--DEFINITION-->         The name of the string file to be loaded
+
column of the zone's line in <code>vme/etc/zonelist</code>, or <code>dil_file_dir</code> from
−
<!--TERM--> '''buff'''
+
<code>server.cfg</code> when it has none) and puts its text in the variable <code>s</code>. The
−
<!--DEFINITION-->         The string that you wish to read the file contents into
+
files are usually written by [[Manual:DIL Manual/savestr()|savestr()]], but any text file in
−
<!--TERM--> '''Return'''
+
that directory will do: basis.zon reads <code>motd</code> and <code>welcome</code> this way.
−
<!--DEFINITION-->         <i><!--CODE-->FILE_LOADED, FILE_NOT_FOUND, FILE_OUT_OF_MEMORY,or FILE_TO_LARGE</i><!--ENDCODE-->
 
  
−
Loadstr is used to load strings from disk that were saved either by savestr
+
== Parameters ==
−
or any text editor.
+
{| class="wikitable"
−
The 'loadstr' is perfect for operations such as
+
! Name !! Type !! Meaning
−
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,
+
| filename || string || The file's name inside the <code>strings</code> directory: letters, digits, <code>_</code> and <code>.</code> only, at most 512 characters.
−
Dil based teachers, message boards, mail system, news command., zone or
+
|-
−
room based help,  competition boards, and much much more.
+
| s || string || A string variable of the program. It receives the text.
−
'''Disk access is always slow'''.
+
|}
−
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_load ();
+
== Returns ==
−
var
+
Integer, constants from <code>vme/include/vme.h</code>:
−
        ret:integer;/*to hold the return value if loaded or not*/
+
* <code>FILE_LOADED</code> (1): read.
−
        buff:string;/*to hold the loaded string*/
+
* <code>FILE_NOT_FOUND</code> (0): no such file, or the name has other characters (also
−
code
+
  written to the zone's log).
−
dilend
+
* <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.
−
 
 
−
</i><!--ENDCODE-->
 
−
'''See Also''' <i><!--CODE-->
 
−
[[#bfdelstr|Delete a String file]] and
 
−
[[#bfsavestr|Save String file]]</i><!--ENDCODE-->
 
−
 
 
−
---~---~---~---~---~---~---~---~---
 
−
<span id="bfloadstr"></span>
 
−
 
 
−
'''Function:'''
 
−
<i><!--CODE-->integer loadstr( filename : string , buff : string );</i><!--ENDCODE-->
 
−
 
 
−
<!--TERM-->  '''filename'''
 
−
<!--DEFINITION-->         The name of the string file to be loaded
 
−
<!--TERM--> '''buff'''
 
−
<!--DEFINITION-->        The string that you wish to read the file contents into
 
−
<!--TERM-->  '''Return'''
 
−
<!--DEFINITION-->        <i><!--CODE-->FILE_LOADED, FILE_NOT_FOUND, FILE_OUT_OF_MEMORY,or FILE_TO_LARGE</i><!--ENDCODE-->
 
  
−
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;
 +
code
 +
{
 +
  err := loadstr("news", buf);
 +
  if (err == FILE_LOADED)
 +
      pagestring(buf, pc);
 +
  else
 +
      sendtext("There is no news.<br/>", pc);
 +
  quit;
 +
}
 
dilend
 
dilend
 +
</pre>
 +
Shows the news file to a player, page by page.
  
−
---~---~---~---~---~---~---~---~---
+
== See also ==
−
 
+
* [[Manual:DIL Manual/savestr()|savestr()]]
−
</i><!--ENDCODE-->
+
* [[Manual:DIL Manual/delstr()|delstr()]]
−
'''See Also''' <i><!--CODE-->
+
* [[Manual:DIL Manual/textformat()|textformat()]]
−
[[#bfdelstr|Delete a String file]] and
+
* [[Manual:DIL Manual/pagestring()|pagestring()]]
−
[[#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