 |
Changeset 3895
- Timestamp:
- 08/19/08 20:36:22
(3 months ago)
- Author:
- keinfarbton
- Message:
Make example compile
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r3724 |
r3895 |
|
| 6 | 6 | /****************************************************************************** |
|---|
| 7 | 7 | |
|---|
| 8 | | Simple example where the current folder is recursively scanned for .d files |
|---|
| | 8 | Simple example where the current folder is recursively scanned for .d files |
|---|
| 9 | 9 | before they are zipped into tmp.zip. |
|---|
| 10 | 10 | |
|---|
| … | … | |
| 15 | 15 | void main() |
|---|
| 16 | 16 | { |
|---|
| 17 | | char[][] files; |
|---|
| | 17 | Cutf8[] files; |
|---|
| 18 | 18 | auto root = new FileFolder ("."); |
|---|
| 19 | 19 | foreach (file; root.tree.catalog ("*.d")) |
|---|
| r3724 |
r3895 |
|
| 20 | 20 | } |
|---|
| 21 | 21 | |
|---|
| 22 | | scope scan = new FileScan; |
|---|
| 23 | | scope regex = Regex(r"\.(d|obj)$"); |
|---|
| | 22 | /+scope+/ auto scan = new FileScan; |
|---|
| | 23 | /+scope+/ auto regex = Regex(r"\.(d|obj)$"); |
|---|
| 24 | 24 | |
|---|
| 25 | 25 | scan(args[1], delegate bool (FilePath fp, bool isDir) { |
|---|
| r3887 |
r3895 |
|
| 31 | 31 | [logging/multilog.d] |
|---|
| 32 | 32 | |
|---|
| 33 | | [manual/chapterStorage.d] |
|---|
| 34 | | |
|---|
| 35 | 33 | [networking/homepage.d] |
|---|
| 36 | 34 | [networking/httpget.d] |
|---|
| r3724 |
r3895 |
|
| 2 | 2 | |
|---|
| 3 | 3 | localtime.d |
|---|
| 4 | | |
|---|
| | 4 | |
|---|
| 5 | 5 | *******************************************************************************/ |
|---|
| 6 | 6 | |
|---|
| … | … | |
| 21 | 21 | { |
|---|
| 22 | 22 | /// list of day names |
|---|
| 23 | | static char[][] days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; |
|---|
| | 23 | static Cutf8[] days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; |
|---|
| 24 | 24 | |
|---|
| 25 | 25 | /// list of month names |
|---|
| 26 | | static char[][] months = |
|---|
| | 26 | static Cutf8[] months = |
|---|
| 27 | 27 | [ |
|---|
| 28 | 28 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
|---|
| … | … | |
| 44 | 44 | months[dt.date.month-1], |
|---|
| 45 | 45 | dt.date.day, |
|---|
| 46 | | dt.time.hours, |
|---|
| | 46 | dt.time.hours, |
|---|
| 47 | 47 | dt.time.minutes, |
|---|
| 48 | 48 | dt.time.seconds, |
|---|
| r3724 |
r3895 |
|
| 15 | 15 | doc.header; |
|---|
| 16 | 16 | |
|---|
| 17 | | // attach an element with some attributes, plus |
|---|
| | 17 | // attach an element with some attributes, plus |
|---|
| 18 | 18 | // a child element with an attached data value |
|---|
| 19 | 19 | doc.root.element (null, "element") |
|---|
| … | … | |
| 66 | 66 | } |
|---|
| 67 | 67 | |
|---|
| 68 | | void result (char[] msg, double time, XmlPath!(char).NodeSet set) |
|---|
| | 68 | void result (Cutf8 msg, double time, XmlPath!(char).NodeSet set) |
|---|
| 69 | 69 | { |
|---|
| 70 | 70 | Stdout.newline.formatln("{} {}", time, msg); |
|---|
| r3724 |
r3895 |
|
| 9 | 9 | on each invocation of newline or formatln, but here we're using '\n' |
|---|
| 10 | 10 | to illustrate how to avoid flushing many individual lines |
|---|
| 11 | | |
|---|
| | 11 | |
|---|
| 12 | 12 | *******************************************************************************/ |
|---|
| 13 | 13 | |
|---|
| 14 | | void main(char[][] args) |
|---|
| 15 | | { |
|---|
| 16 | | char[] root = args.length < 2 ? "." : args[1]; |
|---|
| | 14 | void main(Cutf8[] args) |
|---|
| | 15 | { |
|---|
| | 16 | Cutf8 root = args.length < 2 ? "." : args[1]; |
|---|
| 17 | 17 | Stdout.formatln ("Scanning '{}'", root); |
|---|
| 18 | 18 | |
|---|
| r3724 |
r3895 |
|
| 13 | 13 | import tango.io.vfs.FileFolder; |
|---|
| 14 | 14 | |
|---|
| 15 | | void main(char[][] args) |
|---|
| | 15 | void main(Cutf8[] args) |
|---|
| 16 | 16 | { |
|---|
| 17 | | if (args.length < 2) |
|---|
| | 17 | if (args.length < 2) |
|---|
| 18 | 18 | { |
|---|
| 19 | 19 | Stdout("Please pass a directory to search").newline; |
|---|
| … | … | |
| 21 | 21 | } |
|---|
| 22 | 22 | |
|---|
| 23 | | scope regex = Regex(r"\.(d|obj)$"); |
|---|
| 24 | | scope scan = new FileFolder (args[1]); |
|---|
| | 23 | /+scope+/auto regex = Regex(r"\.(d|obj)$"); |
|---|
| | 24 | /+scope+/auto scan = new FileFolder (args[1]); |
|---|
| 25 | 25 | |
|---|
| 26 | 26 | auto tree = scan.tree; |
|---|
| r3724 |
r3895 |
|
| 15 | 15 | ******************************************************************************/ |
|---|
| 16 | 16 | |
|---|
| 17 | | void main(char[][] args) |
|---|
| | 17 | void main(Cutf8[] args) |
|---|
| 18 | 18 | { |
|---|
| 19 | | char[] zipname; |
|---|
| | 19 | Cutf8 zipname; |
|---|
| 20 | 20 | if (args.length == 2) |
|---|
| 21 | 21 | zipname = args[1]; |
|---|
| r3724 |
r3895 |
|
| 416 | 416 | |
|---|
| 417 | 417 | |
|---|
| 418 | | /** |
|---|
| 419 | | * |
|---|
| | 418 | /* This function is obsoleted; replaced by _d_delarray_t() |
|---|
| 420 | 419 | */ |
|---|
| 421 | 420 | extern (C) void _d_delarray(Array *p) |
|---|
| … | … | |
| 432 | 431 | } |
|---|
| 433 | 432 | |
|---|
| | 433 | /* Delete an array; ti is the element type. |
|---|
| | 434 | * Should we zero out the array when done? |
|---|
| | 435 | */ |
|---|
| | 436 | |
|---|
| | 437 | extern (C) void _d_delarray_t(Array *p, TypeInfo ti) |
|---|
| | 438 | { |
|---|
| | 439 | if (p) |
|---|
| | 440 | { |
|---|
| | 441 | assert(!p.length || p.data); |
|---|
| | 442 | if (p.data) |
|---|
| | 443 | { if (ti) |
|---|
| | 444 | { // Call destructors on all the sub-objects |
|---|
| | 445 | auto sz = ti.tsize(); |
|---|
| | 446 | auto pe = p.data; |
|---|
| | 447 | auto pend = pe + p.length * sz; |
|---|
| | 448 | while (pe != pend) |
|---|
| | 449 | { pend -= sz; |
|---|
| | 450 | ti.destroy(pend); |
|---|
| | 451 | } |
|---|
| | 452 | } |
|---|
| | 453 | gc_free(p.data); |
|---|
| | 454 | } |
|---|
| | 455 | p.data = null; |
|---|
| | 456 | p.length = 0; |
|---|
| | 457 | } |
|---|
| | 458 | } |
|---|
| | 459 | |
|---|
| 434 | 460 | |
|---|
| 435 | 461 | /** |
|---|
| … | … | |
| 445 | 471 | } |
|---|
| 446 | 472 | |
|---|
| | 473 | |
|---|
| | 474 | void new_finalizer(void *p, bool dummy) |
|---|
| | 475 | { |
|---|
| | 476 | //printf("new_finalizer(p = %p)\n", p); |
|---|
| | 477 | _d_callfinalizer(p); |
|---|
| | 478 | } |
|---|
| 447 | 479 | |
|---|
| 448 | 480 | /** |
|---|
| r3890 |
r3895 |
|
| 24 | 24 | CC=gcc |
|---|
| 25 | 25 | LC=$(AR) -qsv |
|---|
| 26 | | DC=~/dmd-2.015/dmd/bin/dmd |
|---|
| | 26 | DC=~/tango-d2/dmd/dmd/bin/dmd |
|---|
| 27 | 27 | |
|---|
| 28 | 28 | ADD_CFLAGS=-m32 |
|---|
| r745 |
r3895 |
|
| 59 | 59 | * Tests the bit. |
|---|
| 60 | 60 | */ |
|---|
| 61 | | int bt( uint* p, uint bitnum ); |
|---|
| | 61 | int bt( in uint* p, uint bitnum ); |
|---|
| 62 | 62 | |
|---|
| 63 | 63 | |
|---|
| r3888 |
r3895 |
|
| 102 | 102 | { |
|---|
| 103 | 103 | private UnicodeBom!(T) bom; |
|---|
| 104 | | private char[] path_; |
|---|
| | 104 | private Cutf8 path_; |
|---|
| 105 | 105 | |
|---|
| 106 | 106 | /*********************************************************************** |
|---|
| … | … | |
| 112 | 112 | ***********************************************************************/ |
|---|
| 113 | 113 | |
|---|
| 114 | | this (char[] path, Encoding encoding) |
|---|
| | 114 | this (Cutf8 path, Encoding encoding) |
|---|
| 115 | 115 | { |
|---|
| 116 | 116 | bom = new UnicodeBom!(T)(encoding); |
|---|
| … | … | |
| 139 | 139 | ***********************************************************************/ |
|---|
| 140 | 140 | |
|---|
| 141 | | static UnicodeFile opCall (char[] name, Encoding encoding) |
|---|
| | 141 | static UnicodeFile opCall (Cutf8 name, Encoding encoding) |
|---|
| 142 | 142 | { |
|---|
| 143 | 143 | return new UnicodeFile (name, encoding); |
|---|
| … | … | |
| 154 | 154 | return new FilePath (path_); |
|---|
| 155 | 155 | } |
|---|
| 156 | | |
|---|
| | 156 | |
|---|
| 157 | 157 | /*********************************************************************** |
|---|
| 158 | 158 | |
|---|
| … | … | |
| 161 | 161 | ***********************************************************************/ |
|---|
| 162 | 162 | |
|---|
| 163 | | char[] toString () |
|---|
| | 163 | override Cutf8 toString () |
|---|
| 164 | 164 | { |
|---|
| 165 | 165 | return path_; |
|---|
| … | … | |
| 214 | 214 | ***********************************************************************/ |
|---|
| 215 | 215 | |
|---|
| 216 | | UnicodeFile write (T[] content, bool writeBom = false) |
|---|
| | 216 | UnicodeFile write (const(T)[] content, bool writeBom = false) |
|---|
| 217 | 217 | { |
|---|
| 218 | 218 | return write (content, FileConduit.ReadWriteCreate, writeBom); |
|---|
| … | … | |
| 229 | 229 | ***********************************************************************/ |
|---|
| 230 | 230 | |
|---|
| 231 | | UnicodeFile append (T[] content) |
|---|
| | 231 | UnicodeFile append (const(T)[] content) |
|---|
| 232 | 232 | { |
|---|
| 233 | 233 | return write (content, FileConduit.WriteAppending, false); |
|---|
| … | … | |
| 242 | 242 | ***********************************************************************/ |
|---|
| 243 | 243 | |
|---|
| 244 | | private final UnicodeFile write (T[] content, FileConduit.Style style, bool writeBom) |
|---|
| | 244 | private final UnicodeFile write (const(T)[] content, FileConduit.Style style, bool writeBom) |
|---|
| 245 | 245 | { |
|---|
| 246 | 246 | // convert to external representation (may throw an exeption) |
|---|
| r3894 |
r3895 |
|
| 966 | 966 | ******************************************************************************/ |
|---|
| 967 | 967 | |
|---|
| 968 | | QuoteFruct!(T) quotes(T) (T[] src, T[] set) |
|---|
| | 968 | QuoteFruct!(T) quotes(T) (const(T)[] src, const(T)[] set) |
|---|
| 969 | 969 | { |
|---|
| 970 | 970 | QuoteFruct!(T) quotes; |
|---|
| … | … | |
| 1402 | 1402 | private struct QuoteFruct(T) |
|---|
| 1403 | 1403 | { |
|---|
| 1404 | | private T[] src; |
|---|
| 1405 | | private T[] set; |
|---|
| 1406 | | |
|---|
| 1407 | | int opApply (int delegate (ref T[] token) dg) |
|---|
| | 1404 | private const(T)[] src; |
|---|
| | 1405 | private const(T)[] set; |
|---|
| | 1406 | |
|---|
| | 1407 | int opApply (int delegate (ref const(T)[] token) dg) |
|---|
| 1408 | 1408 | { |
|---|
| 1409 | 1409 | int ret, |
|---|
| 1410 | 1410 | mark; |
|---|
| 1411 | | T[] token; |
|---|
| | 1411 | const(T)[] token; |
|---|
| 1412 | 1412 | |
|---|
| 1413 | 1413 | if (set.length) |
|---|
| r3724 |
r3895 |
|
| 168 | 168 | ***********************************************************************/ |
|---|
| 169 | 169 | |
|---|
| 170 | | final void[] encode (T[] content, void[] dst=null, uint* ate=null) |
|---|
| | 170 | final void[] encode (const(T)[] content, void[] dst=null, uint* ate=null) |
|---|
| 171 | 171 | { |
|---|
| 172 | 172 | if (settings.test) |
|---|
| … | … | |
| 254 | 254 | ***********************************************************************/ |
|---|
| 255 | 255 | |
|---|
| 256 | | static void[] from (T[] x, uint type, void[] dst=null, uint* ate=null) |
|---|
| | 256 | static void[] from (const(T)[] x, uint type, void[] dst=null, uint* ate=null) |
|---|
| 257 | 257 | { |
|---|
| 258 | 258 | void[] ret; |
|---|
| … | … | |
| 261 | 261 | { |
|---|
| 262 | 262 | if (type == Utf8) |
|---|
| 263 | | return x; |
|---|
| | 263 | return x.dup; |
|---|
| 264 | 264 | |
|---|
| 265 | 265 | if (type == Utf16) |
|---|
| … | … | |
| 273 | 273 | { |
|---|
| 274 | 274 | if (type == Utf16) |
|---|
| 275 | | return x; |
|---|
| | 275 | return x.dup; |
|---|
| 276 | 276 | |
|---|
| 277 | 277 | if (type == Utf8) |
|---|
| … | … | |
| 285 | 285 | { |
|---|
| 286 | 286 | if (type == Utf32) |
|---|
| 287 | | return x; |
|---|
| | 287 | return x.dup; |
|---|
| 288 | 288 | |
|---|
| 289 | 289 | if (type == Utf8) |
|---|
| … | … | |
| 313 | 313 | int type; // type of element (char/wchar/dchar) |
|---|
| 314 | 314 | Encoding encoding; // Encoding.xx encoding |
|---|
| 315 | | char[] bom; // pattern to match for signature |
|---|
| | 315 | Cutf8 bom; // pattern to match for signature |
|---|
| 316 | 316 | bool test, // should we test for this encoding? |
|---|
| 317 | 317 | endian, // this encoding have endian concerns? |
|---|
| … | … | |
| 326 | 326 | {Utf8, Encoding.Unknown, null, true, false, false, Encoding.UTF_8N}, |
|---|
| 327 | 327 | {Utf8, Encoding.UTF_8, null, true, false, false, Encoding.UTF_8N}, |
|---|
| 328 | | {Utf8, Encoding.UTF_8N, cast(char[])x"efbbbf", false}, |
|---|
| | 328 | {Utf8, Encoding.UTF_8N, cast(Cutf8)x"efbbbf", false}, |
|---|
| 329 | 329 | {Utf16, Encoding.UTF_16, null, true, false, false, Encoding.UTF_16BE}, |
|---|
| 330 | | {Utf16, Encoding.UTF_16BE, cast(char[])x"feff", false, true, true}, |
|---|
| 331 | | {Utf16, Encoding.UTF_16LE, cast(char[])x"fffe", false, true}, |
|---|
| | 330 | {Utf16, Encoding.UTF_16BE, cast(Cutf8)x"feff", false, true, true}, |
|---|
| | 331 | {Utf16, Encoding.UTF_16LE, cast(Cutf8)x"fffe", false, true}, |
|---|
| 332 | 332 | {Utf32, Encoding.UTF_32, null, true, false, false, Encoding.UTF_32BE}, |
|---|
| 333 | | {Utf32, Encoding.UTF_32BE, cast(char[])x"0000feff", false, true, true}, |
|---|
| 334 | | {Utf32, Encoding.UTF_32LE, cast(char[])x"fffe0000", false, true}, |
|---|
| | 333 | {Utf32, Encoding.UTF_32BE, cast(Cutf8)x"0000feff", false, true, true}, |
|---|
| | 334 | {Utf32, Encoding.UTF_32LE, cast(Cutf8)x"fffe0000", false, true}, |
|---|
| 335 | 335 | ]; |
|---|
| 336 | 336 | |
|---|
| r3887 |
r3895 |
|
| 5 | 5 | License: BSD style: $(LICENSE) |
|---|
| 6 | 6 | |
|---|
| 7 | | version: Initial release: March 2008 |
|---|
| | 7 | version: Initial release: March 2008 |
|---|
| 8 | 8 | |
|---|
| 9 | 9 | Authors: Kris |
|---|
| … | … | |
| 17 | 17 | /******************************************************************************* |
|---|
| 18 | 18 | |
|---|
| 19 | | Simple Document printer, with support for serialization caching |
|---|
| | 19 | Simple Document printer, with support for serialization caching |
|---|
| 20 | 20 | where the latter avoids having to generate unchanged sub-trees |
|---|
| 21 | 21 | |
|---|
| … | … | |
| 25 | 25 | { |
|---|
| 26 | 26 | /*********************************************************************** |
|---|
| 27 | | |
|---|
| | 27 | |
|---|
| 28 | 28 | Generate a text representation of the document tree |
|---|
| 29 | 29 | |
|---|
| 30 | 30 | ***********************************************************************/ |
|---|
| 31 | | |
|---|
| 32 | | final T[] print (Doc doc) |
|---|
| 33 | | { |
|---|
| 34 | | T[] content; |
|---|
| 35 | 31 | |
|---|
| 36 | | print (doc.root, (T[][] s...){foreach(t; s) content ~= t;}); |
|---|
| | 32 | final const(T)[] print (Doc doc) |
|---|
| | 33 | { |
|---|
| | 34 | const(T)[] content; |
|---|
| | 35 | |
|---|
| | 36 | print (doc.root, (const(T)[][] s...){foreach(t; s) content ~= t;}); |
|---|
| 37 | 37 | return content; |
|---|
| 38 | 38 | } |
|---|
| 39 | | |
|---|
| | 39 | |
|---|
| 40 | 40 | /*********************************************************************** |
|---|
| 41 | | |
|---|
| 42 | | Generate a representation of the given node-subtree |
|---|
| | 41 | |
|---|
| | 42 | Generate a representation of the given node-subtree |
|---|
| 43 | 43 | |
|---|
| 44 | 44 | ***********************************************************************/ |
|---|
| 45 | | |
|---|
| 46 | | final void print (Node root, void delegate(T[][]...) emit) |
|---|
| | 45 | |
|---|
| | 46 | final void print (Node root, void delegate(const(T)[][]...) emit) |
|---|
| 47 | 47 | { |
|---|
| 48 | 48 | T[256] tmp; |
|---|
| … | … | |
| 50 | 50 | |
|---|
| 51 | 51 | // ignore whitespace from mixed-model values |
|---|
| 52 | | T[] rawValue (Node node) |
|---|
| | 52 | const(T)[] rawValue (Node node) |
|---|
| 53 | 53 | { |
|---|
| 54 | 54 | foreach (c; node.rawValue) |
|---|
| … | … | |
| 78 | 78 | printNode (n, indent); |
|---|
| 79 | 79 | break; |
|---|
| 80 | | |
|---|
| | 80 | |
|---|
| 81 | 81 | case XmlNodeType.Element: |
|---|
| 82 | 82 | emit ("\r\n", spaces[0..indent], "<", node.name(tmp)); |
|---|
| 83 | 83 | foreach (attr; node.attributes) |
|---|
| 84 | | emit (` `, attr.name(tmp), `="`, attr.rawValue, `"`); |
|---|
| | 84 | emit (` `, attr.name(tmp), `="`, attr.rawValue, `"`); |
|---|
| 85 | 85 | |
|---|
| 86 | 86 | auto value = rawValue (node); |
|---|
| … | … | |
| 92 | 92 | foreach (child; node.children) |
|---|
| 93 | 93 | printNode (child, indent + 2); |
|---|
| 94 | | |
|---|
| | 94 | |
|---|
| 95 | 95 | // inhibit newline if we're closing Data |
|---|
| 96 | 96 | if (node.lastChild_.type != XmlNodeType.Data) |
|---|
| … | … | |
| 98 | 98 | emit ("</", node.name(tmp), ">"); |
|---|
| 99 | 99 | } |
|---|
| 100 | | else |
|---|
| | 100 | else |
|---|
| 101 | 101 | if (value.length) |
|---|
| 102 | 102 | emit (">", value, "</", node.name(tmp), ">"); |
|---|
| 103 | 103 | else |
|---|
| 104 | | emit ("/>"); |
|---|
| | 104 | emit ("/>"); |
|---|
| 105 | 105 | break; |
|---|
| 106 | | |
|---|
| | 106 | |
|---|
| 107 | 107 | // ingore whitespace data in mixed-model |
|---|
| 108 | 108 | // <foo> |
|---|
| … | … | |
| 115 | 115 | emit (node.rawValue); |
|---|
| 116 | 116 | break; |
|---|
| 117 | | |
|---|
| | 117 | |
|---|
| 118 | 118 | case XmlNodeType.Comment: |
|---|
| 119 | 119 | emit ("<!--", node.rawValue, "-->"); |
|---|
| 120 | 120 | break; |
|---|
| 121 | | |
|---|
| | 121 | |
|---|
| 122 | 122 | case XmlNodeType.PI: |
|---|
| 123 | 123 | emit ("<?", node.rawValue, "?>"); |
|---|
| 124 | 124 | break; |
|---|
| 125 | | |
|---|
| | 125 | |
|---|
| 126 | 126 | case XmlNodeType.CData: |
|---|
| 127 | 127 | emit ("<![CDATA[", node.rawValue, "]]>"); |
|---|
| 128 | 128 | break; |
|---|
| 129 | | |
|---|
| | 129 | |
|---|
| 130 | 130 | case XmlNodeType.Doctype: |
|---|
| 131 | 131 | emit ("<!DOCTYPE ", node.rawValue, ">"); |
|---|
| … | … | |
| 137 | 137 | } |
|---|
| 138 | 138 | } |
|---|
| 139 | | |
|---|
| | 139 | |
|---|
| 140 | 140 | printNode (root, 0); |
|---|
| 141 | 141 | } |
|---|
| r3887 |
r3895 |
|
| 1 | 1 | /******************************************************************************* |
|---|
| 2 | 2 | |
|---|
| 3 | | Copyright: Copyright (C) 2007 Aaron Craelius and Kris Bell |
|---|
| | 3 | Copyright: Copyright (C) 2007 Aaron Craelius and Kris Bell |
|---|
| 4 | 4 | All rights reserved. |
|---|
| 5 | 5 | |
|---|
| 6 | 6 | License: BSD style: $(LICENSE) |
|---|
| 7 | 7 | |
|---|
| 8 | | version: Initial release: February 2008 |
|---|
| | 8 | version: Initial release: February 2008 |
|---|
| 9 | 9 | |
|---|
| 10 | 10 | Authors: Aaron, Kris |
|---|
| … | … | |
| 20 | 20 | /******************************************************************************* |
|---|
| 21 | 21 | |
|---|
| 22 | | Implements a DOM atop the XML parser, supporting document |
|---|
| | 22 | Implements a DOM atop the XML parser, supporting document |
|---|
| 23 | 23 | parsing, tree traversal and ad-hoc tree manipulation. |
|---|
| 24 | 24 | |
|---|
| 25 | | The DOM API is non-conformant, yet simple and functional in |
|---|
| 26 | | style - locate a tree node of interest and operate upon or |
|---|
| 27 | | around it. In all cases you will need a document instance to |
|---|
| 28 | | begin, whereupon it may be populated either by parsing an |
|---|
| | 25 | The DOM API is non-conformant, yet simple and functional in |
|---|
| | 26 | style - locate a tree node of interest and operate upon or |
|---|
| | 27 | around it. In all cases you will need a document instance to |
|---|
| | 28 | begin, whereupon it may be populated either by parsing an |
|---|
| 29 | 29 | existing document or via API manipulation. |
|---|
| 30 | 30 | |
|---|
| … | … | |
| 41 | 41 | is not performed internally, and becomes the responsibility |
|---|
| 42 | 42 | of the client. That is, the client should perform appropriate |
|---|
| 43 | | entity transcoding as necessary. Paying the (high) transcoding |
|---|
| | 43 | entity transcoding as necessary. Paying the (high) transcoding |
|---|
| 44 | 44 | cost for all documents doesn't seem appropriate. |
|---|
| 45 | 45 | |
|---|
| … | … | |
| 62 | 62 | doc.header; |
|---|
| 63 | 63 | |
|---|
| 64 | | // attach an element with some attributes, plus |
|---|
| | 64 | // attach an element with some attributes, plus |
|---|
| 65 | 65 | // a child element with an attached data value |
|---|
| 66 | 66 | doc.root.element (null, "element") |
|---|
| … | … | |
| 77 | 77 | auto doc = new Document!(char); |
|---|
| 78 | 78 | |
|---|
| 79 | | // attach an element with some attributes, plus |
|---|
| | 79 | // attach an element with some attributes, plus |
|---|
| 80 | 80 | // a child element with an attached data value |
|---|
| 81 | 81 | doc.root.element (null, "element") |
|---|
| … | … | |
| 96 | 96 | Note that path queries are temporal - they do not retain content |
|---|
| 97 | 97 | across mulitple queries. That is, the lifetime of a query result |
|---|
| 98 | | is limited unless you explicitly copy it. For example, this will |
|---|
| | 98 | is limited unless you explicitly copy it. For example, this will |
|---|
| 99 | 99 | fail: |
|---|
| 100 | 100 | --- |
|---|
| … | … | |
| 103 | 103 | --- |
|---|
| 104 | 104 | |
|---|
| 105 | | The above will lose elements because the associated document reuses |
|---|
| | 105 | The above will lose elements because the associated document reuses |
|---|
| 106 | 106 | node space for subsequent queries. In order to retain results, do this: |
|---|
| 107 | 107 | --- |
|---|
| … | … | |
| 116 | 116 | --- |
|---|
| 117 | 117 | |
|---|
| 118 | | Typical usage tends to follow the following pattern, Where each query |
|---|
| | 118 | Typical usage tends to follow the following pattern, Where each query |
|---|
| 119 | 119 | result is processed before another is initiated: |
|---|
| 120 | 120 | --- |
|---|
| … | … | |
| 124 | 124 | } |
|---|
| 125 | 125 | --- |
|---|
| 126 | | |
|---|
| | 126 | |
|---|
| 127 | 127 | *******************************************************************************/ |
|---|
| 128 | 128 | |
|---|
| … | … | |
| 140 | 140 | |
|---|
| 141 | 141 | /*********************************************************************** |
|---|
| 142 | | |
|---|
| | 142 | |
|---|
| 143 | 143 | Construct a DOM instance. The optional parameter indicates |
|---|
| 144 | 144 | the initial number of nodes assigned to the freelist |
|---|
| … | … | |
| 159 | 159 | |
|---|
| 160 | 160 | /*********************************************************************** |
|---|
| 161 | | |
|---|
| | 161 | |
|---|
| 162 | 162 | Return an xpath handle to query this document. This starts |
|---|
| 163 | 163 | at the document root. |
|---|
| … | … | |
| 166 | 166 | |
|---|
| 167 | 167 | ***********************************************************************/ |
|---|
| 168 | | |
|---|
| | 168 | |
|---|
| 169 | 169 | final XmlPath!(T).NodeSet query () |
|---|
| 170 | 170 | { |
|---|
| … | … | |
| 173 | 173 | |
|---|
| 174 | 174 | /*********************************************************************** |
|---|
| 175 | | |
|---|
| 176 | | Reset the freelist. Subsequent allocation of document nodes |
|---|
| | 175 | |
|---|
| | 176 | Reset the freelist. Subsequent allocation of document nodes |
|---|
| 177 | 177 | will overwrite prior instances. |
|---|
| 178 | 178 | |
|---|
| 179 | 179 | ***********************************************************************/ |
|---|
| 180 | | |
|---|
| | 180 | |
|---|
| 181 | 181 | final Document reset () |
|---|
| 182 | 182 | { |
|---|
| 183 | | root.lastChild_ = |
|---|
| | 183 | root.lastChild_ = |
|---|
| 184 | 184 | root.firstChild_ = null; |
|---|
| 185 | 185 | freelists = 0; |
|---|
| … | … | |
| 194 | 194 | |
|---|
| 195 | 195 | /*********************************************************************** |
|---|
| 196 | | |
|---|
| | 196 | |
|---|
| 197 | 197 | Add an XML header to the document root |
|---|
| 198 | 198 | |
|---|
| 199 | 199 | ***********************************************************************/ |
|---|
| 200 | | |
|---|
| 201 | | final Document header (T[] encoding = null) |
|---|
| | 200 | |
|---|
| | 201 | final Document header (const(T)[] encoding = null) |
|---|
| 202 | 202 | { |
|---|
| 203 | 203 | if (encoding.length) |
|---|
| … | … | |
| 211 | 211 | |
|---|
| 212 | 212 | /*********************************************************************** |
|---|
| 213 | | |
|---|
| 214 | | Parse the given xml content, which will reuse any existing |
|---|
| | 213 | |
|---|
| | 214 | Parse the given xml content, which will reuse any existing |
|---|
| 215 | 215 | node within this document. The resultant tree is retrieved |
|---|
| 216 | 216 | via the document 'root' attribute |
|---|
| 217 | 217 | |
|---|
| 218 | 218 | ***********************************************************************/ |
|---|
| 219 | | |
|---|
| 220 | | final void parse(T[] xml) |
|---|
| | 219 | |
|---|
| | 220 | final void parse(const(T)[] xml) |
|---|
| 221 | 221 | { |
|---|
| 222 | 222 | reset; |
|---|
| … | … | |
| 225 | 225 | uint defNamespace; |
|---|
| 226 | 226 | |
|---|
| 227 | | while (true) |
|---|
| | 227 | while (true) |
|---|
| 228 | 228 | { |
|---|
| 229 | 229 | auto p = text.point; |
|---|
| 230 | | switch (super.next) |
|---|
| | 230 | switch (super.next) |
|---|
| 231 | 231 | { |
|---|
| 232 | 232 | case XmlTokenType.EndElement: |
|---|
| … | … | |
| 234 | 234 | assert (cur.parent_); |
|---|
| 235 | 235 | cur.end = text.point; |
|---|
| 236 | | cur = cur.parent_; |
|---|
| | 236 | cur = cur.parent_; |
|---|
| 237 | 237 | break; |
|---|
| 238 | | |
|---|
| | 238 | |
|---|
| 239 | 239 | case XmlTokenType.Data: |
|---|
| 240 | 240 | version (discrete) |
|---|
| … | … | |
| 254 | 254 | } |
|---|
| 255 | 255 | break; |
|---|
| 256 | | |
|---|
| | 256 | |
|---|
| 257 | 257 | case XmlTokenType.StartElement: |
|---|
| 258 | 258 | auto node = allocate; |
|---|
| … | … | |
| 262 | 262 | node.localName = super.localName; |
|---|
| 263 | 263 | node.start = p; |
|---|
| 264 | | |
|---|
| | 264 | |
|---|
| 265 | 265 | // inline append |
|---|
| 266 | | if (cur.lastChild_) |
|---|
| | 266 | if (cur.lastChild_) |
|---|
| 267 | 267 | { |
|---|
| 268 | 268 | cur.lastChild_.nextSibling_ = node; |
|---|
| … | … | |
| 270 | 270 | cur.lastChild_ = node; |
|---|
| 271 | 271 | } |
|---|
| 272 | | else |
|---|
| | 272 | else |
|---|
| 273 | 273 | { |
|---|
| 274 | 274 | cur.firstChild_ = node; |
|---|
| … | … | |
| 277 | 277 | cur = node; |
|---|
| 278 | 278 | break; |
|---|
| 279 | | |
|---|
| | 279 | |
|---|
| 280 | 280 | case XmlTokenType.Attribute: |
|---|
| 281 | 281 | auto attr = allocate; |
|---|
| … | … | |
| 286 | 286 | cur.attrib (attr); |
|---|
| 287 | 287 | break; |
|---|
| 288 | | |
|---|
| | 288 | |
|---|
| 289 | 289 | case XmlTokenType.PI: |
|---|
| 290 | 290 | cur.pi (super.rawValue, p[0..text.point-p]); |
|---|
| 291 | 291 | break; |
|---|
| 292 | | |
|---|
| | 292 | |
|---|
| 293 | 293 | case XmlTokenType.Comment: |
|---|
| 294 | 294 | cur.comment (super.rawValue); |
|---|
| 295 | 295 | break; |
|---|
| 296 | | |
|---|
| | 296 | |
|---|
| 297 | 297 | case XmlTokenType.CData: |
|---|
| 298 | 298 | cur.cdata (super.rawValue); |
|---|
| 299 | 299 | break; |
|---|
| 300 | | |
|---|
| | 300 | |
|---|
| 301 | 301 | case XmlTokenType.Doctype: |
|---|
| 302 | 302 | cur.doctype (super.rawValue); |
|---|
| 303 | 303 | break; |
|---|
| 304 | | |
|---|
| | 304 | |
|---|
| 305 | 305 | case XmlTokenType.Done: |
|---|
| 306 | 306 | return; |
|---|
| … | … | |
| 311 | 311 | } |
|---|
| 312 | 312 | } |
|---|
| 313 | | |
|---|
| | 313 | |
|---|
| 314 | 314 | /*********************************************************************** |
|---|
| 315 | | |
|---|
| | 315 | |
|---|
| 316 | 316 | allocate a node from the freelist |
|---|
| 317 | 317 | |
|---|
| … | … | |
| 327 | 327 | p.document = this; |
|---|
| 328 | 328 | p.parent_ = |
|---|
| 329 | | p.prevSibling_ = |
|---|
| 330 | | p.nextSibling_ = |
|---|
| | 329 | p.prevSibling_ = |
|---|
| | 330 | p.nextSibling_ = |
|---|
| 331 | 331 | p.firstChild_ = |
|---|
| 332 | | p.lastChild_ = |
|---|
| | 332 | p.lastChild_ = |
|---|
| 333 | 333 | p.firstAttr_ = |
|---|
| 334 | 334 | p.lastAttr_ = null; |
|---|
| … | … | |
| 338 | 338 | |
|---|
| 339 | 339 | /*********************************************************************** |
|---|
| 340 | | |
|---|
| | 340 | |
|---|
| 341 | 341 | allocate a node from the freelist |
|---|
| 342 | 342 | |
|---|
| … | … | |
| 355 | 355 | |
|---|
| 356 | 356 | /*********************************************************************** |
|---|
| 357 | | |
|---|
| 358 | | Fruct support for nodes. A fruct is a low-overhead |
|---|
| | 357 | |
|---|
| | 358 | Fruct support for nodes. A fruct is a low-overhead |
|---|
| 359 | 359 | mechanism for capturing context relating to an opApply |
|---|
| 360 | 360 | |
|---|
| 361 | 361 | ***********************************************************************/ |
|---|
| 362 | | |
|---|
| | 362 | |
|---|
| 363 | 363 | private struct Visitor |
|---|
| 364 | 364 | { |
|---|
| 365 | 365 | private Node node; |
|---|
| 366 | | |
|---|
| 367 | | /*************************************************************** |
|---|
| 368 | | |
|---|
| | 366 | |
|---|
| | 367 | /*************************************************************** |
|---|
| | 368 | |
|---|
| 369 | 369 | traverse sibling nodes |
|---|
| 370 | 370 | |
|---|
| 371 | 371 | ***************************************************************/ |
|---|
| 372 | | |
|---|
| | 372 | |
|---|
| 373 | 373 | int opApply (int delegate(inout Node) dg) |
|---|
| 374 | 374 | { |
|---|
| … | … | |
| 377 | 377 | while (cur) |
|---|
| 378 | 378 | { |
|---|
| 379 | | if ((ret = dg (cur)) != 0) |
|---|
| | 379 | |
|---|
|