Branch: Tag:

2003-01-03

2003-01-03 16:49:15 by Martin Nilsson <mani@lysator.liu.se>

Another struct implementation.

Rev: lib/modules/ADT.pmod/Struct.pike:1.1

1362:   <dt class='head--doc'>Throws</dt>   <dd class='body--doc'><p>Throws an error if called on an empty stack.</p>   </dd></dl> - </dd></dl><dl><dt><h2 class='header'>Class <b class='ms datatype'>ADT.struct</b></h2> + </dd></dl><dl><dt><h2 class='header'>Class <b class='ms datatype'>ADT.Struct</b></h2>   </dt><dd><dl class='group--doc'>   <dt class='head--doc'>Description</dt> -  + <dd class='body--doc'><p>Implements a struct which can be used for serialization and +  deserialization of data.</p> + </dd> + <dt class='head--doc'>Example</dt> + <dd class='example'><pre><p>class ID3 { +  inherit ADT.Struct; +  Item head = Chars(3); +  Item title = Chars(30); +  Item artist = Chars(30); +  Item album = Chars(30); +  Item year = Chars(4); +  Item genre = Byte(); +  Item Comment = Chars(30); +  }</p> + <p> Stdio.File f = Stdio.File("foo.mp3"); +  f-&gt;seek(-128); +  ADT.Struct tag = ID3(f); +  if(tag-&gt;head=="TAG") { +  write("Title: %s\n", tag-&gt;title); +  tag-&gt;title = "A new title" + "\0"*19; +  f-&gt;seek(-128); +  f-&gt;write( (string)tag ); +  }</p> + </pre></dd></dl> +  + <hr /> + <dl class='group--doc'> + <dt class='head--type'><span class='homogen--type'>Method</span> + <span class='homogen--name'><b>_sizeof</b></span> + </dt> + <dd><p><code><code class='datatype'>int</code> <b><span class='method'>sizeof</span>(</b> <span class='class'>ADT.Struct</span> <span class='argument'>arg</span> <b>)</b></code></p></dd> +  + <dt class='head--doc'>Description</dt> + <dd class='body--doc'><p>The size of the struct object is the number of bytes +  allocated for the struct.</p> + </dd></dl> +  +  + <hr /> + <dl class='group--doc'> + <dt class='head--type'><span class='homogen--type'>Method</span> + <span class='homogen--name'><b>cast</b></span> + </dt> + <dd><p><code><b>(</b><span class='datatype'>int</span><b>)</b><span class='class'>ADT.Struct</span>()<br><b>(</b><span class='datatype'>float</span><b>)</b><span class='class'>ADT.Struct</span>()<br><b>(</b><span class='datatype'>string</span><b>)</b><span class='class'>ADT.Struct</span>()<br><b>(</b><span class='datatype'>array</span><b>)</b><span class='class'>ADT.Struct</span>()<br><b>(</b><span class='datatype'>mapping</span><b>)</b><span class='class'>ADT.Struct</span>()<br><b>(</b><span class='datatype'>multiset</span><b>)</b><span class='class'>ADT.Struct</span>()</code></p></dd> +  + <dt class='head--doc'>Description</dt> + <dd class='body--doc'><p>The struct can be casted into a string, which is eqivivalent +  to running <code>encode</code>, or into an array. When casted into an +  array each array element is the encoded value of that struct +  item.</p> + </dd></dl> +  +  + <hr /> + <dl class='group--doc'> + <dt class='head--type'><span class='homogen--type'>Method</span> + <span class='homogen--name'><b>create</b></span> + </dt> + <dd><p><code><span class='object'>ADT.Struct</span> <span class='class'>ADT.Struct</span><b>(</b><code class='datatype'>void</code>|<code class='datatype'>string</code>|<code class='object unresolved'>Stdio.File</code> <code class='argument'>data</code><b>)</b></code></p></dd> +  + <dt class='head--doc'><span id='p-data'></span>Parameter <code class='parameter'>data</code></dt> + <dd></dd><dd class='body--doc'><p>Data to be decoded and populate the struct. Can +  either be a file object or a string.</p> + </dd></dl> +  +  + <hr /> + <dl class='group--doc'> + <dt class='head--type'><span class='homogen--type'>Method</span> + <span class='homogen--name'><b>decode</b></span> + </dt> + <dd><p><code><code class='datatype'>void</code> <b><span class='method'>decode</span>(</b><code class='datatype'>string</code>|<code class='object unresolved'>Stdio.File</code> <code class='argument'>data</code><b>)</b></code></p></dd> +  + <dt class='head--doc'>Description</dt> + <dd class='body--doc'><p>Decodes <code>data</code> according to the struct and populates +  the struct variables. The <code>data</code> can either be a file +  object or a string.</p> + </dd></dl> +  +  + <hr /> + <dl class='group--doc'> + <dt class='head--type'><span class='homogen--type'>Method</span> + <span class='homogen--name'><b>encode</b></span> + </dt> + <dd><p><code><code class='datatype'>string</code> <b><span class='method'>encode</span>(</b><b>)</b></code></p></dd> +  + <dt class='head--doc'>Description</dt> + <dd class='body--doc'><p>Serializes the struct into a string. This string is equal +  to the string fed to <code>decode</code> if nothing in the struct +  has been altered.</p> + </dd></dl> + <dl><dt><h2 class='header'>Class <b class='ms datatype'>ADT.Struct.Byte</b></h2> + </dt><dd><dl class='group--doc'> + <dt class='head--doc'>Description</dt> + <dd class='body--doc'><p>One byte, integer value between 0 and 255.</p> + </dd></dl> +  + <hr /> + <dl class='group--doc'> + <dt class='head--type'><span class='homogen--type'>Inherit</span> + <span class='homogen--name'><b>Item</b></span> + </dt> + <dd><p><code><span class='datatype'>inherit Item</span> : <span class='inherit'>Item</span></code></p></dd> + </dl> + </dd></dl><dl><dt><h2 class='header'>Class <b class='ms datatype'>ADT.Struct.Chars</b></h2> + </dt><dd><dl class='group--doc'> + <dt class='head--doc'>Description</dt> + <dd class='body--doc'><p>A string of bytes.</p> + </dd></dl> +  + <hr /> + <dl class='group--doc'> + <dt class='head--type'><span class='homogen--type'>Method</span> + <span class='homogen--name'><b>create</b></span> + </dt> + <dd><p><code><span class='object'>ADT.Struct.Chars</span> <span class='class'>ADT.Struct.Chars</span><b>(</b><code class='datatype'>int</code> <code class='argument'>size</code><b>)</b></code></p></dd> +  + <dt class='head--doc'>Description</dt> + <dd class='body--doc'><p>The number of bytes that are part of this struct item.</p> + </dd></dl> +  +  + <hr /> + <dl class='group--doc'> + <dt class='head--type'><span class='homogen--type'>Inherit</span> + <span class='homogen--name'><b>Item</b></span> + </dt> + <dd><p><code><span class='datatype'>inherit Item</span> : <span class='inherit'>Item</span></code></p></dd> + </dl> + </dd></dl><dl><dt><h2 class='header'>Class <b class='ms datatype'>ADT.Struct.Item</b></h2> + </dt><dd><dl class='group--doc'> + <dt class='head--doc'>Description</dt> + <dd class='body--doc'><p>Interface class for struct items.</p> + </dd></dl></dd></dl><dl><dt><h2 class='header'>Class <b class='ms datatype'>ADT.Struct.Word</b></h2> + </dt><dd><dl class='group--doc'> + <dt class='head--doc'>Description</dt> + <dd class='body--doc'><p>One word in network order, integer value between 0 and 65535.</p> + </dd></dl> +  + <hr /> + <dl class='group--doc'> + <dt class='head--type'><span class='homogen--type'>Inherit</span> + <span class='homogen--name'><b>Byte</b></span> + </dt> + <dd><p><code><span class='datatype'>inherit Byte</span> : <span class='inherit'>Byte</span></code></p></dd> + </dl> + </dd></dl></dd></dl><dl><dt><h2 class='header'>Class <b class='ms datatype'>ADT.struct</b></h2> + </dt><dd><dl class='group--doc'> + <dt class='head--doc'>Description</dt>   <dd class='body--doc'><p>generic data structure</p>   </dd></dl>