示例#1
0
文件: Program.cs 项目: x893/Hex2Bin
        static void Main(string[] args)
        {
            string fileIn = null;
            string fileOut = null;
            int exitCode = 0;
            FileFormat input_type = FileFormat.Auto;
            if (args.Length == 0)
            {
                usageHelp();
                exitCode = 1;
            }
            else
            {
                foreach (string arg in args)
                {
                    if (arg.StartsWith("-", StringComparison.InvariantCulture) ||
                        arg.StartsWith("/", StringComparison.InvariantCulture)
                        )
                    {
                        #region Process options

                        string[] option = arg.Substring(1).Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                        string value = string.Empty;
                        if (option.Length == 2)
                            value = option[1].Trim();

                        switch (option[0].ToLowerInvariant())
                        {
                            case "hex":
                                input_type = FileFormat.Hex;
                                break;
                            case "bin":
                                input_type = FileFormat.Binary;
                                break;
                            case "jam":
                                input_type = FileFormat.Jam;
                                break;
                            case "mcu":
                                #region Set PIC type
                                switch (value.ToUpperInvariant())
                                {
                                    case "PIC24FJ256":
                                        Memory = new byte[256 * 1024];
                                        break;
                                    case "PIC24FJ128":
                                        Memory = new byte[128 * 1024];
                                        break;
                                    default:
                                        Console.WriteLine(string.Format("Unknown -mcu {0}", value));
                                        exitCode = 1;
                                        break;
                                }
                                break;
                                #endregion
                            case "b":
                            case "base":
                                #region Set base address
                                if (!string.IsNullOrEmpty(value))
                                {
                                    if (value.StartsWith("0x", StringComparison.InvariantCultureIgnoreCase))
                                    {	// Hex format 0x....
                                        if (uint.TryParse(value.Substring(2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out BaseAddress))
                                            break;
                                    }
                                    else
                                    {
                                        if (uint.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out BaseAddress))
                                            break;
                                    }
                                }
                                exitCode = 1;
                                break;
                                #endregion
                            case "f":
                            case "fill":
                                #region Set empty memory value
                                if (!string.IsNullOrEmpty(value))
                                {
                                    if (value.StartsWith("0x", StringComparison.InvariantCultureIgnoreCase))
                                    {	// Hex format 0x
                                        if (byte.TryParse(value.Substring(2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out EmptyValue))
                                            break;
                                    }
                                    else
                                    {	// Decimal format
                                        if (byte.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out EmptyValue))
                                            break;
                                    }
                                }
                                exitCode = 1;
                                break;
                                #endregion
                            case "s":
                            case "scan":
                                #region Set scan file only mode
                                ScanOnly = true;
                                break;
                                #endregion
                            default:
                                #region Unknown option
                                Console.WriteLine(string.Format("Unknown option {0}", arg));
                                exitCode = 1;
                                break;
                                #endregion
                        }
                        #endregion
                    }
                    else if (fileIn == null)
                        fileIn = arg;	// First paramter as input file
                    else if (fileOut == null)
                        fileOut = arg;	// Second paramter as output file
                    else
                    {
                        Console.WriteLine("Too many files define.");
                        exitCode = 1;
                    }
                    if (exitCode != 0)
                        break;
                }

                if (exitCode == 0)
                {
                    exitCode = 1;

                    if (string.IsNullOrEmpty(fileIn))
                        Console.WriteLine("Input file empty.");
                    else
                    {
                        if (!File.Exists(fileIn))
                            Console.WriteLine(string.Format("Input file {0} not found.", fileIn));
                        else
                        {
                            string ext = Path.GetExtension(fileIn).ToLowerInvariant();
                            switch (input_type)
                            {
                                case FileFormat.Binary:
                                    ext = ".bin";
                                    break;
                                case FileFormat.Hex:
                                    ext = ".hex";
                                    break;
                                case FileFormat.Jam:
                                    ext = ".jam";
                                    break;
                            }
                            switch (ext)
                            {
                                case ".jam":
                                    // Process Microchip JAM file
                                    // Each line is HEX file name
                                    using (TextReader s = new StreamReader(fileIn))
                                    {
                                        string filename;
                                        while ((filename = s.ReadLine()) != null)
                                        {
                                            filename = filename.Trim();
                                            if (filename.Length == 0)
                                                continue;
                                            Console.WriteLine(string.Format("Process file:{0}", filename));
                                            exitCode = hex2bin(filename);
                                            if (exitCode != 0)
                                                break;
                                        }
                                    }
                                    break;

                                case ".s":
                                case ".hex":
                                    exitCode = hex2bin(fileIn);
                                    break;

                                case ".bin":
                                    exitCode = bin2hex(fileIn);
                                    break;
                                default:
                                    Console.WriteLine("Unknown extension {0}", Path.GetExtension(fileIn).ToLowerInvariant());
                                    exitCode = -1;
                                    break;
                            }

                            #region Save internal memory to binary file
                            if (exitCode == 0 && Memory != null && Memory.Length > 0)
                            {
                                Console.WriteLine("Address range: 0x{0:X}-0x{1:X}", AddressMin, AddressMax);
                                if (!ScanOnly)
                                {
                                    if (fileOut == null)
                                        fileOut = string.Concat(Path.GetFileNameWithoutExtension(fileIn), ".bin");

                                    using (Stream s = File.Open(fileOut, File.Exists(fileOut) ? FileMode.Truncate : FileMode.Create, FileAccess.Write))
                                    {
                                        s.Write(Memory, 0, (int)(AddressMax - AddressMin + 1));
                                        s.Close();
                                    }
                                }
                            }
                            #endregion
                        }
                    }
                }
            }
            Environment.Exit(exitCode);
        }
        private DocumentOutput _GetSummary(string root)
        {
            DocumentOutput doc = null;

            /*verificam daca este deja documentul in bd*/
            try {
                doc = this._em.GetDocumentOutput(this._doc.DocumentId, "summ");
                /*ne asiguram ca este scris*/
                using (Stream stream = File.OpenWrite(root + "\\summ.xml")) {
                    byte[] buffer = Encoding.UTF8.GetBytes(doc.Document);
                    stream.Write(buffer, 0, buffer.Length);
                }
            } catch { }

            if (doc != null)
                return doc;

            String input = null;

            using (StreamReader stream = new StreamReader(root + "\\main.txt")) {
                input = stream.ReadToEnd();
            }

            /*cream modulu de sumarizare*/
            /*ISummarizer summarizer = new SimpleSummarizer();
            String output = summarizer.Summarize(input, 40);*/

            String output = null;
            using (StreamReader info = new StreamReader(File.OpenRead(root + "\\info.xml"))) {
                XElement infoDoc = XElement.Parse(info.ReadToEnd());
                output = new Summarisation().ProcessXML(infoDoc);
            }

            if (output == null) return null;

            /*punem in baza de date rezultatul*/

            doc = this._em.AddDocumentOutput(this._doc.DocumentId, "summ",
                DocumentManager.StatusOK, output);

            /*scriem si in fisier*/
            using (Stream stream = File.OpenWrite(root + "\\summ.xml")) {
                byte[] buffer = Encoding.UTF8.GetBytes(output);
                stream.Write(buffer, 0, buffer.Length);
            }

            return doc;
        }