using NLog; using System; using System.Collections.Generic; using System.Configuration; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace IOB.WIN.FileExp { public class utils { /// /// wrapper di log /// public static Logger lg; /// /// folder archiviazione dati configurazione (DATA\CONF) /// public static string confDir { get { return string.Format(@"{0}\{1}", Application.StartupPath, CRS("dataConfPath")); } } /// /// legge conf in formato char /// /// /// public static char CRC(string key) { char answ = '-'; try { answ = ConfigurationManager.AppSettings[key].ToCharArray()[0]; } catch { } return answ; } /// /// legge conf in formato stringa /// /// /// public static string CRS(string key) { string answ = ""; try { answ = ConfigurationManager.AppSettings[key].ToString(); } catch { } return answ; } /// /// legge conf in formato INT /// /// /// public static Int32 CRI(string key) { int answ = 0; try { answ = Convert.ToInt32(CRS(key)); } catch { } return answ; } /// /// legge conf in formato BOOLean /// /// /// public static bool CRB(string key) { bool answ = false; try { answ = Convert.ToBoolean(CRS(key)); } catch { } return answ; } /// /// Decodifica file allarme /// /// /// /// tipo memoria (R/D/...) /// indirizzo di partenza memoria /// dimensione singolo slot in byte /// protected static otherData decodeOtherData(string linea, char separator, string memPre, int baseAddr, int memSize) { string[] valori = linea.Split(separator); int shift = 0; try { shift = Convert.ToInt32(valori[0]) - 1; } catch { } string memAddr = string.Format("{0}{1}", memPre, baseAddr + shift * memSize); return new otherData(valori[0], memAddr, valori[1].Trim(), valori[2].Trim()); } /// /// Decodifica file MAP (caso ESA/IOT) /// /// /// /// indirizzo Byte: indirizzo di partenza memoria /// dimensione singolo slot in byte /// indirizzo bit: numero riga x calcolo indice bit /// protected static otherData decodeBitData(string linea, char separator, int ByteNum, int memSize, int BitNum) { string[] valori = linea.Split(separator); int shift = 0; try { shift = Convert.ToInt32(valori[0]) - 1; } catch { } int resto = 0; Math.DivRem(BitNum, 8, out resto); string memAddr = string.Format("{0}.{1}", ByteNum + shift * memSize, resto); return new otherData(valori[0], memAddr, valori[1].Trim(), valori[2].Trim()); } /// /// Decodifica file MAP (caso FANUC/OSAI/...) /// /// /// /// tipo memoria (R/D/...) /// indirizzo Byte: indirizzo di partenza memoria /// dimensione singolo slot in byte /// indirizzo bit: numero riga x calcolo indice bit /// protected static otherData decodeBitData(string linea, char separator, string memPre, int baseAddr, int memSize, int numRiga) { string[] valori = linea.Split(separator); int shift = 0; try { shift = (Convert.ToInt32(valori[0]) - 1) / (8 * memSize); } catch { } int resto = 0; Math.DivRem(numRiga, 8 * memSize, out resto); string memAddr = string.Format("{0}{1}.{2}", memPre, baseAddr + shift, resto); return new otherData(valori[0], memAddr, valori[1].Trim(), valori[2].Trim()); } /// /// Legge il file di conf di una MAP di informazioni da gestire con lettura set memoria /// /// nome vettore memoria /// file origine /// dimensione (in byte) della memoria /// dimensione (in byte) della memoria public static void loadConfFile(ref otherData[] vettoreConf, string nomeFile, int memSize, ref int numVett) { otherData lastData = new otherData(); int totRighe = 0; string linea; totRighe = File.ReadLines(nomeFile).Count(); // creo un vettore della dimensione corretta... conta anche commenti tanto poi riduco... vettoreConf = new otherData[File.ReadLines(nomeFile).Count()]; // carica da file... StreamReader file = new StreamReader(nomeFile); // leggo 1 linea alla volta... int numRiga = 0; int bitNum = 0; int byteNum = 0; while ((linea = file.ReadLine()) != null) { // SE non è un commento... if (linea.Substring(0, 1) != "#") { // se finisce per BIT allora processo bit-a-bit... if (linea.EndsWith("BOOL")) { try { string[] memIdx = linea.Split(utils.CRC("charSep"))[0].Split('.'); // calcolo bit e byte number... int.TryParse(memIdx[0], out byteNum); if (memIdx.Length > 1) { int.TryParse(memIdx[1], out bitNum); } else { bitNum = 0; } } catch { byteNum = 0; bitNum = 0; } lastData = decodeBitData(linea, utils.CRC("charSep"), byteNum, 1, bitNum); vettoreConf[numRiga] = lastData; } else { lastData = decodeOtherData(linea, utils.CRC("charSep"), "", 1, memSize); vettoreConf[numRiga] = lastData; } numRiga++; } } // salvo lunghezza file... try { numVett = Convert.ToInt32(lastData.memAddr) + 1; } catch { numVett = numRiga + 1; } // chiudo file file.Close(); // ora trimmo vettore al solo numero VERO dei valori caricati... Array.Resize(ref vettoreConf, numRiga); if (utils.CRB("verbose")) lg.Info(string.Format("Fine caricamento vettore di {0} variabili per file {1}", numRiga, nomeFile)); } } /// /// Dato generico (per decodifica) /// public class otherData { public string codNum; public string memAddr; public string varName; public string dataType; public otherData() { codNum = ""; memAddr = ""; varName = ""; dataType = ""; } public otherData(string _codNum, string _memAddr, string _varName, string _dataType) { codNum = _codNum; memAddr = _memAddr; varName = _varName; dataType = _dataType; } } }