-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
202 lines (180 loc) · 8.12 KB
/
Program.cs
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using System;
using System.IO;
using CommandLine;
namespace Convert.Spreadsheet
{
class Program
{
public class Options
{
[Option('i', "inputfilepath", Required = true, HelpText = "The input filepath.")]
public string InputFilepath { get; set; }
[Option('d', "delete", Required = false, HelpText = "Set to delete original file.")]
public bool Delete { get; set; }
[Option('r', "rename", Required = false, HelpText = "Define to rename output file.")]
public string Rename { get; set; }
[Option('p', "policy", Required = false, HelpText = "Set to convert file to comply with regular archival requirements.")]
public bool Policy { get; set; }
[Option('s', "policy-strict", Required = false, HelpText = "Set to convert file to comply with strict archival requirements.")]
public bool PolicyStrict { get; set; }
[Option('l', "libreoffice", Required = false, HelpText = "Set to use LibreOffice instead of Excel for conversion.")]
public bool LibreOffice { get; set; }
[Option('f', "outputfileformat", Required = true, HelpText = "Define output file format.")]
public string OutputFileFormat { get; set; }
[Option('o', "outputfolder", Required = false, HelpText = "Define to save output file in custom folder. Default is same folder.")]
public string OutputFolder { get; set; }
}
public static int Main(string[] args)
{
// Parse user arguments
var parser = new Parser(with => with.HelpWriter = null);
int exitcode = parser.ParseArguments<Options>(args).MapResult((opts) => RunApp(opts), errs => ShowHelp(errs));
return exitcode;
}
static int RunApp(Options arg)
{
string input_extension = Path.GetExtension(arg.InputFilepath).ToLower();
string output_extension = "." + arg.OutputFileFormat.ToLower().Split("-").First();
string output_extension_LibreOffice = arg.OutputFileFormat.ToLower().Split("-").First();
string output_folder, output_filepath;
int fail = 0, success = 1;
// Set output folder
if (arg.OutputFolder != null && Directory.Exists(arg.OutputFolder))
{
output_folder = arg.OutputFolder;
}
else if (arg.OutputFolder != null && !Directory.Exists(arg.OutputFolder))
{
Console.WriteLine($"Output folder \"{arg.OutputFolder}\" does not exist");
return fail;
}
else
{
output_folder = Path.GetDirectoryName(arg.InputFilepath);
}
// Set output filename
if (arg.Rename != null)
{
output_filepath = output_folder + "\\" + arg.Rename + output_extension;
}
else
{
output_filepath = output_folder + "\\" + Path.GetFileNameWithoutExtension(arg.InputFilepath) + output_extension;
}
// End program if no file exists
if (!File.Exists(arg.InputFilepath))
{
Console.WriteLine("No file in input filepath");
return fail;
}
// End program if input or output file formats are not accepted
FileFormats check = new FileFormats();
int input_check = check.CheckInputFileFormat(input_extension);
if (input_check == 0)
{
Console.WriteLine("Input file format is not an accepted file format");
return fail;
}
int output_check = check.CheckOutputFileFormat(output_extension);
if (output_check == 0)
{
Console.WriteLine("Output file format is not an accepted file format");
return fail;
}
// Define data types
bool convert_success = false;
try
{
// Remove file attributes on file
File.SetAttributes(arg.InputFilepath, FileAttributes.Normal);
// Convert file
Convert conversion = new Convert();
// Use LibreOffice
if (arg.LibreOffice == true)
{
convert_success = conversion.AnyFileFormat_LibreOffice(arg.InputFilepath, output_folder, output_extension_LibreOffice);
}
// Use LibreOffice, but file formats are not supported
if (arg.LibreOffice == true && arg.OutputFileFormat == "xlsx-strict")
{
Console.WriteLine("LibreOffice cannot convert to output file format");
}
// Use Excel
else if (input_extension != ".numbers" || input_extension != ".fods")
{
// First transform file format to int
int xlFileFormat = check.ConvertFileFormatToInt(arg.OutputFileFormat);
// Then use int in conversion
convert_success = conversion.AnyFileFormat_Excel(arg.InputFilepath, output_filepath, xlFileFormat);
}
// Use Excel, but file formats are not supported
else if (input_extension == ".numbers" || input_extension == ".fods")
{
Console.WriteLine("Excel cannot convert to output file format");
}
// Convert to comply with archival requirements
if (arg.Policy || arg.PolicyStrict)
{
Policy ArcReq = new Policy();
if (output_extension == ".xlsx")
{
ArcReq.OOXML_Errors(output_filepath);
if (arg.PolicyStrict)
{
ArcReq.OOXML_Warnings(output_filepath);
}
Console.WriteLine("File complies with archival requirements");
}
else if (output_extension == ".ods")
{
ArcReq.ODS(output_filepath, arg.PolicyStrict);
Console.WriteLine("File complies with archival requirements");
}
else
{
Console.WriteLine("File format policy compliance is not supported for output file format");
}
}
}
// If spreadsheet is password protected or otherwise unreadable
catch (FormatException)
{
Console.WriteLine("Input file cannot be read");
return fail;
}
// Post conversion operations
finally
{
if (convert_success == true)
{
// If rename
if (arg.Rename != null && arg.LibreOffice == true)
{
string temp_filepath = output_folder + "\\" + Path.GetFileNameWithoutExtension(arg.InputFilepath) + output_extension;
File.Move(temp_filepath, output_filepath);
}
// If delete
if (arg.Delete == true)
{
// Delete original file, if filepath was not 1.xlsx
if (arg.InputFilepath != output_filepath)
{
File.Delete(arg.InputFilepath);
Console.WriteLine("Input file was deleted");
}
}
}
}
// Return success to user
Console.WriteLine("Program finished");
return success;
}
// Show help to user, if parsing arguments fail
static int ShowHelp(IEnumerable<Error> errs)
{
int fail = 0;
Console.WriteLine("Input arguments have errors");
return fail;
}
}
}