Advertisement
apieceoffruit

FileHeaders

Mar 14th, 2023 (edited)
631
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.80 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. namespace JasonStorey
  5. {
  6.  
  7.     public static class FileHeaders
  8.     {
  9.         public static bool TryGetVersionFromFile(string filePath, string keyword, out int version)
  10.         {
  11.             version = 0;
  12.             try
  13.             {
  14.                 if (!TryReadFirstThreeBytes(filePath, out var fileBytes)) return false;
  15.                 var keywordMatches = fileBytes[0].Equals(KeywordToAsciiByte(keyword));
  16.                 version = BytesToInt(fileBytes[1], fileBytes[2]);
  17.                 return keywordMatches;
  18.             }
  19.             catch (Exception)
  20.             {
  21.                 return false;
  22.             }
  23.         }
  24.  
  25.         public static bool UpdateFileVersion(string path, string keyword, int version)
  26.         {
  27.             try
  28.             {
  29.                 if (TryGetVersionFromFile(path, keyword, out _))
  30.                 {
  31.                     var bytes = IntToBytes(version);
  32.                     UpdateVersionBytes(path, bytes[0], bytes[1]);
  33.                     return true;
  34.                 }
  35.                 WriteHeaderToStartOfFile(path, keyword, version);
  36.                 return true;
  37.             }
  38.             catch (Exception ex)
  39.             {
  40.                 return false;
  41.             }
  42.         }
  43.  
  44.         #region plumbing
  45.  
  46.         static void WriteHeaderToStartOfFile(string filePath, string keyword, int version)
  47.         {
  48.             var intBytes = IntToBytes(version);
  49.             byte[] bytes =
  50.             {
  51.                 KeywordToAsciiByte(keyword),
  52.                 intBytes[0],
  53.                 intBytes[1]
  54.             };
  55.             WriteBytesToStartOfFile(bytes, filePath);
  56.         }
  57.  
  58.         static void WriteBytesToStartOfFile(byte[] bytesToWrite, string filePath)
  59.         {
  60.             if (!File.Exists(filePath)) File.Create(filePath).Dispose();
  61.             byte[] existingBytes = File.ReadAllBytes(filePath);
  62.             byte[] allBytes = new byte[bytesToWrite.Length + existingBytes.Length];
  63.             bytesToWrite.CopyTo(allBytes, 0);
  64.             existingBytes.CopyTo(allBytes, bytesToWrite.Length);
  65.             using var fileStream = new FileStream(filePath, FileMode.Create);
  66.             fileStream.Write(allBytes, 0, allBytes.Length);
  67.         }
  68.  
  69.         static void UpdateVersionBytes(string filePath, byte secondByte, byte thirdByte)
  70.         {
  71.             using var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite);
  72.             if (fileStream.Length < 3)
  73.                 throw new ArgumentException("The specified file does not have at least 3 bytes.", nameof(filePath));
  74.             fileStream.Seek(1, SeekOrigin.Begin);
  75.             fileStream.WriteByte(secondByte);
  76.             fileStream.WriteByte(thirdByte);
  77.         }
  78.  
  79.         static bool TryReadFirstThreeBytes(string filePath, out byte[] bytes)
  80.         {
  81.             bytes = new byte[3];
  82.             if (!File.Exists(filePath)) return false;
  83.             using var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
  84.             if (stream.Length < 3) return false;
  85.             int bytesRead = 0;
  86.             while (bytesRead < 3)
  87.             {
  88.                 var count = stream.Read(bytes, bytesRead, 3 - bytesRead);
  89.                 if (count == 0)
  90.                     break;
  91.                 bytesRead += count;
  92.             }
  93.  
  94.             return true;
  95.         }
  96.  
  97.         static byte KeywordToAsciiByte(string word) => (byte)word.Sum(x => x);
  98.         static int BytesToInt(byte highByte, byte lowByte) => (highByte << 8) | lowByte;
  99.  
  100.         static byte[] IntToBytes(int value)
  101.         {
  102.             byte[] bytes = new byte[2];
  103.             bytes[0] = (byte)(value >> 8); // high byte
  104.             bytes[1] = (byte)value; // low byte
  105.             return bytes;
  106.         }
  107.  
  108.         #endregion
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement