Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
korayustundag committed Mar 14, 2024
1 parent 5612e0e commit e57f2fd
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 0 deletions.
37 changes: 37 additions & 0 deletions cfgsharp.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34408.163
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "cfgsharp", "cfgsharp\cfgsharp.csproj", "{94339523-1030-4F92-AD36-ACA074E48D21}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{94339523-1030-4F92-AD36-ACA074E48D21}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{94339523-1030-4F92-AD36-ACA074E48D21}.Debug|Any CPU.Build.0 = Debug|Any CPU
{94339523-1030-4F92-AD36-ACA074E48D21}.Debug|x64.ActiveCfg = Debug|x64
{94339523-1030-4F92-AD36-ACA074E48D21}.Debug|x64.Build.0 = Debug|x64
{94339523-1030-4F92-AD36-ACA074E48D21}.Debug|x86.ActiveCfg = Debug|x86
{94339523-1030-4F92-AD36-ACA074E48D21}.Debug|x86.Build.0 = Debug|x86
{94339523-1030-4F92-AD36-ACA074E48D21}.Release|Any CPU.ActiveCfg = Release|Any CPU
{94339523-1030-4F92-AD36-ACA074E48D21}.Release|Any CPU.Build.0 = Release|Any CPU
{94339523-1030-4F92-AD36-ACA074E48D21}.Release|x64.ActiveCfg = Release|x64
{94339523-1030-4F92-AD36-ACA074E48D21}.Release|x64.Build.0 = Release|x64
{94339523-1030-4F92-AD36-ACA074E48D21}.Release|x86.ActiveCfg = Release|x86
{94339523-1030-4F92-AD36-ACA074E48D21}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9ECA4CAE-FD54-44AE-9DB8-A7F26BBEA2C0}
EndGlobalSection
EndGlobal
157 changes: 157 additions & 0 deletions cfgsharp/ConfigFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
#if !NET6_0_OR_GREATER
using System.Linq;
#endif
using System.Text;

namespace CfgSharp
{
/// <summary>
/// This class represents a configuration file handler in the CfgSharp namespace.
/// <para>It provides methods to create, read, modify, and delete entries within a configuration file.</para>
/// </summary>
public class ConfigFile
{
private readonly string _path;
private readonly Dictionary<string, string> _cfgs;

/// <summary>
/// Initializes a new instance of the ConfigFile class with the specified file path.
/// <para>If the file does not exist, it creates a new configuration file.</para>
/// <para>If the file exists, it reads the existing configuration.</para>
/// </summary>
/// <param name="path">The path to the configuration file.</param>
public ConfigFile(string path)
{
_cfgs = new Dictionary<string, string>();
_path = path;
if (!File.Exists(_path))
{
CreateConfigFile();
}
else
{
ReadConfigFile();
}
}

private void CreateConfigFile()
{
try
{
using (FileStream fs = new FileStream(_path, FileMode.Create, FileAccess.Write))
{
using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8))
{
sw.WriteLine("# Config File");
}
}
}
catch (Exception ex)
{
Debug.WriteLine($"Error creating config file: {ex.Message}");
}
}


private void ReadConfigFile()
{
try
{
string[] lines = File.ReadAllLines(_path);
foreach (string line in lines)
{
if (!string.IsNullOrWhiteSpace(line) && line.Contains('='))
{
string[] parts = line.Split('=');
string key = parts[0].Trim();
string value = parts[1].Trim();
_cfgs[key] = value;
}
}
}
catch (Exception ex)
{
Debug.WriteLine($"Error reading config file: {ex.Message}");
}
}

/// <summary>
/// Adds a new entry with the provided key and value to the configuration file.
/// <para>If an entry with the same key already exists, it does not add the new entry.</para>
/// </summary>
/// <param name="key">The key of the entry to add.</param>
/// <param name="value">The value of the entry to add.</param>
public void AddEntry(string key, string value)
{
if (!_cfgs.ContainsKey(key))
{
_cfgs[key] = value;
SaveChanges();
}
else
{
Debug.WriteLine("Entry with the same key already exists.");
}
}

/// <summary>
/// Sets the value of an existing entry with the provided key in the configuration file.
/// <para>If the entry does not exist, it creates a new entry with the specified key and value.</para>
/// </summary>
/// <param name="key">The key of the entry to set.</param>
/// <param name="value">The new value of the entry.</param>
public void SetValue(string key, string value)
{
_cfgs[key] = value;
SaveChanges();
}

/// <summary>
/// Retrieves the value of the entry with the specified key from the configuration file.
/// </summary>
/// <param name="key">The key of the entry to retrieve.</param>
/// <returns>The value associated with the specified key. If the entry does <b>not exist</b> returns <b>empty string</b>.</returns>
public string GetValue(string key)
{
if (_cfgs.ContainsKey(key))
return _cfgs[key];
else
return string.Empty;
}

/// <summary>
/// Deletes the entry with the specified key from the configuration file, if it exists.
/// </summary>
/// <param name="key">The key of the entry to delete.</param>
public void DeleteEntry(string key)
{
if (_cfgs.ContainsKey(key))
{
_cfgs.Remove(key);
SaveChanges();
}
}

private void SaveChanges()
{
try
{
using (StreamWriter writer = new StreamWriter(_path))
{
foreach (KeyValuePair<string, string> entry in _cfgs)
{
writer.WriteLine($"{entry.Key} = {entry.Value}");
}
}
}
catch (Exception ex)
{
Debug.WriteLine($"Error writing to config file: {ex.Message}");
}
}
}
}
Binary file added cfgsharp/cfg_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions cfgsharp/cfgsharp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net48;net6;net7;net8</TargetFrameworks>
<Platforms>AnyCPU;x64;x86</Platforms>
<AssemblyName>libcfg</AssemblyName>
<RootNamespace>CfgSharp</RootNamespace>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<PackageId>CfgSharp</PackageId>
<Title>Config File Management</Title>
<Version>1.0.0</Version>
<Authors>Koray USTUNDAG</Authors>
<Company>Koray USTUNDAG</Company>
<Product>Configuration File Management</Product>
<Description>It provides methods to create, read, modify, and delete entries within a configuration file.</Description>
<Copyright>Copyright © Koray Ustundag 2024</Copyright>
<PackageProjectUrl>https://github.com/korayustundag/cfgsharp</PackageProjectUrl>
<PackageIcon>cfg_logo.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryUrl>https://github.com/korayustundag/cfgsharp</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>config file management</PackageTags>
<PackageReleaseNotes>First Release</PackageReleaseNotes>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
<IncludeSymbols>False</IncludeSymbols>
</PropertyGroup>

<ItemGroup>
<None Include="cfg_logo.png">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
<None Include="README.md">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>

</Project>

0 comments on commit e57f2fd

Please sign in to comment.