Virtu/Virtu/Services/StorageService.cs
Sean Fausett 6aa86024d3 Added simple commandline support on Windows, i.e.
Jellyfish.Virtu.exe <FileName>
Includes new support for 'program' (.prg) files.
2012-05-20 13:00:39 +12:00

207 lines
5.8 KiB
C#

using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Security;
using Jellyfish.Virtu.Properties;
namespace Jellyfish.Virtu.Services
{
public abstract class StorageService : MachineService
{
protected StorageService(Machine machine) :
base(machine)
{
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
public bool Load(string fileName, Action<Stream> reader)
{
try
{
OnLoad(fileName, reader);
}
catch (Exception e)
{
Debug.WriteLine(e.ToString());
return false;
}
return true;
}
#if !WINDOWS
[SecuritySafeCritical]
#endif
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
public static bool LoadFile(string fileName, Action<Stream> reader)
{
if (reader == null)
{
throw new ArgumentNullException("reader");
}
try
{
using (var stream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
reader(stream);
}
}
catch (Exception e)
{
Debug.WriteLine(e.ToString());
return false;
}
return true;
}
#if !WINDOWS
[SecuritySafeCritical]
#endif
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
public static bool LoadFile(FileInfo fileInfo, Action<Stream> reader)
{
if (fileInfo == null)
{
throw new ArgumentNullException("fileInfo");
}
if (reader == null)
{
throw new ArgumentNullException("reader");
}
try
{
using (var stream = fileInfo.Open(FileMode.Open, FileAccess.Read, FileShare.Read))
{
reader(stream);
}
}
catch (Exception e)
{
Debug.WriteLine(e.ToString());
return false;
}
return true;
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
public static bool LoadResource(string resourceName, Action<Stream> reader)
{
if (reader == null)
{
throw new ArgumentNullException("reader");
}
try
{
using (var stream = GetResourceStream(resourceName))
{
reader(stream);
}
}
catch (Exception e)
{
Debug.WriteLine(e.ToString());
return false;
}
return true;
}
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
public bool Save(string fileName, Action<Stream> writer)
{
try
{
OnSave(fileName, writer);
}
catch (Exception e)
{
Debug.WriteLine(e.ToString());
return false;
}
return true;
}
#if !WINDOWS
[SecuritySafeCritical]
#endif
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
public static bool SaveFile(string fileName, Action<Stream> writer)
{
if (writer == null)
{
throw new ArgumentNullException("writer");
}
try
{
using (var stream = File.Open(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
{
writer(stream);
}
}
catch (Exception e)
{
Debug.WriteLine(e.ToString());
return false;
}
return true;
}
#if !WINDOWS
[SecuritySafeCritical]
#endif
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
public static bool SaveFile(FileInfo fileInfo, Action<Stream> writer)
{
if (fileInfo == null)
{
throw new ArgumentNullException("fileInfo");
}
if (writer == null)
{
throw new ArgumentNullException("writer");
}
try
{
using (var stream = fileInfo.Open(FileMode.Create, FileAccess.Write, FileShare.None))
{
writer(stream);
}
}
catch (Exception e)
{
Debug.WriteLine(e.ToString());
return false;
}
return true;
}
protected abstract void OnLoad(string fileName, Action<Stream> reader);
protected abstract void OnSave(string fileName, Action<Stream> writer);
private static Stream GetResourceStream(string resourceName)
{
resourceName = "Jellyfish.Virtu." + resourceName.Replace('/', '.');
var resourceStream = typeof(StorageService).Assembly.GetManifestResourceStream(resourceName);
if (resourceStream == null)
{
throw new FileNotFoundException(string.Format(CultureInfo.CurrentUICulture, Strings.ResourceNotFound, resourceName));
}
return resourceStream;
}
}
}