Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2016
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.23 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO;
  11. using System.Text.RegularExpressions;
  12. using System.Globalization;
  13.  
  14. namespace PPF_Converter_v3
  15. {
  16.     public class xmldata
  17.     {
  18.        
  19.         public string colorname { get; set; }
  20.         public string colorvalues { get; set; }
  21.        
  22.     }
  23.    
  24.  
  25.  
  26.     public partial class Form1 : Form
  27.     {
  28.         public Form1()
  29.         {
  30.             InitializeComponent();
  31.         }
  32.         string filename { get; set; }
  33.         List<string> files = new List<string>();
  34.         string aux { get; set; }
  35.         string filecontents  { get; set; }
  36.         string colors { get; set; }
  37.         string[] colornames { get; set; }
  38.         List<xmldata>[] finalcontent = new List<xmldata>[9999];
  39.         int listpos { get; set; }
  40.         List<List<string>> dataadded = new List<List<string>>();
  41.  
  42.  
  43.  
  44.  
  45.         private void button1_Click(object sender, EventArgs e)
  46.         {
  47.             listpos = 0;
  48.             int i = 0;
  49.             aux = "";
  50.             filecontents = "";
  51.             OpenFileDialog of = new OpenFileDialog();
  52.             of.Filter = "PPF Files | *.ppf";
  53.             of.Multiselect = true;
  54.             of.ShowDialog();
  55.            
  56.            
  57.             if (string.IsNullOrEmpty(of.FileName) == false)
  58.             {
  59.                 List<string> fullFileName = new List<string>(of.FileNames);
  60.                 filename = of.FileName;
  61.                 int nfiles = fullFileName.Count();
  62.                 foreach (string fileName in fullFileName) //Start processing each file
  63.                 {
  64.                     // Read the file into <bits>
  65.                     var fs = new FileStream(filename, FileMode.Open);
  66.                     var len = (int)fs.Length;
  67.                     var bits = new byte[len];
  68.                     fs.Read(bits, 0, len);
  69.                     // Dump 16 bytes per line
  70.                     for (int ix = 0; ix < len; ix += 16)
  71.                     {
  72.                         var cnt = Math.Min(16, len - ix);
  73.                         var line = new byte[cnt];
  74.                         Array.Copy(bits, ix, line, 0, cnt);
  75.                                                
  76.                         // Convert non-ascii characters to .
  77.                         for (int jx = 0; jx < cnt; ++jx)
  78.                             if (line[jx] < 0x20 || line[jx] > 0x7f) line[jx] = (byte)'.';
  79.                         //Creating a big string with output
  80.                         aux = Encoding.ASCII.GetString(line);
  81.                         filecontents += aux;
  82.  
  83.                         //Start extracting contents for each file
  84.                         //Since there will be no file with more than one "CIP3EndOfFile", each iteration is going to be a new file
  85.                         bool b = filecontents.Contains("CIP3EndOfFile");
  86.                         if(b)
  87.                         {
  88.                             //Extracting color names (the ones we will have in the file).
  89.                             Regex regex = new Regex("CIP3AdmSeparationNames(.*)CIP3AdmPSExtent");
  90.                             var v = regex.Match(filecontents);
  91.                             string s = v.Groups[1].ToString();
  92.                             s = s.Replace("] def./", "").Replace("[", "").Replace(" (", "(").Replace("(", "").Replace(")", "|");
  93.                             s = s.Remove(s.Length - 1, 1);
  94.  
  95.                             //Colors separated with a delimiter - | . Clean for using later.
  96.                             //Creating an array with colors - one position for each color found
  97.                             string[] colors_str = s.Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
  98.                            
  99.                             //Extracting color values
  100.                             Regex regex2 = new Regex("HDMZones(.*)>>");
  101.                             var v2 = regex2.Match(filecontents);
  102.                             string s2 = v2.Groups[1].ToString();
  103.                             s2 = s2.Replace("HDMZones <</", "").Replace("<</", "").Replace("/", "");
  104.  
  105.  
  106.  
  107.                             var pattern = @"\[(.*?)\]";
  108.                             var query = s2;
  109.                             var matches = Regex.Matches(query, pattern);
  110.                             //ao fim do loop, ele continua no mesmo arquivo, voltando para essa linha.
  111.                             //analisar o porque disso - resolvido, ele irá para o próximo arquivo na iteração
  112.                             finalcontent[listpos] = new List<xmldata>();//Initializing a list for each filename
  113.      
  114.                             foreach (Match m in matches)
  115.                             {
  116.                                
  117.                                 Double[] numbers;
  118.                                 string aux;
  119.                                 aux = m.Groups[1].ToString();
  120.                                 aux = Regex.Replace(aux, @"\s+", "|");
  121.                                 string[] numbers_str = aux.Split(new[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
  122.                                 numbers = new Double[numbers_str.Length];
  123.                                 for (int j = 0; j < numbers.Length; j++)
  124.                                 {
  125.                                     numbers[j] = Double.Parse(numbers_str[j], CultureInfo.InvariantCulture);
  126.                                     //Converts each number on the string to a Double number, store it in a position
  127.                                     //in the Double array
  128.                                     numbers[j] = numbers[j] / 100; //Needed calculus
  129.                                     numbers[j] = Math.Round(numbers[j], 3); //Storing numbers rounded
  130.                                 }
  131.                                 string values = String.Join(" ", numbers.Select(f => f.ToString()));
  132.                                
  133.                                 if (i < colors_str.Length)
  134.                                 {
  135.                                     finalcontent[listpos].Add(new xmldata//The exception is thrown right here
  136.                                     {
  137.                                         colorname = colors_str[i],
  138.                                         colorvalues = values,
  139.  
  140.                                     });//Closing list add declaration
  141.                                  }//Closing if
  142.                                
  143.                                 i++;
  144.                             }//Closing foreach loop
  145.                             if (i >= colors_str.Length)
  146.                             {
  147.                                 listpos++;
  148.                                 i = 0;
  149.                             }
  150.                            // fs.Close();//closing current file
  151.                         }//Closing boolean if
  152.        
  153.                     }//End file reading - closing for
  154.  
  155.  
  156.                    
  157.                  
  158.                 }//Finished processing each filename (string filename in filename)
  159.  
  160.  
  161.             }//End OpenFileDialog
  162.          
  163.        
  164.          }//End Button1Click
  165.      
  166.    }//FOrm1 end
  167. }//namespace end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement