|
| 1 | +/** |
| 2 | + * Compiler implementation of the |
| 3 | + * $(LINK2 http://www.dlang.org, D programming language). |
| 4 | + * |
| 5 | + * Copyright: Copyright (C) 1999-2018 by The D Language Foundation, All Rights Reserved |
| 6 | + * Authors: $(LINK2 http://www.digitalmars.com, Walter Bright) |
| 7 | + * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) |
| 8 | + * Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/filecache.d, _errors.d) |
| 9 | + * Documentation: https://dlang.org/phobos/dmd_filecache.html |
| 10 | + * Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/filecache.d |
| 11 | + */ |
| 12 | + |
| 13 | +module dmd.filecache; |
| 14 | + |
| 15 | +import dmd.root.stringtable; |
| 16 | +import dmd.root.array; |
| 17 | +import dmd.root.file; |
| 18 | + |
| 19 | +import core.stdc.stdio; |
| 20 | + |
| 21 | +// A simple wrapper around a string in D for extern(C++) compatibility |
| 22 | +extern(C++) struct Dstring |
| 23 | +{ |
| 24 | + char* ptr; |
| 25 | + size_t length; |
| 26 | + extern(D) string toString() |
| 27 | + { |
| 28 | + return cast(string) ptr[0 .. length]; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | +A line-by-line representation of a $(REF File, dmd, root, file). |
| 34 | +*/ |
| 35 | +class FileAndLines |
| 36 | +{ |
| 37 | + File* file; |
| 38 | + Array!Dstring* lines; |
| 39 | + |
| 40 | + /** |
| 41 | + File to read and split into its lines. |
| 42 | + */ |
| 43 | + this(const(char)* filename) |
| 44 | + { |
| 45 | + file = File.create(filename); |
| 46 | + lines = new Array!Dstring; |
| 47 | + readAndSplit(); |
| 48 | + } |
| 49 | + |
| 50 | + // Read a file and split the file buffer linewise |
| 51 | + private void readAndSplit() |
| 52 | + { |
| 53 | + file.read(); |
| 54 | + auto buf = file.buffer; |
| 55 | + // slice into lines |
| 56 | + while (*buf) |
| 57 | + { |
| 58 | + auto prevBuf = buf; |
| 59 | + for (; *buf != '\n'; buf++) |
| 60 | + { |
| 61 | + if (!*buf) |
| 62 | + break; |
| 63 | + } |
| 64 | + if (buf != prevBuf) |
| 65 | + lines.push(Dstring(cast(char*) prevBuf, buf - prevBuf)); |
| 66 | + buf++; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + void destroy() |
| 71 | + { |
| 72 | + if (file) |
| 73 | + { |
| 74 | + import core.memory : GC; |
| 75 | + file.__dtor(); |
| 76 | + GC.free(file); |
| 77 | + file = null; |
| 78 | + lines.__dtor(); |
| 79 | + GC.free(lines); |
| 80 | + lines = null; |
| 81 | + } |
| 82 | + } |
| 83 | + ~this() |
| 84 | + { |
| 85 | + destroy(); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | +A simple file cache that can be used to avoid reading the same file multiple times. |
| 91 | +It stores its cached files as $(LREF FileAndLines) |
| 92 | +*/ |
| 93 | +struct FileCache |
| 94 | +{ |
| 95 | + private StringTable files; |
| 96 | + |
| 97 | + /** |
| 98 | + Add or get a file from the file cache. |
| 99 | + If the file isn't part of the cache, it will be read from the filesystem. |
| 100 | + If the file has been read before, the cached file object will be returned |
| 101 | +
|
| 102 | + Params: |
| 103 | + file = file to load in (or get from) the cache |
| 104 | +
|
| 105 | + Returns: a $(LREF FileAndLines) object containing a line-by-line representation of the requested file |
| 106 | + */ |
| 107 | + FileAndLines addOrGetFile(const(char)[] file) |
| 108 | + { |
| 109 | + if (auto payload = files.lookup(file.ptr, file.length)) |
| 110 | + if (payload !is null) |
| 111 | + return cast(typeof(return)) payload.ptrvalue; |
| 112 | + |
| 113 | + auto lines = new FileAndLines(file.ptr); |
| 114 | + auto p = files.insert(file.ptr, file.length, cast(void*) lines); |
| 115 | + return cast(typeof(return)) p.ptrvalue; |
| 116 | + } |
| 117 | + |
| 118 | + void initialize() |
| 119 | + { |
| 120 | + files._init(); |
| 121 | + } |
| 122 | + |
| 123 | + void deinitialize() |
| 124 | + { |
| 125 | + foreach (sv; files) |
| 126 | + sv.destroy(); |
| 127 | + files.reset(); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +static fileCache = FileCache(); |
| 132 | + |
| 133 | +shared static this() |
| 134 | +{ |
| 135 | + fileCache.initialize(); |
| 136 | +} |
| 137 | +shared static ~this() |
| 138 | +{ |
| 139 | + fileCache.deinitialize(); |
| 140 | +} |
0 commit comments