Skip to content

Commit

Permalink
Derive IniFile from Dictionary<string,string>
Browse files Browse the repository at this point in the history
  • Loading branch information
RiJo committed Dec 19, 2014
1 parent 8b4b246 commit 7da58b4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 22 deletions.
27 changes: 8 additions & 19 deletions MediaOrganizer/IniFile.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// IniFile.cs: Data structure used to save/load ini files.
// IniFile.cs: Data structure used to save/load an ini file.
//
// Copyright (C) 2014 Rikard Johansson
//
Expand All @@ -17,6 +17,7 @@
//

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand All @@ -25,25 +26,13 @@

namespace ExifOrganizer.Organizer
{
public class IniFile
public class IniFile : Dictionary<string, string>
{
private Dictionary<string, string> values = new Dictionary<string, string>();

public IniFile()
: base()
{
}

public string this[string key]
{
get { return values[key]; }
set { values[key] = value; }
}

public bool Contains(string key)
{
return values.ContainsKey(key);
}

public bool TryLoad(string filename)
{
try
Expand All @@ -64,7 +53,7 @@ public void Load(string filename)
if (!File.Exists(filename))
throw new FileNotFoundException(filename);

values.Clear();
Clear();

foreach (string line in File.ReadAllLines(filename))
{
Expand All @@ -79,7 +68,7 @@ public void Load(string filename)

string key = keyValue[0].Trim();
string value = keyValue[1].Trim();
values[key] = value;
Add(key, value);
}
}

Expand All @@ -102,9 +91,9 @@ public void Save(string filename)
throw new ArgumentNullException("filename");

List<string> lines = new List<string>();
foreach (string key in values.Keys)
foreach (KeyValuePair<string, string> kvp in this)
{
string line = String.Format("{0}: {1}", key, values[key]);
string line = String.Format("{0}: {1}", kvp.Key, kvp.Value);
lines.Add(line);
}

Expand Down
6 changes: 3 additions & 3 deletions MediaOrganizer/MediaOrganizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,11 @@ public void LoadConfig() {
IniFile iniFile = new IniFile();
iniFile.TryLoad(iniFilePath);

if (iniFile.Contains("sourcePath"))
if (iniFile.ContainsKey("sourcePath"))
sourcePath = iniFile["sourcePath"];
if (iniFile.Contains("destinationPath"))
if (iniFile.ContainsKey("destinationPath"))
destinationPath = iniFile["destinationPath"];
if (iniFile.Contains("recursive"))
if (iniFile.ContainsKey("recursive"))
Recursive = iniFile["recursive"] == "1";
}

Expand Down

0 comments on commit 7da58b4

Please sign in to comment.