SimpleTool Source

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//
using System.IO;
using MobiLibrary;

namespace SimpleTool
{
    class Program
    {
        private enum WHATTODO { LIST, DRM, NOT_DRM, STANDARD, NOT_STANDARD, RENAME_AT, RENAME_TA, SWAPFL, ADDCOMMA, LOCALE, CLEAN }
        //
        private static WHATTODO wtd;
        private static int lc = 0;//LocaleCode
        //
        static void Main(string[] args)
        {
            //
            MobiLib ml = new MobiLib();
            List<string> l = new List<string>();
            //
            string s;
            if (args.Length > 0)
            {
                s = args[0].ToUpper();
            }
            else
            {
                s = Path.GetFileName(Environment.GetCommandLineArgs()[0]).ToUpper();
            }
            //
            wtd = WHATTODO.LIST;
            //
            if(s.Contains("DRM"))
            {
                if (s.Contains("NO") || s.Contains("NIE") || s.Contains("!")) wtd = WHATTODO.NOT_DRM;
                else wtd= WHATTODO.DRM;
            }
            else if(s.Contains("STANDARD"))
            {
                if (s.Contains("NO") || s.Contains("NIE") || s.Contains("!")) wtd = WHATTODO.NOT_STANDARD;
                else wtd = WHATTODO.STANDARD;
            }
            else if (s.Contains("RENAME"))
            {
                if (s.Contains("TA")) wtd = WHATTODO.RENAME_TA;
                else wtd = WHATTODO.RENAME_AT;
            }
            else if (s.Contains("SWAP"))
            {
                wtd = WHATTODO.SWAPFL;
            }
            else if (s.Contains("COMMA"))
            {
                wtd = WHATTODO.ADDCOMMA;
            }
            else if (s.StartsWith("LOCALE", StringComparison.InvariantCultureIgnoreCase))
            {
                wtd = WHATTODO.LOCALE;
                string sl = s.Substring(6);
                lc = MobiLibrary.LanguageCodes.Name2LCID(sl);
                if (lc <= 0)
                {
                    Console.WriteLine("*** INVALID LOCALE: [{0}]", s);
                    return;
                }

            }
            else if (s.Contains("CLEAN"))
            {
                wtd = WHATTODO.CLEAN;
            }
            else if (s.Contains("HELP") || s.Contains("?"))
            {
                Usage();
                return;
            }
            //
            l = Directory.GetFiles(".", "*.mobi", SearchOption.AllDirectories).ToList().Union<string>(Directory.GetFiles(".", "*.prc", SearchOption.AllDirectories)).ToList<string>();
            l.Sort();
            //
            switch (wtd)
            {
                case WHATTODO.LIST:
                    Console.WriteLine("*** List ***");
                    break;
                case WHATTODO.DRM:
                    Console.WriteLine("*** List DRM ***");
                    break;
                case WHATTODO.NOT_DRM:
                    Console.WriteLine("*** List NOT DRM ***");
                    break;
                case WHATTODO.STANDARD:
                    Console.WriteLine("*** List STANDARD (EXTH+FullName) ***");
                    break;
                case WHATTODO.NOT_STANDARD:
                    Console.WriteLine("*** List NOT STANDARD ***");
                    break;
                case WHATTODO.RENAME_AT:
                    Console.WriteLine("*** Rename Author-Title ***");
                    break;
                case WHATTODO.RENAME_TA:
                    Console.WriteLine("*** Rename Title-Author***");
                    break;
                case WHATTODO.SWAPFL:
                    Console.WriteLine("*** Put Lastname First (F L) => (L, F) ***");
                    break;
                case WHATTODO.ADDCOMMA:
                    Console.WriteLine("*** Add Comma after Lastname(L F) => (L, F) ***");
                    break;
                case WHATTODO.LOCALE:
                    Console.WriteLine("*** Locale ***");
                    break;
                case WHATTODO.CLEAN:
                    Console.WriteLine("*** Clean EXTH ***");
                    break;
            }
            //
            foreach (string f in l)
            {
                if (ml.ReadFile(f))
                {
                    try
                    {
                        switch (wtd)
                        {
                            case WHATTODO.LIST:
                                Console.WriteLine("[{0}] - [{1}] : {2}", null != ml.exthAuthor ? ml.exthAuthor : "(null)", null != ml.FullName ? ml.FullName : "(null)", f);
                                break;
                            case WHATTODO.DRM:
                                if (ml.IsDRM) Console.WriteLine("[{0}]", f);
                                break;
                            case WHATTODO.NOT_DRM:
                                if (!ml.IsDRM) Console.WriteLine("[{0}]", f);
                                break;
                            case WHATTODO.STANDARD:
                                if (ml.IsEXTH && ml.IsFullName) Console.WriteLine("[{0}]", f);
                                break;
                            case WHATTODO.NOT_STANDARD:
                                if (!ml.IsEXTH || !ml.IsFullName) Console.WriteLine("[{0}]", f);
                                break;
                            case WHATTODO.RENAME_AT:
                            case WHATTODO.RENAME_TA:
                                string dst;
                                if (string.IsNullOrEmpty(ml.exthAuthor))
                                {
                                    dst = Utils.CleanFileName(string.Format("{0}{1}", ml.FullName, Path.GetExtension(f)));
                                }
                                else if(string.IsNullOrEmpty(ml.FullName))
                                {
                                    dst = Utils.CleanFileName(string.Format("{0}{1}", ml.exthAuthor, Path.GetExtension(f)));
                                }
                                else
                                {
                                    if(WHATTODO.RENAME_AT == wtd)dst = Utils.CleanFileName(string.Format("{0} - {1}{2}", ml.exthAuthor, ml.FullName, Path.GetExtension(f)));
                                    else dst = Utils.CleanFileName(string.Format("{0} - {1}{2}", ml.FullName, ml.exthAuthor, Path.GetExtension(f)));
                                }
                                //
                                if (0 != string.Compare(f, dst, true))
                                {
                                    File.Move(f, Path.Combine(Path.GetDirectoryName(f), dst));
                                    Console.WriteLine("[{0}] => [{1}]", f, dst);
                                }
                                else
                                {
                                    Console.WriteLine("[{0}]", f);
                                }
                                break;
                            case WHATTODO.SWAPFL:
                                {
                                    string src = ml.exthAuthor;
                                    if (!string.IsNullOrEmpty(src))
                                    {
                                        string t = Utils.swapLastFirstName(ml.exthAuthor);
                                        if (src != t)
                                        {
                                            ml.exthAuthor = t;
                                            ml.WriteFile();
                                            Console.WriteLine("[{0}]->[{1}] : {2}", src, t, f);
                                        }
                                        else
                                        {
                                            Console.WriteLine("{0}", f);
                                        }
                                    }
                                }
                                break;
                            case WHATTODO.ADDCOMMA:
                                {
                                    string src = ml.exthAuthor;
                                    if (!string.IsNullOrEmpty(src))
                                    {
                                        string t = Utils.addCommaAfterLastName(ml.exthAuthor);
                                        if (src != t)
                                        {
                                            ml.exthAuthor = t;
                                            ml.WriteFile();
                                            Console.WriteLine("[{0}]->[{1}] : {2}", src, t, f);
                                        }
                                        else
                                        {
                                            Console.WriteLine("{0}", f);
                                        }
                                    }
                                }
                                break;
                            case WHATTODO.LOCALE:
                                {
                                    ml.Locale = lc;
                                    Console.WriteLine("{0}", f);
                                }
                                break;
                            case WHATTODO.CLEAN:
                                {
                                    ml.exthDeleteExcept(new int [] { 1, 2, 3, 103, 104, 105, 107, 108, 113, 208, 300, 403, 503 });
                                    Console.WriteLine("{0}", f);
                                }
                                break;
                        }
                    }
                    catch
                    {
                        Console.Error.WriteLine("*** EXCEPTION: {0}", f);
                    }
                }
                else
                {
                    Console.Error.WriteLine("*** ERROR: {0}", f);
                }
            }
        }
        static void Usage()
        {
            Console.WriteLine("Usage {0} [options]", Path.GetFileNameWithoutExtension(Environment.GetCommandLineArgs()[0]));
            //        private enum WHATTODO { LIST, DRM, NOT_DRM, STANDARD, NOT_STANDARD, RENAME_AT, RENAME_TA, SWAPFL, ADDCOMMA, LOCALE, CLEAN }
            switch (LanguageCodes.CurrentLCID())
            {
                case 21:
                case 1045:
                    Console.WriteLine("\tOpcje:");
                    Console.WriteLine("\tLIST\t\t- wylistuj wszystkie pliki MOBI i PRC (bieżący katalog i podkatalogi)");
                    Console.WriteLine("\tDRM\t\t- wylistuj pliki z DRM");
                    Console.WriteLine("\tNOT_DRM\t\t- wylistuj pliki bez DRM");
                    Console.WriteLine("\tSTANDARD\t\t- wylistuj pliki standardowe (z FileName i EXTH)");
                    Console.WriteLine("\tNOT_STANDARD\t\t- wylistuj pliki niestandardowe (bez FileName lub EXTH)");
                    Console.WriteLine("\tRENAME_AT\t\t- zmień nazwę pliku na (Autor - Tytuł)");
                    Console.WriteLine("\tRENAME_TA\t\t- zmień nazwę pliku na (Tytuł - Autor)");
                    Console.WriteLine("\tSWAPFL\t\t- Zamień miejscami imię i nazwisko autora");
                    Console.WriteLine("\tADDCOMMA\t\t- dodaj przecinek po pierwszym wyrazie w nazwie autora");
                    Console.WriteLine("\tLOCALE{{LocaleName}}\t\t- ustaw Locale; przykład: LOCALEen, locale9");
                    Console.WriteLine("\tCLEAN\t\t- usuń zbędne wpisy z EXTH");
                    break;
                //
                default:
                    Console.WriteLine("\tOptions:");
                    Console.WriteLine("\tLIST\t\t- list all MOBI and PRC files (current directory and subdirectories)");
                    Console.WriteLine("\tDRM\t\t- list files with DRM");
                    Console.WriteLine("\tNOT_DRM\t\t- list files without DRM");
                    Console.WriteLine("\tSTANDARD\t\t- list standard files (with FileName and EXTH)");
                    Console.WriteLine("\tNOT_STANDARD\t\t- list nonstandard files (without FileName or EXTH)");
                    Console.WriteLine("\tRENAME_AT\t\t- rename file accordingly (Author - Title)");
                    Console.WriteLine("\tRENAME_TA\t\t- rename file accordingly (Title - Author)");
                    Console.WriteLine("\tSWAPFL\t\t- swap Author's FirstName with LastName");
                    Console.WriteLine("\tADDCOMMA\t\t- add comma after first word of Author");
                    Console.WriteLine("\tLOCALE{{LocaleName}}\t\t- set Locale; example: LOCALEen, locale9");
                    Console.WriteLine("\tCLEAN\t\t- remove superfluos entries in EXTH");
                    break;
            }
            return;
        }
    }//class
}//program

Last edited Jan 4, 2012 at 9:16 AM by Miroslaw_Maczka, version 2

Comments

No comments yet.