root/trunk/ddl/DDLWriter.d

Revision 279, 3.6 kB (checked in by pragma, 2 years ago)

Dirty Update Please use r278 for something stable.

Line 
1 /+
2     Copyright (c) 2006 Eric Anderton
3         
4     Permission is hereby granted, free of charge, to any person
5     obtaining a copy of this software and associated documentation
6     files (the "Software"), to deal in the Software without
7     restriction, including without limitation the rights to use,
8     copy, modify, merge, publish, distribute, sublicense, and/or
9     sell copies of the Software, and to permit persons to whom the
10     Software is furnished to do so, subject to the following
11     conditions:
12
13     The above copyright notice and this permission notice shall be
14     included in all copies or substantial portions of the Software.
15
16     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17     EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18     OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20     HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21     WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23     OTHER DEALINGS IN THE SOFTWARE.
24 +/
25 /**
26     Provides Tango binary Writer support, with a few enhancements
27     
28     Authors: Eric Anderton
29     License: BSD Derivative (see source for details)
30     Copyright: 2006 Eric Anderton
31 */
32 module ddl.DDLWriter;
33
34 private import ddl.Utils;
35 private import ddl.FileBuffer;
36
37 enum Anchor{
38     None,
39     Begin,
40     End,
41     Current
42 }
43
44 /**
45     Tango IO Writer subclass.
46     
47     DDLWriter, apart from adding symmetry to DDLReader, provides seeking capability for
48     random-access <i>output</i>.
49 */
50 public class DDLWriter{
51     ubyte[] data;
52     uint position;
53    
54     /**
55         IBuffer style constructor.
56         
57         Params:
58             buffer = buffer to read
59     */
60     public this (){
61         position = 0;
62     }
63    
64     public this (ubyte[] data){
65         this.data = data;
66         position = data.length;
67     }   
68    
69     public this(FileBuffer buffer){
70         this.data = buffer.data;
71         position = 0;
72     }   
73    
74     DDLWriter putAll(void[] x){
75         ubyte[] newData = cast(ubyte[])x;
76         if(position == data.length){
77             data ~= newData;
78             position = data.length;
79         }
80         else{
81             data[position..position+newData.length] = newData;
82         }
83         return this;
84     }
85    
86     DDLWriter put(T)(T x){
87         ubyte[] newData = (cast(ubyte*)(cast(void*)&x))[0..T.sizeof];
88         if(position == data.length){
89             data ~= newData;
90             position = data.length;
91         }
92         else{
93             data[position..position+newData.length] = newData;
94             position += newData.length;
95         }
96         return this;
97     }
98        
99     /**
100         Perform a seek relative to the current buffer position and status using the conduit.
101         
102         Some Tango conduits are seekable directly.  For all others, this method seeks by
103         manipulating the buffer and the current read position instead.
104         
105         <b>NOTE:</b> this will clear out the current buffer, which may have some performance
106         implications.
107         
108         Params:
109             offset = the number of bytes to seek from the specified anchor
110             anchor = (default: Begin) specifies the point in the conduit from where seeks are
111             calculated (see: ISeekable.SeekAnchor for more info).
112     */
113     DDLWriter seek(ulong offset, Anchor anchor = Anchor.Begin){
114         switch(anchor){
115         case Anchor.Begin:
116             assert(offset < data.length);
117             position = offset;
118             break;
119         case Anchor.End:
120             assert(data.length + offset < data.length);
121             position = data.length + offset;
122             break;
123         default:
124         case Anchor.None:
125         case Anchor.Current:
126             assert(position + offset < data.length);
127             position += offset;
128             break;
129         }
130         return this;
131     }
132    
133     /**
134         Returns: The position relative to the current buffer position.
135     */
136     size_t getPosition(){
137         return position;
138     }
139    
140     ubyte[] getData(){
141         return data;
142     }
143 }
Note: See TracBrowser for help on using the browser.