/*----------------------------------------------------------------------
Prof-It for C#
Copyright (c) 2004 Klaus Lehner, University of Linz
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
----------------------------------------------------------------------*/
using System;
using System.Collections;
using System.IO;
using System.Xml;
namespace at.jku.ssw.ProfIt.Components{
/// <summary>
/// This class represents one SourceFile and all of its blocks.
/// It does not contain the content of the file itself!!
/// </summary>
[Serializable]
public class SourceFile {
private string filename;
private BuildAction buildAction;
private bool profile = true;
private Project project;
/// <summary>
/// The counters of a source file aren't stored in the project file.
/// </summary>
[NonSerialized]
private CounterCollection counters;
public SourceFile(Project project, string filename) {
this.project = project;
this.filename = filename;
}
/// <summary>
/// Returns the current CounterCollection, if the SourceFile hasn't changed
/// since the last parsing; null otherwise.
/// </summary>
public CounterCollection Counters {
get { if (IsParsingNecessary) return null; else return this.counters; }
}
/// <summary>
/// Sets the corresponding project of the SourceFile
/// </summary>
internal void SetProject(Project project) {
this.project = project;
}
/// <summary>
/// Returns the absolute path of the filename
/// </summary>
public string FileName {
get { return project.RootPath + "\\" + filename; }
}
/// <summary>
/// Returns a block at a specified position in the file
/// </summary>
/// <param name="p">the position of the block</param>
/// <returns>null, if IsParsingNecessary is false or no block can be
/// found at the specified position</returns>
public IBlock GetBlock(Position p) {
if (IsParsingNecessary) return null;
IBlock result = findBlock(p, this.counters.blocks);
if (result != null) return result;
result = findBlock(p, this.counters.methods);
if (result != null) return result;
result = findBlock(p, this.counters.classes);
return result;
}
private IBlock findBlock(Position p, ArrayList list) {
foreach (IBlock block in list) {
if (block.Start.CompareTo(p) <= 0 && block.End.CompareTo(p) >= 0) return block;
}
return null;
}
/// <summary>
/// Returns true if the file has to be included in profiling
/// </summary>
public bool Profile {
get { return this.profile; }
}
/// <summary>
/// Sets the value whether the file has to be profiled or not
/// </summary>
internal void SetProfile(bool value) {
this.profile = value;
}
/// <summary>
/// Returns true, if the file has been modified since the last parsing.
/// </summary>
public bool IsParsingNecessary {
get {
if (counters == null) return true;
DateTime time = File.GetLastWriteTime(this.FileName);
if (counters.FileName == null || counters.FileName.Length == 0) return true;
return (time >= this.counters.LastParseTime);
}
}
public Project Project { get { return project; } }
public void SetCounters(CounterCollection c) {
if (c == null) {
this.counters = new CounterCollection("");
} else {
this.counters = c;
this.counters.SetFileName(this.FileName);
this.counters.LastParseTime = c.LastParseTime;
}
}
/// <summary>
/// Gets or sets the BuildAction for this file. (Compile, EmbeddedResource, Content)
/// This is needed for VS.NET projects.
/// </summary>
public BuildAction BuildAction {
get { return buildAction; }
set { buildAction = value; }
}
#region Implementation of Equals and HashCode
/// <summary>
/// A CSFile is equal to another if it has the same FileName
/// </summary>
/// <param name="obj">object to compare, must be a CSFile</param>
/// <returns>true if filenames are identical</returns>
public override bool Equals(object obj) {
if (!(obj is SourceFile)) return false;
return this.FileName == ((SourceFile)obj).FileName;
}
public override int GetHashCode() {
return FileName.GetHashCode ();
}
#endregion
#region Print methods
/// <summary>
/// Prints all blocks of the file to the console
/// </summary>
public void PrintBlocks() {
foreach (IBlock block in counters.blocks) {
Console.WriteLine(block.ToString());
}
}
/// <summary>
/// For testing only
/// </summary>
/// <returns>A summary of all blocks</returns>
public override string ToString() {
string s = "";
foreach (IBlock block in counters.blocks) {
s += block.ToString() + "\r\n";
}
return s;
}
#endregion
}
public enum BuildAction { Compile, EmbeddedResource, Content }
}
|