access returned object in another class - c#

I'm trying to read id3 tags from a directory and I found some code online i've been trying to understand.
I understand most of it except the method GetTag(), it creates a new instance and returns an object but i can't access it from my main program.
public class Mp3Reader
{
private string _fileName;
private Stream _stream;
private byte[] data;
private const int SIZE = 128;
public Mp3Reader(string fileName)
{
_fileName = fileName;
_stream = new FileStream(_fileName, FileMode.Open, FileAccess.Read);
}
public Mp3Tag GetTag()
{
Mp3Tag tag = new Mp3Tag();
data = new byte[SIZE];
_stream.Seek(-128, SeekOrigin.End);
_stream.Read(data, 0, SIZE);
//_stream.Close();
byte b1 = data[0];
byte b2 = data[1];
byte b3 = data[2];
if ((Convert.ToChar(b1) != 'T') || (Convert.ToChar(b2) != 'A') || (Convert.ToChar(b3) != 'G'))
{
throw new Exception("This File is NOT a MP3 file with ID3 v1");
}
for (int i = 3; i < 33; i++)
{
if (data[i] != 0)
tag.Title += Convert.ToChar(data[i]);
}
for (int i = 33; i < 63; i++)
{
if (data[i] != 0)
tag.Artist += Convert.ToChar(data[i]);
}
for (int i = 63; i < 93; i++)
{
if (data[i] != 0)
tag.Album += Convert.ToChar(data[i]);
}
for (int i = 93; i < 97; i++)
{
if (data[i] != 0)
tag.Year += Convert.ToChar(data[i]);
}
for (int i = 97; i < 127; i++)
{
if (data[i] != 0)
tag.Comment += Convert.ToChar(data[i]);
}
tag.Genere = data[127].ToString();
return tag;
}
}
main
static void Main(string[] args)
{
string folder = #"D:\\508-507-2209 (2017)";
var files = Directory.GetFiles(folder);
foreach(var val in files)
{
Mp3Reader tag2 = new Mp3Reader(val);
tag2.GetTag();
Console.WriteLine("");
}
}
mp3tag.cs
public class Mp3Tag
{
public string Title { get; set; }
public string Artist { get; set; }
public string Album { get; set; }
public string Year { get; set; }
public string Genere { get; set; }
public string Comment { get; set; }
}
If I manually Console.WriteLine(tag.xxx) right above my return it'll output to the terminal fine, what I don't understand is what to do with the "tag" variable that it created. why can't i access tag.Title in my main program?
tag.Genere = data[127].ToString();
Console.WriteLine("Title: " + tag.Title);
Console.WriteLine("Artist: " + tag.Artist);
Console.WriteLine("Album: " + tag.Album); ;
Console.WriteLine("Year: " + tag.Year);
Console.WriteLine("Comments: " + tag.Comment);
Console.WriteLine("Genere" + tag.Genere);
return tag;

