Skip to content

Commit 6259fd3

Browse files
committed
Migrate
0 parents  commit 6259fd3

26 files changed

+4492
-0
lines changed

Docs/img/favicon-16x16.png

1.84 KB
Loading

Docs/img/favicon-32x32.png

2.91 KB
Loading

Docs/img/logo.png

5.81 KB
Loading

Example/Example.csproj

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<ProjectReference Include="..\Src\SharpConfig.csproj" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<None Update="SampleCfg.txt">
16+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
17+
</None>
18+
</ItemGroup>
19+
20+
</Project>

Example/Program.cs

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
// Copyright (c) 2013-2022 Cemalettin Dervis, MIT License.
2+
// https://github.com/cemdervis/SharpConfig
3+
4+
using SharpConfig;
5+
6+
class SomeClass
7+
{
8+
public string SomeString { get; set; }
9+
10+
public int SomeInt { get; set; }
11+
12+
public int[] SomeInts { get; set; }
13+
14+
public DateTime SomeDate { get; set; }
15+
16+
// This field will be ignored by SharpConfig
17+
// when creating sections from objects and vice versa.
18+
[SharpConfig.Ignore]
19+
public int SomeIgnoredField;
20+
21+
// Same for this property.
22+
[SharpConfig.Ignore]
23+
public int SomeIgnoredProperty { get; set; }
24+
}
25+
26+
internal static class Program
27+
{
28+
public static void Main()
29+
{
30+
// Call the methods in this file here to see their effect.
31+
32+
//HowToLoadAConfig();
33+
//HowToCreateAConfig();
34+
//HowToSaveAConfig(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "TestCfg.ini"));
35+
//HowToCreateObjectsFromSections();
36+
//HowToCreateSectionsFromObjects();
37+
//HowToHandleArrays();
38+
39+
Console.ReadLine();
40+
}
41+
42+
/// <summary>
43+
/// Shows how to load a configuration from a string (our sample config).
44+
/// The same works for loading files and streams. Just call the appropriate method
45+
/// such as LoadFromFile and LoadFromStream, respectively.
46+
/// </summary>
47+
private static void HowToLoadAConfig()
48+
{
49+
// Read our example config.
50+
Configuration cfg = Configuration.LoadFromFile("SampleCfg.txt");
51+
52+
// Just print all sections and their settings.
53+
PrintConfig(cfg);
54+
}
55+
56+
/// <summary>
57+
/// Shows how to create a configuration in memory.
58+
/// </summary>
59+
private static void HowToCreateAConfig()
60+
{
61+
var cfg = new Configuration();
62+
63+
cfg["SomeStructure"]["SomeString"].StringValue = "foobar";
64+
cfg["SomeStructure"]["SomeInt"].IntValue = 2000;
65+
cfg["SomeStructure"]["SomeInts"].IntValueArray = new[] { 1, 2, 3 };
66+
cfg["SomeStructure"]["SomeDate"].DateTimeValue = DateTime.Now;
67+
68+
// We can obtain the values directly as strings, ints, floats, or any other (custom) type,
69+
// as long as the string value of the setting can be converted to the type you wish to obtain.
70+
string nameValue = cfg["SomeStructure"]["SomeString"].StringValue;
71+
72+
int ageValue = cfg["SomeStructure"]["SomeInt"].IntValue;
73+
74+
DateTime dateValue = cfg["SomeStructure"]["SomeDate"].DateTimeValue;
75+
76+
// Print our config just to see that it works.
77+
PrintConfig(cfg);
78+
}
79+
80+
/// <summary>
81+
/// Shows how to save a configuration to a file.
82+
/// </summary>
83+
/// <param name="filename">The destination filename.</param>
84+
private static void HowToSaveAConfig(string filename)
85+
{
86+
var cfg = new Configuration();
87+
88+
cfg["SomeStructure"]["SomeString"].StringValue = "foobar";
89+
cfg["SomeStructure"]["SomeInt"].IntValue = 2000;
90+
cfg["SomeStructure"]["SomeInts"].IntValueArray = new[] { 1, 2, 3 };
91+
cfg["SomeStructure"]["SomeDate"].DateTimeValue = DateTime.Now;
92+
93+
cfg.SaveToFile(filename);
94+
95+
Console.WriteLine("The config has been saved to {0}!", filename);
96+
}
97+
98+
/// <summary>
99+
/// Shows how to create C#/.NET objects directly from sections.
100+
/// </summary>
101+
private static void HowToCreateObjectsFromSections()
102+
{
103+
var cfg = new Configuration();
104+
105+
// Create the section.
106+
cfg["SomeStructure"]["SomeString"].StringValue = "foobar";
107+
cfg["SomeStructure"]["SomeInt"].IntValue = 2000;
108+
cfg["SomeStructure"]["SomeInts"].IntValueArray = new[] { 1, 2, 3 };
109+
cfg["SomeStructure"]["SomeDate"].DateTimeValue = DateTime.Now;
110+
111+
// Now create an object from it.
112+
var p = cfg["SomeStructure"].ToObject<SomeClass>();
113+
114+
// Test.
115+
Console.WriteLine("SomeString: " + p.SomeString);
116+
Console.WriteLine("SomeInt: " + p.SomeInt);
117+
PrintArray("SomeInts", p.SomeInts);
118+
Console.WriteLine("SomeDate: " + p.SomeDate);
119+
}
120+
121+
/// <summary>
122+
/// Shows how to create sections directly from C#/.NET objects.
123+
/// </summary>
124+
private static void HowToCreateSectionsFromObjects()
125+
{
126+
var cfg = new Configuration();
127+
128+
// Create an object.
129+
var p = new SomeClass
130+
{
131+
SomeString = "foobar",
132+
SomeInt = 2000,
133+
SomeInts = new[] { 1, 2, 3 },
134+
SomeDate = DateTime.Now
135+
};
136+
137+
// Now create a section from it.
138+
cfg.Add(Section.FromObject("SomeStructure", p));
139+
140+
// Print the config to see that it worked.
141+
PrintConfig(cfg);
142+
}
143+
144+
/// <summary>
145+
/// Shows the usage of arrays in SharpConfig.
146+
/// </summary>
147+
private static void HowToHandleArrays()
148+
{
149+
var cfg = new Configuration();
150+
151+
cfg["GeneralSection"]["SomeInts"].IntValueArray = new[] { 1, 2, 3 };
152+
153+
// Get the array back.
154+
int[] someIntValuesBack = cfg["GeneralSection"]["SomeInts"].GetValueArray<int>();
155+
float[] sameValuesButFloats = cfg["GeneralSection"]["SomeInts"].GetValueArray<float>();
156+
string[] sameValuesButStrings = cfg["GeneralSection"]["SomeInts"].GetValueArray<string>();
157+
158+
// There is also a non-generic variant of GetValueArray:
159+
object[] sameValuesButObjects = cfg["GeneralSection"]["SomeInts"].GetValueArray(typeof(int));
160+
161+
PrintArray("someIntValuesBack", someIntValuesBack);
162+
PrintArray("sameValuesButFloats", sameValuesButFloats);
163+
PrintArray("sameValuesButStrings", sameValuesButStrings);
164+
PrintArray("sameValuesButObjects", sameValuesButObjects);
165+
}
166+
167+
/// <summary>
168+
/// Prints all sections and their settings to the console.
169+
/// </summary>
170+
/// <param name="cfg">The configuration to print.</param>
171+
private static void PrintConfig(Configuration cfg)
172+
{
173+
foreach (Section section in cfg)
174+
{
175+
Console.WriteLine("[{0}]", section.Name);
176+
177+
foreach (Setting setting in section)
178+
{
179+
Console.Write(" ");
180+
181+
if (setting.IsArray)
182+
Console.Write("[Array, {0} elements] ", setting.ArraySize);
183+
184+
Console.WriteLine(setting.ToString());
185+
}
186+
187+
Console.WriteLine();
188+
}
189+
}
190+
191+
// Small helper method for printing objects of an arbitrary type.
192+
private static void PrintArray<T>(string arrName, IReadOnlyList<T> arr)
193+
{
194+
Console.Write(arrName + " = { ");
195+
196+
for (int i = 0; i < arr.Count - 1; i++)
197+
Console.Write(arr[i] + ", ");
198+
199+
Console.Write(arr[^1]!.ToString());
200+
Console.WriteLine(" }");
201+
}
202+
}

