Advertisement
AlFas

Code file that edits code files from other code files

Oct 6th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using static System.Console;
  8. using static System.IO.File;
  9.  
  10. namespace FunctionDefinitionCreator
  11. {
  12.     public static class Program
  13.     {
  14.         public static string fileName;
  15.         public static string[] lines;
  16.  
  17.         public static void Main(string[] args)
  18.         {
  19.             WriteLineWithColor("Please enter the name of the C++ code file to add definitions in non-defined functions to.", ConsoleColor.Yellow);
  20.             ForegroundColor = ConsoleColor.Cyan;
  21.             fileName = ReadLine();
  22.             lines = ReadAllLines(fileName);
  23.             int currentParenthesisIndex = 0;
  24.             bool omitNextParentheses = false, isValidFunction = false;
  25.             for (int i = 0; i < lines.Length; i++)
  26.             {
  27.                 string indent = "";
  28.                 int indChars = 0;
  29.                 while (indChars < lines[i].Length && (lines[i][indChars] == ' ' || lines[i][indChars] == '\t'))
  30.                     indChars++;
  31.                 indent = lines[i].Substring(0, indChars);
  32.                 for (int j = 0; j < lines[i].Length; j++)
  33.                 {
  34.                     if (lines[i][j] == '=' && currentParenthesisIndex == 0) omitNextParentheses = true;
  35.                     else if (lines[i][j] == '(' && !omitNextParentheses) currentParenthesisIndex++;
  36.                     else if (lines[i][j] == ')' && !omitNextParentheses)
  37.                     {
  38.                         currentParenthesisIndex--;
  39.                         omitNextParentheses = false;
  40.                         isValidFunction = !omitNextParentheses && currentParenthesisIndex == 0;
  41.                     }
  42.                     else if (lines[i][j] == ';' && isValidFunction) lines[i] = lines[i].Replace("\n" + indent + "{\n\n" + indent + "}", j, 1);
  43.                 }
  44.             }
  45.             WriteAllLines(fileName, lines);
  46.             WriteLineWithColor("\nThe non-defined functions in that C++ file have been successfully defined.", ConsoleColor.Green);
  47.             ReadKey();
  48.         }
  49.  
  50.         public static void WriteWithColor(string s, ConsoleColor c)
  51.         {
  52.             ForegroundColor = c;
  53.             Write(s);
  54.         }
  55.         public static void WriteLineWithColor(string s, ConsoleColor c)
  56.         {
  57.             ForegroundColor = c;
  58.             WriteLine(s);
  59.         }
  60.     }
  61.     public static class Extensions
  62.     {
  63.         public static int Find(this string s, string match)
  64.         {
  65.             for (int i = 0; i <= s.Length - match.Length; i++)
  66.             {
  67.                 string sub = s.Substring(i, match.Length);
  68.                 if (sub == match)
  69.                     return i;
  70.             }
  71.             return -1;
  72.         }
  73.         public static int Find(this string s, string match, int occurence)
  74.         {
  75.             int occurences = 0;
  76.             for (int i = 0; i <= s.Length - match.Length; i++)
  77.             {
  78.                 string sub = s.Substring(i, match.Length);
  79.                 if (sub == match)
  80.                 {
  81.                     occurences++;
  82.                     if (occurences == occurence)
  83.                         return i;
  84.                 }
  85.             }
  86.             return -1;
  87.         }
  88.         public static int Find(this string s, string match, int start, int end)
  89.         {
  90.             for (int i = start; i <= end - match.Length; i++)
  91.             {
  92.                 string sub = s.Substring(i, match.Length);
  93.                 if (sub == match)
  94.                     return i;
  95.             }
  96.             return -1;
  97.         }
  98.         public static int GetLastNumber(this string s)
  99.         {
  100.             int n = 0;
  101.             bool isLastNumber = false;
  102.             for (int i = 0; i < s.Length; i++)
  103.             {
  104.                 isLastNumber = int.TryParse(s.Substring(i), out n);
  105.                 if (isLastNumber)
  106.                     return n;
  107.             }
  108.             throw new ArgumentException("The string has no number in the end.");
  109.         }
  110.         public static int[] FindAll(this string s, string match)
  111.         {
  112.             List<int> indices = new List<int>();
  113.             for (int i = 0; i <= s.Length - match.Length; i++)
  114.             {
  115.                 string sub = s.Substring(i, match.Length);
  116.                 if (sub == match)
  117.                     indices.Add(i);
  118.             }
  119.             return indices.ToArray();
  120.         }
  121.         public static int[] FindAll(this string s, string match, int start, int end)
  122.         {
  123.             List<int> indices = new List<int>();
  124.             for (int i = start; i <= end - match.Length; i++)
  125.             {
  126.                 string sub = s.Substring(i, match.Length);
  127.                 if (sub == match)
  128.                     indices.Add(i);
  129.             }
  130.             return indices.ToArray();
  131.         }
  132.         public static string FixBase64String(this string s)
  133.         {
  134.             int lastNumber = 0;
  135.             int lastInvalidCharacter = 0;
  136.             bool continueChecking = true;
  137.             while (continueChecking)
  138.             {
  139.                 if (s[s.Length - lastNumber - 1].IsNumber())
  140.                     lastNumber++;
  141.                 else if (!s[s.Length - lastNumber - 1].IsBase64Character())
  142.                     lastInvalidCharacter = ++lastNumber;
  143.                 else
  144.                     continueChecking = false;
  145.             }
  146.             s = s.Substring(0, s.Length - lastInvalidCharacter);
  147.             while (s.Length % 4 != 0)
  148.                 s += "=";
  149.             return s;
  150.         }
  151.         public static string Substring(this string s, string from, string to)
  152.         {
  153.             int startIndex = s.Find(from) + from.Length;
  154.             int endIndex = s.Find(to);
  155.             int length = endIndex - startIndex;
  156.             return s.Substring(startIndex, length);
  157.         }
  158.         public static string RemoveLastNumber(this string s)
  159.         {
  160.             string l = s;
  161.             for (int i = 0; i < s.Length; i++)
  162.             {
  163.                 l = s.Substring(i);
  164.                 if (int.TryParse(l, out int shit))
  165.                     return s.Substring(0, i);
  166.             }
  167.             return s;
  168.         }
  169.         public static string Replace(this string originalString, string stringToReplaceWith, int startIndex, int length)
  170.         {
  171.             string result = originalString;
  172.             result = result.Remove(startIndex, length);
  173.             result = result.Insert(startIndex, stringToReplaceWith);
  174.             return result;
  175.         }
  176.         public static string ReplaceWholeWord(this string originalString, string oldString, string newString)
  177.         {
  178.             for (int i = originalString.Length - oldString.Length; i >= 0; i--)
  179.             {
  180.                 if (originalString.Substring(i, oldString.Length) == oldString)
  181.                     if ((i > 0 ? (!originalString[i - 1].IsLetterOrNumber()) : true) && (i < originalString.Length - oldString.Length ? (!originalString[i + oldString.Length].IsLetterOrNumber()) : true))
  182.                     {
  183.                         originalString = originalString.Replace(newString, i, oldString.Length);
  184.                         i -= oldString.Length;
  185.                     }
  186.             }
  187.             return originalString;
  188.         }
  189.         public static bool ContainsWholeWord(this string s, string match)
  190.         {
  191.             for (int i = s.Length - match.Length; i >= 0; i--)
  192.             {
  193.                 if (s.Substring(i, match.Length) == match)
  194.                     if ((i > 0 ? (!s[i - 1].IsLetterOrNumber()) : true) && (i < s.Length - match.Length ? (!s[i + match.Length].IsLetterOrNumber()) : true))
  195.                         return true;
  196.             }
  197.             return false;
  198.         }
  199.         public static bool MatchesStringCaseFree(this string s, string match)
  200.         {
  201.             if (s.Length != match.Length)
  202.                 return false;
  203.             for (int i = 0; i < s.Length; i++)
  204.             {
  205.                 if (s[i].IsUpperCaseLetter())
  206.                     if (s[i] + 32 != match[i] && s[i] != match[i])
  207.                         return false;
  208.                     else if (s[i].IsLowerCaseLetter())
  209.                         if (s[i] - 32 != match[i] && s[i] != match[i])
  210.                             return false;
  211.             }
  212.             return true;
  213.         }
  214.         public static bool IsBase64Character(this char c)
  215.         {
  216.             return IsNumber(c) || IsLetter(c) || c == '/' || c == '+' || c == '=';
  217.         }
  218.         public static bool IsLetter(this char c) => (c >= 65 && c <= 90 || c >= 97 && c <= 122);
  219.         public static bool IsLowerCaseLetter(this char c) => (c >= 97 && c <= 122);
  220.         public static bool IsNumber(this char c) => (c >= 48 && c <= 57);
  221.         public static bool IsUpperCaseLetter(this char c) => (c >= 65 && c <= 90);
  222.         public static bool IsLetterOrNumber(this char c) => IsLetter(c) || IsNumber(c);
  223.  
  224.         public static int[] FindOccurences(this string[] a, string match)
  225.         {
  226.             if (a != null)
  227.             {
  228.                 List<int> occurences = new List<int>();
  229.                 for (int i = 0; i < a.Length; i++)
  230.                     if (a[i] == match)
  231.                         occurences.Add(i);
  232.                 return occurences.ToArray();
  233.             }
  234.             else return new int[0];
  235.         }
  236.         public static string[] Replace(this string[] a, char oldChar, char newChar)
  237.         {
  238.             for (int i = 0; i < a.Length; i++)
  239.                 a[i] = a[i].Replace(oldChar, newChar);
  240.             return a;
  241.         }
  242.         public static string[] Replace(this string[] a, string oldString, string newString)
  243.         {
  244.             for (int i = 0; i < a.Length; i++)
  245.                 a[i] = a[i].Replace(oldString, newString);
  246.             return a;
  247.         }
  248.         public static string[] ReplaceWholeWord(this string[] a, string oldString, string newString)
  249.         {
  250.             for (int i = 0; i < a.Length; i++)
  251.                 a[i] = a[i].ReplaceWholeWord(oldString, newString);
  252.             return a;
  253.         }
  254.         public static string[] RemoveEmptyElements(this string[] a)
  255.         {
  256.             List<string> result = new List<string>();
  257.             for (int i = 0; i < a.Length; i++)
  258.                 if (a[i].Length > 0)
  259.                     result.Add(a[i]);
  260.             return result.ToArray();
  261.         }
  262.         public static bool ContainsAtLeast(this string[] a, string match)
  263.         {
  264.             if (a != null)
  265.             {
  266.                 for (int i = 0; i < a.Length; i++)
  267.                     if (a[i].Contains(match))
  268.                         return true;
  269.                 return false;
  270.             }
  271.             return false;
  272.         }
  273.         public static bool ContainsAtLeastWholeWord(this string[] a, string match)
  274.         {
  275.             if (a != null)
  276.             {
  277.                 for (int i = 0; i < a.Length; i++)
  278.                     if (a[i].ContainsWholeWord(match))
  279.                         return true;
  280.                 return false;
  281.             }
  282.             return false;
  283.         }
  284.         public static string Combine(this string[] s)
  285.         {
  286.             StringBuilder str = new StringBuilder();
  287.             for (int i = 0; i < s.Length; i++)
  288.                 str = str.Append(s[i]);
  289.             return str.ToString();
  290.         }
  291.         public static string Combine(this string[] s, string separator)
  292.         {
  293.             if (s.Length > 0)
  294.             {
  295.                 StringBuilder str = new StringBuilder();
  296.                 str = str.Append(s[0]);
  297.                 for (int i = 1; i < s.Length; i++)
  298.                     str = str.Append(separator + s[i]);
  299.                 return str.ToString();
  300.             }
  301.             else return "";
  302.         }
  303.         public static string[,] Split(this string[] s, char separator)
  304.         {
  305.             List<string[]> separated = new List<string[]>();
  306.             for (int i = 0; i < s.Length; i++)
  307.                 separated.Add(s[i].Split(separator));
  308.             return separated.ToTwoDimensionalArray();
  309.         }
  310.  
  311.         public static T[] GetInnerArray<T>(this T[,] ar, int innerArrayIndex)
  312.         {
  313.             T[] innerAr = new T[ar.GetLength(1)];
  314.             for (int i = 0; i < innerAr.Length; i++)
  315.                 innerAr[i] = ar[innerArrayIndex, i];
  316.             return innerAr;
  317.         }
  318.         public static int[] GetLengths<T>(this List<T[]> l)
  319.         {
  320.             int[] lengths = new int[l.Count];
  321.             for (int i = 0; i < l.Count; i++)
  322.                 lengths[i] = l[i].Length;
  323.             return lengths;
  324.         }
  325.         public static int[] GetLengths(this int[,] ar)
  326.         {
  327.             int[] lengths = new int[ar.Length];
  328.             for (int i = 0; i < ar.Length; i++)
  329.                 lengths[i] = ar.GetInnerArray(i).Length;
  330.             return lengths;
  331.         }
  332.         public static int[] GetLengths(this string[,] ar)
  333.         {
  334.             int[] lengths = new int[ar.GetLength(0)];
  335.             for (int i = 0; i < lengths.Length; i++)
  336.                 lengths[i] = ar.GetInnerArray(i).Length;
  337.             return lengths;
  338.         }
  339.  
  340.         public static T[,] ToTwoDimensionalArray<T>(this List<T[]> l)
  341.         {
  342.             T[,] ar = new T[l.Count, l.GetLengths().Max()];
  343.             for (int i = 0; i < l.Count; i++)
  344.                 for (int j = 0; j < l[i].Length; j++)
  345.                     ar[i, j] = l[i][j];
  346.             return ar;
  347.         }
  348.     }
  349. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement