/// <summary> /// Parse the static csv file and send back the json keys with the minor keys /// </summary> /// <param name="cmdLineOpts"></param> /// <param name="serializer">JavaScriptSerializer</param> /// <param name="serializedResult">serialized results</param> /// <returns></returns> internal static int HandleStaticSqlFile(CommandLineOptions cmdLineOpts, JavaScriptSerializer serializer, out string serializedResult) { serializedResult = String.Empty; string pathToFile; if (cmdLineOpts.useDefaultKeyFile && string.IsNullOrEmpty(cmdLineOpts.pathToSqlFile)) { pathToFile = RingtailStaticKeyFile; } else { pathToFile = cmdLineOpts.pathToSqlFile; } if (!File.Exists(pathToFile)) { Console.WriteLine("Error, static data file not found."); { return(3); } } // Simple filtering based on type KeyTypesFilter filterType = KeyTypesFilter.ALL; if (!String.IsNullOrEmpty(cmdLineOpts.filter)) { // Might have a direct match passed in, so handle that first if (!Enum.TryParse(cmdLineOpts.filter, true, out filterType)) { // TRY PREVIEW, BETA, RC FIRST - simple mappings on the command line if (string.Compare("RC", cmdLineOpts.filter, StringComparison.InvariantCultureIgnoreCase) == 0) { filterType = KeyTypesFilter.RC; } else if (string.Compare("DEVELOPMENT", cmdLineOpts.filter, StringComparison.InvariantCultureIgnoreCase) == 0) { filterType = KeyTypesFilter.ALL; } else if (string.Compare("BETA", cmdLineOpts.filter, StringComparison.InvariantCultureIgnoreCase) == 0) { filterType = KeyTypesFilter.BETA; } else { filterType = KeyTypesFilter.ALL; // Default back to all if we go garbage } } } // Parse the csv and serialize the results var darkLaunchKeysList = TextOperations.ParseCSV(pathToFile, filterType); serializedResult = serializer.Serialize(darkLaunchKeysList); return(0); }
/// <summary> /// Parse the static CSV keys file and return a list of keys and descriptions based on the filter and rules /// </summary> /// <param name="path">full path to csv file to operate on</param> /// <param name="keyFilter">Filter target</param> /// <returns>List of KeyDataObject</returns> public static IEnumerable <KeyDataObject> ParseCSV(string path, KeyTypesFilter keyFilter = KeyTypesFilter.ALL) { if (!File.Exists(path)) { yield break; } using (TextFieldParser parser = new TextFieldParser(path)) { parser.CommentTokens = new string[] { "#" }; parser.SetDelimiters(new string[] { ";", "," }); parser.HasFieldsEnclosedInQuotes = true; // Skip over header line. parser.ReadLine(); while (!parser.EndOfData) { string[] fields = parser.ReadFields(); KeyDataObject data; if (fields.Length == 4) { data = new KeyDataObject() { Description = fields[1], FeatureKey = fields[0], MinorKey = fields[3], KeyType = fields[2] }; bool includeKeyRow = false; KeyTypesFilter rowKeyType; if (Enum.TryParse(fields[2], true, out rowKeyType)) { bool includeInBeta = BETA_KEYS.HasFlag(rowKeyType); bool includeInPreAlpha = DEVELOPMENT_KEYS.HasFlag(rowKeyType); bool includeInGA = RC_KEYS.HasFlag(rowKeyType); if (keyFilter.HasFlag(KeyTypesFilter.RC)) { includeKeyRow = includeInGA; } else if (keyFilter.HasFlag(KeyTypesFilter.BETA)) { includeKeyRow = includeInBeta; } else if (keyFilter.HasFlag(KeyTypesFilter.DEVELOPMENT)) { includeKeyRow = includeInPreAlpha; } else { includeKeyRow = includeInPreAlpha; // No key, show only in development mode } } // Ignore type and include if (keyFilter == KeyTypesFilter.ALL) { includeKeyRow = true; } if (includeKeyRow) { yield return(data); } } } } }