|
| 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, filecache.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 | +/** |
| 22 | +A line-by-line representation of a $(REF File, dmd,root,file). |
| 23 | +*/ |
| 24 | +class FileAndLines |
| 25 | +{ |
| 26 | + File* file; |
| 27 | + const(char[])[] lines; |
| 28 | + |
| 29 | + /** |
| 30 | + File to read and split into its lines. |
| 31 | + */ |
| 32 | + this(const(char)[] filename) |
| 33 | + { |
| 34 | + file = new File(filename); |
| 35 | + readAndSplit(); |
| 36 | + } |
| 37 | + |
| 38 | + // Read a file and split the file buffer linewise |
| 39 | + private void readAndSplit() |
| 40 | + { |
| 41 | + file.read(); |
| 42 | + auto buf = file.buffer; |
| 43 | + // slice into lines |
| 44 | + while (*buf) |
| 45 | + { |
| 46 | + auto prevBuf = buf; |
| 47 | + for (; *buf != '\n' && *buf != '\r'; buf++) |
| 48 | + { |
| 49 | + if (!*buf) |
| 50 | + break; |
| 51 | + } |
| 52 | + // handle Windows line endings |
| 53 | + if (*buf == '\r' && *(buf + 1) == '\n') |
| 54 | + buf++; |
| 55 | + lines ~= cast(const(char)[]) prevBuf[0 .. buf - prevBuf]; |
| 56 | + buf++; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + void destroy() |
| 61 | + { |
| 62 | + if (file) |
| 63 | + { |
| 64 | + file.destroy(); |
| 65 | + file = null; |
| 66 | + lines.destroy(); |
| 67 | + lines = null; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + ~this() |
| 72 | + { |
| 73 | + destroy(); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | +A simple file cache that can be used to avoid reading the same file multiple times. |
| 79 | +It stores its cached files as $(LREF FileAndLines) |
| 80 | +*/ |
| 81 | +struct FileCache |
| 82 | +{ |
| 83 | + private StringTable files; |
| 84 | + |
| 85 | + /** |
| 86 | + Add or get a file from the file cache. |
| 87 | + If the file isn't part of the cache, it will be read from the filesystem. |
| 88 | + If the file has been read before, the cached file object will be returned |
| 89 | +
|
| 90 | + Params: |
| 91 | + file = file to load in (or get from) the cache |
| 92 | +
|
| 93 | + Returns: a $(LREF FileAndLines) object containing a line-by-line representation of the requested file |
| 94 | + */ |
| 95 | + FileAndLines addOrGetFile(const(char)[] file) |
| 96 | + { |
| 97 | + if (auto payload = files.lookup(file)) |
| 98 | + { |
| 99 | + if (payload !is null) |
| 100 | + return cast(typeof(return)) payload.ptrvalue; |
| 101 | + } |
| 102 | + |
| 103 | + auto lines = new FileAndLines(file); |
| 104 | + files.insert(file, cast(void*) lines); |
| 105 | + return lines; |
| 106 | + } |
| 107 | + |
| 108 | + __gshared fileCache = FileCache(); |
| 109 | + |
| 110 | + // Initializes the global FileCache singleton |
| 111 | + static __gshared void _init() |
| 112 | + { |
| 113 | + fileCache.initialize(); |
| 114 | + } |
| 115 | + |
| 116 | + void initialize() |
| 117 | + { |
| 118 | + files._init(); |
| 119 | + } |
| 120 | + |
| 121 | + void deinitialize() |
| 122 | + { |
| 123 | + foreach (sv; files) |
| 124 | + sv.destroy(); |
| 125 | + files.reset(); |
| 126 | + } |
| 127 | +} |
0 commit comments