comparison src/dil/SourceText.d @ 806:bcb74c9b895c

Moved out files in the trunk folder to the root.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Sun, 09 Mar 2008 00:12:19 +0100
parents trunk/src/dil/SourceText.d@c24be8d4f6ab
children 372fa4fbbb1d
comparison
equal deleted inserted replaced
805:a3fab8b74a7d 806:bcb74c9b895c
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 /// Constructs a SourceText object.
22 /// Params:
23 /// filePath = file path to the source file.
24 /// loadFile = whether to load the file in the constructor.
25 this(string filePath, bool loadFile = false)
26 {
27 this.filePath = filePath;
28 loadFile && load();
29 }
30
31 /// Constructs a SourceText object.
32 /// Params:
33 /// filePath = file path for error messages.
34 /// data = memory buffer.
35 this(string filePath, char[] data)
36 {
37 this(filePath);
38 this.data = data;
39 addSentinelCharacter();
40 }
41
42 /// Loads the source text from a file.
43 void load(InfoManager infoMan = null)
44 {
45 if (!infoMan)
46 infoMan = new InfoManager;
47 assert(filePath.length);
48 // Read the file.
49 auto rawdata = cast(ubyte[]) (new File(filePath)).read();
50 // Convert the data.
51 auto converter = Converter(filePath, infoMan);
52 data = converter.data2UTF8(rawdata);
53 addSentinelCharacter();
54 }
55
56 /// Adds '\0' to the text (if not already there.)
57 private void addSentinelCharacter()
58 {
59 if (data.length == 0 || data[$-1] != 0)
60 data ~= 0;
61 }
62 }