using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
using System.Text.RegularExpressions;
using ZennoLab.CommandCenter;
using ZennoLab.InterfacesLibrary;
using ZennoLab.InterfacesLibrary.ProjectModel;
using ZennoLab.InterfacesLibrary.ProjectModel.Collections;
using ZennoLab.InterfacesLibrary.ProjectModel.Enums;
using ZennoLab.Macros;
using Global.ZennoExtensions;
using ZennoLab.Emulation;
namespace ZennoLab.OwnCode
{
/// <summary>
/// A simple class of the common code
/// </summary>
public class CommonCode
{
/// <summary>
/// Lock this object to mark part of code for single thread execution
/// </summary>
public static object SyncObject = new object();
// Insert your code here
}
public static class IniFileHelper
{
public static int capacity = 512;
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
private static extern int GetPrivateProfileString(string section, string key, string defaultValue, StringBuilder value, int size, string filePath);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
static extern int GetPrivateProfileString(string section, string key, string defaultValue, [In, Out] char[] value, int size, string filePath);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
private static extern int GetPrivateProfileSection(string section, IntPtr keyValue, int size, string filePath);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool WritePrivateProfileString(string section, string key, string value, string filePath);
public static bool WriteValue(string section, string key, string value, string filePath)
{
return WritePrivateProfileString(section, key, value, filePath);
}
public static bool DeleteSection(string section, string filepath)
{
return WritePrivateProfileString(section, null, null, filepath);
}
public static bool DeleteKey(string section, string key, string filepath)
{
return WritePrivateProfileString(section, key, null, filepath);
}
public static string ReadValue(string section, string key, string filePath, string defaultValue = "")
{
var value = new StringBuilder(capacity);
GetPrivateProfileString(section, key, defaultValue, value, value.Capacity, filePath);
return value.ToString();
}
public static string[] ReadSections(string filePath)
{
// first line will not recognize if ini file is saved in UTF-8 with BOM
while (true)
{
char[] chars = new char[capacity];
int size = GetPrivateProfileString(null, null, "", chars, capacity, filePath);
if (size == 0)
{
return null;
}
if (size < capacity - 2)
{
string result = new String(chars, 0, size);
string[] sections = result.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries);
return sections;
}
capacity = capacity * 2;
}
}
public static string[] ReadKeys(string section, string filePath)
{
// first line will not recognize if ini file is saved in UTF-8 with BOM
while (true)
{
char[] chars = new char[capacity];
int size = GetPrivateProfileString(section, null, "", chars, capacity, filePath);
if (size == 0)
{
return null;
}
if (size < capacity - 2)
{
string result = new String(chars, 0, size);
string[] keys = result.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries);
return keys;
}
capacity = capacity * 2;
}
}
public static string[] ReadKeyValuePairs(string section, string filePath)
{
while (true)
{
IntPtr returnedString = Marshal.AllocCoTaskMem(capacity * sizeof(char));
int size = GetPrivateProfileSection(section, returnedString, capacity, filePath);
if (size == 0)
{
Marshal.FreeCoTaskMem(returnedString);
return null;
}
if (size < capacity - 2)
{
string result = Marshal.PtrToStringAuto(returnedString, size - 1);
Marshal.FreeCoTaskMem(returnedString);
string[] keyValuePairs = result.Split('\0');
return keyValuePairs;
}
Marshal.FreeCoTaskMem(returnedString);
capacity = capacity * 2;
}
}
}
}