public void CreateCRCFile(string fileName, CarsonBackend database, List <string> dataFields, FileStream readStream, BinaryReader reader, TableInstance tableInstance, bool headerOnly)
        {
            List <Definition> fieldDefinitions = new List <Definition>();

            for (int x = 0; x < dataFields.Count; x++)
            {
                Definition definition = database.FindFieldDefinition(dataFields[x]);

                if (definition == null)
                {
                    throw new Exception("Invalid Field Name: " + dataFields[x]);
                }

                fieldDefinitions.Add(definition);
            }

            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            using (FileStream writeStream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
                using (BinaryWriter writer = new BinaryWriter(writeStream))
                {
                    writeStream.Seek(0, SeekOrigin.Begin);
                    writer.Write(StringToByte("CARSONDB" + CarsonBackend.DatabaseVersion));

                    List <int> fieldOffsets          = new List <int>();
                    string     fieldDefinitionString = database.DatabaseName + "\0";

                    for (int x = 0; x < fieldDefinitions.Count; x++)
                    {
                        fieldDefinitionString += fieldDefinitions[x].FieldName + (x < fieldDefinitions.Count - 1 ? "\0" : "");
                        fieldOffsets.Add(Convert.ToInt32(fieldDefinitions[x].FieldOrdinal));
                        fieldOffsets.Add(ComputeFieldSize(fieldDefinitions[x].FieldType));
                        fieldOffsets.Add(fieldDefinitions[x].FieldType == AVImarkDataType.AVImarkDynamicString ? 1 : 0);
                    }

                    writer.Write(fieldDefinitionString);

                    int[] fieldData = fieldOffsets.ToArray();

                    if (!headerOnly)
                    {
                        using (CarsonLibDll carsonLib = new CarsonLibDll())
                        {
                            carsonLib.ComputerCompareCrc(readStream.SafeFileHandle, writeStream.SafeFileHandle, (int)tableInstance, ref fieldData[0], fieldDefinitions.Count, CrcFunction.Create, (int)writeStream.Position, database.IsClassic());
                        }
                    }
                }
        }
        public List <int> FindChangedRecords(string fileName, CarsonBackend database, FileStream readStream, out int recordsFound, TableInstance tableInstance)
        {
            using (FileStream diffStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None))
                using (BinaryReader diffReader = new BinaryReader(diffStream))
                {
                    IntPtr            recordPtr;
                    List <Definition> fieldDefinitions = new List <Definition>();

                    ValidateFileHeader(diffStream, diffReader);

                    string   fieldDefinitionString = diffReader.ReadString();
                    string[] fields = fieldDefinitionString.Split('\0');

                    for (int x = 1; x < fields.Length; x++)
                    {
                        fieldDefinitions.Add(database.FindFieldDefinition(fields[x]));
                    }

                    List <int> fieldOffsets = new List <int>();

                    for (int x = 0; x < fieldDefinitions.Count; x++)
                    {
                        fieldOffsets.Add(Convert.ToInt32(fieldDefinitions[x].FieldOrdinal));
                        fieldOffsets.Add(ComputeFieldSize(fieldDefinitions[x].FieldType));
                        fieldOffsets.Add(fieldDefinitions[x].FieldType == AVImarkDataType.AVImarkDynamicString ? 1 : 0);
                    }

                    int[] fieldData = fieldOffsets.ToArray();

                    using (CarsonLibDll carsonLib = new CarsonLibDll())
                    {
                        recordPtr = carsonLib.ComputerCompareCrc(readStream.SafeFileHandle, diffStream.SafeFileHandle, (int)tableInstance, ref fieldData[0], fieldDefinitions.Count, CrcFunction.Compare, (int)diffStream.Position, database.IsClassic());

                        int   recordCount = carsonLib.RecordCountFromHandle(recordPtr);
                        int[] result      = new int[recordCount];
                        Marshal.Copy(recordPtr, result, 0, recordCount);

                        //Number of records in differential file is calculated on the file length less 16 (diff file header + version) and the field definitions.
                        recordsFound = ((int)diffStream.Length - 16 - fieldDefinitionString.Length) / 4;
                        return(new List <int>(result));
                    }
                }
        }
        public static ICrc GetTableInstance(TableInstance tableInstance, CarsonBackend instance, int recordNumber)
        {
            switch (tableInstance)
            {
            case TableInstance.Account:
                return(new Account.AccountData((Account)instance, recordNumber));

            case TableInstance.Animal:
                return(new Animal.AnimalData((Animal)instance, recordNumber));

            case TableInstance.Appointment:
                return(new Appointment.AppointmentData((Appointment)instance, recordNumber));

            case TableInstance.Attach:
                return(new Attachment.AttachmentData((Attachment)instance, recordNumber));

            case TableInstance.Audit:
                return(new Audit.AuditData((Audit)instance, recordNumber));

            case TableInstance.Client:
                return(new Client.ClientData((Client)instance, recordNumber));

            case TableInstance.Diagnose:
                return(new Diagnose.DiagnoseData((Diagnose)instance, recordNumber));

            case TableInstance.Entry:
                return(new Entry.EntryData((Entry)instance, recordNumber));

            case TableInstance.Follow:
                return(new Follow.FollowData((Follow)instance, recordNumber));

            case TableInstance.Item:
                return(new Item.ItemData((Item)instance, recordNumber));

            case TableInstance.Lab:
                return(new Lab.LabData((Lab)instance, recordNumber));

            case TableInstance.Problem:
                return(new Problem.ProblemData((Problem)instance, recordNumber));

            case TableInstance.PurchaseOrder:
                return(new PurchaseOrder.PurchaseOrderData((PurchaseOrder)instance, recordNumber));

            case TableInstance.Price:
                return(new Price.PriceData((Price)instance, recordNumber));

            case TableInstance.Quotail:
                return(new Quotail.QuotailData((Quotail)instance, recordNumber));

            case TableInstance.Quote:
                return(new Quote.QuoteData((Quote)instance, recordNumber));

            case TableInstance.Reminder:
                return(new Reminder.ReminderData((Reminder)instance, recordNumber));

            case TableInstance.Service:
                return(new Service.ServiceData((Service)instance, recordNumber));

            case TableInstance.Split:
                return(new Split.SplitData((Split)instance, recordNumber));

            case TableInstance.Table:
                return(new Table.TableData((Table)instance, recordNumber));

            case TableInstance.Test:
                return(new Test.TestData((Test)instance, recordNumber));

            case TableInstance.Treatment:
                return(new Treatment.TreatmentData((Treatment)instance, recordNumber));

            case TableInstance.User:
                return(new User.UserData((User)instance, recordNumber));

            default:
                return(null);
            }
        }