comparison trunk/src/dil/SourceText.d @ 755:90668b83ae5e

Introduced new module dil.SourceText and class SourceText.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Wed, 13 Feb 2008 20:21:25 +0100
parents
children c24be8d4f6ab
comparison
equal deleted inserted replaced
754:c7a5499faa77 755:90668b83ae5e
1 /++
2 Author: Aziz Köksal
3 License: GPL3
4 +/
5 module dil.SourceText;
6
7 import dil.Converter;
8 import dil.Information;
9 import common;
10
11 import tango.io.File;
12
13 /// Represents D source code.
14 ///
15 /// The source text may come from a file or from a memory buffer.
16 final class SourceText
17 {
18 string filePath; /// The file path to the source text. Mainly used for error messages.
19 char[] data; /// The UTF-8, zero-terminated source text.
20
21 /// Params:
22 /// filePath = file path to the source file.
23 /// loadFile = whether to load the file in the constructor.
24 this(string filePath, bool loadFile = false)
25 {
26 this.filePath = filePath;
27 loadFile && load();
28 }
29
30 /// Params:
31 /// filePath = file path for error messages.
32 /// data = memory buffer.
33 this(string filePath, char[] data)
34 {
35 this(filePath);
36 this.data = data;
37 addSentinelCharacter();
38 }
39
40 void load(InfoManager infoMan = null)
41 {
42 if (!infoMan)
43 infoMan = new InfoManager;
44 assert(filePath.length);
45 // Read the file.
46 auto rawdata = cast(ubyte[]) (new File(filePath)).read();
47 // Convert the data.
48 auto converter = Converter(filePath, infoMan);
49 data = converter.data2UTF8(rawdata);
50 addSentinelCharacter();
51 }
52
53 private void addSentinelCharacter()
54 {
55 if (data.length == 0 || data[$-1] != 0)
56 data ~= 0;
57 }
58 }