Thursday, April 26, 2018

A copy paste, when it accumulated just search it to fix.

public class AutonomeDuplicates
    {
        public class LineInfo
        {
            public string FileName { get; set;}
            public int LineNumber { get; set;}
        }

        public class Lines : List<LineInfo>
        {
            public string Text  { get; set;}
        }
    

        public static void Execute (string[] args)
        {
            //"/home/user/Projects/operation_control/" "*.c" "False" "/home/user/Projects/operation_control/log_duplicates"
            var folder = args[0];
            var ext = args[1];
            var allFiles = Convert.ToBoolean(args[2]);
            var output = args[3];

            File.Delete(output);

            var files = Directory.GetFiles(folder, ext, SearchOption.AllDirectories);

            var hashes = new Dictionary<int, Lines> ();
            var regex = new Regex(@"\s+");

            foreach (var file in files) {
                var lines = File.ReadAllLines (file);
                int lineNumber = 0;
                foreach (var in lines) {
                    var line = regex.Replace(l.Trim (), " ");
                    var hash = line.GetHashCode ();

                    if (hashes.ContainsKey (hash)) {
                        hashes [hash].Add (new LineInfo{ FileName = file, LineNumber = lineNumber });
                    } else {
                        hashes.Add (hash, new Lines { new LineInfo{ FileName = file, LineNumber = lineNumber } });
                        hashes [hash].Text = line;
                    }
                    lineNumber++;
                }
            }

            var results = hashes
                .Select (h => h.Value)
                .Where(c => c.Count > 1)
                .Where(c => !string.IsNullOrWhiteSpace(c.Text) &&
                    !c.Text.Contains("#include"));

            var siblings = results.Where (c => c.Any (li => results.Any (c1 => c1.Any (li1 => (li.LineNumber == li1.LineNumber + 1
            || li.LineNumber == li1.LineNumber - 1) && li.FileName == li1.FileName))));

            foreach (var result in siblings) {
                File.AppendAllText (output, string.Format ("{0}\ncount{1}", result.Text, result.Count));
            }
        }
    }

No comments:

debug magazine archive

  71 jounals still available on issuu with great story of netlabels time.  debug_mag Publisher Publications - Issuu