diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/dil/SourceText.d	Sun Mar 09 00:12:19 2008 +0100
@@ -0,0 +1,62 @@
+/++
+  Author: Aziz Köksal
+  License: GPL3
++/
+module dil.SourceText;
+
+import dil.Converter;
+import dil.Information;
+import common;
+
+import tango.io.File;
+
+/// Represents D source code.
+///
+/// The source text may come from a file or from a memory buffer.
+final class SourceText
+{
+  string filePath; /// The file path to the source text. Mainly used for error messages.
+  char[] data; /// The UTF-8, zero-terminated source text.
+
+  /// Constructs a SourceText object.
+  /// Params:
+  ///   filePath = file path to the source file.
+  ///   loadFile = whether to load the file in the constructor.
+  this(string filePath, bool loadFile = false)
+  {
+    this.filePath = filePath;
+    loadFile && load();
+  }
+
+  /// Constructs a SourceText object.
+  /// Params:
+  ///   filePath = file path for error messages.
+  ///   data = memory buffer.
+  this(string filePath, char[] data)
+  {
+    this(filePath);
+    this.data = data;
+    addSentinelCharacter();
+  }
+
+  /// Loads the source text from a file.
+  void load(InfoManager infoMan = null)
+  {
+    if (!infoMan)
+      infoMan = new InfoManager;
+    assert(filePath.length);
+    // Read the file.
+    auto rawdata = cast(ubyte[]) (new File(filePath)).read();
+    // Convert the data.
+    auto converter = Converter(filePath, infoMan);
+    data = converter.data2UTF8(rawdata);
+    addSentinelCharacter();
+  }
+
+  /// Adds '\0' to the text (if not already there.)
+  private void addSentinelCharacter()
+  {
+    if (data.length == 0 || data[$-1] != 0)
+      data ~= 0;
+  }
+}