/// <summary> /// In a single thread loops through the useragents in the file /// provided perform a match with the data set provided passing /// control back to the method provided for further processing. /// </summary> /// <param name="provider"></param> /// <param name="userAgents"></param> /// <param name="method"></param> /// <returns>Counts for each of the methods used</returns> internal static Results DetectLoopSingleThreaded(Provider provider, IEnumerable<string> userAgents, ProcessMatch method, object state) { provider.DataSet.ResetCache(); var match = provider.CreateMatch(); var results = new Results(); foreach (var line in userAgents) { provider.Match(line.Trim(), match); method(results, match, state); results.Count++; results.Methods[match.Method]++; } AssertPool(provider); ReportMethods(results.Methods); ReportProvider(provider); ReportTime(results); return results; }
// Snippet Start public static void Run(string fileName, string inputFile) { // DataSet is the object used to interact with the data file. // StreamFactory creates Dataset with pool of binary readers to // perform device lookup using file on disk. DataSet dataSet = StreamFactory.Create(fileName, false); // Provides access to device detection functions. Provider provider = new Provider(dataSet); // Used to store and access detection results. // Same match will be reused multiple times to avoid unnecessary // object creation. Match match = provider.CreateMatch();; // Name of the output file results will be saved to. string outputFile = "OfflineProcessingOutput.csv"; // How many lines of the input CSV file to read. const int linesToRead = 20; // Line currently being read. int currentLine = 0; // HTTP User-Agent string to match, taken from input the CSV file. string userAgent; // 51Degrees properties to append the CSV file with. // For the full list of properties see: // https://51degrees.com/resources/property-dictionary string[] properties = { "IsMobile", "PlatformName", "PlatformVersion" }; // Opens input and output files. StreamReader fin = new StreamReader(inputFile); StreamWriter fout = new StreamWriter(outputFile); Console.WriteLine("Starting Offline Processing Example."); // Print CSV headers to output file. fout.Write("User-Agent"); for (int i = 0; i < properties.Count(); i++) { fout.Write("|" + properties[i]); } fout.Write("\n"); while ((userAgent = fin.ReadLine()) != null && currentLine < linesToRead) { // Match current User-Agent. Match object reused. provider.Match(userAgent, match); // Write the User-Agent. fout.Write(userAgent); // Append properties. foreach (var property in properties) { fout.Write("|" + match[property]); } fout.Write("\n"); currentLine++; } fin.Close(); fout.Close(); Console.WriteLine("Output Written to " + outputFile); // Finally close the dataset, releasing resources and file locks. dataSet.Dispose(); }