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
|
with
Ada.Characters.Latin_1,
Ada.Characters.Handling,
GNAT.Command_Line,
GNAT.Strings,
Ada.Command_Line,
Ada.Directories,
Ada.Strings.Unbounded,
Ada.Text_IO,
SQLite3,
Deck_IO;
use
Ada.Text_IO;
procedure Deck_Convert is
package Latin renames Ada.Characters.Latin_1;
package Charhand renames Ada.Characters.Handling;
package ACom renames Ada.Command_Line;
package GCom renames GNAT.Command_Line;
package GStr renames GNAT.Strings;
package FD renames Ada.Directories;
package SU renames Ada.Strings.Unbounded;
use type FD.File_Kind;
use type SU.Unbounded_String;
function "+"
(S : in String)
return SU.Unbounded_String
renames SU.To_Unbounded_String;
function "-"
(US : in SU.Unbounded_String)
return String
renames SU.To_String;
Config : GCom.Command_Line_Configuration;
Further_Help : String := "Try ""deckconv --help"" for more information.";
Verbose : aliased Boolean;
Help : aliased Boolean;
Overwrite : aliased Boolean;
Format_Arg : aliased GStr.String_Access;
Input_Arg : aliased GStr.String_Access;
Output_Arg : aliased GStr.String_Access;
Deck_Format : SU.Unbounded_String;
Input_Name : SU.Unbounded_String;
Output_Name : SU.Unbounded_String;
Deck : Deck_IO.Deck_Handle;
Models : Deck_IO.Model_Maps.Map;
Notes : Deck_IO.Note_Vectors.Vector;
Media : Deck_IO.Media_Maps.Map;
begin
GCom.Define_Switch
(Config => Config, Output => Verbose'Access,
Switch => "-v", Long_Switch => "--verbose",
Help => "chatty output on stderr");
GCom.Define_Switch
(Config => Config, Output => Help'Access,
Switch => "-h", Long_Switch => "--help",
Help => "show this help information");
GCom.Define_Switch
(Config => Config, Output => Overwrite'Access,
Switch => "-f", Long_Switch => "--force",
Help => "overwrite selected output file if present");
GCom.Define_Switch
(Config => Config, Output => Format_Arg'Access,
Switch => "-t:", Long_Switch => "--type=",
Help => "format of output data, valid options are CSV or FMD");
GCom.Define_Switch
(Config => Config, Output => Input_Arg'Access,
Switch => "-i:", Long_Switch => "--input=",
Help => "file name of input deck");
GCom.Define_Switch
(Config => Config, Output => Output_Arg'Access,
Switch => "-o:", Long_Switch => "--output=",
Help => "base name to store output data");
GCom.Set_Usage
(Config => Config,
Usage => "[switches]",
Help =>
"Utility to convert Anki flashcard decks to Fresh Memory dictionaries." & Latin.LF &
"At minimum the type, input, and output options are required." & Latin.LF);
begin
GCom.Getopt (Config);
exception
when GCom.Exit_From_Command_Line =>
ACom.Set_Exit_Status (ACom.Failure);
return;
when GCom.Invalid_Switch =>
ACom.Set_Exit_Status (ACom.Failure);
return;
end;
if Format_Arg.all = "" then
if Verbose then
Put_Line (Standard_Error, "WARNING: No output deck format selected. " &
"Proceeding with FMD as default.");
end if;
Deck_Format := +"fmd";
else
Deck_Format := +(Charhand.To_Lower (Format_Arg.all));
end if;
if Deck_Format /= "csv" and Deck_Format /= "fmd" then
Put_Line (Standard_Error, "Output deck format required. Valid options are CSV or FMD." &
Latin.LF & Further_Help);
ACom.Set_Exit_Status (ACom.Failure);
return;
end if;
if Input_Arg.all = "" then
Put_Line (Standard_Error, "File name of input deck was not provided." &
Latin.LF & Further_Help);
ACom.Set_Exit_Status (ACom.Failure);
return;
end if;
Input_Name := +(Input_Arg.all);
if not FD.Exists (-Input_Name) then
Put_Line (Standard_Error, "Input deck does not exist." &
Latin.LF & Further_Help);
ACom.Set_Exit_Status (ACom.Failure);
return;
end if;
if Output_Arg.all = "" then
Put_Line (Standard_Error, "Base file name for output deck was not provided." &
Latin.LF & Further_Help);
ACom.Set_Exit_Status (ACom.Failure);
return;
end if;
Output_Name := +(Output_Arg.all);
if FD.Exists ((-Output_Name) & "." & (-Deck_Format)) and not Overwrite then
Put_Line (Standard_Error, "Output deck file name already exists." &
Latin.LF & Further_Help);
ACom.Set_Exit_Status (ACom.Failure);
return;
end if;
-- Program functionality proper starts here
Deck_IO.Open_Database (-Input_Name, Deck);
Deck_IO.Query_Models (Deck, Models);
Deck_IO.Query_Notes (Deck, Notes);
Deck_IO.Close_Database (Deck);
declare
Contain_Dir : String := FD.Containing_Directory (-Output_Name);
Simple_Name : String := FD.Simple_Name (-Output_Name);
begin
if Deck_Format = "csv" then
Deck_IO.Write_CSV (Contain_Dir, Simple_Name, Models, Notes, Overwrite);
elsif Deck_Format = "fmd" then
Deck_IO.Read_Media_Map (-Input_Name, Media);
Deck_IO.Write_FMD (Contain_Dir, Simple_Name, Models, Notes, Media, Overwrite);
else
raise Constraint_Error;
end if;
end;
end Deck_Convert;
|