Esempio n. 1
0
        private void btnFixAss_Click(object sender, EventArgs e)
        {
            AssFile af = new AssFile();

            af.Load(txtAssFile.Text);
            if (af.Warnings.Count > 0)
            {
                Debug.WriteLine("Found warnings, writing down fixed file");
                af.SaveAs(txtAssFile.Text + ".FIXED.ass");
                AcControls.AcMessageBox.AcMessageBox.Show("Fixed file!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                AcControls.AcMessageBox.AcMessageBox.Show("No need to fix file!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
Esempio n. 2
0
        private void btnTestCharset_Click(object sender, EventArgs e)
        {
            //Load a file
            AssFile af = new AssFile();

            af.Load(txtAssFile.Text);
            //Load b file
            AssFile bf = new AssFile();

            bf.Load(txtSectionsFile.Text);

            //sort a file by time
            //af.SectionEvents.SortEventLines(AssEventsSortType.ByStartTime, Core.ASS.SortOrder.Ascending);
            //sort b file by time
            //bf.SectionEvents.SortEventLines(AssEventsSortType.ByStartTime, Core.ASS.SortOrder.Ascending);

            List <String> diffs = new List <String>();

            //Compare event lines
            Int32 bIdx = 0;

            //second more accurate attempt
            diffs.Clear();
            //Create a working copy for the event lines
            List <AssLineEvent> bLines = new List <AssLineEvent>(bf.SectionEvents.EventLines);

            foreach (AssLineEvent aLine in af.SectionEvents.EventLines)
            {
                //begin loop for each line in b file
                Int32 minDiff     = LD(aLine.Text, bLines[0].Text);
                Int64 minTimeDiff = Math.Abs(aLine.StartValue - bLines[0].StartValue);
                //lines are the same
                if (minDiff == 0)
                {
                    //remove the line from the working copy
                    bLines.RemoveAt(0);
                    continue;
                }
                else
                {
                    bIdx = 0;
                    for (Int32 i = 1; i < bLines.Count; i++)
                    {
                        Int32 cDiff     = LD(aLine.Text, bLines[i].Text);
                        Int64 cTimeDiff = Math.Abs(aLine.StartValue - bLines[i].StartValue);
                        Debug.WriteLine(String.Format("a: {0} b: {1} cdiff: {2}", aLine.OriginalOrder, bLines[i].OriginalOrder, cDiff));
                        //lines are the same
                        if (cDiff == 0)
                        {
                            minDiff = 0;
                            //remove the line from the working copy
                            bLines.RemoveAt(i);
                            break;
                        }
                        else
                        {
                            //Find the minimum difference
                            if (cDiff + cTimeDiff < minDiff + minTimeDiff)
                            {
                                minDiff     = cDiff;
                                minTimeDiff = cTimeDiff;
                                bIdx        = i;
                            }
                        }
                    }
                    //lines are the same
                    if (minDiff == 0)
                    {
                        continue;
                    }
                    else
                    {
                        //lines are different
                        //check the line with the smallest text difference
                        //Find the differences
                        StringBuilder     oldBuilder = new StringBuilder();
                        StringBuilder     newBuilder = new StringBuilder();
                        AcStringTokenizer a          = new AcStringTokenizer(aLine.Text);
                        AcStringTokenizer b          = new AcStringTokenizer(bLines[bIdx].Text);
                        a.IgnoreWhiteSpace = false;
                        b.IgnoreWhiteSpace = false;

                        List <String> aList = a.GetAllTokenValues();
                        List <String> bList = b.GetAllTokenValues();

                        foreach (String token in aList)
                        {
                            oldBuilder.AppendLine(token);
                        }
                        if (oldBuilder.Length > 1)
                        {
                            oldBuilder.Length -= 1;
                        }
                        foreach (String token in bList)
                        {
                            newBuilder.AppendLine(token);
                        }
                        if (newBuilder.Length > 1)
                        {
                            newBuilder.Length -= 1;
                        }

                        AcDiff.AcDiffItem[] f = AcDiff.DiffText(oldBuilder.ToString(), newBuilder.ToString(), true, true, false);

                        //Change tokens
                        foreach (AcDiff.AcDiffItem it in f)
                        {
                            for (Int32 i = it.StartA; i < it.StartA + it.deletedA; i++)
                            {
                                aList[i] = "--" + aList[i] + "--";
                            }
                            for (Int32 i = it.StartB; i < it.StartB + it.insertedB; i++)
                            {
                                bList[i] = "++" + bList[i] + "++";
                            }
                        }

                        //Add the old and new string to the list
                        oldBuilder.Length = 0;
                        foreach (String token in aList)
                        {
                            oldBuilder.Append(token);
                        }
                        newBuilder.Length = 0;
                        foreach (String token in bList)
                        {
                            newBuilder.Append(token);
                        }

                        diffs.Add("-- [" + aLine.OriginalOrder + "] " + oldBuilder.ToString());
                        diffs.Add("++ [" + bLines[bIdx].OriginalOrder + "] " + newBuilder.ToString());
                    }
                }
            }
            lstAss2.SuspendLayout();
            lstAss2.Items.Clear();
            foreach (String s in diffs)
            {
                lstAss2.Items.Add(s);
            }
            lstAss2.ResumeLayout();


            String oldLine = txtAssFile.Text;
            String newLine = txtSectionsFile.Text;

            //Check if lines are the same:
            if (oldLine != newLine)
            {
                StringBuilder     oldBuilder = new StringBuilder();
                StringBuilder     newBuilder = new StringBuilder();
                AcStringTokenizer a          = new AcStringTokenizer(oldLine);
                AcStringTokenizer b          = new AcStringTokenizer(newLine);
                a.IgnoreWhiteSpace = false;
                a.SymbolChars      = new List <char> {
                    ',', ':', '\\'
                };
                b.IgnoreWhiteSpace = false;
                b.SymbolChars      = new List <char> {
                    ',', ':', '\\'
                };

                List <AcStringToken> aList = a.GetAllTokens();
                List <AcStringToken> bList = b.GetAllTokens();

                foreach (AcStringToken token in aList)
                {
                    oldBuilder.AppendLine(token.Value);
                }
                oldBuilder.Length -= 1;
                foreach (AcStringToken token in bList)
                {
                    newBuilder.AppendLine(token.Value);
                }
                newBuilder.Length -= 1;

                AcDiff.AcDiffItem[] f = AcDiff.DiffText(oldBuilder.ToString(), newBuilder.ToString(), true, true, false);
            }
        }