Exemplo n.º 1
0
        private static int parseCliArgument(string arg, ref CliParams cliParams)
        {
            int indexDelta = 0;

            // Parse input directory
            if (_argIndex == 0)
            {
                try {
                    DirectoryInfo di = new DirectoryInfo(arg);
                    cliParams.InputDir = di;
                }
                catch (Exception e) {
                    string errMsg = $"{arg} is not a valid directory path or could not be opened.";
                    throw new CliException(errMsg, "inputDirectory", arg, e);
                }
            }

            else if (_argIndex >= 1)
            {
                string errMsg = $"Too many arguments provided.";
                throw new CliException(errMsg);
            }

            ++_argIndex;
            return(indexDelta);
        }
Exemplo n.º 2
0
        // HELPERS
        private static CliParams defaultParams()
        {
            CliParams cp = new CliParams()
            {
                ValuesOnly = false,
                ShowHelp   = false,
            };

            return(cp);
        }
Exemplo n.º 3
0
        private static void run(CliParams cp)
        {
            // Analyze all provided recordings in that directory
            Console.Write($"Reading data from the provided recordings...  ");
            IEnumerable <FileInfo> files = cp.InputDir.EnumerateFiles("*.txt");

            if (files.Count() == 0)
            {
                Console.WriteLine("Complete!");
                Console.WriteLine("No files found.");
                return;
            }
            Recording[] recs = files.Select(f => RecordingWrapper.FromText(f)).ToArray();
            Console.WriteLine("Complete!");

            // Get STTC vs distance for all Recordings
            Console.Write("Calculating STTCs...  ");
            IDictionary <Recording, IDictionary <double, double[]> > results =
                recs.ToDictionary(
                    rec => rec,
                    rec => RecordingWrapper.STTCvsDistance(rec, CORRELATION_DT)
                    );

            Console.WriteLine("Complete!");

            // Output values to a file
            Console.Write($"Saving values to {cp.OutputFile.FullName}...  ");
            using (FileStream fs = cp.OutputFile.Create())
                using (BufferedStream bs = new BufferedStream(fs))
                    using (StreamWriter sw = new StreamWriter(bs)) {
                        if (!cp.ValuesOnly)
                        {
                            sw.WriteLine($"STTC results generated at {DateTime.Now.ToShortTimeString()} on {DateTime.Now.ToShortDateString()}:");
                            sw.WriteLine("Unit-Distance\tSTTC");
                        }
                        foreach (Recording rec in results.Keys)
                        {
                            if (!cp.ValuesOnly)
                            {
                                int    minutes = (int)(rec.Duration / 60d);
                                double seconds = rec.Duration - 60d * minutes;
                                sw.WriteLine($"\nRecording from {rec.TextFile} ({minutes}m {seconds}s long):");
                            }
                            foreach (double dist in results[rec].Keys)
                            {
                                foreach (double sttc in results[rec][dist])
                                {
                                    sw.WriteLine($"{dist}\t{sttc}");
                                }
                            }
                        }
                    }
            Console.WriteLine("Complete!");
        }
Exemplo n.º 4
0
        private static void validateParams(ref CliParams cliParams)
        {
            // If we're just showing the help text then don't bother validating further
            if (cliParams.ShowHelp)
            {
                return;
            }

            // Make sure an input directory has been provided
            if (cliParams.InputDir == null)
            {
                string errMsg = $"You must provide the path to a directory containing recording text files!";
                throw new CliException(errMsg, "inputDirectory");
            }

            // If so, default the output file to a file in that directory (if one wasn't provided explicitly)
            else if (cliParams.OutputFile == null)
            {
                cliParams.OutputFile = new FileInfo(cliParams.InputDir.FullName + @"\sttc.txt");
            }
        }
Exemplo n.º 5
0
        // INTERFACE
        public static CliParams Parse(string[] args)
        {
            // Set default command line values
            CliParams cp = defaultParams();

            // Parse options/arguments provided by the user
            for (int a = 0; a < args.Length; ++a)
            {
                if (isCliOption(args[a]))
                {
                    a += parseCliOption(args, a, ref cp);
                }
                else
                {
                    a += parseCliArgument(args[a], ref cp);
                }
            }

            // Provide final high-level validation
            validateParams(ref cp);

            return(cp);
        }
Exemplo n.º 6
0
        private static int parseCliOption(string[] args, int index, ref CliParams cliParams)
        {
            // If there are no other tokens in the option string, then throw an Exception
            if (args[index].Length == 1)
            {
                string errMsg = $"'{args[index]}' is not a valid option.";
                throw new CliException(errMsg);
            }

            char opt        = args[index][1];
            int  indexDelta = 0;

            // Parse help switch
            if (opt == 'h' || opt == 'H' || opt == '?')
            {
                cliParams.ShowHelp = true;
            }

            // Parse output file option
            else if (opt == 'o' || opt == 'O')
            {
                string paramName = "outputFile";
                if (args.Length <= index + 1)
                {
                    string errMsg = "You must provide a filename with the /O flag.";
                    throw new CliException(errMsg, paramName);
                }
                else
                {
                    string fileName = args[index + 1];
                    if (isCliOption(fileName))
                    {
                        string errMsg = $"{fileName} is not a valid filename.";
                        throw new CliException(errMsg, paramName, fileName);
                    }
                    else
                    {
                        try {
                            FileInfo fi = new FileInfo(fileName);
                            cliParams.OutputFile = fi;
                            indexDelta           = 1;
                        }
                        catch (Exception e) {
                            string errMsg = $"{fileName} is not a valid filename or could not be created.";
                            throw new CliException(errMsg, paramName, fileName, e);
                        }
                    }
                }
            }

            // Parse values-only switch
            else if (opt == 'v' || opt == 'V')
            {
                cliParams.ValuesOnly = true;
            }

            // If this is not a valid option, then throw an exception
            else
            {
                string errMsg = $"'{args[index]}' is not a valid option.";
                throw new CliException(errMsg);
            }

            return(indexDelta);
        }