view trunk/src/dil/SourceText.d @ 798:c24be8d4f6ab

Added documentation comments.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Sat, 01 Mar 2008 02:53:06 +0100
parents 90668b83ae5e
children
line wrap: on
line source

/++
  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;
  }
}