Пример #1
0
        public CatOptions()
        {
            envars = new EnvironmentVariables("cat");

            appname = Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location);

            showHelp = false;
            showPlugins = false;
            showEnvVars = false;

            defaultBackColor = Console.BackgroundColor;
            defaultForeColor = Console.ForegroundColor;
            lineNumBackColor = ConsoleColor.Blue;
            lineNumForeColor = ConsoleColor.Gray;

            files = new List<string>();

            showLineNumbers = false;
            wrapText = false;
            forcePlainText = false;
            forceSpecificPlugin = "";

            pauseAfterEachPage = false;
            pauseAtEnd = false;

            ignoreLines = "";
            ignoreBlankLines = false;
            ignoreWhitespaceLines = false;

            indentCharacters = 0;

            normalExpanded = true;
            extraExpanded = true; // false
        }
Пример #2
0
        public static int Main( string[] args )
        {
            envars = new EnvironmentVariables(appName);

            string file = null;
            SortedDictionary<string, string> dc;
            string line;
            bool exists;
            string name;
            string comment;
            StringBuilder cb;
            int index;
            string a;
            bool vf; // verify file exists
            string sfn; // saved backup file name
            bool sf; // don't delete the backup file
            bool oc; // output to the console, instead of back to the file
            Encoding enc; // the encoding used to open the source descript.ion file

            vf = false;
            sf = false;
            oc = false;

            // Load environment variables first.
            // Command-line arguments will overwrite them when present.
            if (envars.contains("vf")) {
                vf = envars.attr<bool>( "vf");
            }
            if (envars.contains( "sf")) {
                sf = envars.attr<bool>("sf");
            }

            if (args.Length > 0) {
                for (int i = 0; i < args.Length; i++) {
                    a = args[i];
                    if (a.StartsWith("/") || a.StartsWith("-") || a.StartsWith("!")) {
                        while (a.StartsWith("/") || a.StartsWith("-")) {
                            a = a.Substring(1);
                        }

                        if (a.StartsWith("h", StringComparison.CurrentCultureIgnoreCase) || a.Equals("?", StringComparison.CurrentCultureIgnoreCase)) {
                            usage();
                            return 0;

                        } else if (a.Equals("vf", StringComparison.CurrentCultureIgnoreCase)) {
                            vf = true;
                        } else if (a.Equals("!vf", StringComparison.CurrentCultureIgnoreCase)) {
                            vf = false;

                        } else if (a.Equals("sf", StringComparison.CurrentCultureIgnoreCase)) {
                            sf = true;
                        } else if (a.Equals("!sf", StringComparison.CurrentCultureIgnoreCase)) {
                            sf = false;

                        } else if (a.Equals("oc", StringComparison.CurrentCultureIgnoreCase)) {
                            oc = true;
                        } else if (a.Equals("!oc", StringComparison.CurrentCultureIgnoreCase)) {
                            oc = false;

                        }
                    } else {
                        // get the filename
                        file = a;
                    }
                }
            }

            if (StdInEx.IsInputRedirected) {
                file = StdInEx.RedirectInputToFile();
                oc = true;
            } else {
                if (file == null || file.Length == 0) {
                    //Console.WriteLine("Using: " + Environment.CurrentDirectory);
                    file = Path.Combine(Environment.CurrentDirectory, "descript.ion");
                }
            }

            if (!File.Exists(file)) {
                Console.WriteLine("there is no descript.ion file in the current folder");
                return 1;
            }

            try {
                BackupFile(file, "bak_", out sfn);
            } catch (Exception ex) {
                Console.WriteLine("error backing up descript.ion file: " + ex.Message);
                return 2;
            }

            dc = new SortedDictionary<string, string>();
            cb = new StringBuilder();
            enc = Encoding.Default;

            using (StreamReader r = new StreamReader(File.OpenRead(file), Encoding.Default, true)) {
                enc = r.CurrentEncoding;

                while (!r.EndOfStream) {
                    if ((line = r.ReadLine()) != null) {
                        // do not trim the line itself!
                        if (line.Trim().Length == 0) {
                            continue;
                        }

                        // Get the file name and comments
                        if (line.StartsWith("\"")) {
                            index = line.IndexOf("\" ", 1);
                            if (index == -1) {
                                Console.WriteLine("--> invalid line:");
                                Console.WriteLine("    " + line.Substring(0, Math.Min(80, line.Length)));
                                continue;
                            }
                            index += 2;
                        } else {
                            index = line.IndexOf(' ');
                        }

                        if (index == -1) {
                            continue;
                        }

                        name = line.Substring(0, index).Trim().Trim('"', ' ', '\t');
                        comment = line.Substring(index).Trim();

                        // Verify that the file exists in the current folder.
                        if (vf) {
                            if (!File.Exists(name) && !Directory.Exists(name)) {
                                Console.WriteLine("--> file/folder was not found: " + name);
                                sf = true;
                                continue;
                            }
                        }

                        // Ensure a unique file name for the dictionary
                        exists = false;
                        while (dc.ContainsKey(name)) {
                            exists = true;
                            name += "_";
                        }
                        if (exists) {
                            Console.WriteLine("--> duplicate entry was found.. saved as: " + name);
                        }

                        // Cleanup the comment
                        //while (comment.IndexOf('’') > -1) {
                        //   comment = comment.Replace('’', '\'');
                        //}

                        //cb.Clear();
                        //foreach (char ch in comment) {
                        //   if (ch < 32 || ch > 126) {
                        //      // 9=tab
                        //      // 10=new line
                        //      // 13=carriage return
                        //      // 169=copyright symbol
                        //      // 174=registered symbol
                        //      //
                        //      if (ch != 9 && ch != 10 && ch != 13 && ch != 169 && ch != 174 && ch != '•') {
                        //         //cb.Append(' ');
                        //         continue;
                        //      }
                        //   }
                        //   cb.Append(ch);
                        //}
                        //comment = cb.ToString().Trim();

                        while (comment.IndexOf("\\\\n") > -1) {
                            comment = comment.Replace("\\\\n", "\\n");
                        }

                        while (comment.IndexOf((char)4) == comment.Length - 1 || comment.IndexOf((char)194) == comment.Length - 1) {
                            comment = comment.Substring(0, comment.Length - 1);
                        }

                        while (comment.EndsWith("\\n")) {
                            comment = comment.Substring(0, comment.Length - 2).TrimEnd();
                        }

                        dc.Add(name, comment);
                    }
                }

                r.Close();
            }

            try {
                File.SetAttributes(file, FileAttributes.Normal);
                File.Delete(file);
            } catch (Exception ex) {
                Console.WriteLine("error: " + ex.Message);
                return 3;
            }

            if (dc.Count > 0) {
                if (oc) {
                    foreach (KeyValuePair<string, string> p in dc) {
                        Console.Write(string.Format("{0,-30}{1}", p.Key, p.Value));
                    }
                } else if (enc != null) {
                    try {
                        using (FileStream fs = File.Create(file)) {
                            foreach (KeyValuePair<string, string> p in dc) {
                                byte[] b;

                                if (p.Key.Contains(" ")) {
                                    name = "\"" + p.Key + "\"";
                                } else {
                                    name = p.Key;
                                }
                                comment = p.Value;

                                b = new byte[name.Length];
                                enc.GetBytes(name.ToCharArray(), 0, name.Length, b, 0);
                                fs.Write(b, 0, b.Length);

                                fs.Write(new byte[] { 32 }, 0, 1);

                                b = new byte[comment.Length];
                                enc.GetBytes(comment.ToCharArray(), 0, comment.Length, b, 0);
                                fs.Write(b, 0, b.Length);

                                // Write out the special characters to indicate to Total Commander that there are new lines in the comments.
                                if (comment.IndexOf("\\n") > -1) {
                                    fs.Write(new byte[] { 4, 194 }, 0, 2);
                                }

                                // New line and carriage return.
                                fs.Write(new byte[] { 13, 10 }, 0, 2);
                            }
                            fs.Flush();
                            fs.Close();
                        }
                    } catch (Exception ex) {
                        Console.WriteLine("error writing descript.ion file: " + ex.Message);
                        if (File.Exists(file)) {
                            File.SetAttributes(file, FileAttributes.Normal);
                            File.Delete(file);
                        }
                        File.Move(sfn, file);
                        Console.WriteLine("original file was put back");
                        return 4;
                    }
                } else {
                    Console.WriteLine("**** ERROR: failed to get the encoding of the input.");
                }
            }

            // Since we're not updating the file, there is no
            // need to create a backup of it. --> !oc
            if (!sf && !oc) {
                try {
                    File.SetAttributes(sfn, FileAttributes.Normal);
                    File.Delete(sfn);
                } catch (Exception ex) {
                    Console.WriteLine("error deleting backup file: " + ex.Message);
                    return 5;
                }
            }

            return 0;
        }