1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
using System;
using System.IO;
using System.Text;
using System.Collections;
using Mono.Debugger;
namespace Mono.Debugger
{
public class SourceFileFactory : DebuggerMarshalByRefObject
{
Hashtable files = new Hashtable ();
public SourceBuffer FindFile (string name)
{
if (files.Contains (name))
return (SourceBuffer) ((ObjectCache) files [name]).Data;
ObjectCache cache = (ObjectCache) files [name];
if (cache == null) {
cache = new ObjectCache (new ObjectCacheFunc (read_file), name, 10);
files.Add (name, cache);
}
return (SourceBuffer) cache.Data;
}
public bool Exists (string name)
{
if (files.Contains (name))
return true;
FileInfo file_info = new FileInfo (name);
return file_info.Exists;
}
object read_file (object user_data)
{
string name = (string) user_data;
FileInfo file_info = new FileInfo (name);
if (!file_info.Exists) {
Report.Debug (DebugFlags.SourceFiles, "Can't find source file: " + name);
return null;
}
ArrayList contents = new ArrayList ();
try {
/* 28591 = Windows ISO Latin1 code page */
Encoding encoding = Encoding.GetEncoding (28591);
using (StreamReader reader = new StreamReader (file_info.OpenRead (), encoding)) {
string line;
while ((line = reader.ReadLine ()) != null)
contents.Add (line);
}
} catch {
return null;
}
return new SourceBuffer (name, contents);
}
}
}
|