Files
cuetools.net/CUETools.Processor/Settings.cs

154 lines
4.6 KiB
C#
Raw Normal View History

2008-12-07 23:38:00 +00:00
// ****************************************************************************
//
// CUE Tools
// Copyright (C) 2006-2007 Moitah (moitah@yahoo.com)
//
// 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 of the License, 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.Generic;
using System.IO;
using System.Text;
namespace CUETools.Processor
{
static class SettingsShared
{
public static string GetMyAppDataDir(string appName) {
string appDataDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string myAppDataDir = Path.Combine(appDataDir, appName);
if (Directory.Exists(myAppDataDir) == false) {
Directory.CreateDirectory(myAppDataDir);
}
return myAppDataDir;
}
}
public class SettingsReader {
Dictionary<string, string> _settings;
2009-05-01 15:16:26 +00:00
public SettingsReader(string appName, string fileName, string appPath) {
2008-12-07 23:38:00 +00:00
_settings = new Dictionary<string, string>();
2009-05-01 15:16:26 +00:00
bool userProfilesEnabled = (appPath == null || File.Exists(Path.Combine(Path.GetDirectoryName(appPath), "user_profiles_enabled")));
2008-12-07 23:38:00 +00:00
2009-05-01 15:16:26 +00:00
string path = Path.Combine(
userProfilesEnabled ? SettingsShared.GetMyAppDataDir(appName) : Path.GetDirectoryName(appPath),
userProfilesEnabled ? fileName : appName + "." + fileName);
2008-12-07 23:38:00 +00:00
if (!File.Exists(path)) {
return;
}
using (StreamReader sr = new StreamReader(path, Encoding.UTF8)) {
2009-05-01 15:16:26 +00:00
string line, name = null, val;
2008-12-07 23:38:00 +00:00
int pos;
while ((line = sr.ReadLine()) != null) {
pos = line.IndexOf('=');
if (pos != -1) {
2009-05-01 15:16:26 +00:00
if (pos > 0)
{
name = line.Substring(0, pos);
val = line.Substring(pos + 1);
if (!_settings.ContainsKey(name))
_settings.Add(name, val);
}
else
{
val = line.Substring(pos + 1);
if (_settings.ContainsKey(name))
_settings[name] += "\r\n" + val;
2008-12-07 23:38:00 +00:00
}
}
}
}
}
public string Load(string name) {
return _settings.ContainsKey(name) ? _settings[name] : null;
}
public bool? LoadBoolean(string name) {
string val = Load(name);
if (val == "0") return false;
if (val == "1") return true;
return null;
}
public int? LoadInt32(string name, int? min, int? max) {
int val;
if (!Int32.TryParse(Load(name), out val)) return null;
if (min.HasValue && (val < min.Value)) return null;
if (max.HasValue && (val > max.Value)) return null;
return val;
}
public uint? LoadUInt32(string name, uint? min, uint? max) {
uint val;
if (!UInt32.TryParse(Load(name), out val)) return null;
if (min.HasValue && (val < min.Value)) return null;
if (max.HasValue && (val > max.Value)) return null;
return val;
}
}
public class SettingsWriter {
StreamWriter _sw;
2009-05-01 15:16:26 +00:00
public SettingsWriter(string appName, string fileName, string appPath)
{
bool userProfilesEnabled = (appPath == null || File.Exists(Path.Combine(Path.GetDirectoryName(appPath), "user_profiles_enabled")));
string path = Path.Combine(
userProfilesEnabled ? SettingsShared.GetMyAppDataDir(appName) : Path.GetDirectoryName(appPath),
userProfilesEnabled ? fileName : appName + "." + fileName);
2008-12-07 23:38:00 +00:00
_sw = new StreamWriter(path, false, Encoding.UTF8);
}
public void Save(string name, string value) {
_sw.WriteLine(name + "=" + value);
}
2009-05-01 15:16:26 +00:00
public void SaveText(string name, string value)
{
_sw.Write(name);
using (StringReader sr = new StringReader(value))
{
string lineStr;
while ((lineStr = sr.ReadLine()) != null)
_sw.WriteLine("=" + lineStr);
}
}
2008-12-07 23:38:00 +00:00
public void Save(string name, bool value) {
Save(name, value ? "1" : "0");
}
public void Save(string name, int value) {
Save(name, value.ToString());
}
public void Save(string name, uint value) {
Save(name, value.ToString());
}
public void Close() {
_sw.Close();
}
}
}