diff options
Diffstat (limited to 'src/deck_convert.adb')
-rw-r--r-- | src/deck_convert.adb | 113 |
1 files changed, 77 insertions, 36 deletions
diff --git a/src/deck_convert.adb b/src/deck_convert.adb index 6d140a4..94b7098 100644 --- a/src/deck_convert.adb +++ b/src/deck_convert.adb @@ -10,7 +10,8 @@ with Ada.Directories, Ada.Strings.Unbounded, Ada.Text_IO, - UnZip; + SQLite3, + Deck_IO; use @@ -50,16 +51,23 @@ procedure Deck_Convert is Further_Help : String := "Try ""deckconv --help"" for more information."; - Verbose : aliased Boolean; - Help : aliased Boolean; - Overwrite : aliased Boolean; - Output_Format : aliased GStr.String_Access; - Input_Name : aliased GStr.String_Access; - Output_Name : aliased GStr.String_Access; + 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; - Temp : File_Type; - Temp_Name : SU.Unbounded_String; + 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 @@ -81,19 +89,19 @@ begin Help => "overwrite selected output file if present"); GCom.Define_Switch - (Config => Config, Output => Output_Format'Access, + (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_Name'Access, + (Config => Config, Output => Input_Arg'Access, Switch => "-i:", Long_Switch => "--input=", Help => "file name of input deck"); GCom.Define_Switch - (Config => Config, Output => Output_Name'Access, + (Config => Config, Output => Output_Arg'Access, Switch => "-o:", Long_Switch => "--output=", - Help => "file name to store output data"); + Help => "base name to store output data"); GCom.Set_Usage @@ -116,10 +124,16 @@ begin end; - if Charhand.To_Upper (Output_Format.all) /= "CSV" and - Charhand.To_Upper (Output_Format.all) /= "FMD" - then - Put_Line (Standard_Error, Output_Format.all); + 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); @@ -127,13 +141,14 @@ begin end if; - if Input_Name.all = "" then + 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; - if not File.Exists (Input_Name.all) then + Input_Name := +(Input_Arg.all); + if not File.Exists (-Input_Name) then Put_Line (Standard_Error, "Input deck does not exist." & Latin.LF & Further_Help); ACom.Set_Exit_Status (ACom.Failure); @@ -141,13 +156,14 @@ begin end if; - if Output_Name.all = "" then + if Output_Arg.all = "" then Put_Line (Standard_Error, "File name for output deck was not provided." & Latin.LF & Further_Help); ACom.Set_Exit_Status (ACom.Failure); return; end if; - if File.Exists (Output_Name.all) and not Overwrite then + Output_Name := +(Output_Arg.all); + if File.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); @@ -155,24 +171,49 @@ begin end if; - if File.Exists ("collection.anki2") and not Overwrite then - Put_Line (Standard_Error, "Temporary collection.anki2 file already exists." & - Latin.LF & Further_Help); - ACom.Set_Exit_Status (ACom.Failure); - return; + -- if File.Exists ("collection.anki2") and not Overwrite then + -- Put_Line (Standard_Error, "Temporary collection.anki2 file already exists." & + -- Latin.LF & Further_Help); + -- ACom.Set_Exit_Status (ACom.Failure); + -- return; + -- end if; + + + -- 1. extract media to a temporary file + -- 2. generate mapping of what media filenames correspond to what names in the zip + -- 3. extract collection.anki2 to a temporary file + -- 4. open collection.anki2 as an SQL database + -- 5. SELECT models from col + -- 6. store models in vector with id, flds, tmpls + -- 7. SELECT mid, flds FROM notes + -- 8. store as a map from mid -> array of flds values + -- 9. generate required output file names and dir names for each mid from base name + -- 10. for each of the models... + + -- CSV process + -- 10.1. write header to output file using the fld names in model + -- 10.2. write csv formatted row of each note for that model + -- 10.3. as that is being done, if any note needs media, extract from zip into relevant dir + + -- FMD process + -- 10.1. + + + Deck_IO.Open_Database (-Input_Name, Deck); + Deck_IO.Query_Models (Deck, Models); + Deck_IO.Query_Notes (Deck, Notes); + Deck_IO.Close_Database (Deck); + + if Deck_Format = "csv" then + Deck_IO.Write_CSV ("", "", Models, Notes); + elsif Deck_Format = "fmd" then + Deck_IO.Read_Media_Map (-Input_Name, Media); + Deck_IO.Write_FMD ("", "", Models, Notes, Media); + else + raise Constraint_Error; end if; - -- Generate a temporary filename - Create (File => Temp); - Temp_Name := +Name (Temp); - Close (Temp); - - UnZip.Extract (Input_Name.all, "collection.anki2", (-Temp_Name)); - Put_Line ("Extracted collection as " & (-Temp_Name)); - File.Delete_File (-Temp_Name); - - end Deck_Convert; |