Example #1
0
            public string RepairMissing(bool isFinalRepair)
            {
                if (Data.fBuf == null || actualFlacs == null || actualFlacs.Count == 0 || actualFlacs.Count != Data.Files.Items.Count || Data.Issues.MaxSeverity >= Severity.Error)
                {
                    return("Invalid attempt");
                }

                string err = null;

                int buf2Size = Data.fBuf.Length;

                for (int ix = 0; ix < actualFlacs.Count; ++ix)
                {
                    buf2Size += actualFlacs[ix].Name.Length - Data.Files.Items[ix].Name.Length;
                }
                var buf2   = new byte[buf2Size];
                int bufIx0 = Data.Files.Items[0].BufIndex;
                int length = bufIx0;

                Array.Copy(Data.fBuf, buf2, bufIx0);

                int dstIx = 0;

                for (int ix = 0;;)
                {
                    dstIx += length;
                    if (dstIx >= buf2.Length)
                    {
                        break;
                    }
                    byte[] nameBytes = FormatBase.Cp1252.GetBytes(actualFlacs[ix].Name);
                    Array.Copy(nameBytes, 0, buf2, dstIx, nameBytes.Length);
                    dstIx += nameBytes.Length;

                    string try1252 = FormatBase.Cp1252.GetString(nameBytes);
                    if (err == null && try1252 != actualFlacs[ix].Name)
                    {
                        err = "Track file name(s) not Windows-1252 clean.";
                    }

                    int srcIx = Data.Files.Items[ix].BufIndex2;
                    ++ix;
                    length = (ix == actualFlacs.Count ? Data.fBuf.Length : Data.Files.Items[ix].BufIndex) - srcIx;
                    Array.Copy(Data.fBuf, srcIx, buf2, dstIx, length);
                }

                if (err == null)
                {
                    try
                    {
                        Data.fbs.Position = bufIx0;
                        Data.fbs.Write(buf2, bufIx0, buf2.Length - bufIx0);
                        if (Data.fbs.Length != buf2.Length)
                        {
                            Data.fbs.SetLength(buf2.Length);
                        }

                        if (isFinalRepair)
                        {
                            CloseFile();
                        }

                        for (int ix = 0; ix < Data.Files.Items.Count; ++ix)
                        {
                            FilesModel.SetIsFound(ix, true);
                            FilesModel.SetName(ix, actualFlacs[ix].Name);
                        }
                    }
                    catch (Exception ex) when(ex is UnauthorizedAccessException || ex is IOException)
                    {
                        err = ex.Message.TrimEnd(null);
                    }
                }

                return(err);
            }