Example/SampleCfg.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Declare a section called "General".
2+
# All settings that are below the section belong to it,
3+
# until another section is declared.
4+
[General]
5+
SomeInteger = 10
6+
SomeFloat = 20.05
7+
8+
# An array is identified by the brackets surrounding the value.
9+
AnArray = {0,2,5,7}
10+
11+
# Another section called "Video".
12+
[Video]
13+
Width = 1920 # this is an inline comment.
14+
Height = 1200 # inline comments are parsed by SharpConfig.
15+
Format = RGB24
16+
17+
# Another section.
18+
[OtherProperties]
19+
20+
# A boolean can be represented by one of the following values:
21+
# true: yes, 1, true, on
22+
# false: no, 0, false, off
23+
ABoolean = yes
24+
ABooleanArray = {on,off,yes,no,true,false}
25+
26+
AnEmptyArray = {}
27+
AnotherEmptyArray = { }

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2013-2024 Cemalettin Dervis
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# SharpConfig
2+
3+
![logo](Docs/img/logo.png)
4+
5+
SharpConfig is an easy to use cfg/ini configuration library for .NET.
6+
7+
You can use SharpConfig to read, modify and save configuration files and streams, in either text or binary format.
8+
9+
[![NuGet Version](https://img.shields.io/nuget/v/sharpconfig)](https://www.nuget.org/packages/sharpconfig)
10+
[![NuGet Downloads](https://img.shields.io/nuget/dt/sharpconfig)](https://www.nuget.org/packages/sharpconfig)
11+
12+
Install via:
13+
14+
- .NET CLI: `> dotnet add package sharpconfig`
15+
- Package Manager: `> NuGet\Install-Package sharpconfig`
16+
- [Download latest]()
17+
18+
---
19+
20+
## Documentation
21+
22+
The documentation is available on the [home page](https://dervis.de/sharpconfig).
23+

SharpConfig.sln

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30011.22
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example", "Example\Example.csproj", "{EAE46547-311B-4E0F-B8BD-47026D5BF0B9}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{87F48902-C817-4922-96FC-296B2D48B575}"
9+
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpConfig", "Src\SharpConfig.csproj", "{47435426-6569-4208-868E-25C0519F1F0F}"
11+
EndProject
12+
Global
13+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
14+
Debug|Any CPU = Debug|Any CPU
15+
Release|Any CPU = Release|Any CPU
16+
EndGlobalSection
17+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
18+
{EAE46547-311B-4E0F-B8BD-47026D5BF0B9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19+
{EAE46547-311B-4E0F-B8BD-47026D5BF0B9}.Debug|Any CPU.Build.0 = Debug|Any CPU
20+
{EAE46547-311B-4E0F-B8BD-47026D5BF0B9}.Release|Any CPU.ActiveCfg = Release|Any CPU
21+
{EAE46547-311B-4E0F-B8BD-47026D5BF0B9}.Release|Any CPU.Build.0 = Release|Any CPU
22+
{87F48902-C817-4922-96FC-296B2D48B575}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
23+
{87F48902-C817-4922-96FC-296B2D48B575}.Debug|Any CPU.Build.0 = Debug|Any CPU
24+
{87F48902-C817-4922-96FC-296B2D48B575}.Release|Any CPU.ActiveCfg = Release|Any CPU
25+
{87F48902-C817-4922-96FC-296B2D48B575}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{47435426-6569-4208-868E-25C0519F1F0F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{47435426-6569-4208-868E-25C0519F1F0F}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{47435426-6569-4208-868E-25C0519F1F0F}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{47435426-6569-4208-868E-25C0519F1F0F}.Release|Any CPU.Build.0 = Release|Any CPU
30+
EndGlobalSection
31+
GlobalSection(SolutionProperties) = preSolution
32+
HideSolutionNode = FALSE
33+
EndGlobalSection
34+
GlobalSection(ExtensibilityGlobals) = postSolution
35+
SolutionGuid = {7546E96E-68C4-49D7-AAE8-27F5CA7531AB}
36+
EndGlobalSection
37+
EndGlobal

0 commit comments

Comments
 (0)