Exemplo n.º 1
0
            public static BglBlock Read(Stream s)
            {
                BglBlock block = new BglBlock();

                block.Length = BabylonBglParser.ReadNum(s, 1);
                block.Type   = block.Length & 0xF;
                if (block.Type == 4)
                {
                    return(null);
                }                                     // end-of-file marker
                block.Length >>= 4;
                block.Length   = block.Length < 4 ?
                                 BabylonBglParser.ReadNum(s, block.Length + 1) : block.Length - 4;

                if (block.Length > 0)
                {
                    block.Data = new byte[block.Length];
                    s.Read(block.Data, 0, block.Data.Length);
                }
                return(block);
            }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            Console.Clear();
            Console.ResetColor();
            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.WriteLine("Dictionary Babylon -> HTML v2.0");
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.WriteLine("Alon Rotem, 2012\n");

            Encoding sourceEnc = null;
            Encoding targetEnc = null;
            string fileName = string.Empty;

            for (int arg = 0; arg < args.Length; arg++)
            {
                if (args[arg].ToUpper().Trim() == "-SE")
                {
                    if (args.Length > arg + 1)
                    {
                        try
                        {
                            sourceEnc = System.Text.Encoding.GetEncoding(args[arg + 1]);
                        }
                        catch (Exception ex)
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.Write("\nError interpreting encoding numerical code \"" + args[arg + 1] + "\": ");
                            Console.ForegroundColor = ConsoleColor.White;
                            Console.Write(ex.Message + "\n");
                            Console.ForegroundColor = ConsoleColor.Gray;
                        }
                        arg++;
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.Write("\nWarning: ");
                        Console.ForegroundColor = ConsoleColor.White;
                        Console.Write("-se argument requested but no valid encoding numerical code given.\n");
                        Console.ForegroundColor = ConsoleColor.Gray;
                    }
                }

                else if (args[arg].ToUpper().Trim() == "-TE")
                {
                    if (args.Length > arg + 1)
                    {
                        try
                        {
                            targetEnc = System.Text.Encoding.GetEncoding(args[arg + 1]);
                        }
                        catch (Exception ex)
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.Write("\nError interpreting encoding numerical code \"" + args[arg + 1] + "\": ");
                            Console.ForegroundColor = ConsoleColor.White;
                            Console.Write(ex.Message + "\n");
                            Console.ForegroundColor = ConsoleColor.Gray;
                        }
                        arg++;
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.Write("\nWarning: ");
                        Console.ForegroundColor = ConsoleColor.White;
                        Console.Write("-te argument requested but no valid encoding numerical code given.\n");
                        Console.ForegroundColor = ConsoleColor.Gray;
                    }
                }

                else
                {
                    fileName = args[arg];
                    if (!File.Exists(fileName))
                    {
                        //input file doesn't exist
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write("Error: ");
                        Console.ForegroundColor = ConsoleColor.White;
                        Console.Write("File \"" + fileName + "\" not found.\n\n");
                        Console.ForegroundColor = ConsoleColor.Gray;
                        Help();
                    }
                }
            }

            if (string.IsNullOrWhiteSpace(fileName))
            {
                //input file doesn't exist
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write("Error: ");
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write("No input file provided.\n\n");
                Console.ForegroundColor = ConsoleColor.Gray;
                Help();
            }
            else
            {
                string outputFile = Path.GetFullPath(Path.GetFileNameWithoutExtension(fileName)) + ".html";

                WriteKeyValue("Path", Path.GetFullPath(fileName) + "\n", "", 0);
                WriteKeyValue("Input file", fileName);
                WriteKeyValue("Output file", Path.GetFileName(outputFile) + "\n");

                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write("Processing input...\n\n");

                BabylonBglParser parser;
                parser = new BabylonBglParser(sourceEnc, targetEnc);
                XDict dict = parser.Parse(fileName);

                WriteKeyValue("Dictionary title", parser.Title);
                WriteKeyValue("Author", parser.Author);
                WriteKeyValue("Languages", parser.SrcLng + " -> " + parser.DstLng + "");
                if (sourceEnc == null)
                {
                    if (!string.IsNullOrWhiteSpace(parser.SrcEnc))
                        WriteKeyValue("Source encoding", parser.SrcEnc + " (" + parser.SrcEncName + ")","*detected");
                    else
                        WriteKeyValue("Source encoding", "Failed to detect!");
                }
                else
                {
                    WriteKeyValue("Source encoding", sourceEnc.WebName + " (" + sourceEnc.EncodingName + ")", "*provided by user");
                }

                if (targetEnc == null)
                {
                    if (!string.IsNullOrWhiteSpace(parser.DstEnc))
                        WriteKeyValue("Target encoding", parser.DstEnc + " (" + parser.DstEncName + ")","*detected\n");
                    else
                        WriteKeyValue("Target encoding","", "Failed to detect!\n");
                }
                else
                {
                    WriteKeyValue("Target encoding", targetEnc.WebName + " (" + targetEnc.EncodingName + ")", "*provided by user\n");
                }

                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write("Generating output...\n");

                Console.ForegroundColor = ConsoleColor.Yellow;
                HtmlGenerator.GenerateHtml(dict, outputFile);
            }
            Console.ResetColor();
        }