56cb0966fe
- aggiunta tags su richiesta
87 lines
3.0 KiB
C#
87 lines
3.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace ConfMan.IOB.CApp
|
|
{
|
|
public class TagManager
|
|
{
|
|
protected string _filePath;
|
|
public TagManager(string filePath)
|
|
{
|
|
_filePath = filePath;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Aggiunge i tag al file con cui è stato inizializzato
|
|
/// </summary>
|
|
/// <param name="tags"></param>
|
|
/// <returns></returns>
|
|
public bool addTagList(Dictionary<string, string> tags)
|
|
{
|
|
bool fatto = false;
|
|
// in primis vedo se ho tags da scrivere
|
|
if (tags.Count > 0)
|
|
{
|
|
// verifico file esista
|
|
if (File.Exists(_filePath))
|
|
{
|
|
// leggo file
|
|
var fileRows = File.ReadAllLines(_filePath).ToList();
|
|
//string fileContent = File.ReadAllText(_filePath);
|
|
bool tagFound = false;
|
|
string tagHead = "[TAGS]";
|
|
int currIdx = 0;
|
|
int rowTagIndex = 0;
|
|
foreach (var riga in fileRows)
|
|
{
|
|
if (riga.Contains(tagHead))
|
|
{
|
|
tagFound = true;
|
|
rowTagIndex = currIdx;
|
|
break;
|
|
}
|
|
currIdx++;
|
|
}
|
|
if (!tagFound)
|
|
{
|
|
fileRows.Add("");
|
|
fileRows.Add("; Tags manuali");
|
|
fileRows.Add(tagHead);
|
|
rowTagIndex = fileRows.Count() - 1;
|
|
}
|
|
int numRows = fileRows.Count();
|
|
// ora aggiungo le varie chiavi dal dizionario
|
|
foreach (var tag in tags)
|
|
{
|
|
bool rowChanged = false;
|
|
// cerco se ci fosse una riga precedente e la levo... vengono DOPO il tags finbo alla lunghezza massima precedente
|
|
for (int i = rowTagIndex; i < numRows; i++)
|
|
{
|
|
if (fileRows[i].Contains($"{tag.Key}="))
|
|
{
|
|
// sostituisco con vuoto
|
|
fileRows[i] = $"{tag.Key}={tag.Value}";
|
|
rowChanged = true;
|
|
}
|
|
}
|
|
// aggiungo tag in coda se no sostituito
|
|
if (!rowChanged)
|
|
{
|
|
fileRows.Add($"{tag.Key}={tag.Value}");
|
|
}
|
|
}
|
|
// salvo!
|
|
File.WriteAllLines(_filePath, fileRows);
|
|
fatto = true;
|
|
}
|
|
}
|
|
return fatto;
|
|
}
|
|
|
|
|
|
}
|
|
}
|