Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- using System.Text.RegularExpressions;
- using System.Globalization;
- namespace PPF_Converter_v3
- {
- public class xmldata
- {
- public string colorname { get; set; }
- public string colorvalues { get; set; }
- }
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- string filename { get; set; }
- List<string> files = new List<string>();
- string aux { get; set; }
- string filecontents { get; set; }
- string colors { get; set; }
- string[] colornames { get; set; }
- List<xmldata>[] finalcontent = new List<xmldata>[9999];
- int listpos { get; set; }
- List<List<string>> dataadded = new List<List<string>>();
- private void button1_Click(object sender, EventArgs e)
- {
- listpos = 0;
- int i = 0;
- aux = "";
- filecontents = "";
- OpenFileDialog of = new OpenFileDialog();
- of.Filter = "PPF Files | *.ppf";
- of.Multiselect = true;
- of.ShowDialog();
- if (string.IsNullOrEmpty(of.FileName) == false)
- {
- List<string> fullFileName = new List<string>(of.FileNames);
- filename = of.FileName;
- int nfiles = fullFileName.Count();
- foreach (string fileName in fullFileName) //Start processing each file
- {
- // Read the file into <bits>
- var fs = new FileStream(filename, FileMode.Open);
- var len = (int)fs.Length;
- var bits = new byte[len];
- fs.Read(bits, 0, len);
- // Dump 16 bytes per line
- for (int ix = 0; ix < len; ix += 16)
- {
- var cnt = Math.Min(16, len - ix);
- var line = new byte[cnt];
- Array.Copy(bits, ix, line, 0, cnt);
- // Convert non-ascii characters to .
- for (int jx = 0; jx < cnt; ++jx)
- if (line[jx] < 0x20 || line[jx] > 0x7f) line[jx] = (byte)'.';
- //Creating a big string with output
- aux = Encoding.ASCII.GetString(line);
- filecontents += aux;
- //Start extracting contents for each file
- //Since there will be no file with more than one "CIP3EndOfFile", each iteration is going to be a new file
- bool b = filecontents.Contains("CIP3EndOfFile");
- if(b)
- {
- //Extracting color names (the ones we will have in the file).
- Regex regex = new Regex("CIP3AdmSeparationNames(.*)CIP3AdmPSExtent");
- var v = regex.Match(filecontents);
- string s = v.Groups[1].ToString();
- s = s.Replace("] def./", "").Replace("[", "").Replace(" (", "(").Replace("(", "").Replace(")", "|");
- s = s.Remove(s.Length - 1, 1);
- //Colors separated with a delimiter - | . Clean for using later.
- //Creating an array with colors - one position for each color found
- string[] colors_str = s.Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
- //Extracting color values
- Regex regex2 = new Regex("HDMZones(.*)>>");
- var v2 = regex2.Match(filecontents);
- string s2 = v2.Groups[1].ToString();
- s2 = s2.Replace("HDMZones <</", "").Replace("<</", "").Replace("/", "");
- var pattern = @"\[(.*?)\]";
- var query = s2;
- var matches = Regex.Matches(query, pattern);
- //ao fim do loop, ele continua no mesmo arquivo, voltando para essa linha.
- //analisar o porque disso - resolvido, ele irá para o próximo arquivo na iteração
- finalcontent[listpos] = new List<xmldata>();//Initializing a list for each filename
- foreach (Match m in matches)
- {
- Double[] numbers;
- string aux;
- aux = m.Groups[1].ToString();
- aux = Regex.Replace(aux, @"\s+", "|");
- string[] numbers_str = aux.Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
- numbers = new Double[numbers_str.Length];
- for (int j = 0; j < numbers.Length; j++)
- {
- numbers[j] = Double.Parse(numbers_str[j], CultureInfo.InvariantCulture);
- //Converts each number on the string to a Double number, store it in a position
- //in the Double array
- numbers[j] = numbers[j] / 100; //Needed calculus
- numbers[j] = Math.Round(numbers[j], 3); //Storing numbers rounded
- }
- string values = String.Join(" ", numbers.Select(f => f.ToString()));
- if (i < colors_str.Length)
- {
- finalcontent[listpos].Add(new xmldata//The exception is thrown right here
- {
- colorname = colors_str[i],
- colorvalues = values,
- });//Closing list add declaration
- }//Closing if
- i++;
- }//Closing foreach loop
- if (i >= colors_str.Length)
- {
- listpos++;
- i = 0;
- }
- // fs.Close();//closing current file
- }//Closing boolean if
- }//End file reading - closing for
- }//Finished processing each filename (string filename in filename)
- }//End OpenFileDialog
- }//End Button1Click
- }//FOrm1 end
- }//namespace end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement