Example #1
0
        public IWritable Write(
            [InputPin(Name = "M", Description = "source Matrix", PropertyMode = PropertyMode.Allow)]
            M m,
            [InputPin(Name = "Delimiter", Description = "Character which should be used as delimiter between two data fields", PropertyMode = PropertyMode.Default, Editor = WellKnownEditors.SingleLineText)]
            string delimiter = ",",
            [InputPin(Name = "Headline", Description = "Write a Headline", PropertyMode = PropertyMode.Default, Editor = WellKnownEditors.SingleLineText)]
            string[] headline = null
            )
        {
            return(Writable.Create(async(fileStream, cancel) =>
            {
                using (var writer = new StreamWriter(fileStream))
                {
                    var sb = new StringBuilder();
                    if (headline != null)
                    {
                        for (int i = 0; i < headline.Length; ++i)
                        {
                            if (i > 0)
                            {
                                sb.Append(delimiter);
                            }

                            var s = headline[i];
                            s = s.Replace("\"", "\"\""); // duplicate quotation marks " -> ""
                            sb.Append('"');              // start of string
                            sb.Append(s);
                            sb.Append('"');              // end of string
                        }

                        await writer.WriteLineAsync(sb.ToString());
                        sb.Clear();
                    }

                    var a = m.UnderlyingArray;
                    for (int i = 0; i < a.Dimension[0]; i++)
                    {
                        if ((i % 100) == 0)
                        {
                            cancel.ThrowIfCancellationRequested();
                        }

                        for (int j = 0; j < a.Dimension[1]; j++)
                        {
                            if (j > 0)
                            {
                                sb.Append(delimiter);
                            }
                            sb.Append(Convert.ToString(a.GetValue(i, j), CultureInfo.InvariantCulture));
                        }

                        await writer.WriteLineAsync(sb.ToString());      // we always add a line break - even when writing the last line of the CSV file
                        sb.Clear();
                    }

                    await writer.FlushAsync();
                }
            }));
        }
Example #2
0
 public IWritable CreateWritable(
     [InputPin(PropertyMode = PropertyMode.Allow)] string data,
     [InputPin(PropertyMode = PropertyMode.Default)] Encoding encoding = null
     )
 {
     return(Writable.Create(async(destination, cancel) =>
     {
         using (var sw = new StreamWriter(destination, encoding ?? Encoding.UTF8, 512, true))
         {
             await sw.WriteAsync(data).ConfigureAwait(false);
         }
     }));
 }
Example #3
0
 public static IWritable EncodeBitmap(
     [InputPin(PropertyMode = PropertyMode.Never)] SKBitmap bitmap,
     [InputPin(PropertyMode = PropertyMode.Default)] SKEncodedImageFormat format = SKEncodedImageFormat.Png,
     [InputPin(PropertyMode = PropertyMode.Default)] int quality = 95
     )
 {
     return(Writable.Create(stream =>
     {
         using (var skStream = new SKManagedWStream(stream))
         {
             SKPixmap.Encode(skStream, bitmap, format, quality);
         }
     }));
 }