Exemplo n.º 1
0
        //=========================================================================

        private void LoadSourceData(TextReader a_stream, bool a_compress = false)
        {
            var sw = new iStopwatch(TileFile);

            CellData = new List <short[]>(RowCount);

            var           thisLine  = new short[ColCount];
            const short[] emptyLine = null;

            // in theory a compressed line could be a maximum of 1.5 times the length
            // of an uncompressed line
            var thisLineCompressed = new List <short>(ColCount * 2);

            int rowNum = 0;

            // skip the leading rows
            while (rowNum < RowStart)
            {
                a_stream.ReadLine();
                rowNum++;
            }

            // now read the rows we are interested in
            rowNum = 0;
            while (rowNum < RowCount)
            {
                sw.PulseStart();

                var lineIn = a_stream.ReadLine();

                // this is a read error
                if (lineIn == null)
                {
                    throw new InvalidDataException($"Unexpeted EOF in data at line {rowNum}");
                }

                // returns ColCount for a full row of data or 1 for a row of zeros - any other
                // return value is an error
                int dataCount = Splitter.DoSplit(lineIn.ToCharArray(), thisLine, ColStart, ColCount);

                if (dataCount == ColCount)
                {                 // a full row was detected
                    if (a_compress)
                    {
                        // remove the last compressed data line
                        thisLineCompressed.Clear();

                        // do the actual compression
                        CompressLine(thisLine, thisLineCompressed);

                        // if we actually have compressed, then use that
                        if (thisLineCompressed.Count < ColCount)
                        {
                            CellData.Add(thisLineCompressed.ToArray());
                        }
                        else
                        {                         // otherwise use the original uncompressed line
                            CellData.Add(thisLine);
                            thisLine = new short[ColCount];
                        }
                    }
                    else
                    {
                        CellData.Add(thisLine);
                        thisLine = new short[ColCount];
                    }
                }
                else if (dataCount == 1)
                {                 // a return value of 1 means all zeroes were detected in the row
                    CellData.Add(emptyLine);
                }
                else
                {                 // a bad row was detected
                    Console.WriteLine("Error : unexpected return value from Splitter.DoSplit(): {0}", dataCount);
                    throw new InvalidDataException($"File {TileFile} has bad data at line {rowNum}");
                }

                sw.PulseStop();

                rowNum++;

                if ((rowNum % 100) == 0)
                {
                    Console.WriteLine("Processed line {0} of {1}", rowNum, RowCount);
                }
            }

            sw.Report();
        }