CGC_Codes

Extractor

Jul 3rd, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.95 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Text.RegularExpressions;
  5. using System.Text;
  6. using System.Threading;
  7.  
  8. namespace TestTask
  9. {
  10.     class Extractor
  11.     {
  12.         private string _src;
  13.         private string _dst;
  14.  
  15.         private Queue<string> _sources;
  16.         private List<Token> _tokens;
  17.  
  18.         private Mutex _queueLock;
  19.         private Mutex _outputLock;
  20.  
  21.         public Extractor(string src, string dst)
  22.         {
  23.             this._src = src;
  24.             this._dst = dst;
  25.  
  26.             this._sources = new Queue<string>();
  27.  
  28.             StreamReader sr = new StreamReader(this._src);
  29.             String line;
  30.             while ((line = sr.ReadLine()) != null)
  31.                 _sources.Enqueue(line.Trim());
  32.  
  33.             this._queueLock = new Mutex();
  34.             this._outputLock = new Mutex();
  35.  
  36.             this._tokens = new List<Token>();
  37.             this._tokens.Add(new TokenA());
  38.             this._tokens.Add(new TokenComment());
  39.             this._tokens.Add(new TokenArea());
  40.             this._tokens.Add(new TokenImg());
  41.             this._tokens.Add(new TokenScript());
  42.             this._tokens.Add(new TokenLink());
  43.             this._tokens.Add(new TokenEmbed());
  44.         }
  45.  
  46.         public void Run(int threads_num)
  47.         {
  48.             List<Thread> threads = new List<Thread>();
  49.             for (int i = 0; i < threads_num; i++)
  50.                 threads.Add(new Thread(this.ProcessSources));
  51.  
  52.             foreach (Thread thread in threads)
  53.                 thread.Start();
  54.             foreach (Thread thread in threads)
  55.                 thread.Join();
  56.         }
  57.  
  58.         private void ProcessSources()
  59.         {
  60.             while (true)
  61.             {
  62.                
  63.                 this._queueLock.WaitOne();
  64.                 if (this._sources.Count == 0)
  65.                 {
  66.                     this._queueLock.ReleaseMutex();
  67.                     break;
  68.                 }
  69.                 string source = this._sources.Dequeue();
  70.                 this._queueLock.ReleaseMutex();
  71.  
  72.                 string content = PageLoader.Load(source);
  73.  
  74.                
  75.                 List<Token> data = (new TextParser(content, source, this._tokens)).parsedTokens;
  76.  
  77.                
  78.                 this._outputLock.WaitOne();
  79.                 StreamWriter sw = new StreamWriter(this._dst, true);
  80.                 Console.WriteLine("{0} {1}", System.Web.HttpUtility.UrlPathEncode(source), DateTime.Now.ToString("yyyy-MM       ddTHH:mm:sszzz"));
  81.                 sw.WriteLine("{0} {1}", System.Web.HttpUtility.UrlPathEncode(source), DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz"));
  82.                 foreach (Token item in data)
  83.                 {
  84.                     Console.WriteLine("    {0}", item.ToString());
  85.                     sw.WriteLine("    {0}", item.ToString());
  86.                 }
  87.                 sw.Close();
  88.                 this._outputLock.ReleaseMutex();
  89.             }
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment