1 /** 2 * Temple (C) Dylan Knutson, 2013, distributed under the: 3 * Boost Software License - Version 1.0 - August 17th, 2003 4 * 5 * Permission is hereby granted, free of charge, to any person or organization 6 * obtaining a copy of the software and accompanying documentation covered by 7 * this license (the "Software") to use, reproduce, display, distribute, 8 * execute, and transmit the Software, and to prepare derivative works of the 9 * Software, and to permit third-parties to whom the Software is furnished to 10 * do so, all subject to the following: 11 * 12 * The copyright notices in the Software and this entire statement, including 13 * the above license grant, this restriction and the following disclaimer, 14 * must be included in all copies of the Software, in whole or in part, and 15 * all derivative works of the Software, unless such copies or derivative 16 * works are solely in the form of machine-executable object code generated by 17 * a source language processor. 18 */ 19 20 module temple.output_stream; 21 22 version(Have_vibe_d) 23 { 24 public import vibe.core.stream : OutputStream; 25 private import vibe.core.stream : InputStream; 26 } 27 else 28 { 29 // Semi-vibe.d OutputStream 30 // Taken from vide.core.stream 31 32 interface OutputStream { 33 /** Writes an array of bytes to the stream. 34 */ 35 void write(in ubyte[] bytes); 36 37 /** Writes an array of chars to the stream. 38 */ 39 final void write(in char[] bytes) 40 { 41 write(cast(const(ubyte)[])bytes); 42 } 43 44 /** These methods provide an output range interface. 45 46 Note that these functions do not flush the output stream for performance reasons. flush() 47 needs to be called manually afterwards. 48 49 See_Also: $(LINK http://dlang.org/phobos/std_range.html#isOutputRange) 50 */ 51 final void put(ubyte elem) { write((&elem)[0 .. 1]); } 52 /// ditto 53 final void put(in ubyte[] elems) { write(elems); } 54 /// ditto 55 final void put(char elem) { write((&elem)[0 .. 1]); } 56 /// ditto 57 final void put(in char[] elems) { write(elems); } 58 /// ditto 59 final void put(dchar elem) { import std.utf; char[4] chars; encode(chars, elem); put(chars); } 60 /// ditto 61 final void put(in dchar[] elems) { foreach( ch; elems ) put(ch); } 62 } 63 } 64 65 class AppenderOutputStream : OutputStream 66 { 67 import std.array; 68 69 Appender!string accum; 70 71 void write(in ubyte[] bytes) 72 { 73 accum.put(cast(string) bytes); 74 } 75 76 string data() 77 { 78 return accum.data; 79 } 80 81 void clear() 82 { 83 if(__ctfe) 84 { 85 // Screw memory usage! We're a compiler! 86 accum = appender!string; 87 } 88 else 89 { 90 accum.clear(); 91 } 92 } 93 94 version(Have_vibe_d) 95 { 96 // Stubbed out to conform to vibed interface 97 void flush() {} 98 void finalize() {} 99 void write(InputStream stream, ulong nbytes = 0LU) 100 { 101 writeDefault(stream, nbytes); 102 } 103 } 104 }