Skip to content

Instantly share code, notes, and snippets.

@dkellycollins
Last active December 11, 2015 07:19
Show Gist options
  • Save dkellycollins/4565694 to your computer and use it in GitHub Desktop.
Save dkellycollins/4565694 to your computer and use it in GitHub Desktop.
///<summary>
/// Contains functions to load text files as common collections.
///</summary>
public class IOHelper
{
#region Text IO
/// <summary>
/// Attempts to load in a dictionary from a basic text file.
/// </summary>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <param name="file">File where the dictionary is stored.</param>
/// <param name="token">The charater that separates the key and value in the file.</param>
/// <returns>Will return an initialized Dictionary.</returns>
public static IDictionary<TKey, TValue> LoadDictionary<TKey, TValue>(string file, char token)
where TKey : IConvertible
where TValue : IConvertible
{
Dictionary<TKey, TValue> dictionary = new Dictionary<TKey, TValue>();
try
{
using (StreamReader reader = File.OpenText(file))
{
string buf;
while ((buf = reader.ReadLine()) != null)
{
string[] tok = buf.Split(token);
dictionary.Add(
(TKey)Convert.ChangeType(tok[0].Trim(), typeof(TKey)),
(TValue)Convert.ChangeType(tok[1].Trim(), typeof(TValue))
);
}
}
}
catch (Exception e)
{
//Handle error
}
return dictionary;
}
/// <summary>
/// Attempts to save a dictionary to a basic text file.
/// </summary>
/// <typeparam name="TKey">Type of the key.</typeparam>
/// <typeparam name="TValue">Type of the value.</typeparam>
/// <param name="dictionary">Dictionary to write to file.</param>
/// <param name="file">File to write to. Will create file if it does not already exist.</param>
/// <param name="token">Character to separt the key and value.</param>
/// <returns>True if the save was successful.</returns>
public static bool SaveDictionary<TKey, TValue>(IDictionary<TKey, TValue> dictionary, string file, char token)
{
bool result = false;
try
{
if (File.Exists(file))
File.Delete(file);
using (StreamWriter writer = File.CreateText(file))
{
foreach (KeyValuePair<TKey, TValue> item in dictionary)
{
writer.WriteLine(item.Key.ToString() + token + item.Value.ToString());
}
writer.Flush();
}
result = true;
}
catch (Exception e)
{
//Handle error
}
return result;
}
/// <summary>
/// Attempts to load in a list from a basic text file.
/// </summary>
/// <typeparam name="T">Type of the values.</typeparam>
/// <param name="file">File where the list is saved.</param>
/// <returns>Will return an initalized list.</returns>
public static IList<T> LoadList<T>(string file)
where T : IConvertible
{
List<T> list = new List<T>();
try
{
using (StreamReader reader = File.OpenText(file))
{
string buf;
while ((buf = reader.ReadLine()) != null)
{
list.Add((T)Convert.ChangeType(buf.Trim(), typeof(T)));
}
}
}
catch (Exception e)
{
//Handle error
}
return list;
}
/// <summary>
/// Attempts to save the given list.
/// </summary>
/// <typeparam name="T">Type of the values.</typeparam>
/// <param name="list">The list to save.</param>
/// <param name="file">File to save the list to. If does not exist then will be created.</param>
/// <returns>True, if the save was successful.</returns>
public static bool SaveList<T>(IList<T> list, string file)
{
bool result = false;
try
{
if (File.Exists(file))
File.Delete(file);
using (StreamWriter writer = File.CreateText(file))
{
foreach (T item in list)
{
writer.WriteLine(item.ToString());
}
writer.Flush();
}
result = true;
}
catch (Exception e)
{
//Handle error
}
return result;
}
#endregion Text IO
#region XML IO
public static bool SaveDictionaryToXML<TKey, TValue>(IDictionary<TKey, TValue> dictionary, string file)
{
throw new NotImplementedException();
}
public static IDictionary<TKey, TValue> LoadDictionaryFromXML<TKey, TValue>(string fileName)
where TKey : IConvertible
where TValue : IConvertible
{
throw new NotImplementedException();
}
public static bool SaveListToXML<T>(IList<T> list, string fileName)
{
throw new NotImplementedException();
}
public static IList<T> LoadListFromXML(string fileName)
{
throw new NotImplementedException();
}
#endregion XML IO
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment