Ejemplo n.º 1
0
        private static object[] ProcessParameter(object parameter)
        {
            if (parameter == null)
            {
                return new object[] { string.Empty }
            }
            ;

            var bytes = parameter as byte[];

            if (bytes != null)
            {
                return(bytes.OfType <object>().ToArray());
            }

            var s = parameter as string;

            if (s != null)
            {
                parameter = CsvUtilities.EscapeString(s);
            }

            return(new[] { parameter });
        }

        #endregion
    }
Ejemplo n.º 2
0
        private static Record ReadRecord(
            StreamReader streamReader,
            int lineNumber,
            MidiFileCsvConversionSettings settings)
        {
            var line = Enumerable.Range(0, int.MaxValue)
                       .Select(i => new
            {
                LineNumber = lineNumber + i,
                Line       = streamReader.ReadLine()?.Trim()
            })
                       .FirstOrDefault(l => l.Line != string.Empty);

            if (string.IsNullOrEmpty(line.Line))
            {
                return(null);
            }

            var lineNumberOffset = 0;
            var parts            = CsvUtilities.SplitCsvValues(line.Line, settings.CsvDelimiter, () =>
            {
                var nextLine = streamReader.ReadLine();
                if (nextLine != null)
                {
                    lineNumberOffset++;
                }

                return(nextLine);
            });

            if (parts.Length < 3)
            {
                ThrowBadFormat(line.LineNumber, "Missing required parameters.");
            }

            int parsedTrackNumber;
            var trackNumber = int.TryParse(parts[0], out parsedTrackNumber)
                ? (int?)parsedTrackNumber
                : null;

            ITimeSpan time = null;

            TimeSpanUtilities.TryParse(parts[1], settings.TimeType, out time);

            var recordType = parts[2];

            if (string.IsNullOrEmpty(recordType))
            {
                ThrowBadFormat(line.LineNumber, "Record type isn't specified.");
            }

            var parameters = parts.Skip(3).ToArray();

            lineNumber = line.LineNumber + lineNumberOffset;

            return(new Record(line.LineNumber, trackNumber, time, recordType, parameters));
        }
        private static void WriteRecord(
            StreamWriter streamWriter,
            int?trackNumber,
            long?time,
            string type,
            MidiFileCsvConversionSettings settings,
            TempoMap tempoMap,
            params object[] parameters)
        {
            var convertedTime = time == null
                ? null
                : TimeConverter.ConvertTo(time.Value, settings.TimeType, tempoMap);

            var processedParameters = parameters.SelectMany(ProcessParameter);

            streamWriter.WriteLine(CsvUtilities.MergeCsvValues(
                                       settings.CsvDelimiter,
                                       new object[] { trackNumber, convertedTime, type }.Concat(processedParameters)));
        }