Esempio n. 1
0
        private static void ConvertFile(String sourcePath)
        {
            using (FileStream input = File.OpenRead(sourcePath))
                using (BinaryReader br = new BinaryReader(input))
                {
                    CshHeader header = new CshHeader
                    {
                        Type         = br.ReadBigUInt32(),
                        ColumnNumber = br.ReadBigUInt32(),
                        RowNumber    = br.ReadBigUInt32()
                    };

                    if (header.Type != 0)
                    {
                        throw new NotSupportedException($"Unknown type: {header.Type}");
                    }

                    CshCell[] cells = new CshCell[header.ColumnNumber * header.RowNumber];
                    input.DangerousReadStructs(cells, cells.Length);

                    String targetPath = Path.ChangeExtension(sourcePath, ".csv");

                    ICellContent[] buff = new ICellContent[header.ColumnNumber];

                    using (CsvWriter writer = new CsvWriter(targetPath))
                    {
                        Int32 cellIndex = 0;
                        for (Int32 r = 0; r < header.RowNumber; r++)
                        {
                            for (Int32 c = 0; c < header.ColumnNumber; c++)
                            {
                                CshCell      cell    = cells[cellIndex++];
                                ICellContent content = ReadContent(input, cell);
                                buff[c] = content;
                            }

                            ContentEntry entry = new ContentEntry(buff);
                            writer.WriteEntry(entry, null);
                        }
                    }
                }
        }
Esempio n. 2
0
        private static void TransformBackFile(String csvPath, String decPath)
        {
            String tmpPath;

            CshCell[] cells;
            using (FileStream decInput = File.OpenRead(decPath))
                using (BinaryReader decBr = new BinaryReader(decInput))
                {
                    CshHeader header = new CshHeader
                    {
                        Type         = decBr.ReadBigUInt32(),
                        ColumnNumber = decBr.ReadBigUInt32(),
                        RowNumber    = decBr.ReadBigUInt32()
                    };

                    if (header.Type != 0)
                    {
                        throw new NotSupportedException($"Unknown type: {header.Type}");
                    }

                    cells = new CshCell[header.ColumnNumber * header.RowNumber];
                    decInput.DangerousReadStructs(cells, cells.Length);

                    RowCsvEntry[] data = CsvReader.Read <RowCsvEntry>(csvPath);
                    if (header.RowNumber != data.Length)
                    {
                        throw new InvalidDataException($"header.RowNumber ({header.RowNumber}) != data.Length ({data.Length})");
                    }

                    tmpPath = decPath + ".tmp";
                    using (FileStream decOutput = File.Create(tmpPath))
                        using (BinaryWriter decBw = new BinaryWriter(decOutput))
                        {
                            decBw.WriteBig(header.Type);
                            decBw.WriteBig(header.ColumnNumber);
                            decBw.WriteBig(header.RowNumber);
                            Int64 headerOffset = decOutput.Position;

                            // Reserve space for header
                            decOutput.DangerousWriteStructs(cells, cells.Length);

                            if (decOutput.Position != decInput.Position)
                            {
                                throw new InvalidDataException();
                            }

                            AlignData(decOutput);

                            Int32 cellIndex = 0;
                            for (Int32 r = 0; r < header.RowNumber; r++)
                            {
                                RowCsvEntry row = data[r];

                                for (Int32 c = 0; c < header.ColumnNumber; c++)
                                {
                                    CshCell      cell        = cells[cellIndex++];
                                    ICellContent cellContent = ReadContent(decInput, cell);

                                    if (cell.Offset != 0)
                                    {
                                        cell.Offset          = (UInt32)decOutput.Position;
                                        cells[cellIndex - 1] = cell; // It's a struct
                                    }

                                    if (cellContent is StringContent str)
                                    {
                                        str.ParseValue(CsvParser.String(row.Raw[c]));
                                    }

                                    cellContent.Write(decOutput, decBw);
                                    AlignData(decOutput);
                                }
                            }

                            // Update offsets
                            decOutput.Position = headerOffset;
                            decOutput.DangerousWriteStructs(cells, cells.Length);


                            // For test
                            //CheckFiles(decInput, decOutput);
                        }
                }

            File.Delete(decPath);
            File.Move(tmpPath, decPath);
        }