Entwicklung_BLAZOR/InterneDLLs/LambertzSocketKommunikation/LambertzSocketKommunikation/LaPoSatzartFeld.cs
2025-08-23 19:30:21 +02:00

458 lines
14 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace De.Lambertz.Socket
{
public sealed class LaPoSatzartFeld
{
public LaPoSatzart Parent { get; private set; }
public string Name { get; private set; }
public string Description { get; private set; }
public int Start { get; private set; }
public int Length { get; private set; }
public char Type { get; private set; }
public int NachKomma { get; private set; }
public string Inhalt { get; private set; }
public LaPoSatzartFeld(LaPoSatzart parent, String line)
{
Parent = parent;
string buf = line.PadRight(23);
///Zerlegen des Strings in die Felder
Name = buf.Substring(2, 10).Trim();
Type = buf[12];
Start = Convert.ToInt32(buf.Substring(13, 5));
Length = Convert.ToInt32(buf.Substring(18, 4));
NachKomma = (buf[22] == ' ' ? 0 : Convert.ToInt32(buf[22].ToString()));
Description = buf.Substring(23).Trim();
}
public void Clear()
{
switch (Type)
{
// Zahl
case 'N':
case 'S':
SetDouble(0);
break;
// Datum
case 'L':
SetDate(DateTime.MinValue);
break;
// Uhrzeit
case 'T':
SetTime(DateTime.MinValue);
break;
default:
Inhalt = "";
break;
}
}
public bool SetString(string value)
{
string strVal;
double dblVal;
DateTime datVal;
bool retVal = true;
switch (Type)
{
// Zahl
case 'N':
case 'S':
dblVal = 0;
strVal = value.Trim();
if (strVal != "" && !Double.TryParse(strVal, out dblVal))
{
Clear();
retVal = false;
Parent.FehlerText = "Das Feld ist nicht numerisch.";
}
else
{
retVal = SetDouble(dblVal);
}
break;
// Datum
case 'L':
datVal = DateTime.MinValue;
strVal = value.Trim();
if (strVal != "" && !DateTime.TryParse(strVal, out datVal))
{
Clear();
retVal = false;
Parent.FehlerText = "Das Feld ist kein gültiges Datum.";
}
else
{
retVal = SetDate(datVal);
}
break;
// Uhrzeit
case 'T':
datVal = DateTime.MinValue;
strVal = value.Trim();
if (strVal != "" && !DateTime.TryParse(strVal, out datVal))
{
Clear();
retVal = false;
Parent.FehlerText = "Das Feld ist keine gültige Uhrzeit.";
}
else
{
retVal = SetTime(datVal);
}
break;
// String
default:
strVal = value.TrimEnd();
if (strVal.Length > Length)
{
retVal = false;
// Nicht schön wenn hinten abgeschnitten würde...
Inhalt = strVal;
Parent.FehlerText = String.Format("Das Feld ist zu lang (max. {0} Zeichen).", Length);
}
else
{
Inhalt = strVal;
}
break;
}
return retVal;
}
public bool SetDouble(double value)
{
bool retVal = true;
string strVal;
decimal decVal = Convert.ToDecimal(value) * (decimal)Math.Pow(10, NachKomma);
double dblVal = (double)Math.Round(decVal, 0, MidpointRounding.AwayFromZero);
if (dblVal < 0)
strVal = Parent.Handler.reverseNegativeCheck(dblVal);
else
strVal = dblVal.ToString();
Inhalt = strVal;
if (strVal.Length > Length)
{
retVal = false;
// Nicht schön, wenn vorne abschnitten würde...
Inhalt = strVal;
if (NachKomma == 0)
Parent.FehlerText = String.Format("Das Feld ist zu lang (max. {0} Ziffern).", Length);
else
Parent.FehlerText = String.Format("Das Feld ist zu lang (max. {0} Vor- und {1} Nachkommastellen).", Length - NachKomma, NachKomma);
}
else
{
Inhalt = strVal;
}
return retVal;
}
public bool SetDecimal(decimal value)
{
bool retVal = true;
string strVal;
decimal decVal = value * (decimal)Math.Pow(10, NachKomma);
double dblVal = (double)Math.Round(decVal, 0, MidpointRounding.AwayFromZero);
if (dblVal < 0)
strVal = Parent.Handler.reverseNegativeCheck(dblVal);
else
strVal = dblVal.ToString();
Inhalt = strVal;
if (strVal.Length > Length)
{
retVal = false;
// Nicht schön, wenn vorne abschnitten würde...
Inhalt = strVal;
if (NachKomma == 0)
Parent.FehlerText = String.Format("Das Feld ist zu lang (max. {0} Ziffern).", Length);
else
Parent.FehlerText = String.Format("Das Feld ist zu lang (max. {0} Vor- und {1} Nachkommastellen).", Length - NachKomma, NachKomma);
}
else
{
Inhalt = strVal;
}
return retVal;
}
public bool SetInt(int value)
{
bool result = true;
string strVal;
if (value < 0)
strVal = Parent.Handler.reverseNegativeCheck(value);
else
strVal = value.ToString();
Inhalt = strVal;
if (strVal.Length > Length)
{
result = false;
// Nicht schön, wenn vorne abschnitten würde...
Inhalt = strVal;
Parent.FehlerText = String.Format("Das Feld ist zu lang (max. {0} Ziffern).", Length);
}
else
{
Inhalt = strVal;
}
return result;
}
public bool SetBool(bool value)
{
bool result = true;
if(value)
{
Inhalt = "1";
}
else
{
Inhalt = "0";
}
return result;
}
public bool SetDate(DateTime value)
{
bool retVal = true;
if (value == DateTime.MinValue)
Inhalt = "01.01.0001";
else
Inhalt = String.Format("{0:dd.MM.yyyy}", value);
return retVal;
}
public bool SetTime(DateTime value)
{
bool retVal = true;
if (value == DateTime.MinValue)
Inhalt = "00:00:00";
else
Inhalt = String.Format("{0:HH:mm:ss}", value);
return retVal;
}
public string GetString()
{
switch(Type)
{
case 'N':
case 'S':
return GetDouble().ToString();
default:
return Inhalt;
}
}
public double GetDouble()
{
decimal decVal;
if (Inhalt == "")
decVal = 0;
else
decVal = Convert.ToDecimal(Inhalt) / (decimal)Math.Pow(10, NachKomma);
return (double)decVal;
}
public decimal GetDecimal()
{
decimal decVal;
if (Inhalt == "")
decVal = 0;
else
decVal = Convert.ToDecimal(Inhalt) / (decimal)Math.Pow(10, NachKomma);
return decVal;
}
public DateTime GetDate()
{
DateTime dateVal;
if (Inhalt == "")
{
dateVal = DateTime.MinValue;
}
else
{
try
{
if (Inhalt.Length < 11)
{
String[] zerlegen = Inhalt.Split('.');
dateVal = new DateTime(Convert.ToInt32(zerlegen[2]), Convert.ToInt32(zerlegen[1]), Convert.ToInt32(zerlegen[0]));
}
else
{
dateVal = Convert.ToDateTime(Inhalt);
}
}
catch (Exception)
{
dateVal = Convert.ToDateTime(Inhalt);
}
}
return dateVal;
}
public int GetInt()
{
int val;
if (Inhalt == "")
val = 0;
else
val = Convert.ToInt32(Inhalt);
return val;
}
public bool GetBool()
{
bool val;
if (Inhalt == "" || Inhalt == "0")
val = false;
else
val = true;
return val;
}
public string StringInhalt
{
get
{
return GetString();
}
set
{
SetString(value);
}
}
public double DoubleInhalt
{
get
{
return GetDouble();
}
set
{
SetDouble(value);
}
}
// Setzt den Inhalt in voller Länge!
public void SetRawInhalt(string value)
{
switch (Type)
{
// Zahlen mit führenden Nullen auffüllen
case 'N':
case 'S':
Inhalt = negativeCheck(value.PadLeft(Length, '0'));
break;
default:
Inhalt = value.TrimEnd();
break;
}
}
private String negativeCheck(String zahl)
{
String result = zahl;
String last = null;
bool build = false;
//Nachsehen nach letzter "Ziffer"
switch (zahl.Substring(zahl.Length - 1))
{
case "ü":
last = "0";
build = true;
break;
case "J":
last = "1";
build = true;
break;
case "K":
last = "2";
build = true;
break;
case "L":
last = "3";
build = true;
break;
case "M":
last = "4";
build = true;
break;
case "N":
last = "5";
build = true;
break;
case "O":
last = "6";
build = true;
break;
case "P":
last = "7";
build = true;
break;
case "Q":
last = "8";
build = true;
break;
case "R":
last = "9";
build = true;
break;
}
if (build)
{
result = "-" + zahl.Substring(0, zahl.Length - 1) + last;
}
return result;
}
// Gibt den Inhalt in voller Länge zurück!
public string GetRawInhalt()
{
string strVal;
switch (Type)
{
// Zahlen mit führenden Nullen auffüllen
case 'N':
case 'S':
strVal = Inhalt.PadLeft(Length, '0');
break;
// Texte variabler Länge nicht auffüllen
case 'V':
strVal = Inhalt;
break;
// Rest hinten mit Leerzeichen auffüllen
default:
strVal = Inhalt.PadRight(Length);
break;
}
// Hier knallen lassen wenn der Inhalt nicht passt
if(strVal.Length > Length)
{
throw (new Exception(String.Format("Das Feld {0} ist zu lang: '{1}' (max. {2} Zeichen).", Name, Inhalt, Length)));
}
return strVal;
}
}
}