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

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="bfstrdir"></span>
+
= strdir() =
 +
'''Function''' (stringlist). Lists the string files that DIL has saved, by a pattern.
  
−
  '''Function:'''  <i><!--CODE-->stringlist strdir( match : string ) ;</i><!--ENDCODE-->
+
  strdir(pattern : string)
  
−
<!--TERM--> '''match'''
+
Returns the names of the files in the <code>strings</code> directory that match <code>pattern</code>: the files <code>savestr()</code> writes and <code>loadstr()</code> and <code>delstr()</code> read. Competition.zon, for instance, uses <code>strdir(".*.comp")</code> to find its saved competitions at boot.
−
<!--DEFINITION-->         The wild card file you want to match or '*' for all.
 
−
<!--TERM--> '''return'''
 
−
<!--DEFINITION-->         a Stringlist with all the filenames that match the 'match' argument.
 
  
−
The 'match' argument uses the same wild cards as the Linux 'ls' command
+
== Parameters ==
−
so the following will work.
+
{| class="wikitable"
 +
! Name !! Type !! Meaning
 +
|-
 +
| pattern || string || A regular expression that must match the ''whole'' file name. "" means all files.
 +
|}
  
−
<!--TERM-->  '''&ast;'''
+
== Returns ==
−
<!--DEFINITION-->        Match any character or group of characters
+
Stringlist: the matching file names, in no particular order (the order the directory gives them). Empty when nothing matches or the directory doesn't exist. It fails if <code>pattern</code> fails or is a string variable that has never had a value; assigning a failed list leaves the variable as it was.
−
<!--TERM-->  '''&quest;'''
 
−
<!--DEFINITION-->         Match one of any character
 
−
<!--TERM--> '''[...]'''
 
−
<!--DEFINITION-->        Match one of a set of characters
 
  
−
'''Example:'''
+
== Notes ==
−
<i><!--CODE-->
+
* The pattern is a regular expression (Perl style), not a shell wildcard: <code>".*"</code> is everything, <code>"dead[.].*"</code> is every name starting with "dead.", and a plain <code>.</code> matches any character. A shell-style <code>"*"</code> is not a valid expression.
−
---~---~---~---~---~---~---~---~---
+
* If the pattern is not a valid expression, the program stops with a type error in <code>vme.log</code> (unlike <code>unitdir()</code>, which just fails).
 +
* The directory is <code>strings/</code> under the zone's DIL file directory: the sixth column of the zone's line in <code>vme/etc/zonelist</code>, or else <code>dil_file_dir</code> in <code>server.cfg</code>. The zone is that of the program the unit runs; a function from another zone that it calls (<code>f@otherzone</code>) still looks in the calling program's directory.
 +
* Only file names are returned, without a path, ready for <code>loadstr()</code>.
  
−
"corpse*" matches:  corpse.10938 corpse.whistler corpseofwhistler ...
+
== Example ==
−
"corpse?" matches corpse1 corpses corpse5 ...
+
<pre>
−
"[abc]*" matches ability about cost back ...
+
dilbegin wanted_list(pc : unitptr);
−
"[a-z]*" about zoo man father ...
+
var
−
"start[nm]end" matches startnend startmend
+
  files : stringlist;
−
 
+
  parts : stringlist;
−
---~---~---~---~---~---~---~---~---
+
  i : integer;
−
 
+
  n : integer;
−
</i><!--ENDCODE-->
+
code
−
'''Example DIL:'''
+
{
−
<i><!--CODE-->
+
  files := strdir("dead[.].*");
−
---~---~---~---~---~---~---~---~---
+
  n := length(files);
−
 
+
  if (n == 0)
−
dilbegin wanted ();
+
  {
−
var
+
      sendtext("Nobody is wanted.<br/>", pc);
−
        wantedlist:stringlist;
+
      return;
−
        templist:stringlist;
+
  }
−
        i:integer;
+
  i := 0;
−
        ln:integer;
+
  while (i < n)
−
code
+
  {
−
dilend
+
      parts := split(files.[i], ".");
−
 
+
      sendtext("Wanted dead: " + parts.[1] + "<br/>", pc);
−
 
+
      i := i + 1;
−
---~---~---~---~---~---~---~---~---
+
  }
−
 
+
  return;
−
</i><!--ENDCODE-->
+
}
−
The previous DIL would be an example of a command to check the wanted dead
 
−
players on the VME if you saved the files with the first word being
 
−
'dead' and separated it with a '.' and the players name. For example if
 
−
'whistler' was wanted dead  the file name would be 'dead.whistler'
 
−
<span id="bfstrdir"></span>
 
