Difference between revisions of "Manual:DIL Manual/loadstr()"
Jump to navigation
Jump to search
(XML import) |
(checked against the compiler and the interpreter; example compiles) |
||
| Line 1: | Line 1: | ||
| − | + | = loadstr() = | |
| + | '''Function''' (integer). Reads a whole file into a string variable. | ||
| − | + | loadstr(filename : string, s : string) | |
| − | |||
| − | + | 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. | |
| − | |||
| − | + | == Parameters == | |
| − | + | {| 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. | |
| − | + | |} | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | == Returns == | |
| − | + | Integer, constants from <code>vme/include/vme.h</code>: | |
| − | + | * <code>FILE_LOADED</code> (1): read. | |
| − | + | * <code>FILE_NOT_FOUND</code> (0): no such file, or the name has other characters (also | |
| − | + | written to the zone's log). | |
| − | + | * <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. | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | < | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | == Notes == | |
| − | + | * Test the result. When the file is missing or empty, <code>s</code> 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 <code>u.title</code>, 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 <code>vme/etc/zonelist</code>) | |
| − | + | may use it. Elsewhere it writes "attempt to violate system access security (loadstr)" to the | |
| − | + | zone's log and the program stops. | |
| − | + | * The text comes as it is in the file. Use [[Manual:DIL Manual/textformat()|textformat()]] to | |
| − | + | turn <code>\n</code> written in it into new lines. | |
| − | + | * Disk access is slow; keep the files small and read them only when needed. | |
| − | |||
| − | + | == Example == | |
| − | + | <pre> | |
| − | + | 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 | dilend | ||
| + | </pre> | ||
| + | Shows the news file to a player, page by page. | ||
| − | + | == See also == | |
| − | + | * [[Manual:DIL Manual/savestr()|savestr()]] | |
| − | + | * [[Manual:DIL Manual/delstr()|delstr()]] | |
| − | + | * [[Manual:DIL Manual/textformat()|textformat()]] | |
| − | + | * [[Manual:DIL Manual/pagestring()|pagestring()]] | |
| − | |||
| − | |||
| − | |||
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,
skeeps 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.
- The text comes as it is in the file. Use textformat() to
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.