Merging changes

This commit is contained in:
Taha committed 2023-09-04 16:25:40 -04:00
1 parent c4216d2272
commit 797816be7e
437 files changed
+54228 -28

No files matched your search

@@ -0,0 +1,178 @@
using System;
using System.Runtime.Serialization;
using System.Collections;
using System.Reflection;
using System.IO;
using System.Text;
using BCG;
//using BCG.Utilities;
namespace BCG
{
public class MetaData : ICloneable
{
private Hashtable myHash = new Hashtable();
int mySize = -1;
public MetaData()
{}
public MetaData(object o)
{copyFrom(o);}
public MetaData(Stream s)
{
long initPos = s.Position;
load(s);
mySize = Convert.ToInt32(s.Position - initPos);
}
public string this[string name]
{
get
{return (string) myHash[name];}
set
{
if (value != null)
myHash[name] = value;
}
}
public void add(string name, string [] values)
{
StringBuilder sb = new StringBuilder();
foreach (string s in values)
{
if (sb.Length != 0)
sb.Append(" ");
sb.Append(s);
}
myHash[name] = sb.ToString();
}
public int getAsInt(string name)
{return Int32.Parse(this[name]);}
public string[] getAsStringArray (string name)
{
if (this[name] != "" && this[name] != null)
return this[name].Split(' ');
else
return new string[0];
}
public int size()
{
if (mySize < 0)
{
MemoryStream ms = new MemoryStream();
this.save(ms);
return mySize = (int)ms.Position;
}
else
return mySize;
}
public bool isEqual (MetaData md)
{
if (md.myHash.Keys.Count != myHash.Keys.Count)
return false;
foreach (string k in myHash.Keys)
if (!myHash[k].Equals(md.myHash[k]))
return false;
return true;
}
public void load(Stream s)
{
myHash = new Hashtable();
string [] state =
new BinaryReader(s).ReadString().Split('\n');
for (int i = 0; i < state.Length-1; i+= 2)
myHash[state[i]] = state[i+1];
}
public void save(Stream s)
{
new BinaryWriter(s).Write(this.ToString());
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
foreach (string key in myHash.Keys)
sb.AppendFormat("{0}\n{1}\n",key,myHash[key]);
return sb.ToString();
}
public void copyFrom(object o)
{
this["op-code"] = o.GetType().Name;
foreach (FieldInfo fi in o.GetType().GetFields())
{
if (fi.GetValue(o) == null)
continue;
else if (fi.GetValue(o) is String[])
add(fi.Name, (string[])fi.GetValue(o));
else
myHash[fi.Name] = fi.GetValue(o).ToString();
}
}
public void copyTo(object o)
{
foreach (FieldInfo fi in o.GetType().GetFields())
{
string value = this[fi.Name];
//this should only be here temporarily, while the new decompression changes are waiting to go into prod.
if (value == null)
{
//BlueEventLog.logEvent("Error: Metadata missing field " + fi.Name, BlueEventLog2Level.MissingMetadata);
}
else
{
if (fi.FieldType.Equals(typeof(string)))
fi.SetValue(o, value);
else if (fi.FieldType.Equals(typeof(int)))
fi.SetValue(o, Int32.Parse(value));
else if (fi.FieldType.Equals(typeof(long)))
fi.SetValue(o, long.Parse(value));
else if (fi.FieldType.Equals(typeof(double)))
fi.SetValue(o, double.Parse(value));
else if (fi.FieldType.Equals(typeof(DateTime)))
fi.SetValue(o, DateTime.Parse(value));
else if (fi.FieldType.Equals(typeof(Boolean)))
fi.SetValue(o, Boolean.Parse(value));
else if (fi.FieldType.Equals(typeof(TimeSpan)))
fi.SetValue(o, TimeSpan.Parse(value));
else if (fi.FieldType.Equals(typeof(String[])))
fi.SetValue(o, value.Split(' '));
else
throw new Exception("Field type not supported");
}
}
}
public object Clone()
{
MemoryStream ms = new MemoryStream();
this.save(ms);
ms.Seek(0,SeekOrigin.Begin);
return new MetaData(ms);
}
}
}