−
 
 
−
'''Function:'''  <i><!--CODE-->stringlist strdir( match : string ) ;</i><!--ENDCODE-->
 
−
 
 
−
<!--TERM-->  '''match'''
 
−
<!--DEFINITION-->        The wild card file you want to match or '*' for all.
 
−
<!--TERM--> '''return'''
 
−
<!--DEFINITION-->        a Stringlist with all the filenames that match the 'match' argument.
 
−
 
 
−
The 'match' argument uses the same wild cards as the Linux 'ls' command
 
−
so the following will work.
 
−
 
 
−
<!--TERM-->  '''&ast;'''
 
−
<!--DEFINITION-->        Match any character or group of characters
 
−
<!--TERM-->  '''&quest;'''
 
−
<!--DEFINITION-->        Match one of any character
 
−
<!--TERM-->  '''[...]'''
 
−
<!--DEFINITION-->        Match one of a set of characters
 
−
 
 
−
'''Example:'''
 
−
<i><!--CODE-->
 
−
---~---~---~---~---~---~---~---~---
 
−
 
 
−
"corpse*" matches: corpse.10938 corpse.whistler corpseofwhistler ...
 
−
"corpse?" matches corpse1 corpses corpse5 ...
 
−
"[abc]*" matches ability about cost back ...
 
−
"[a-z]*" about zoo man father ...
 
−
"start[nm]end" matches startnend startmend
 
−
 
 
−
---~---~---~---~---~---~---~---~---
 
−
 
 
−
</i><!--ENDCODE-->
 
−
'''Example DIL:'''
 
−
<i><!--CODE-->
 
−
---~---~---~---~---~---~---~---~---
 
−
 
 
−
dilbegin wanted ();
 
−
var
 
−
        wantedlist:stringlist;
 
−
        templist:stringlist;
 
−
        i:integer;
 
−
        ln:integer;
 
−
code
 
 
dilend
 
dilend
 +
</pre>
 +
Lists the players for whom a file "dead.<i>name</i>" was saved with <code>savestr()</code>.
  
−
 
+
== See also ==
−
---~---~---~---~---~---~---~---~---
+
* [[Manual:DIL Manual/savestr()|savestr()]]
−
 
+
* [[Manual:DIL Manual/loadstr()|loadstr()]]
−
</i><!--ENDCODE-->
+
* [[Manual:DIL Manual/delstr()|delstr()]]
−
The previous DIL would be an example of a command to check the wanted dead
+
* [[Manual:DIL Manual/unitdir()|unitdir()]]
−
players on the VME if you saved the files with the first word being
+
* [[Manual:DIL Manual/split()|split()]]
−
'dead' and separated it with a '.' and the players name. For example if
 
−
'whistler' was wanted dead  the file name would be 'dead.whistler'
 

Latest revision as of 23:15, 14 November 2026

strdir()

Function (stringlist). Lists the string files that DIL has saved, by a pattern.

strdir(pattern : string)

Returns the names of the files in the strings directory that match pattern: the files savestr() writes and loadstr() and delstr() read. Competition.zon, for instance, uses strdir(".*.comp") to find its saved competitions at boot.

Parameters

Name Type Meaning
pattern string A regular expression that must match the whole file name. "" means all files.

Returns

Stringlist: the matching file names, in no particular order (the order the directory gives them). Empty when nothing matches or the directory doesn't exist. It fails if pattern fails or is a string variable that has never had a value; assigning a failed list leaves the variable as it was.

Notes

  • The pattern is a regular expression (Perl style), not a shell wildcard: ".*" is everything, "dead[.].*" is every name starting with "dead.", and a plain . matches any character. A shell-style "*" is not a valid expression.
  • If the pattern is not a valid expression, the program stops with a type error in vme.log (unlike unitdir(), which just fails).
  • The directory is strings/ under the zone's DIL file directory: the sixth column of the zone's line in vme/etc/zonelist, or else dil_file_dir in server.cfg. The zone is that of the program the unit runs; a function from another zone that it calls (f@otherzone) still looks in the calling program's directory.
  • Only file names are returned, without a path, ready for loadstr().

Example

dilbegin wanted_list(pc : unitptr);
var
   files : stringlist;
   parts : stringlist;
   i : integer;
   n : integer;
code
{
   files := strdir("dead[.].*");
   n := length(files);
   if (n == 0)
   {
      sendtext("Nobody is wanted.<br/>", pc);
      return;
   }
   i := 0;
   while (i < n)
   {
      parts := split(files.[i], ".");
      sendtext("Wanted dead: " + parts.[1] + "<br/>", pc);
      i := i + 1;
   }
   return;
}
dilend

Lists the players for whom a file "dead.name" was saved with savestr().

See also