Esempio n. 1
0
 /// <summary>
 ///     Parses the specified file with the specified hook.
 /// </summary>
 /// <param name="target">Target file.</param>
 /// <param name="hook">Target hook used when parsing.</param>
 /// <remarks>
 ///     If no hook is specified <see cref="EmptyHook{T}.Instance" /> will be use.
 /// </remarks>
 internal static void ParseFile(string target, [CanBeNull] IParserHook <string> hook)
 {
     if (target == null)
     {
         throw new ArgumentNullException(nameof(target));
     }
     if (!File.Exists(target))
     {
         throw new FileNotFoundException("Could not find the specified file.", target);
     }
     using (var stream = new FileInputStream(target))
     {
         var lexer  = new OfcLexer(stream, true);
         var parser = new OfcParser(lexer, hook ?? EmptyHook <string> .Instance);
         parser.Parse();
     }
 }
Esempio n. 2
0
        public void Conduction()
        {
            try
            {
                Status = 0;
                Directory.CreateDirectory(System.IO.Path.GetDirectoryName(_dataPath));
                Directory.CreateDirectory(System.IO.Path.GetDirectoryName(_metaPath));
                // open the binary output file
                using (var binaryOutput = File.OpenWrite(_dataPath + ActionUtils.TempFileExtention))
                {
                    Status = 1;
                    MarerHook hook;
                    if (_configuaration.True("rounding"))
                    {
                        if (typeof(TAlgorithm) == typeof(string))
                        {
                            hook = new MarerHook <string>(new RoundingDigitsAlgorithm <string>((IAlgorithm <string>)_algorithm), NoDataConverter.Instance, binaryOutput, _configuaration);
                        }
                        // if not we have to use the converting algorithm
                        else
                        {
                            hook = new MarerHook <string>(new RoundingDigitsAlgorithm <TAlgorithm>(new StringSourceAlgorithm <TAlgorithm>(_converter, _algorithm)), NoDataConverter.Instance, binaryOutput, _configuaration);
                        }
                    }
                    else
                    {
                        hook = new MarerHook <TAlgorithm>(_algorithm, _converter, binaryOutput, _configuaration);
                    }

                    Status = 2;
                    //var hook = new MarerHook<OfcNumber>(algorithm, converter, binaryOutput, _configuaration);

                    Status = 3;
                    var f = false;
                    using (var source = new FileInputStream(_sourcePath))
                    {
                        Status = 4;
                        var lexer  = new OfcLexer(source);
                        var parser = new OfcParser(lexer, hook);
                        hook.PositionProvider = parser;
                        Status = 5;
                        try
                        {
                            parser.Parse();
                            _generatedDataFile = hook.CompressedDataSections.Count != 0;
                        }
                        catch (Exception)
                        {
                            f = true;
                        }
                    }
                    Status = 6;
                    if (f || hook.CompressedDataSections.Count == 0)
                    {
                        _generatedDataFile = false;
                        Directory.CreateDirectory(System.IO.Path.GetDirectoryName(_lzmaPath));
                        Status = 100;
                        using (var source = File.OpenRead(_sourcePath))
                        {
                            Status = 101;
                            using (var outp = File.OpenWrite(_lzmaPath))
                            {
                                Status = 102;
                                LzmaHelper.CompressLzma(source, outp);
                                Status = 103;
                            }
                        }
                        Status = 104;
                    }
                    else
                    {
                        Status = 200;
                        using (var source = File.OpenText(_sourcePath))
                        {
                            Status = 201;
                            using (var outp = File.CreateText(_metaPath + ActionUtils.TempFileExtention))
                            {
                                Status = 202;
                                using (var writer = new MarerWriter(source, outp, hook.CompressedDataSections))
                                {
                                    Status = 203;
                                    writer.Do();
                                    Status = 204;
                                }
                            }
                        }
                        Status = 205;
                        using (var source = File.OpenRead(_metaPath + ActionUtils.TempFileExtention))
                        {
                            Status = 206;
                            using (var outp = File.OpenWrite(_metaPath))
                            {
                                Status = 207;
                                LzmaHelper.CompressLzma(source, outp);
                                Status = 208;
                            }
                        }
                        Status = 209;
                        File.Delete(_metaPath + ActionUtils.TempFileExtention);
                        Status = 210;
                    }
                }

                if (_generatedDataFile)
                {
                    Status = 212;
                    using (var input = File.OpenRead(_dataPath + ActionUtils.TempFileExtention))
                    {
                        Status = 213;
                        using (var output = File.OpenWrite(_dataPath))
                        {
                            Status = 214;
                            LzmaHelper.CompressLzma(input, output);
                            Status = 215;
                        }
                        Status = 216;
                    }
                    Status = 217;
                }
                File.Delete(_dataPath + ActionUtils.TempFileExtention);
            }
            catch (UnauthorizedAccessException)
            {
                Message = "Access failed.";
                throw;
            }
            catch (Exception exception)
            {
                Message = "Internal: " + exception.Message;
                throw;
            }
        }