shouldn't this be
static void Main(string[] args)
{
string folder = #"D:\\508-507-2209 (2017)";
var files = Directory.GetFiles(folder);
foreach(var val in files)
{
Mp3Reader tag2 = new Mp3Reader(val);
Mp3Tag mp3tag = tag2.GetTag(); // <- this here
Console.WriteLine("");
}

Related

How to implement lesssignificant24bytes Function

Example :
Input (accountId): 63617264686F6C6465726E616D654077616C6C657470726F 76696465722E636F6D
Expected Output : 123CCB2F30BA420B17F837DF60E2FC9D6965A74476849FC D43A640F792A2B358
public String accountIdHash (String accountId)
{
String random8Bytes = 123CCB2F30BA420B
return random8Bytes + lessSignificant24bytes(strongerHash(accountId + random8Bytes))
}
public String strongerHash(String dataToHash)
{
String currentHash = dataToHash;
for (int i = 0; i < 5000; i++)
{
currentHash = sha256(currentHash);
}
return sha256(sha256(dataToHash) + currentHash);
}

Network Map with full detail and hierarchy

I wrote a piece of code to run from the first IP address to the last and retrieve the MAC and Hostname of devices in my network.
But, i do not know how to get the hierachy of then. Information like what router is the device conected (via Cable of WiFi also). And some routers are not managed (they don't have IP - they are just "switchs").
First, i didn't think it was possible, since a "tracert" on the CMD does not show then, but when i call the "Network Complete Map" on Windows Control Panel, they get all the information i need - so there is a way. See the image below:
The "switchs" circled in red have no IP, the "TL-xxxx" are routers with DHCP turned of. The "gateway" is a server that has the DHCP and provides connection to the internet.
Thus far, i wrote the following code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Net;
using System.Runtime.InteropServices;
namespace Network
{
[Serializable] public sealed class Map
{
private bool mMARKED = true;
private long mDB = 0L;
private string mIP = string.Empty;
private string mMAC = string.Empty;
private string mBRAND = string.Empty;
private string mNAME = string.Empty;
public bool Save { get { return this.mMARKED; } set { this.mMARKED = value; } }
public long DBId { get { return this.mDB; } set { this.mDB = value; } }
public string IP { get { return this.mIP; } set { this.mIP = value; } }
public string MAC { get { return this.mMAC; } set { this.mMAC = value; } }
public string BRAND { get { return this.mBRAND; } set { this.mBRAND = value; } }
public string NAME { get { return this.mNAME; } set { this.mNAME = value; } }
}
[Serializable] public sealed class Scanner
{
public const string WebOUIFile = "http://standards-oui.ieee.org/oui.txt";
public const string LocalOUIFileName = "oui.txt";
private const long MaxOUIAge = (TimeSpan.TicksPerDay * 90L);
internal Dictionary<string, string> OUIList;
private List<Map> mDevices = new List<Map>(50);
public List<Map> Devices { get { return this.mDevices; } }
public static Scanner Scan;
public Thread Worker;
public bool AutoSave { get; set; }
private string Node;
private byte mInitial;
public static string UploadPath { get; set; }
public byte Initial { get { return this.mInitial; } set { this.mInitial = value; } }
public byte Current { get; private set; }
public byte Limit { get; set; }
public bool Working { get; private set; }
public string Errors;
public string Message { get; private set; }
public bool UpdatingOUI { get; private set; }
public void Interrupt()
{
this.Working = false;
if (this.Worker != null)
{
this.Worker.Abort();
this.Worker = null;
}
this.Node = string.Empty;
this.Initial = 0;
this.Current = 0;
this.Limit = 0;
this.Working = false;
}
public void ToDestroy()
{
this.Interrupt();
this.Errors = string.Empty;
this.Message = string.Empty;
}
public void Stop(bool immediate) { if (immediate) { this.Interrupt(); } }
[DllImport("iphlpapi.dll", SetLastError = true, CharSet = CharSet.Auto)] private static extern int SendARP(int DestIP, int SrcIP, out long pMacAddr, ref int PhyAddrLen);
public void ToBegin() { }
public void Stop() { this.Stop(true); }
private static int IPToInt(string Expression)
{
try
{
byte[] IPAddress = System.Net.IPAddress.Parse(Expression).GetAddressBytes();
return (Convert.ToInt32(IPAddress[3]) << 24) | (Convert.ToInt32(IPAddress[2]) << 16) | (Convert.ToInt32(IPAddress[1]) << 8) | Convert.ToInt32(IPAddress[0]);
} catch { return 0; }
}
private Map GetBasics(string IPString)
{
int res = Scanner.IPToInt(IPString);
if (res > 0)
{
long mem = 0L;
int PhyAddrLen = 6;
if (Scanner.SendARP(res, 0, out mem, ref PhyAddrLen) == 0)
{
Map dev = new Map();
byte[] macbytes = BitConverter.GetBytes(mem);
dev.IP = IPString;
string Tmp = BitConverter.ToString(macbytes, 0, 3);
if (this.OUIList != null && this.OUIList.ContainsKey(Tmp)) { dev.BRAND = this.OUIList[Tmp]; }
dev.MAC = Tmp + "-" + BitConverter.ToString(macbytes, 3, 3);
try { dev.NAME = Dns.GetHostEntry(IPString).HostName.ToLower(); } catch { dev.NAME = "unknow"; }
return dev;
}
}
return null;
}
private static void GetNode(ref string IP, ref string Node, ref byte Device)
{
string[] NodeComp = IP.Split('.');
Node = NodeComp[0] + "." + NodeComp[1] + "." + NodeComp[2] + ".";
Device = Convert.ToByte(NodeComp[3]);
}
public static Dictionary<string, string> DonwloadOUTFile(bool ForceUpdate = true)
{
Dictionary<string, string> List = null;
try
{
string Aux = Scanner.UploadPath;
if (Aux == null) { Aux = string.Empty; }
else if (Aux != string.Empty)
{
string Tmp = Aux + "~" + Scanner.LocalOUIFileName;
Aux += Scanner.LocalOUIFileName;
bool FileExists = File.Exists(Aux);
if (FileExists && ((DateTime.UtcNow.Ticks - (new FileInfo(Aux)).CreationTimeUtc.Ticks) > Scanner.MaxOUIAge))
{
File.Delete(Aux);
ForceUpdate = true;
}
string Aux2 = string.Empty;
if (ForceUpdate)
{
List = new Dictionary<string, string>(25000);
using (WebClient Downloader = new WebClient()) { Downloader.DownloadFile(Scanner.WebOUIFile, Tmp); }
using (StreamReader Reader = new StreamReader(Tmp))
using (StreamWriter Writer = new StreamWriter(Aux))
{
do
{
Aux = Reader.ReadLine();
if (Aux.ToLower().Contains("(hex)"))
{
Aux2 = Aux.Substring(0, 8).ToUpper();
Aux = Aux.Substring(Aux.LastIndexOf('\t') + 1);
if (!List.ContainsKey(Aux2))
{
List.Add(Aux2, Aux);
Writer.WriteLine(Aux2 + "\t" + Aux);
}
}
} while (Reader.Peek() >= 0);
Reader.Close();
Writer.Close();
}
try { File.Delete(Tmp); } catch { /* NOTHING */ }
}
else if (FileExists)
{
List = new Dictionary<string, string>(25000);
using (StreamReader Reader = new StreamReader(Aux))
{
do
{
Aux = Reader.ReadLine();
if (Aux != null && Aux.Length > 9)
{
Aux2 = Aux.Substring(0, 8);
if (!List.ContainsKey(Aux2)) { List.Add(Aux2, Aux.Substring(9)); }
}
} while (Reader.Peek() >= 0);
Reader.Close();
}
}
}
}
catch
{
if (List != null) { List.Clear(); }
List = null;
}
return List;
}
private void ReadScaner()
{
this.UpdatingOUI = true;
try { this.OUIList = Scanner.DonwloadOUTFile(ForceUpdate: false); } catch { /* NOTHING */ }
this.UpdatingOUI = false;
if (this.OUIList == null || this.OUIList.Count == 0) { this.Errors += "\nErrorOUIFileDownload"; }
Map Dev = null;
this.Current = this.Initial;
if (this.Limit < this.Initial)
{
Dev = this.GetBasics(this.Node + this.Initial.ToString());
if (Dev != null) { this.Devices.Add(Dev); }
}
else
{
bool ToAdd = true;
while (this.Current <= this.Limit)
{
Dev = this.GetBasics(this.Node + this.Current.ToString());
this.Current += 1;
if (Dev != null)
{
ToAdd = true;
foreach (Map iDev in this.Devices)
{
if (iDev.MAC == Dev.MAC)
{
ToAdd = false;
break;
}
}
if (ToAdd) { this.Devices.Add(Dev); }
}
}
}
this.Message = "Finished!";
this.Interrupt();
}
public void GetRange(string IPInitial, byte Limit, bool AutoSave = true)
{
if (!this.Working)
{
this.AutoSave = AutoSave;
this.Working = true;
Scanner.GetNode(ref IPInitial, ref this.Node, ref this.mInitial);
this.Limit = Limit;
this.Worker = new Thread(this.ReadScaner);
this.Worker.IsBackground = true;
this.ToBegin();
this.Worker.Start();
}
}
public static void GetRange(bool AutoSave, string IPInitial, byte Limit)
{
if (Scanner.Scan == null)
{
Scanner.Scan = new Scanner();
Scanner.Scan.GetRange(IPInitial, Limit, AutoSave: AutoSave);
}
}
~Scanner()
{
if (this.OUIList != null)
{
this.OUIList.Clear();
this.OUIList = null;
}
}
}
}
How do i make that code able to get the hierarchy like windows does?

How can I convert the C# code to python?

I am trying to read a large data file. I have the following code in C#, which can pick up the rows meet the requirement and save them into a separate file. The whole code takes about 20mins to run.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1 {
public class CSVFileConverter {
public string FilePath { get; set; }
public string OutputDirectory { get; set; }
public int numStartLine { get; set; }
public string strFeature { get; set; }
public string[] colNames {get;set;}
private string strcolNames;
private int maxNumOfFiles;
private int m_numofLines = -1;
public int numofLines {
get {
return this.m_numofLines;
}
}
private int colIndex;
private Dictionary<int, StreamWriter> dictSW = new Dictionary<int, StreamWriter>();
public CSVFileConverter(string filepath, string output, string feature, int numstartline) {
this.FilePath = filepath;
this.OutputDirectory = output;
this.strFeature = feature;
this.numStartLine = numstartline;
this.GetColNames();
this.Converter();
this.CloseStreams();
}
private void GetColNames() {
try {
var strline = ReadCSVLines(this.FilePath).First();
strcolNames = strline;
colNames = strline.Split(',');
if (!colNames.Contains(this.strFeature)) {
throw new Exception(string.Format("The File doesn't have this specified feature: {0}", this.strFeature));
} else {
this.colIndex = Array.IndexOf(colNames, this.strFeature);
}
}catch (Exception ex){
Console.WriteLine(ex);
Environment.Exit(0);
}
}
private void OpenFiles() {
for (int i = 0; i <= this.maxNumOfFiles; i++) {
}
}
private void Converter() {
foreach (var strline in ReadCSVLines(this.FilePath)) {
m_numofLines++;
if (m_numofLines < this.numStartLine) continue; //skip lines;
string[] strsplit = strline.Split(',');
int id = 0;
try {
id = Convert.ToInt32(strsplit[this.colIndex]);
} catch {
Console.WriteLine("Line {0} is invalid input: {1} = {2}.",m_numofLines, this.strFeature, strsplit[this.colIndex]);
continue;
}
if (dictSW.ContainsKey(id)) {
var sw = dictSW[id];
sw.WriteLine(strline);
sw.Flush();
} else {
string filename = OutputDirectory + "file"+ id.ToString() + ".csv";
StreamWriter sw = new StreamWriter(filename);
dictSW.Add(id, sw);
sw.WriteLine(this.strcolNames);
sw.WriteLine(strline);
sw.Flush();
}
if (id < 0 || id > 100) {
Console.WriteLine("Line {0} is invalid input: {1} = {2}.", m_numofLines, this.strFeature, id);
continue;
}
if ((m_numofLines % 10000) == 0) {
Console.WriteLine("numLines = {0}", m_numofLines);
}
}
}
private void CloseStreams() {
foreach (var sw in dictSW.Values) {
sw.Dispose();
}
}
private static IEnumerable<string> ReadCSVLines(string filepath) {
using (StreamReader sr = new StreamReader(filepath)) {
while (!sr.EndOfStream) {
string strline = sr.ReadLine();
yield return strline;
}
}
}
}
}
namespace ConsoleApplication1 {
class Program {
static void Main(string[] args) {
//Dictionary<int, List<double>> dict = new Dictionary<int, List<double>> ();
string csvpath = #"D:\data\train.csv";
string folderpath = #"D:\data\processedfiles\";
CSVFileConverter csv_conv = new CSVFileConverter(csvpath, folderpath, "a", 1);
Console.ReadKey();
}
}
}
I wrote something in python that can achieve the same function using chunksize, but it will take 25 hours to run. I am wondering if there is a way to write the similar code in python based on the C# code above.
My python code is below. The command to convert the rows to DataFrame takes the most time.
for chunk in iter_csv:
df=[]
df1=[]
for i in range(0,55):
a_list=[]
df.append(a_list)
for index, row in chunk.iterrows():
i = row['a']
row1 = pd.DataFrame(row)
row1 = row1.transpose()
df[i].append(row1)
j=0
for df1 in df:
if len(df1)>0:
dfnew = pd.concat(df1)
if not os.path.isfile('dfnew' + str(j) + '.csv'):
dfnew.to_csv('dfnew' + str(j) + '.csv')
else: # else it exists so append without writing the header
dfnew.to_csv('dfnew' + str(j) + '.csv',mode = 'a',header=False)
j=j+1
Thank you.

Writing a list of arrays using csvhelper C#

I am trying to write a List<double[]> into a csv file. Based on my experiments it is not possible using WriteRecords() directly, so I am looking for alternatives.
I cannot create a class to map this data because the number of elements in the arrays varies from execution to execution, and ranges from 1 to 75.
I have tried to go through each element of the array manually with WriteField() but it is really slow., and I have thousands of elements.
Is there anyway to accomplish this fast enough?
Thanks.
Edit:
Code
class ExportData
{
private Dictionary<int, Dictionary<SensorT, ListIndex>> _dict_sensorN;
private List<double[]> _values_list;
private int _current_max_Count=-1;
private static int _max_Count = 3000;
int n_columns = 0;
private CsvWriter csv;
public ExportData(List<SensorControl> sensorsUC)
{
//We create a dictionary with two keys that will store all the info from the sensors
_dict_sensorN = new Dictionary<int, Dictionary<SensorT, ListIndex>>();
foreach (SensorControl SensorC in sensorsUC)
{
Dictionary<SensorT, ListIndex> dict_sensorM = new Dictionary<SensorT, ListIndex>();
if (SensorC.emg)
{
ListIndex Index = new ListIndex();
Index.Column = n_columns;
Index.Row = 0;
dict_sensorM.Add(SensorT.EMG, Index);
n_columns++;
}
if (SensorC.triggers)
{
ListIndex Index = new ListIndex();
Index.Column = n_columns;
Index.Row = 0;
dict_sensorM.Add(SensorT.Trigger1, Index);
n_columns++;
Index.Column = n_columns;
Index.Row = 0;
dict_sensorM.Add(SensorT.Trigger2, Index);
n_columns++;
}
if (SensorC.acc)
{
ListIndex Index = new ListIndex();
Index.Column = n_columns;
Index.Row = 0;
dict_sensorM.Add(SensorT.ACC, Index);
n_columns++;
}
_dict_sensorN.Add(SensorC.sensorNumber, dict_sensorM);
}
//Initialize the array
_values_list = new List<double[]>();
//Initialize the document
DateTime currentDate = DateTime.Now;
string fileName = "exp_" + currentDate.ToString("yyyy-dd-M--HH-mm-ss") + ".csv";
try
{
var textWriter = new StreamWriter(fileName);
csv = new CsvWriter(textWriter);
}
catch (IOException e)
{
Console.WriteLine(e.Message + "\n Cannot create file.");
return;
}
}
public void AddToBuffer(SensorPoint sp)
{
Dictionary<SensorT, ListIndex> dict_sensorM = new Dictionary<SensorT, ListIndex>();
ListIndex Index = new ListIndex();
if (_dict_sensorN.TryGetValue(sp.ID, out dict_sensorM))
{
SensorT type = ToSensorT(sp.SensorType, sp.Battery);
if(type == SensorT.Trigger1) {
if (dict_sensorM.TryGetValue(type, out Index))
{
if (_current_max_Count < Index.Row)
{
_current_max_Count = Index.Row + 1;
_values_list[Index.Row] = new double[n_columns];
}
_values_list[Index.Row][Index.Column] =sp.Trigger1_int;
Index.Row++;
}
if (dict_sensorM.TryGetValue(SensorT.Trigger2, out Index))
{
if (_current_max_Count < Index.Row)
{
_current_max_Count = Index.Row + 1;
_values_list[Index.Row] = new double[n_columns];
}
_values_list[Index.Row][Index.Column] = sp.Trigger2_int_pos;
Index.Row++;
}
}
else {
if (dict_sensorM.TryGetValue(type, out Index))
{
if (_current_max_Count < Index.Row)
{
_current_max_Count = Index.Row;
_values_list.Add(new double[n_columns]);
}
_values_list[Index.Row][Index.Column] = sp.YPoint;
Index.Row++;
}
}
}
if (_current_max_Count > _max_Count) AddToFile();
}
private void AddToFile() {
csv.WriteRecords(_values_list);
}
private SensorT ToSensorT(SensorType stype, int battery)
{
if (stype == SensorType.EMG)
{
return SensorT.EMG;
}
else if (stype == SensorType.EMG_Trigger )
{
if (battery < 0) return SensorT.Trigger1;
else return SensorT.EMG;
}
else if (stype == SensorType.ACC)
{
return SensorT.ACC;
}
else {
return SensorT.Unknown;
}
}
}
/// <summary>
/// In order to create a multivalue dictionary it is necessary another class
/// </summary>
class ListIndex {
public int Column { get; set; }
public int Row { get; set; }
}
}

