Port Selection and Batch Runner Application - c#

I have never written an application, so I will post the entirety of the code (80 lines). (I come from a background of putting together scripts.)
My goal is to load, or create a list of "used" ports, choose a number within a range that isn't on the list, and if the amount of tries to reach an unused port reaches 129, to run a windows batch file.
This also would turn the chosen port into a .cmd
(some of this is an amalgamation of sources from SO)
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Diagnostics;
using System.IO;
namespace randomport
{
class Core
{
public const int
minval = 8001,
maxval = 8128;
public static int[] usedPorts = new int[]{};
public static int
chosenPort = 0,
tries = 0,
timeout = 10;
public static bool read = false;
public static void Main()
{
if(!read)
{
Read();
read = true;
}
RandGen();
}
public static void RandGen()
{
Process proc = null;
Random rand = new Random();
if(tries < 129) chosenPort = rand.Next(minval, maxval);
else
{
proc.StartInfo.FileName = #"C:\Users\Administrator\Desktop\TerrariaServer\filebin\sendservfull.bat";
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.Start();
proc.WaitForExit
(
(timeout <= 0)
? int.MaxValue : timeout * 100 * 60
);
}
for(int i = 0; i < usedPorts.Length; i++)
{
if(chosenPort != usedPorts[i])
{
Write();
// Application.Exit();
}
else
{
tries += 1;
return;
}
}
}
public static void Read()
{
using (StreamReader sr = new StreamReader(#"C:\Users\Administrator\Desktop\TerrariaServer\filebin\activeports.txt"))
{
string[] values = sr.ReadToEnd().Split(';');
for(int i = 0; i < values.Length; i++)
{
int.TryParse(values[i], out usedPorts[i]);
}
}
}
public static void Write()
{
File.AppendAllText(#"C:\Users\Administrator\Desktop\TerrariaServer\filebin\activeports.txt", "set port="+chosenPort+";");
File.Move(#"C:\Users\Administrator\Desktop\TerrariaServer\filebin\activeports.txt", Path.ChangeExtension(#"C:\Users\Administrator\Desktop\TerrariaServer\filebin\activeports.txt", ".cmd"));
}
}
}
I have a little work to do on the final export (like removing ";").
The script compiles, but does not run as intended. Something is definitely wrong, but I am unaware of it. If it is something obvious, I guess that would be handy, otherwise if it is simply format and so on, I clearly need to do a little more studying.

Compiled using visual studio 2008 express this time, and cleaned it up. It was hard to tell which were the issues without a debugger, such as a missing parenthesis.
File writing, compiling, and crashing solved..
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Diagnostics;
using System.IO;
namespace randomport
{
class Core
{
public static int[] usedPorts = new int[] { };
public static int
minval = 8001,
maxval = 8129,
chosenPort = 0,
timeout = 10,
temp = 1024;
public static bool read = false;
public static void Main(string[] args)
{
string path = System.Environment.GetEnvironmentVariable("USERPROFILE");
if (!Directory.Exists(String.Concat(path, "\\desktop\\TerrariaServer\\filebin\\activeports.txt")))
{
Directory.CreateDirectory(String.Concat(path, "\\desktop\\TerrariaServer\\filebin"));
// using (StreamWriter sw = File.CreateText(String.Concat(path, "\\desktop\\TerrariaServer\\filebin\\activeports.txt"))) { }
}
if (!Directory.Exists(String.Concat(path, "\\desktop\\TerrariaServer\\filebin\\chosenport.cmd")))
{
Directory.CreateDirectory(String.Concat(path, "\\desktop\\TerrariaServer\\filebin"));
using (StreamWriter sw = File.CreateText(String.Concat(path, "\\desktop\\TerrariaServer\\filebin\\chosenport.cmd"))) { }
}
if (args.Length > 0)
{
if (args[0] == "-noread")
{
}
else if (args[0] == "-read" || args[0] == "-default")
{
if (!read)
{
Read();
read = true;
}
}
}
else
{
if (!read)
{
Read();
read = true;
}
}
if (args.Length >= 3)
{
if (args[1] != "-default" || args[1] != "0")
{
int.TryParse(args[1], out temp);
if (temp > 1024 && temp < 65535)
{
minval = temp;
}
}
if (args[2] != "-default" || args[2] != "0")
{
int.TryParse(args[2], out temp);
if (temp > 1024 && temp < 65535)
{
maxval = temp;
}
}
}
RandGen();
}
public static void RandGen()
{
string path = System.Environment.GetEnvironmentVariable("USERPROFILE");
Random rand = new Random();
chosenPort = rand.Next(minval, maxval);
for (int i = 0; i < usedPorts.Length; i++)
{
if (chosenPort != usedPorts[i])
{
Write();
}
else return;
}
}
public static void Read()
{
string path = System.Environment.GetEnvironmentVariable("USERPROFILE");
if (!File.Exists(String.Concat(path, "\\desktop\\TerrariaServer\\filebin\\activeports.txt")))
{
File.Create(String.Concat(path, "\\desktop\\TerrariaServer\\filebin\\activeports.txt"));
}
using (StreamReader sr = new StreamReader(String.Concat(path, "\\desktop\\TerrariaServer\\filebin\\activeports.txt")))
{
string[] values = sr.ReadToEnd().Split(';');
usedPorts = new int[values.Length];//Initialize the array with the same length as values
for (int i = 0; i < values.Length; i++)
{
int.TryParse(values[i], out usedPorts[i]);
}
}
}
public static void Write()
{
string path = System.Environment.GetEnvironmentVariable("USERPROFILE");
File.AppendAllLines(String.Concat(path, "\\desktop\\TerrariaServer\\filebin\\activeports.txt"), new string[] { chosenPort + ";" });
using (StreamWriter sw2 = File.CreateText(String.Concat(path, "\\desktop\\TerrariaServer\\filebin\\chosenport.cmd")))
{
sw2.WriteLine("set port=" + chosenPort);
}
Environment.Exit(0);
}
}
}

Related

how can in receive data from serial port in unity?

I want to establish a full duplex connection between the microcontroller(ARM Stm32F4) and the program made by Unity software via c#. This connection must be made through the serial port. I need a two-way connection between these two parts.
At first the sent data from the program (made in unity software) can send command to the microcontroller(ARM Stm32F4), then the microcontroller must check the received data and verifying finishing commands to the Unity software
If the data is correct, send the next data to the program. My problem is that my program, which is made by the unity, does not receive finished data that sent from the microcontroller.
How can I have full duplex connection?
*
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO.Ports;
using System.IO;
using UnityEditor;
using SerialPortUtility;
using System;
public class gcode : MonoBehaviour
{
string str;
string str1;
public SerialPort sina;
public InputField sendgcode;
public InputField plusoffset;
public InputField minusoffset;
public InputField lenghtoffset;
public InputField counter;
public Button plus_inc;
public Button plus_dec;
public Button minus_inc;
public Button minus_dec;
public Button lenght_inc;
public Button lenght_dec;
string board;
Array[] inserial;
//int resend = 0;
// Start is called before the first frame update
void Start()
{
sina = new SerialPort("com1", 115200);
sina.ReadTimeout = 100;
sina.Open();
sina.DtrEnable = true;
}
// Update is called once per frame
void Update()
{
//sina.Close();
//str = sina.ReadLine();
//sendgcode.text = str;
}
public void sendserial()
{
str = sendgcode.text;
TextWriter a40 = new StreamWriter("c:\\1.txt");
a40.WriteLine(str);
a40.Close();
str = "";
TextWriter a41 = new StreamWriter("c:\\2.txt");
TextWriter a43 = new StreamWriter("c:\\3.txt");
TextWriter a22 = new StreamWriter("c:\\22.txt");
TextWriter plus = new StreamWriter("c:\\p.txt");
TextWriter minus = new StreamWriter("c:\\m.txt");
TextWriter lenght = new StreamWriter("c:\\t.txt");
System.IO.StreamReader file = new System.IO.StreamReader("c:\\1.txt");
while ((str = file.ReadLine()) != null)
{
str1 = str.Replace(" ", "");
str1 = str1.Replace("L", "\r\n");
str1 = str1.Replace("A", "\r\n");
str1 = str1.Replace("S", "");
a41.WriteLine(str1);
}
a41.WriteLine("S");
a41.Close();
file.Close();
System.IO.StreamReader file22 = new System.IO.StreamReader("c:\\2.txt");
while ((str = file22.ReadLine()) != "S")
{
if (str.Length >= 1) { a22.WriteLine(str); }
}
a22.WriteLine("S");
a22.Close();
file22.Close();
System.IO.StreamReader file1 = new System.IO.StreamReader("c:\\22.txt");
while ((str = file1.ReadLine()) != "S")
{
int idx = str.IndexOf('-');
if (idx >= 0)
{
if (str.Length == 2)
{
str = str.Replace("-", "-00");
a43.WriteLine(str);
}
else if (str.Length == 3)
{
str = str.Replace("-", "-0");
a43.WriteLine(str);
}
else
{
a43.WriteLine(str);
}
}
else if (str.Length == 1)
{
a43.WriteLine("00" + str);
}
else if (str.Length == 2)
{
a43.WriteLine("0" + str);
}
else
{
a43.WriteLine(str);
}
}
a43.WriteLine("S");
a43.Close();
file1.Close();
str1 = null;
int c = 0;
System.IO.StreamReader file2 = new System.IO.StreamReader("c:\\3.txt");
while ((str = file2.ReadLine()) != "S")
{
if (str.Length > 2)
{
str1 = str1 + str; //read line by line to 4 line
}
c++;
if (c == 4) //end read 4 lines
{
c = 0;
int n = 0;
str1 = "#" + str1;
int sp;
do
{
sina.WriteLine(str1);
board = sina.ReadExisting();
Debug.Log(board);
}
while (board != str1);
sina.WriteLine("$");
str1 = null;
}
}
/*
System.Threading.Thread.Sleep(1000);
sina.WriteLine(str1); //send last line of gcode
file2.Close();
System.Threading.Thread.Sleep(1000);
sina.WriteLine("p" + plusoffset.text);
System.Threading.Thread.Sleep(1000);
sina.WriteLine("m" + minusoffset.text);
System.Threading.Thread.Sleep(1000);
sina.WriteLine("t" + lenghtoffset.text);
System.Threading.Thread.Sleep(1000);
System.IO.StreamReader filecrc = new System.IO.StreamReader("c:\\3.txt");
int crc = 0;
int s1;
int Poffset = 0;
int Moffset = 0;
int Toffset = 0;
int Ecount = 0;
while ((str = filecrc.ReadLine()) != "S")
{
int.TryParse(str, out s1);
crc = crc + s1;
}
filecrc.Close();
int.TryParse(plusoffset.text, out Poffset);
int.TryParse(minusoffset.text, out Moffset);
int.TryParse(lenghtoffset.text, out Toffset);
int.TryParse(counter.text, out Ecount);
crc = crc + Poffset;
crc = crc + Moffset;
crc = crc + Toffset;
crc = crc + Ecount;
System.Threading.Thread.Sleep(1000);
sina.WriteLine("M" + crc.ToString());
System.Threading.Thread.Sleep(1000);
sina.WriteLine("E" + counter.text);
plus.WriteLine(plusoffset.text); plus.Close();
minus.WriteLine(minusoffset.text); minus.Close();
lenght.WriteLine(lenghtoffset.text); lenght.Close();
*/
}
public void plusinc()
{
int value;
int.TryParse(plusoffset.text, out value);
value++;
plusoffset.text = value.ToString();
}
public void plusdec()
{
int value;
int.TryParse(plusoffset.text, out value);
value--;
plusoffset.text = value.ToString();
}
public void minusinc()
{
int value;
int.TryParse(minusoffset.text, out value);
value++;
minusoffset.text = value.ToString();
}
public void minusdec()
{
int value;
int.TryParse(minusoffset.text, out value);
value--;
minusoffset.text = value.ToString();
}
public void lenghtinc()
{
int value;
int.TryParse(lenghtoffset.text, out value);
value++;
lenghtoffset.text = value.ToString();
}
public void lenghtdec()
{
int value;
int.TryParse(lenghtoffset.text, out value);
value--;
lenghtoffset.text = value.ToString();
}
public void load_offest()
{
System.Threading.Thread.Sleep(500);
System.IO.StreamReader offset = new System.IO.StreamReader("c:\\offset.txt");
string stroffset;
stroffset = offset.ReadLine();
plusoffset.text = stroffset;
stroffset = offset.ReadLine();
minusoffset.text = stroffset;
stroffset = offset.ReadLine();
lenghtoffset.text = stroffset;
}
public void clear()
{
sendgcode.text = "";
plusoffset.text = "";
minusoffset.text = "";
lenghtoffset.text = "";
}
void OnSerialLine(string line)
{
Debug.Log("Got a line: " + line);
}
}*```
In your do - while loop you constantly keep writing the string str1. This looks already quite odd.
And then you do board = sina.ReadExisting() which returns all currently available stream and buffer content.
Reads all immediately available bytes, based on the encoding, in both the stream and the input buffer of the SerialPort object.
Since this is a stream and there is no sich concept as "messages" might be multiple lines/messages or also only a part of one => pretty unlikely that this matches exactly with what you have sent immediately.
You would need to come up with a proper protocol like e.g. using a special separator character between messages.
Rather use ReadLine which reads until the next encounter of the configured NewLine character (by default \n) or use ReadTo(string) for a string pattern.
Both will "freeze" until according character is received making sure the entire message is fully received before continuing in your code.
So instead of
do
{
sina.WriteLine(str1);
board = sina.ReadExisting();
Debug.Log(board);
}
while (board != str1);
sina.WriteLine("$");
you would probably rather do something like e.g.
sina.WriteLine(str1);
do
{
board = sina.ReadLine();
Debug.Log(board);
}
while (board != str1);
sina.WriteLine("$");
IF a loop is what you want to use at all.
In general I would always use two separate threads for sending and receiving. Except you have a clear protocol where you only need to be able to receive in certain slots in the logic.

Linked list implementation of array using c# console

Hi i was wondering if anyone could help me. im stuck in a dead end here. I dont know how to make a peek and working pop function. i need assistance.
here is my code so far:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp13
{
class Program
{
int nextFree;
int End;
int Start;
Names[] Stack;
Names steven = new Names();
Names jacques = new Names();
Names samantha = new Names();
Names lilly = new Names();
public struct Names
{
public Int32 pointer;
public string data;
}
static void Main(string[] args)
{
Program prog = new Program();
do
{
prog.DisplayMenu();
}
while (true);
}
public void DisplayMenu()
{
Int32 userInput = 0;
Console.WriteLine("Enter number of choice:");
Console.WriteLine("=======================");
Console.WriteLine("1: Sign up for consultation");
Console.WriteLine("2: Begin consultation");
Console.WriteLine("3: Enter room");
Console.WriteLine("4: Closing time");
Console.WriteLine("5: Exit");
userInput = Int32.Parse(Console.ReadLine());
switch (userInput)
{
case 1:
this.Push();
break;
case 2:
this.Pop();
break;
case 5:
System.Environment.Exit(1);
break;
}
}
public Program()
{
Stack = new Names[20];
steven.data = "Steven";
steven.pointer = 1;
jacques.data = "Jacques";
jacques.pointer = 2;
samantha.data = "Samantha";
samantha.pointer = 3;
lilly.data = "Lilly";
lilly.pointer = -1;
Stack[0] = steven;
Stack[1] = jacques;
Stack[2] = samantha;
Stack[3] = lilly;
nextFree = 4;
End = 20;
Start = 0;
}
public string Pop()
{
string value = string.Empty;
if (nextFree == -1)
{
Console.WriteLine("Stack is empty");
Console.ReadLine();
}
else
{
Names thisNode = Stack[End];
int temp = End;
End = thisNode.pointer;
thisNode.pointer = nextFree;
nextFree = temp;
}
this.ListAllNames();
return value;
}
public void Push()
{
if (nextFree >= Stack.Length)
{
Console.WriteLine("Stackoverflow, to many elements for the stack");
Console.ReadLine();
}
else
{
Console.WriteLine("Enter a name to be added");
string input = Console.ReadLine();
Stack[nextFree].data = input;
Stack[nextFree].pointer = End;
End = nextFree;
nextFree++;
}
this.ListAllNames();
}
public void ListAllNames()
{
foreach (Names name in Stack)
{
Console.WriteLine("Name:" + name.data);
}
}
}
}
as you can see it is unfinished. im at a standstill and cannot move one.
I have trouble in using the elements here so that i could make a function such as peek and pop properly.

I need to find all the peaks and valleys in a list of data points using c#

This is my entire program. I have been working on this project for a week now and I am simply stuck. I have managed to read the CSV file and put the information in two lists that I then converted into arrays. Now I don't HAVE to do it like this. It's just simply what I managed to write and work and make sense in my head. This is where I've managed to get to and where I'm stuck. All I need is to be able to parse out ALL the peaks, or maximums, and valleys, or minimums, from the data points, and display them. If I can get that to happen then I can finally be done with this program. Any help and suggestions, code, anything like that would greatly appreciate. Like I said if you have a better formula that I can plug in and use without hard-coding points (because there's over 2000 of them) and display the outcome in a listbox or something then that would make my day. Thank y, everyone,ne in advance
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApp6
{
public partial class Form1 : Form
{
double firstX, firstY, secondX, secondY, thirdX, thirdY, rw, cl = 0.0;
int count = 0;
string x = "";
string y = "";
byte MinMax = 0; //0 equals rising, 1 equals falling
double Max = new Array[];
//double Min = new Array[];
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (var reader = new StreamReader(#"D:\data.csv"))
{
List<string> listA = new List<string>();
List<string> listB = new List<string>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
listA.Add(values[0]);
listB.Add(values[1]);
string[] xPoints = listA.ToArray();
string[] yPoints = listB.ToArray();
while (yPoints.Last() != null)
{
if (Convert.ToDouble(yPoints.First()) == 0.0)
{
firstY = Convert.ToDouble(yPoints.First());
}
if (Convert.ToDouble(yPoints.First()) != 0.0 && secondY == 0.0)
{
secondY = Convert.ToDouble(yPoints.Last());
if (secondY > firstY)
{
MinMax = 0;
}
else
{
MinMax = 1;
}
}
if (secondY != 0.0 && thirdY == 0.0)
{
thirdY = Convert.ToDouble(yPoints.Last());
if (MinMax == 0 && thirdY > secondY)
{
firstY = secondY;
secondY = thirdY;
}
else
{
Max[count,0] = secondY;
}
}
}
listBox1.Items.Add(values[0]);
listBox2.Items.Add(values[1]);
}
}
}
}
}
This snippet will get valleys and peaks on an array of y values, ignoring [0]
public void GetValleysAndPeaks(double[] yValues, out List<int> peakIndexes, out List<int> valleyIndexes)
{
peakIndexes = new List<int>();
valleyIndexes = new List<int>();
bool directionUp = yValues[0] <= yValues[1];
for(int i = 1; i < yValues.Length - 1; i++)
{
if(directionUp && yValues[i + 1] < yValues[i])
{
peakIndexes.Add(i);
directionUp = false;
}
else if(!directionUp && yValues[i + 1] > yValues[i])
{
valleyIndexes.Add(i);
directionUp = true;
}
}
}

Importing and removing duplicates from a massive amount of text files using C# and Redis

This is a bit of a doozy and it's been a while since I worked with C#, so bear with me:
I'm running a jruby script to iterate through 900 files (5 Mb - 1500 Mb in size) to figure out how many dupes STILL exist within these (already uniq'd) files. I had little luck with awk.
My latest idea was to insert them into a local MongoDB instance like so:
db.collection('hashes').update({ :_id => hash}, { $inc: { count: 1} }, { upsert: true)
... so that later I could just query it like db.collection.where({ count: { $gt: 1 } }) to get all the dupes.
This is working great except it's been over 24 hours and at the time of writing I'm at 72,532,927 Mongo entries and growing.
I think Ruby's .each_line is bottlnecking the IO hardcore:
So what I'm thinking now is compiling a C# program which fires up a thread PER EACH FILE and inserts the line (md5 hash) into a Redis list.
From there, I could have another compiled C# program simply pop the values off and ignore the save if the count is 1.
So the questions are:
Will using a compiled file reader and multithreading the file reads significantly improve performance?
Is using Redis even necessary? With a tremendous amount of AWS memory, could I not just use the threads to fill some sort of a list atomically and proceed from there?
Thanks in advance.
Updated
New solution. Old solution. The main idea is to calculate dummy hashes(just sum of all chars in string) of each line and store it in Dictionary<ulong, List<LinePosition>> _hash2LinePositions. It's possible to have multiple hashes in the same stream and it solves by List in Dictionary Value. When the hashes are the same, we read and compare the strings from the streams. LinePosition is using for storing info about line - position in stream and its length. I don't have such huge files as you, but my tests shows that it works. Here is the full code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
public class Solution
{
struct LinePosition
{
public long Start;
public long Length;
public LinePosition(long start, long count)
{
Start = start;
Length = count;
}
public override string ToString()
{
return string.Format("Start: {0}, Length: {1}", Start, Length);
}
}
class TextFileHasher : IDisposable
{
readonly Dictionary<ulong, List<LinePosition>> _hash2LinePositions;
readonly Stream _stream;
bool _isDisposed;
public HashSet<ulong> Hashes { get; private set; }
public string Name { get; private set; }
public TextFileHasher(string name, Stream stream)
{
Name = name;
_stream = stream;
_hash2LinePositions = new Dictionary<ulong, List<LinePosition>>();
Hashes = new HashSet<ulong>();
}
public override string ToString()
{
return Name;
}
public void CalculateFileHash()
{
int readByte = -1;
ulong dummyLineHash = 0;
// Line start position in file
long startPosition = 0;
while ((readByte = _stream.ReadByte()) != -1) {
// Read until new line
if (readByte == '\r' || readByte == '\n') {
// If there was data
if (dummyLineHash != 0) {
// Add line hash and line position to the dict
AddToDictAndHash(dummyLineHash, startPosition, _stream.Position - 1 - startPosition);
// Reset line hash
dummyLineHash = 0;
}
}
else {
// Was it new line ?
if (dummyLineHash == 0)
startPosition = _stream.Position - 1;
// Calculate dummy hash
dummyLineHash += (uint)readByte;
}
}
if (dummyLineHash != 0) {
// Add line hash and line position to the dict
AddToDictAndHash(dummyLineHash, startPosition, _stream.Position - startPosition);
// Reset line hash
dummyLineHash = 0;
}
}
public List<LinePosition> GetLinePositions(ulong hash)
{
return _hash2LinePositions[hash];
}
public List<string> GetDuplicates()
{
List<string> duplicates = new List<string>();
foreach (var key in _hash2LinePositions.Keys) {
List<LinePosition> linesPos = _hash2LinePositions[key];
if (linesPos.Count > 1) {
duplicates.AddRange(FindExactDuplicates(linesPos));
}
}
return duplicates;
}
public void Dispose()
{
if (_isDisposed)
return;
_stream.Dispose();
_isDisposed = true;
}
private void AddToDictAndHash(ulong hash, long start, long count)
{
List<LinePosition> linesPosition;
if (!_hash2LinePositions.TryGetValue(hash, out linesPosition)) {
linesPosition = new List<LinePosition>() { new LinePosition(start, count) };
_hash2LinePositions.Add(hash, linesPosition);
}
else {
linesPosition.Add(new LinePosition(start, count));
}
Hashes.Add(hash);
}
public byte[] GetLineAsByteArray(LinePosition prevPos)
{
long len = prevPos.Length;
byte[] lineBytes = new byte[len];
_stream.Seek(prevPos.Start, SeekOrigin.Begin);
_stream.Read(lineBytes, 0, (int)len);
return lineBytes;
}
private List<string> FindExactDuplicates(List<LinePosition> linesPos)
{
List<string> duplicates = new List<string>();
linesPos.Sort((x, y) => x.Length.CompareTo(y.Length));
LinePosition prevPos = linesPos[0];
for (int i = 1; i < linesPos.Count; i++) {
if (prevPos.Length == linesPos[i].Length) {
var prevLineArray = GetLineAsByteArray(prevPos);
var thisLineArray = GetLineAsByteArray(linesPos[i]);
if (prevLineArray.SequenceEqual(thisLineArray)) {
var line = System.Text.Encoding.Default.GetString(prevLineArray);
duplicates.Add(line);
}
#if false
string prevLine = System.Text.Encoding.Default.GetString(prevLineArray);
string thisLine = System.Text.Encoding.Default.GetString(thisLineArray);
Console.WriteLine("PrevLine: {0}\r\nThisLine: {1}", prevLine, thisLine);
StringBuilder sb = new StringBuilder();
sb.Append(prevPos);
sb.Append(" is '");
sb.Append(prevLine);
sb.Append("'. ");
sb.AppendLine();
sb.Append(linesPos[i]);
sb.Append(" is '");
sb.Append(thisLine);
sb.AppendLine("'. ");
sb.Append("Equals => ");
sb.Append(prevLine.CompareTo(thisLine) == 0);
Console.WriteLine(sb.ToString());
#endif
}
else {
prevPos = linesPos[i];
}
}
return duplicates;
}
}
public static void Main(String[] args)
{
List<TextFileHasher> textFileHashers = new List<TextFileHasher>();
string text1 = "abc\r\ncba\r\nabc";
TextFileHasher tfh1 = new TextFileHasher("Text1", new MemoryStream(System.Text.Encoding.Default.GetBytes(text1)));
tfh1.CalculateFileHash();
textFileHashers.Add(tfh1);
string text2 = "def\r\ncba\r\nwet";
TextFileHasher tfh2 = new TextFileHasher("Text2", new MemoryStream(System.Text.Encoding.Default.GetBytes(text2)));
tfh2.CalculateFileHash();
textFileHashers.Add(tfh2);
string text3 = "def\r\nbla\r\nwat";
TextFileHasher tfh3 = new TextFileHasher("Text3", new MemoryStream(System.Text.Encoding.Default.GetBytes(text3)));
tfh3.CalculateFileHash();
textFileHashers.Add(tfh3);
List<string> totalDuplicates = new List<string>();
Dictionary<ulong, Dictionary<TextFileHasher, List<LinePosition>>> totalHashes = new Dictionary<ulong, Dictionary<TextFileHasher, List<LinePosition>>>();
textFileHashers.ForEach(tfh => {
foreach(var dummyHash in tfh.Hashes) {
Dictionary<TextFileHasher, List<LinePosition>> tfh2LinePositions = null;
if (!totalHashes.TryGetValue(dummyHash, out tfh2LinePositions))
totalHashes[dummyHash] = new Dictionary<TextFileHasher, List<LinePosition>>() { { tfh, tfh.GetLinePositions(dummyHash) } };
else {
List<LinePosition> linePositions = null;
if (!tfh2LinePositions.TryGetValue(tfh, out linePositions))
tfh2LinePositions[tfh] = tfh.GetLinePositions(dummyHash);
else
linePositions.AddRange(tfh.GetLinePositions(dummyHash));
}
}
});
HashSet<TextFileHasher> alreadyGotDuplicates = new HashSet<TextFileHasher>();
foreach(var hash in totalHashes.Keys) {
var tfh2LinePositions = totalHashes[hash];
var tfh = tfh2LinePositions.Keys.FirstOrDefault();
// Get duplicates in the TextFileHasher itself
if (tfh != null && !alreadyGotDuplicates.Contains(tfh)) {
totalDuplicates.AddRange(tfh.GetDuplicates());
alreadyGotDuplicates.Add(tfh);
}
if (tfh2LinePositions.Count <= 1) {
continue;
}
// Algo to get duplicates in more than 1 TextFileHashers
var tfhs = tfh2LinePositions.Keys.ToArray();
for (int i = 0; i < tfhs.Length; i++) {
var tfh1Positions = tfhs[i].GetLinePositions(hash);
for (int j = i + 1; j < tfhs.Length; j++) {
var tfh2Positions = tfhs[j].GetLinePositions(hash);
for (int k = 0; k < tfh1Positions.Count; k++) {
var tfh1Pos = tfh1Positions[k];
var tfh1ByteArray = tfhs[i].GetLineAsByteArray(tfh1Pos);
for (int m = 0; m < tfh2Positions.Count; m++) {
var tfh2Pos = tfh2Positions[m];
if (tfh1Pos.Length != tfh2Pos.Length)
continue;
var tfh2ByteArray = tfhs[j].GetLineAsByteArray(tfh2Pos);
if (tfh1ByteArray.SequenceEqual(tfh2ByteArray)) {
var line = System.Text.Encoding.Default.GetString(tfh1ByteArray);
totalDuplicates.Add(line);
}
}
}
}
}
}
Console.WriteLine();
if (totalDuplicates.Count > 0) {
Console.WriteLine("Total number of duplicates: {0}", totalDuplicates.Count);
Console.WriteLine("#######################");
totalDuplicates.ForEach(x => Console.WriteLine("{0}", x));
Console.WriteLine("#######################");
}
// Free resources
foreach (var tfh in textFileHashers)
tfh.Dispose();
}
}
If you have tons of ram... You guys are overthinking it...
var fileLines = File.ReadAllLines(#"c:\file.csv").Distinct();

Null pointer Exception in static array C#

I am initializing static array by a method, then want to use it in non static method and it throws nullpointerException. When I have created small example everything worked. I don't know what is wrong with that. Attaching solution.
http://www.speedyshare.com/QRjW5/Funkcjonalnosc-Kopia-2.zip
Main method is in the class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Funkcjonalnosc
{
class Dzwiek
{
static Dzwiek[] tabRefDzwiekow;// = zaladujDzwieki();//przy uruchomeniu dzwieki referencyjne wczytaja sie raz
double hz;
String dzwiek;
bool first = true;
Dzwiek(double hz) {
this.hz = hz;
dzwiek = dopasujDzwiek(hz);
}
Dzwiek(String dzwiek, double hz) {
this.dzwiek = dzwiek;
this.hz = hz;
}
public static void zaladujDzwieki() {
System.IO.StreamReader sr = System.IO.File.OpenText("dzwieki.txt");
tabRefDzwiekow = new Dzwiek[100];
string s = "";
int i = 0;
string[] splitted;
while ((s = sr.ReadLine()) != null) {
splitted = s.Split('\t');
tabRefDzwiekow[i] = new Dzwiek(splitted[0], Double.Parse(splitted[1]));
Console.WriteLine(tabRefDzwiekow[i].hz);
}
sr.Close();
}
//Znajduje odpowiedni dzwiek w tablicy dzwiekow
String dopasujDzwiek(double hz) {
double obecnaRoznica, poprzedniaRoznica = int.MaxValue;
string dopasowanyDzwiek = "";
for (int i = 0; i < tabRefDzwiekow.Length; i++) {
obecnaRoznica = Math.Abs(hz - tabRefDzwiekow[i].hz);//THROWS EXCEPTION!
if (obecnaRoznica > poprzedniaRoznica)
return tabRefDzwiekow[i - 1].dzwiek;
poprzedniaRoznica = obecnaRoznica;
}
return dopasowanyDzwiek;
}
static void Main(string[] args) {
zaladujDzwieki(); //initilize the static ARRAY tabRefDzwieki
Dzwiek dzwiek = new Dzwiek(440); //uses that array by calling function in //constructor doPasujDzwieki()
Console.Read();
}
}
}
unless I'm mising something, whenever you call
tabRefDzwiekow[i] = new Dzwiek(splitted[0], Double.Parse(splitted[1]));
i will always be zero, and then when you access it later, in your for loop,
obecnaRoznica = Math.Abs(hz - tabRefDzwiekow[i].hz);//THROWS EXCEPTION
you're trying to access an uninitialized Dzwiek object's hz property after the first iteration

Categories