aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.CSV/CsvDestination.cs
blob: d88a34684f4cbff879f8bba904c46c3651092de7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Collections.Concurrent;
using System.Threading.Tasks;

namespace Tango.CSV
{
    /// <summary>
    /// Represents a <see cref="CsvFile"/> destination.
    /// </summary>
    public class CsvDestination
    {
        public StreamWriter StreamWriter;

        /// <summary>
        /// Performs an implicit conversion from <see cref="System.String"/> to <see cref="CsvDestination"/>.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns>
        /// The result of the conversion.
        /// </returns>
        public static implicit operator CsvDestination(string path)
        {
            return new CsvDestination(path);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CsvDestination"/> class.
        /// </summary>
        /// <param name="streamWriter">The stream writer.</param>
        private CsvDestination(StreamWriter streamWriter)
        {
            this.StreamWriter = streamWriter;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CsvDestination"/> class.
        /// </summary>
        /// <param name="stream">The stream.</param>
        private CsvDestination(Stream stream)
        {
            this.StreamWriter = new StreamWriter(stream);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CsvDestination"/> class.
        /// </summary>
        /// <param name="fullName">The full name.</param>
        public CsvDestination(string fullName)
        {
            FixCsvFileName(ref fullName);
            this.StreamWriter = new StreamWriter(fullName);
        }

        /// <summary>
        /// Fixes the name of the CSV file.
        /// </summary>
        /// <param name="fullName">The full name.</param>
        private static void FixCsvFileName(ref string fullName)
        {
            fullName = Path.GetFullPath(fullName);
            var path = Path.GetDirectoryName(fullName);
            if (path != null && !Directory.Exists(path))
                Directory.CreateDirectory(path);
            if (!String.Equals(Path.GetExtension(fullName), ".csv"))
                fullName += ".csv";
        }
    }
}