Reading values from an .acf / manifest file

The file I'm trying to read is presented in the format below. How using c# can I read this file to extract property values? Are there any libraries I can leverage?
"AppState"
{
"appid" "244210"
"Universe" "1"
"name" "Assetto Corsa"
"StateFlags" "4"
"installdir" "assettocorsa"
"LastUpdated" "1469806809"
"UpdateResult" "0"
"SizeOnDisk" "23498042501"
"buildid" "1251512"
"LastOwner" "76561198018619129"
"BytesToDownload" "11541616"
"BytesDownloaded" "11541616"
"AutoUpdateBehavior" "0"
"AllowOtherDownloadsWhileRunning" "0"
"UserConfig"
{
"language" "english"
}
"MountedDepots"
{
"228983" "8124929965194586177"
"228984" "3215975441341951460"
"228985" "5758075142805954616"
"228990" "1829726630299308803"
"229002" "7260605429366465749"
"244211" "3837890045968273966"
}
"InstallScripts"
{
"0" "_CommonRedist\\vcredist\\2010\\installscript.vdf"
"1" "_CommonRedist\\vcredist\\2012\\installscript.vdf"
"2" "_CommonRedist\\vcredist\\2013\\installscript.vdf"
"3" "_CommonRedist\\DirectX\\Jun2010\\installscript.vdf"
"4" "_CommonRedist\\DotNet\\4.0\\installscript.vdf"
}
}
So I don't know if you still need it, but I wrote it myself.
Here is my code.
In my tests it worked perfectly.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Steam_acf_File_Reader
{
class AcfReader
{
public string FileLocation { get; private set; }
public AcfReader(string FileLocation)
{
if (File.Exists(FileLocation))
this.FileLocation = FileLocation;
else
throw new FileNotFoundException("Error", FileLocation);
}
public bool CheckIntegrity()
{
string Content = File.ReadAllText(FileLocation);
int quote = Content.Count(x => x == '"');
int braceleft = Content.Count(x => x == '{');
int braceright = Content.Count(x => x == '}');
return ((braceleft == braceright) && (quote % 2 == 0));
}
public ACF_Struct ACFFileToStruct()
{
return ACFFileToStruct(File.ReadAllText(FileLocation));
}
private ACF_Struct ACFFileToStruct(string RegionToReadIn)
{
ACF_Struct ACF = new ACF_Struct();
int LengthOfRegion = RegionToReadIn.Length;
int CurrentPos = 0;
while (LengthOfRegion > CurrentPos)
{
int FirstItemStart = RegionToReadIn.IndexOf('"', CurrentPos);
if (FirstItemStart == -1)
break;
int FirstItemEnd = RegionToReadIn.IndexOf('"', FirstItemStart + 1);
CurrentPos = FirstItemEnd + 1;
string FirstItem = RegionToReadIn.Substring(FirstItemStart + 1, FirstItemEnd - FirstItemStart - 1);
int SecondItemStartQuote = RegionToReadIn.IndexOf('"', CurrentPos);
int SecondItemStartBraceleft = RegionToReadIn.IndexOf('{', CurrentPos);
if (SecondItemStartBraceleft == -1 || SecondItemStartQuote < SecondItemStartBraceleft)
{
int SecondItemEndQuote = RegionToReadIn.IndexOf('"', SecondItemStartQuote + 1);
string SecondItem = RegionToReadIn.Substring(SecondItemStartQuote + 1, SecondItemEndQuote - SecondItemStartQuote - 1);
CurrentPos = SecondItemEndQuote + 1;
ACF.SubItems.Add(FirstItem, SecondItem);
}
else
{
int SecondItemEndBraceright = RegionToReadIn.NextEndOf('{', '}', SecondItemStartBraceleft + 1);
ACF_Struct ACFS = ACFFileToStruct(RegionToReadIn.Substring(SecondItemStartBraceleft + 1, SecondItemEndBraceright - SecondItemStartBraceleft - 1));
CurrentPos = SecondItemEndBraceright + 1;
ACF.SubACF.Add(FirstItem, ACFS);
}
}
return ACF;
}
}
class ACF_Struct
{
public Dictionary<string, ACF_Struct> SubACF { get; private set; }
public Dictionary<string, string> SubItems { get; private set; }
public ACF_Struct()
{
SubACF = new Dictionary<string, ACF_Struct>();
SubItems = new Dictionary<string, string>();
}
public void WriteToFile(string File)
{
}
public override string ToString()
{
return ToString(0);
}
private string ToString(int Depth)
{
StringBuilder SB = new StringBuilder();
foreach (KeyValuePair<string, string> item in SubItems)
{
SB.Append('\t', Depth);
SB.AppendFormat("\"{0}\"\t\t\"{1}\"\r\n", item.Key, item.Value);
}
foreach (KeyValuePair<string, ACF_Struct> item in SubACF)
{
SB.Append('\t', Depth);
SB.AppendFormat("\"{0}\"\n", item.Key);
SB.Append('\t', Depth);
SB.AppendLine("{");
SB.Append(item.Value.ToString(Depth + 1));
SB.Append('\t', Depth);
SB.AppendLine("}");
}
return SB.ToString();
}
}
static class Extension
{
public static int NextEndOf(this string str, char Open, char Close, int startIndex)
{
if (Open == Close)
throw new Exception("\"Open\" and \"Close\" char are equivalent!");
int OpenItem = 0;
int CloseItem = 0;
for (int i = startIndex; i < str.Length; i++)
{
if (str[i] == Open)
{
OpenItem++;
}
if (str[i] == Close)
{
CloseItem++;
if (CloseItem > OpenItem)
return i;
}
}
throw new Exception("Not enough closing characters!");
}
}
}

Categories