Advertisement
kijato

kill-LF

Aug 16th, 2019
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.82 KB | None | 0 0
  1. /*
  2.  * Created by SharpDevelop & Notepad++
  3.  * Date: 2019.08.21.
  4.  * Time: 10:13
  5.  *
  6.  * 0x0A byte-ok cseréje 0x20 byte-ra
  7.  *
  8.  */
  9. using System;
  10. using System.IO;
  11. using System.Collections.Generic;
  12.  
  13. namespace kill_LF
  14. {
  15.     class Program
  16.     {
  17.         public static void Main(string[] args)
  18.         {
  19.             if ( args.Length == 2 )
  20.             {
  21.                 Console.WriteLine(args[0]+" "+args[1]);
  22.                 // Environment.CurrentDirectory
  23.                 string[] exes = Directory.GetFiles(args[0], args[1]);
  24.                 foreach (string file in exes)
  25.                 {
  26.                     Console.WriteLine(Path.GetFileName(file));
  27.                     DoIt(file);
  28.                 }
  29.             } else {
  30.                 System.Console.WriteLine(@"Fordítás: 'c:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe kill_LF.cs'");
  31.                 System.Console.WriteLine( "Használat: kill_LF.exe paraméter-1 paraméter-2");
  32.                 System.Console.WriteLine( "   Paraméterek:\n\t1. útvonal (aktuális könyvtár: '.')\n\t2. fájlnév (*.dat)\n");
  33.             }
  34.             Console.Write("\nPress any key to continue . . . ");
  35.             Console.ReadKey(true);
  36.         } // Main()
  37.  
  38.        
  39.         public static void DoIt(String filename)
  40.         {
  41.             /* Too long way for read...
  42.             FileInfo f = new FileInfo(filename);
  43.             byte[] buffer = new byte[f.Length];
  44.             using (FileStream stream = new FileStream(f.FullName, FileMode.Open))
  45.             {
  46.                 using (BinaryReader reader = new BinaryReader(stream))
  47.                 {
  48.                     if ( buffer.Length != reader.Read(buffer,0,buffer.Length) )
  49.                     {
  50.                         System.Console.WriteLine("Hiba törpént...?");
  51.                     }
  52.                 }
  53.             }
  54.             */
  55.             // The fast way for read...
  56.             byte[] buffer = File.ReadAllBytes(filename);
  57.             File.WriteAllBytes(filename.Replace(".dat",".dat.old"), buffer);
  58.            
  59.             int rownum = 0;
  60.             var lfCount = new List<Int32>();
  61.             int lastItem = 0;
  62.             //byte[] newbuffer = new byte[f.Length];
  63.             for(int i=1; i<buffer.Length; i++)
  64.             {
  65.                 if ( buffer[i-1]==13 && buffer[i]==10 )
  66.                 {
  67.                     rownum++;
  68.                 }
  69.                 if ( buffer[i-1]!=13 && buffer[i]==10 )
  70.                 {
  71.                     lfCount.Add(i);
  72.                     buffer[i] = 32;
  73.                     //newbuffer[i] = 32;
  74.                     System.Console.WriteLine("\t"+lfCount.Count+". érintett sor száma: "+rownum+", karakterszám: "+i+" ("+(i-lastItem)+")");
  75.                     lastItem = i;
  76.                     continue;
  77.                 } else {
  78.                     //newbuffer[i-lfCount.Count] = buffer[i];
  79.                 }
  80.             }
  81.  
  82.             if ( lfCount.Count > 0 )
  83.             {
  84.                 /* Too long way for write...
  85.                 using (FileStream stream = new FileStream(filename.Replace(".dat","_new.dat"),FileMode.Create))
  86.                 {
  87.                     using (BinaryWriter writer = new BinaryWriter(stream))
  88.                     {
  89.                         //buffer = newbuffer;
  90.                         writer.Write(buffer);
  91.                         writer.Close();
  92.                     } // using
  93.                 } // using                     
  94.                 */
  95.                 // The fast way for write...
  96.                 File.WriteAllBytes(filename, buffer);
  97.             } else {
  98.                 File.Delete(filename.Replace(".dat",".dat.old"));
  99.             } // if
  100.            
  101.         } // DoIt()
  102.  
  103.     } // class
  104.  
  105. } // namespace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement