API

CsConfig - Advanced Configuration Parser Library

紹介

このライブラリはあなたのアプリケーションに簡単にコンフィグレーションファイルパーサーを実装するものです。 実装は非常に簡単で、アドバンスドなコンフィグレーションの読み込みができ、参照も非常にシンプルになっています。 コンフィグレーションパーサーではコンフィグレーションファイルの正当性チェック構文も備えており、 基本的な設定ではアプリケーションでエラー処理する手間を省けるのが特徴になっています。

インストール

Visual Studio の[ツール]メニューから [NuGet パッケージマネージャー] - [パッケージマネージャーコンソール]
PM> Install-Package CsConfig
												

ダウンロード

APIリファレンス

実装例

以下のように簡単に実装できます。
	internal class Program
	{
		// Enumeration type used in the configuration
		internal enum Part
		{
			First = 0,
			Second = 2,
			Third = 5,
		};

		// User validator class in the configuration
		internal class UserValidator
		{
			// User validator method
			public string CheckMultiplsOf3(object v)
			{
				// If a value id valid, returns null, otherwise a string of an error message
				if (((int)v % 3) == 0) return null;
				else return "It must be a multiple of 3.";
			}
		}

		/// 
		/// Main function
		/// 
		public static void Main(string[] args)
		{
			if(args.Length < 1)
			{
				Environment.Exit(-1);
			}

			// Implementation with the SConfig class
			var config = new SConfig();
			// Add variable programmatically
			config.AddVariable("Def1000", 1000);
			// Add enumeration type programmatically
			config.AddEnumeration(typeof(Part));
			// Add user validator class object
			config.AddValidatorObject(new UserValidator());
			if (!config.Parse(args))
			{
				foreach(var error in config.Errors)
				{
					Console.WriteLine(error);
				}

				Environment.Exit(-1);
			} else
			{
				// Show all configuration variables
				foreach(var v in config.Variables)
				{
					Console.WriteLine(v.ToString());
				}

				// Get an expression result
				var v2 = config.GetExpressionResult("Section.Sub.C[\"b\"]");
				Console.WriteLine("Result : " + v2.ToString());
			}
			Environment.Exit(0);
		}
	}
												

コンフィグサンプル

コンフィグレーションの構文は以下のようになっています。
/** Example Configration **/

# One line comment
//One line comment
/*
 *  Multi line comment
 */

// Define enumeration
enum Color {
	Red,
	Yellow = 4,
	Green
}

// Define an integer variable
Int = 35;

// Define a float variable
Float = 50.5;

// Define a string variable
String = "Hello";

// Define a path variable
Path = '%JAVA_HOME%/bin';

// Define a enumeration variable
Enum = Color.Green;

// Define a base64 variable
Base64 = b"SGVsbG8gV29ybGQ=";

// Define a tuple variable
Tuple = (12, "ABC", 25.5);

// Define a array variable
Array = [
	"ABC",
	"DEFG",
	"HIJKL",
];

// Define a hash variable
Hash = {
	first : 1,
	second : 2.0,
	third : "3",
};

// Define a node variable
Node : {
	Int = 102;
	Float = 32.9;
	
	Section = node {
		Int = 8;
		String  = "Section";
		Items = [
			"Hello",
			"Good night",
			"Hi",
		];
	};
	
	Nodes = [
		node {
			A = "A character";
			B = "B character";
			ARRAY = [
				node {
					A = 1;
				},
				node {
					B = 5;
				}
			];
		},
		node {
			A = "First";
			C = "Second";
			ARRAY = [
				node {
					A = 10;
				},
				node {
					B = 50;
				}
			];
		},
	];
	
	Hash = {
		abc : node {
			B = "1";
		},
		def : node {
			A = "2";
			B = "5";
		}
	};
}

// Expression examples
Value1 = Int * 5 + 10;
Value2 = "{" + String + "}";
Value3 = Node.Float * Float / 7.0;
Value4 = Array[2];
Value5 = Hash["second"];
Value6 = Node.Int + Node.Section.Int;
Value7 = (Float == 50.5 ? "Ok" : "Ng");
Value8 = Node.Nodes[1].A;
Value9 = Part.Third;
Value10 = Def1000;
Value11 = Value1 < 500 ? "Less" : "Greater";

// If statement example
if(Int >= 20) {
	Value13 = "Greater";
} else {
	Value13 = "Less";
}



//----- Validator definitions -----

StringValidatorArray = [
	"Good", "Hi", "Hello"
];

validator {
	// Mandatory definition with an Int type value between 10 and 100, with a default value of 50.
	Int = int(10, 100) @ 50;
	
	// Optional definition with a Float type value of 50 or more, with a default value of 200.
	Float ? float(50.0, *) @ 200.0;
	
	// Mandatory definition with a String type value that matches the regular expression, with a default value of 'Default'.
	String = string("[a-zA-Z]+") @ "Default;";
	
	// Mandatory definition with a String type value that matches one of the StringValidatorArray elements, with a default value of 'Good'.
	String2 = string(StringValidatorArray) @ "Good";
	
	// Mandatory definition with a Path type value, with a default value of 'C:/'.
	Path = path @ "C:/";
	
	// Optional definition with an Enum type value of 'Color', with a default value of 'Color.Yellow'.
	Enum ? enum Color @ Color.Yellow;
	
	// Mandatory definition with a Base64 type value, with a default value of 'V29ybGQ='.
	Base64 = base64 @ b"V29ybGQ=";
	
	// Mandatory definition with a Tuple type value containing three elements defined below.
	Tuple = tuple(int(10, 20), string("[A-Z]+"), float);
	
	// Optional definition with an array type value consisting of String elements that match the regular expression.
	Array ? array string("[A-Z]+");
	
	// Hash value with an arbitrary definition.
	Hash = hash;
	
	// Node value with a mandatory definition, followed by the one below.
	Node = node {
		
		// Integer value with a mandatory definition, validated by a user-defined function.
		Int = int(@CheckMultiplsOf3) @ 30;
		
		Float = float @ 200.25;
		
		Section = node {
			Int ? int;
			String ? string;
			Items = array;
		};
		
		// It is node array validator
		Nodes = array node {
			A = string @ "(A)";
			B = string @ "(B)";
			C = string("[a-zA-Z\\(\\)]+") @ "(C)";
			
			ARRAY = array node {
				A = int @ 100;
				B = int @ 500;
			};
		};
		
		// It is node hash validator
		Hash = hash node {
			A = string @ "Abc";
			B = string;
		};
		
	};
	
}

												

ライセンス

Copyright SO-SOFT

All rights reserved.

The redistribution and use in unmodified binary form are permitted for personal or commercial use, provided the following conditions are met:
・Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Third Party Notices

												

配布承認先一覧

以下のサイトはこのソフトの配布用ファイルの取り扱いについて、SO-SOFTが把握し正式に承認しているものです。
  • nuget.org : Nuget
  • 株式会社ベクター : Vectorソフトライブラリ
  • 本サイト
これら以外のサイトでの配布では、インストーラーファイルは改竄されている可能性も含め十分にご注意ください。