From 8f2f6a5e2074a5da684f9f1a5f5e8f3c51a82b76 Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Mon, 6 Feb 2017 13:34:23 +1100 Subject: More or less completed main procedure, fixed some bugs --- src/candidates.adb | 7 +++--- src/candidates.ads | 8 +++++-- src/csv.adb | 6 ++++- src/simple_time.adb | 18 ++++++++++----- src/stv.adb | 64 ++++++++++++++++++++++++++++++++++++++++++++++++----- 5 files changed, 86 insertions(+), 17 deletions(-) diff --git a/src/candidates.adb b/src/candidates.adb index bb09a40..33d3901 100644 --- a/src/candidates.adb +++ b/src/candidates.adb @@ -31,8 +31,9 @@ package body Candidates is procedure Read_Candidates - (Filename, State : in String; - Candidate_List : out Candidate_Vector) + (Filename : in String; + State : in State_Name; + Candidate_List : out Candidate_Vector) is package My_CSV is new CSV; use Ada.Text_IO; @@ -53,7 +54,7 @@ package body Candidates is -- AEC Senate candidate data is arranged in csv format if Current_Record.Length = 25 and then Current_Record.Element (2) = "S" and then - Current_Record.Element (3) = State + Current_Record.Element (3) = State_Name'Image (State) then Current_Candidate := (First_Name => Current_Record.Element (8), diff --git a/src/candidates.ads b/src/candidates.ads index fb04a4e..8b76707 100644 --- a/src/candidates.ads +++ b/src/candidates.ads @@ -7,6 +7,9 @@ private with Ada.Containers.Vectors; package Candidates is + type State_Name is (ACT, NT, TAS, SA, WA, VIC, QLD, NSW); + + type Candidate is private; type CandidateID is new Positive; @@ -24,8 +27,9 @@ package Candidates is procedure Read_Candidates - (Filename, State : in String; - Candidate_List : out Candidate_Vector); + (Filename : in String; + State : in State_Name; + Candidate_List : out Candidate_Vector); function First diff --git a/src/csv.adb b/src/csv.adb index eab633b..1b8fd7f 100644 --- a/src/csv.adb +++ b/src/csv.adb @@ -28,7 +28,11 @@ package body CSV is exit when SU.Length (This_In) > 0 and then SU.Element (This_In, 1) = Quote; end loop; Output := Result; - Remaining := SU.Tail (This_In, SU.Length (This_In) - 1); + if SU.Length (This_In) > 0 then + Remaining := SU.Tail (This_In, SU.Length (This_In) - 1); + else + Remaining := SU.To_Unbounded_String (0); + end if; return True; else return False; diff --git a/src/simple_time.adb b/src/simple_time.adb index cf1995c..1b10dc8 100644 --- a/src/simple_time.adb +++ b/src/simple_time.adb @@ -17,13 +17,19 @@ package body Simple_Time is Hours : Integer := Integer (Raw_Secs) / 3600; Minutes : Integer := (Integer (Raw_Secs) - Hours * 3600) / 60; Seconds : Duration := Raw_Secs - Duration (Hours) * 3600 - Duration (Minutes) * 60; + + Year_Str : String (1 .. 4); + Month_Str, Day_Str, Hour_Str, Minute_Str : String (1 .. 2); + Second_Str : String (1 .. 12); begin - return Trim (Year_Number'Image (Year (Moment)), Both) & "-" & - Trim (Month_Number'Image (Month (Moment)), Both) & "-" & - Trim (Day_Number'Image (Day (Moment)), Both) & " " & - Trim (Integer'Image (Hours), Both) & ":" & - Trim (Integer'Image (Minutes), Both) & ":" & - Trim (Duration'Image (Seconds), Both); + Move (Trim (Year_Number'Image (Year (Moment)), Left), Year_Str, Left, Right, '0'); + Move (Trim (Month_Number'Image (Month (Moment)), Left), Month_Str, Left, Right, '0'); + Move (Trim (Day_Number'Image (Day (Moment)), Left), Day_Str, Left, Right, '0'); + Move (Trim (Integer'Image (Hours), Left), Hour_Str, Left, Right, '0'); + Move (Trim (Integer'Image (Minutes), Left), Minute_Str, Left, Right, '0'); + Move (Trim (Duration'Image (Seconds), Left), Second_Str, Left, Right, '0'); + return Year_Str & '-' & Month_Str & '-' & Day_Str & " " & + Hour_Str & ':' & Minute_Str & ':' & Second_Str; end To_String; diff --git a/src/stv.adb b/src/stv.adb index 01c7a38..4d2691a 100644 --- a/src/stv.adb +++ b/src/stv.adb @@ -23,6 +23,7 @@ procedure STV is use type File.File_Kind; use type SU.Unbounded_String; + use type Simple_Time.Time; Config : GCom.Command_Line_Configuration; @@ -31,6 +32,7 @@ procedure STV is Further_Help : String := "Try ""stv --help"" for more information."; + -- I'm not fond of accesses to string accesses Verbose : aliased Boolean; Version : aliased Boolean; Help : aliased Boolean; @@ -41,12 +43,19 @@ procedure STV is State_String : aliased GStr.String_Access; - type State_Name is (ACT, NT, TAS, SA, WA, VIC, QLD, NSW); - State : State_Name; + State : Candidates.State_Name; Start_Time, Finish_Time : Simple_Time.Time; + + Main_Log, Log_Msg : SU.Unbounded_String; + Log_File : File_Type; + + + Candidate_List : Candidates.Candidate_Vector; + Above_Ballot : Candidates.Above_Line_Ballot; + Below_Ballot : Candidates.Below_Line_Ballot; begin @@ -174,7 +183,7 @@ begin -- check state option is valid begin - State := State_Name'Value (State_String.all); + State := Candidates.State_Name'Value (State_String.all); exception when Constraint_Error => Put_Line ("Invalid State/Territory or State/Territory not provided." & ASCII.LF & Further_Help); @@ -184,14 +193,59 @@ begin -- set up logging - --File.Create_Directory (Output_Dir.all); + File.Create_Directory (Output_Dir.all); Start_Time := Simple_Time.Now; Main_Log := SU.To_Unbounded_String (Output_Dir.all & "/" & "log.txt"); Log_Msg := SU.To_Unbounded_String ("Started election count at " & Simple_Time.To_String (Start_Time)); - Put_Line (SU.To_String (Log_Msg)); + Create (Log_File, Append_File, SU.To_String (Main_Log)); + Put_Line (Log_File, SU.To_String (Log_Msg)); + Close (Log_File); + if Verbose then + Put_Line (Standard_Error, SU.To_String (Log_Msg)); + end if; + + + -- set up the election processing + if Verbose then + Put_Line (Standard_Error, "Reading candidate data..."); + end if; + Candidates.Read_Candidates (Candidate_File.all, State, Candidate_List); + Candidates.Generate_Ballots (Candidate_List, Above_Ballot, Below_Ballot); + if Verbose then + Put_Line (Standard_Error, "Reading preference data..."); + end if; + -- read in preference data here + if Verbose then + Put_Line (Standard_Error, "Done." & ASCII.LF); + Put_Line (Standard_Error, "Setting up election..."); + end if; + -- set up election here + if Verbose then + Put_Line (Standard_Error, "Done." & ASCII.LF); + end if; + + -- run the show + if Verbose then + Put_Line (Standard_Error, "Running..." & ASCII.LF); + end if; + -- run election here + if Verbose then + New_Line (Standard_Error); + end if; + + -- finish up logging Finish_Time := Simple_Time.Now; + Log_Msg := SU.To_Unbounded_String + ("Finished election count at " & Simple_Time.To_String (Finish_Time) & ASCII.LF & + Duration'Image (Finish_Time - Start_Time) & " seconds elapsed." & ASCII.LF); + Open (Log_File, Append_File, SU.To_String (Main_Log)); + Put_Line (Log_File, SU.To_String (Log_Msg)); + Close (Log_File); + if Verbose then + Put_Line (Standard_Error, SU.To_String (Log_Msg)); + end if; end STV; -- cgit