Abbozzato metodo conversione

This commit is contained in:
Samuele Locatelli
2022-12-23 10:43:49 +01:00
parent 3a5d675801
commit 967757ad23
8 changed files with 570 additions and 24 deletions
+316
View File
@@ -0,0 +1,316 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace IobConf.Core
{
/// <summary>
/// Create a new INI file to store or load data
/// </summary>
public class IniFile
{
#region Public Fields
public string FileName;
#endregion Public Fields
#region Public Constructors
/// <summary>
/// Constructor
/// </summary>
/// <PARAM name="INIPath"></PARAM>
public IniFile(string INIPath)
{
FileName = INIPath;
}
#endregion Public Constructors
#region Public Methods
/// <summary>
/// Delete a key from section
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
public void IniDeleteKey(string Section, string Key)
{
WritePrivateProfileString(Section, Key, null, FileName);
}
/// <summary>
/// Completely remove one section
/// </summary>
/// <param name="Section"></param>
public void IniDeleteSection(string Section)
{
WritePrivateProfileSection(Section, null, FileName);
}
/// <summary>
/// Return true if section exists
/// </summary>
/// <param name="Section"></param>
/// <returns></returns>
public bool IniSectionExists(string Section)
{
int bytesReturned = 0;
const int bufferSize = 2048; // max is 32767
IntPtr pReturnedString = Marshal.AllocCoTaskMem(bufferSize);
try
{
bytesReturned = GetPrivateProfileSection(Section, pReturnedString, bufferSize, FileName);
}
finally
{
Marshal.FreeCoTaskMem(pReturnedString);
}
return (bytesReturned > 0);
}
/// <summary>
/// Read a boolean
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <returns></returns>
public bool ReadBoolean(string Section, string Key)
{
return (ReadInteger(Section, Key, 0) != 0);
}
/// <summary>
/// Read a boolean with default value
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <param name="DefaultVal"></param>
/// <returns></returns>
public bool ReadBoolean(string Section, string Key, bool DefaultVal)
{
int v = DefaultVal ? 1 : 0;
return (ReadInteger(Section, Key, v) != 0);
}
/// <summary>
/// Read an integer
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <returns></returns>
public int ReadInteger(string Section, string Key)
{
return GetPrivateProfileInt(Section, Key, 0, FileName);
//System.Convert.ToInt32(IniReadValue(Section, Key));
}
/// <summary>
/// Read an integer. If not found use default value
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <param name="DefaultVal"></param>
/// <returns></returns>
public int ReadInteger(string Section, string Key, int DefaultVal)
{
//int temp = System.Convert.ToInt32(IniReadString(Section, Key, Convert.ToString(DefaultVal)));
//return temp;
return GetPrivateProfileInt(Section, Key, DefaultVal, FileName);
}
/// <summary>
/// Read a complete section (keys=values)
/// </summary>
/// <param name="Section"></param>
/// Section name
/// <returns>
/// restituisce delle stringhe keys=values da suddividere appunto come key/val successivamente
/// </returns>
public string[] ReadSection(string Section)
{
const int bufferSize = 2048; // max is 32767
StringBuilder returnedString = new StringBuilder();
IntPtr pReturnedString = Marshal.AllocCoTaskMem(bufferSize);
try
{
int bytesReturned = GetPrivateProfileSection(Section, pReturnedString, bufferSize, FileName);
if (bytesReturned > 0)
{
//bytesReturned -1 to remove trailing \0
for (int i = 0; i < bytesReturned - 1; i++)
{
var currPointer = IntPtr.Add(pReturnedString, i);
var currChar = (char)Marshal.ReadByte(currPointer);
returnedString.Append(currChar);
}
}
}
finally
{
Marshal.FreeCoTaskMem(pReturnedString);
}
string sectionData = returnedString.ToString();
return sectionData.Split('\0');
}
/// <summary>
/// Read data from the Ini file
/// </summary>
/// <PARAM name="Section"></PARAM>
/// <PARAM name="Key"></PARAM>
/// <PARAM name="Path"></PARAM>
/// <returns></returns>
public string ReadString(string Section, string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp, 255, FileName);
return temp.ToString();
}
/// <summary>
/// Read a string. If not found use default value
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <param name="DefaultVal"></param>
/// <returns></returns>
public string ReadString(string Section, string Key, string DefaultVal)
{
string temp = ReadString(Section, Key);
if (temp == "") temp = DefaultVal;
return temp;
}
/// <summary>
/// Return true if value exists
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <returns></returns>
public bool ValueExists(string Section, string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp, 255, FileName);
return (i > 0);
}
/// <summary>
/// Write a boolean value
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <param name="Value"></param>
public void WriteBoolean(string Section, string Key, bool Value)
{
int flag = Value ? 1 : 0;
WriteString(Section, Key, Convert.ToString(flag));
}
/// <summary>
/// Write a double value
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <param name="Value"></param>
public void WriteDouble(string Section, string Key, double Value)
{
WriteString(Section, Key, Convert.ToString(Value, NumberFormatInfo.InvariantInfo));
}
/// <summary>
/// Write an integer value
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <param name="Value"></param>
public void WriteInteger(string Section, string Key, int Value)
{
WriteString(Section, Key, Convert.ToString(Value));
}
/// <summary>
/// Write data to the INI file
/// </summary>
/// <PARAM name="Section"></PARAM>
/// Section name
/// <PARAM name="Key"></PARAM>
/// Key Name
/// <PARAM name="Value"></PARAM>
/// Value Name
public void WriteString(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, FileName);
}
#endregion Public Methods
#region Private Methods
/// <summary>
/// GetPrivateProfileInt: import windows dll functions
/// </summary>
/// <param name="section"></param>
/// <param name="key"></param>
/// <param name="def"></param>
/// <param name="filePath"></param>
/// <returns></returns>
[DllImport("kernel32")]
private static extern int GetPrivateProfileInt(string section, string key, int def, string filePath);
/// <summary>
/// GetPrivateProfileSection: import windows dll functions
/// </summary>
/// <param name="section"></param>
/// <param name="retVal"></param>
/// <param name="size"></param>
/// <param name="filePath"></param>
/// <returns></returns>
[DllImport("kernel32")]
private static extern int GetPrivateProfileSection(string section, IntPtr retVal, uint size, string filePath);
/// <summary>
/// GetPrivateProfileString: import windows dll functions
/// </summary>
/// <param name="section"></param>
/// <param name="key"></param>
/// <param name="def"></param>
/// <param name="retVal"></param>
/// <param name="size"></param>
/// <param name="filePath"></param>
/// <returns></returns>
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
/// <summary>
/// WritePrivateProfileSection: import windows dll functions
/// </summary>
/// <param name="section"></param>
/// <param name="value"></param>
/// <param name="filePath"></param>
/// <returns></returns>
[DllImport("kernel32")]
private static extern bool WritePrivateProfileSection(string section, string value, string filePath);
/// <summary>
/// WritePrivateProfileString: import windows dll functions
/// </summary>
/// <param name="section"></param>
/// <param name="key"></param>
/// <param name="val"></param>
/// <param name="filePath"></param>
/// <returns></returns>
[DllImport("kernel32", CharSet = CharSet.Auto, BestFitMapping = false)]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
#endregion Private Methods
}
}
+47 -4
View File
@@ -20,6 +20,49 @@ namespace IobConf.Core
public IobConfTree()
{ }
/// <summary>
/// Restituisce un oggetto di conf leggendo INI ed effettuando conversione
/// </summary>
/// <param name="iniFilePath"></param>
/// <returns></returns>
public static IobConfTree LoadFromINI(string iniFilePath)
{
IobConfTree newConfObj = new IobConfTree();
try
{
// leggo file INI
IniFile fIni = new IniFile(iniFilePath);
// effettuo conversione
newConfObj = ConvertFromINI(fIni);
}
catch
{ }
return newConfObj;
}
/// <summary>
/// Restituisce un oggetto di conf leggendo INI ed effettuando conversione
/// </summary>
/// <param name="iniFilePath"></param>
/// <returns></returns>
protected static IobConfTree ConvertFromINI(IniFile fIni)
{
IobConfTree newConfObj = new IobConfTree();
try
{
// effettuo conversione...
// leggo vendor e modello...
newConfObj.Vendor = fIni.ReadString("MACHINE", "VENDOR", "STEAMWARE");
newConfObj.Model = fIni.ReadString("MACHINE", "MODEL", "NONE");
}
catch
{ }
return newConfObj;
}
/// <summary>
/// Codice Cliente/Installazione
/// </summary>
@@ -96,7 +139,7 @@ namespace IobConf.Core
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public string getJson()
public string GetJson()
{
string rawdata = JsonConvert.SerializeObject(this, Formatting.Indented);
return rawdata;
@@ -106,7 +149,7 @@ namespace IobConf.Core
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public string getYaml()
public string GetYaml()
{
var serializer = new SerializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
@@ -129,7 +172,7 @@ namespace IobConf.Core
bool answ = false;
try
{
string rawdata = getJson();
string rawdata = GetJson();
File.WriteAllText(filePath, rawdata);
answ = true;
}
@@ -147,7 +190,7 @@ namespace IobConf.Core
bool answ = false;
try
{
var rawdata = getYaml();
var rawdata = GetYaml();
File.WriteAllText(filePath, rawdata);
answ = true;
}
+61
View File
@@ -0,0 +1,61 @@
<div class="d-flex justify-content-between mb-2">
<div class="px-2">
<h1>Converter</h1>
Conversione INI --> YAML / JSON
</div>
<div class="px-2 text-end">
<h3>@CodIOB</h3>
<button class="btn btn-sm btn-dark" @onclick="() => UpdateConf()">Update Conf</button>
</div>
</div>
<div class="row">
<div class="col-4">
<div class="card">
<div class="card-header d-flex justify-content-between">
<div class="px-1">
<h2>INI</h2>
</div>
<div class="px-1">
<div class="input-group input-group-sm">
<input class="form-control small" type="text" @bind="@iniPath" placeholder="Ini Path" aria-label="Ini Path" aria-describedby="basic-addon2" />
<button class="btn btn-sm btn-primary" @onclick="() => LoadINI()">Load INI</button>
</div>
</div>
</div>
<div class="card-body small textConsensed">
<p>@confINI</p>
</div>
</div>
</div>
<div class="col-4">
<div class="card">
<div class="card-header d-flex justify-content-between">
<div class="px-1">
<h2>JSON</h2>
</div>
<div class="px-1">
<button class="btn btn-sm btn-primary" @onclick="() => SaveJson()">Save Json</button>
</div>
</div>
<div class="card-body small textConsensed">
<p>@confJson</p>
</div>
</div>
</div>
<div class="col-4">
<div class="card">
<div class="card-header d-flex justify-content-between">
<div class="px-1">
<h2>YAML</h2>
</div>
<div class="px-1">
<button class="btn btn-sm btn-primary" @onclick="() => SaveYaml()">Save Yaml</button>
</div>
</div>
<div class="card-body small textConsensed">
<p>@confYaml</p>
</div>
</div>
</div>
</div>
+111
View File
@@ -0,0 +1,111 @@
using IobConf.Core;
using Microsoft.AspNetCore.Components;
namespace IobConf.UI.Components
{
public partial class IniConverter
{
#region Public Methods
public async Task LoadINI()
{
checkOutDir();
await Task.Delay(1);
rawFileContent = File.ReadAllText(iniPath);
confINI=getMarkup(rawFileContent);
CurrentConf = IobConfTree.LoadFromINI(iniPath);
updateOutConf();
}
public async Task SaveJson()
{
checkOutDir();
await Task.Delay(1);
string fileName = Path.Combine(baseDir, $"Conf.json");
CurrentConf.SaveJson(fileName);
}
public async Task SaveYaml()
{
checkOutDir();
await Task.Delay(1);
string fileName = Path.Combine(baseDir, $"Conf.yml");
CurrentConf.SaveYaml(fileName);
}
public async Task UpdateConf()
{
await Task.Delay(1);
CodIOB = $"NewIOB_{DateTime.Now.Second:00}";
CurrentConf = new IobConfTree()
{
CodIOB = CodIOB
};
updateOutConf();
}
private void updateOutConf()
{
// aggiorno conf JSON/YAML
confJson = getMarkup(CurrentConf.GetJson());
confYaml = getMarkup(CurrentConf.GetYaml());
//confJson = (MarkupString)CurrentConf.GetJson().Replace("\n", "<br/>").Replace(" ", "&nbsp;&nbsp;");
//confYaml = (MarkupString)CurrentConf.GetYaml().Replace("\n", "<br/>").Replace(" ", "&nbsp;&nbsp;");
}
/// <summary>
/// Converte la stringa in formato markup valido
/// </summary>
/// <param name="rawData"></param>
/// <returns></returns>
protected MarkupString getMarkup(string rawData)
{
return new MarkupString(rawData.Replace("\n", "<br/>").Replace(" ", "&nbsp;&nbsp;"));
}
protected string iniPath = @"C:\temp\DATA\CONF\SIMUL_01.ini";
#endregion Public Methods
#region Protected Fields
protected string baseDir = @"c:\temp\IobConf";
protected string CodIOB = "NewIOB_00";
#endregion Protected Fields
#region Protected Properties
protected MarkupString confINI { get; set; }
protected MarkupString confJson { get; set; }
protected MarkupString confYaml { get; set; }
protected string rawFileContent = "";
protected IobConfTree CurrentConf { get; set; } = new IobConfTree();
#endregion Protected Properties
#region Protected Methods
protected override async Task OnInitializedAsync()
{
await UpdateConf();
}
#endregion Protected Methods
#region Private Methods
private void checkOutDir()
{
if (!Directory.Exists(baseDir))
{
Directory.CreateDirectory(baseDir);
}
}
#endregion Private Methods
}
}
+2 -2
View File
@@ -32,8 +32,8 @@ namespace IobConf.UI.Components
CodIOB = CodIOB
};
// aggiorno conf JSON/YAML
confJson = (MarkupString)CurrentConf.getJson().Replace("\n", "<br/>").Replace(" ", "&nbsp;&nbsp;");
confYaml = (MarkupString)CurrentConf.getYaml().Replace("\n", "<br/>").Replace(" ", "&nbsp;&nbsp;");
confJson = (MarkupString)CurrentConf.GetJson().Replace("\n", "<br/>").Replace(" ", "&nbsp;&nbsp;");
confYaml = (MarkupString)CurrentConf.GetYaml().Replace("\n", "<br/>").Replace(" ", "&nbsp;&nbsp;");
}
#endregion Public Methods
+5
View File
@@ -0,0 +1,5 @@
@page "/Converter"
<PageTitle>Converter</PageTitle>
<IniConverter></IniConverter>
+28
View File
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using System.Net.Http;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.Web.Virtualization;
using Microsoft.JSInterop;
using IobConf.UI;
using IobConf.UI.Shared;
using IobConf.UI.Components;
namespace IobConf.UI.Pages
{
public partial class Converter
{
private int currentCount = 0;
private void IncrementCount()
{
currentCount++;
}
}
}
-18
View File
@@ -1,18 +0,0 @@
@page "/counter"
<PageTitle>Counter</PageTitle>
<h1>Counter</h1>
<p role="status">Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
@code {
private int currentCount = 0;
private void IncrementCount()
{
currentCount++;
}
}