Example #1
0
        /// <summary>
        /// Create the "original" (backup) copy of the file to be modified,
        /// if it doesn't already exist.
        /// </summary>
        /// <param name="inputFilespec">This is the file to make a copy of.</param>
        public static void BackupOrig(string inputFilespec)
        {
            if (!File.Exists(inputFilespec))
            {
                LogFile.AddVerboseLine("No Orig to back up: <" + inputFilespec);
                return;
            }

            string outputFilespec = CreateNewFileName(inputFilespec, original);

            if (!File.Exists(outputFilespec))
            {
                try
                {
                    FileCopyWithLogging(inputFilespec, outputFilespec, true);
                }
                catch
                {
                    LogFile.AddErrorLine("Error creating " + original + " copy: " + inputFilespec);
                    throw new LDExceptions(ErrorCodes.FileWrite);
                }
            }
        }
Example #2
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        ///
        /// </summary>
        /// <param name="directoryName"></param>
        /// <param name="extension"></param>
        /// <param name="removeOrig"></param>
        /// <returns></returns>
        /// ------------------------------------------------------------------------------------
        public static int RestoreOrigFiles(string directoryName, string extension, bool removeOrig)
        {
            int           numCopied   = 0;
            DirectoryInfo di          = new DirectoryInfo(directoryName);
            string        origPattern = CreateNewFileName("*" + extension, original);

            System.IO.FileInfo[] fi = di.GetFiles(origPattern);

            LogFile.AddLine("RestoreOrigFiles: " + directoryName + origPattern);

            foreach (System.IO.FileInfo f in fi)
            {
                string savedName = f.FullName;
                string defName   = UndoCreateNewFileName(savedName, original);
                try
                {
                    FileCopyWithLogging(savedName, defName, true);
                    if (removeOrig)
                    {
                        // delete the orig file here...
                        DeleteFile(savedName);
                    }
                    numCopied++;
                }
                catch
                {
                    LogFile.AddErrorLine("Error restoring " + original + " file: " + f.FullName);
                    throw new LDExceptions(ErrorCodes.FileWrite);
                }
            }
            if (numCopied == 0)
            {
                LogFile.AddLine("RestoreOrigFiles: No files copied.");
            }

            return(numCopied);
        }
Example #3
0
        /// <summary>
        /// This method will create the temporary work file copy of the original input file.
        /// </summary>
        /// <param name="inputFilespec">This is the file to make a copy of.<</param>
        private static string CreateXXFile(string inputFilespec, string suffix)
        {
            string outputFilespec = CreateNewFileName(inputFilespec, suffix);

            try
            {
                if (!File.Exists(inputFilespec))
                {
                    // Have to save the handle in an object so that we can close it!
                    FileStream fs = File.Create(outputFilespec);
                    fs.Close();
                }
                else
                {
                    FileCopyWithLogging(inputFilespec, outputFilespec, true);
                }
            }
            catch
            {
                LogFile.AddErrorLine("Error creating file with suffix: " + suffix + " from: " + inputFilespec);
                throw new LDExceptions(ErrorCodes.FileWrite);
            }
            return(outputFilespec);
        }
Example #4
0
        public static int Main(string[] args)
        {
            LogFile.AddLine("================= Start of process =================");
            LogFile.AddLine("Command line: <" + Environment.CommandLine + ">");
            bool testedLock = false;

            ErrorCodes ret = ErrorCodes.None;

            try
            {
                CommandLineParser             argParser   = new CommandLineParser();
                CommandLineParser.ParseResult parseResult = argParser.Parse(args);
                switch (parseResult)
                {
                case CommandLineParser.ParseResult.error:
                    ret = ErrorCodes.CommandLine;
                    int iRet = (int)ErrorCodes.CommandLine;
                    LogFile.Release();
                    return(iRet);

                case CommandLineParser.ParseResult.stop:
                    LogFile.Release();
                    // no error - success
                    return((int)(ErrorCodes.Success));
                }

                // tests the command line parser to make sure we have valid data
                if (argParser.FlagUsed(CommandLineParser.Flag.dontDoAnything))
                {
                    foreach (CommandLineParser.Flag flag in argParser.FlagsUsed())
                    {
                        Console.Write("{0}:{1}, ", flag.ToString(), argParser.FlagData(flag));
                    }
                    Console.WriteLine();
                    return((int)ErrorCodes.Success);
                }

                // don't restrict the GUI
                bool runSilent = false;
                if (argParser.FlagUsed(CommandLineParser.Flag.q))
                {
                    runSilent = true;
                }

                bool slow = false;
                // Use the old slower file reading method
                if (argParser.FlagUsed(CommandLineParser.Flag.slow))
                {
                    slow = true;
                }

                if (argParser.FlagUsed(CommandLineParser.Flag.testMainParserRoutine))
                {
                    LocaleFileClass localeObject   = new LocaleFileClass();
                    bool            successParsing = localeObject.TestBaseLocaleParsers(false);
                    if (successParsing)
                    {
                        ret = ErrorCodes.Success;
                    }
                    else
                    {
                        ret = ErrorCodes.LDParsingError;
                    }
                }


                if (argParser.FlagUsed(CommandLineParser.Flag.testICUDataParser))
                {
                    try
                    {
                        IcuDataNode.TestIcuDataNode(argParser.FlagData(CommandLineParser.Flag.testICUDataParser));
                        ret = ErrorCodes.Success;
                    }
                    catch (LDExceptions e)
                    {
                        ret = e.ec;
                        LogFile.AddErrorLine("LDException: " + e.ec.ToString() + "-" + Error.Text(e.ec));
                        if (e.HasConstructorText)
                        {
                            LogFile.AddErrorLine("LDException Msg: " + e.ConstructorText);
                        }
                    }
                    catch (System.Exception e)
                    {
                        ret = ErrorCodes.NonspecificError;
                        LogFile.AddErrorLine(e.Message);
                        LogFile.AddErrorLine(e.StackTrace);
                    }
                }

                if (argParser.FlagUsed(CommandLineParser.Flag.s))
                {
                    LocaleFileClass localeObject = new LocaleFileClass();
                    localeObject.RunSilent = runSilent;
                    ret = localeObject.ShowCustomLocales();
                }

                if (argParser.FlagUsed(CommandLineParser.Flag.customLanguages))
                {
                    LocaleFileClass localeObject = new LocaleFileClass();
                    localeObject.RunSilent = runSilent;
                    ret = localeObject.ShowCustomLanguages();
                }

                if (argParser.FlagUsed(CommandLineParser.Flag.o))                       // restore origs'
                {
                    CallingID cid = CallingID.CID_RESTORE;
                    if (argParser.FlagUsed(CommandLineParser.Flag.newlang))
                    {
                        cid = CallingID.CID_NEW;                                // internal new lang case
                    }
                    if (!CheckForIcuLocked(null, runSilent, cid))
                    {
                        return((int)ErrorCodes.CancelAccessFailure);
                    }
                    testedLock = true;
                    // only perform this command by itself even if given with other options
                    LocaleFileClass restoreFiles = new LocaleFileClass();
                    restoreFiles.RunSlow   = slow;
                    restoreFiles.RunSilent = runSilent;
                    // Restore all the original files, installing the PUA characters if requested.
                    ret = restoreFiles.RestoreOrigFiles(true);

                    // LT-5374 : wasn't moving the res files from the Icu34 dir to the Icudt34l dir
                    if (ret == ErrorCodes.Success)
                    {
                        // remove the backup files from the undo/recover file name list
                        restoreFiles.RemoveBackupFiles();
                    }
                    restoreFiles.RemoveTempFiles();
                    restoreFiles.MoveNewResFilesToSubDir();
                }

                if (argParser.FlagUsed(CommandLineParser.Flag.r))                       // remove locale
                {
                    if (!testedLock && !CheckForIcuLocked(argParser.FlagData(CommandLineParser.Flag.r), runSilent, CallingID.CID_REMOVE))
                    {
                        return((int)ErrorCodes.CancelAccessFailure);
                    }
                    testedLock = true;
                    string deleteLocale = argParser.FlagData(CommandLineParser.Flag.r);
                    argParser.RemoveFlag(CommandLineParser.Flag.r);
                    bool rUsed = argParser.FlagUsed(CommandLineParser.Flag.r);

                    LocaleFileClass removeLocaleObject = new LocaleFileClass();
                    try
                    {
                        removeLocaleObject.RunSilent = runSilent;
                        ret = removeLocaleObject.RemoveLocale(deleteLocale);
                        removeLocaleObject.MoveNewResFilesToSubDir();
                    }
                    catch (System.Exception e)
                    {
                        ret = ErrorCodes.NonspecificError;

                        LogFile.AddErrorLine(e.Message);
                        LogFile.AddErrorLine(e.StackTrace);
                        LogFile.AddErrorLine("Exception: " + ret.ToString() + "-" + Error.Text(ret));
                        removeLocaleObject.RestoreFiles();                              // copy backup files to original files.
                    }
                }
                // install locale
                if ((ret == ErrorCodes.Success || ret == ErrorCodes.None) &&
                    argParser.FlagUsed(CommandLineParser.Flag.i))
                {
                    CallingID cid = CallingID.CID_INSTALL;
                    if (argParser.FlagUsed(CommandLineParser.Flag.newlang))
                    {
                        cid = CallingID.CID_NEW;                                // internal new lang case
                    }
                    if (!testedLock && !CheckForIcuLocked(
                            argParser.FlagData(CommandLineParser.Flag.i), runSilent, cid))
                    {
                        return((int)ErrorCodes.CancelAccessFailure);
                    }
                    testedLock = true;
                    LocaleFileClass localeFileObject = new LocaleFileClass();
                    localeFileObject.RunSilent = runSilent;
                    localeFileObject.RunSlow   = slow;
                    try
                    {
                        string addLocale = argParser.FlagData(CommandLineParser.Flag.i);
                        localeFileObject.InstallLDFile(addLocale);
                        ret = ErrorCodes.Success;
                    }
                    catch (LDExceptions e)
                    {
                        localeFileObject.RestoreFiles();                                // copy backup files to original files.
                        ret = e.ec;
                        LogFile.AddErrorLine("LDException: " + e.ec.ToString() + "-" + Error.Text(e.ec));
                        if (e.HasConstructorText)
                        {
                            LogFile.AddErrorLine("LDException Msg: " + e.ConstructorText);
                        }
                    }
                    catch (System.Exception e)
                    {
                        ret = ErrorCodes.NonspecificError;

                        LogFile.AddErrorLine(e.Message);
                        LogFile.AddErrorLine(e.StackTrace);
                        LogFile.AddErrorLine("Exception: " + ret.ToString() + "-" + Error.Text(ret));
                        localeFileObject.RestoreFiles();                                // copy backup files to original files.
                    }

                    if (ret == ErrorCodes.Success)
                    {
                        // remove the backup files from the undo/recover file name list
                        localeFileObject.RemoveBackupFiles();
                    }

                    localeFileObject.RemoveTempFiles();
                    localeFileObject.MoveNewResFilesToSubDir();
                }

                //Add PUA Character
                if ((ret == ErrorCodes.Success || ret == ErrorCodes.None) &&
                    argParser.FlagUsed(CommandLineParser.Flag.c))
                {
                    if (!testedLock && !CheckForIcuLocked(argParser.FlagData(CommandLineParser.Flag.c), runSilent, CallingID.CID_UNKNOWN))
                    {
                        return((int)ErrorCodes.CancelAccessFailure);
                    }
                    LocaleFileClass localeFileObject = new LocaleFileClass();
                    localeFileObject.RunSilent = runSilent;
                    try
                    {
                        string addLocale = argParser.FlagData(CommandLineParser.Flag.c);
                        localeFileObject.InstallPUACharacters(addLocale);
                        ret = ErrorCodes.Success;
                    }
                    catch (LDExceptions e)
                    {
                        // copy backup files to original files.
                        localeFileObject.RestoreFiles();
                        ret = e.ec;
                        LogFile.AddErrorLine("LDException: " + e.ec.ToString() + "-" + Error.Text(e.ec));
                        if (e.HasConstructorText)
                        {
                            LogFile.AddErrorLine("LDException Msg: " + e.ConstructorText);
                        }
                    }
                    catch (System.Exception e)
                    {
                        ret = ErrorCodes.NonspecificError;
                        LogFile.AddErrorLine("Exception: " + e.Message);
                        LogFile.AddErrorLine(e.StackTrace);
                        LogFile.AddErrorLine("Error Code: " + ret.ToString() + "-" + Error.Text(ret));
                        localeFileObject.RestoreFiles();                                // copy backup files to original files.
                    }
                    if (ret == ErrorCodes.Success)
                    {
                        // remove the backup files from the undo/recover file name list
                        localeFileObject.RemoveBackupFiles();
                    }

                    localeFileObject.RemoveTempFiles();
                }

                if (ret == ErrorCodes.Success)
                {
                    if (argParser.FlagUsed(CommandLineParser.Flag.q))
                    {
                        LogFile.AddLine("--- Success ---");
                    }
                    else
                    {
                        LogFile.AddErrorLine("--- Success ---");
                    }
                }
            }
            catch (System.Exception e)
            {
                ret = ErrorCodes.NonspecificError;
                LogFile.AddErrorLine("Exception: " + e.Message + " " + e.StackTrace);
                LogFile.AddErrorLine("Exception: " + ret.ToString() + "-" + Error.Text(ret));
            }
            finally
            {
                LogFile.Release();
            }

            int nret = (int)ret;

            return(nret);
        }
Example #5
0
        public void PopulateFromLanguageClass()
        {
            //Extracts the locale filename from a given path
            int    icuName    = m_inputFilename.LastIndexOf("\\");
            string icuPortion = m_inputFilename.Substring(icuName + 1);

            //Appears this maps the XML file to a LanguageDefinition class
            /////////////////
            ILgWritingSystemFactory wsf = LgWritingSystemFactoryClass.Create();

            LanguageDefinitionFactory langDefFactory = new LanguageDefinitionFactory(wsf, icuPortion);

            LanguageDefinition langDef = langDefFactory.InitializeFromXml(wsf, icuPortion) as LanguageDefinition;

            if (langDef == null)
            {
                throw new Exception("Unable to read and parse the input XML file " + m_inputFilename);
            }
            /////////////////

            int i    = 0;
            int cpua = langDef.PuaDefinitionCount;

            // if we have PUA characters in the LD file make an array of PUACharacters.  But be careful
            // to handle invalid definitions gracefully.
            if (langDef.PuaDefinitions != null && cpua != 0)
            {
                puaChars = new PUACharacter[cpua];
                foreach (CharDef charDef in langDef.PuaDefinitions)
                {
                    try
                    {
                        puaChars[i] = new PUACharacter(charDef);
                        ++i;
                    }
                    catch
                    {
                    }
                }
            }
            if (i < cpua)
            {
                if (i == 0)
                {
                    puaChars = null;
                }
                else
                {
                    PUACharacter[] puaGoodChars = new PUACharacter[i];
                    for (int ic = 0; ic < i; ++ic)
                    {
                        puaGoodChars[ic] = puaChars[ic];
                    }
                    puaChars = puaGoodChars;
                }
                if (LogFile.IsLogging())
                {
                    LogFile.AddErrorLine("Warning, " + (cpua - i) + " out of " + cpua +
                                         " PUA character definitions are invalid.");
                }
            }
            baseLocale      = langDef.BaseLocale;
            newLocale       = langDef.XmlWritingSystem.WritingSystem.IcuLocale;
            localeResources = langDef.LocaleResources;
            // Get the collation elements, whether from the CollationElements element directly,
            // or from the WritingSystem element.
            collationElements = langDef.CollationElements;
            if (collationElements == null)
            {
                IWritingSystem lws   = langDef.WritingSystem;
                int            ccoll = lws.CollationCount;
                if (ccoll > 0)
                {
                    collationElements = lws.get_Collation(0).IcuRules;
                }
            }
            localeWinLCID = langDef.XmlWritingSystem.WritingSystem.Locale.ToString();

            // make sure the newlocale has the proper case for each property:
            // lang, country and variant
            InstallLanguage.LocaleParser lp = new LocaleParser(newLocale);
            newLocale = lp.Locale;

            // Make sure the display names [Name, Country & Variant] have Unicode characters
            // greater than 7F converted to the \uxxxx format where xxxx is the unicode
            // hex value of the character.
            localeName    = ConvertToUnicodeNotation(langDef.LocaleName);
            localeScript  = ConvertToUnicodeNotation(langDef.LocaleScript);
            localeCountry = ConvertToUnicodeNotation(langDef.LocaleCountry);
            localeVariant = ConvertToUnicodeNotation(langDef.LocaleVariant);

            // Save the multilingual names of the writing system, together with the
            // ICU locale for each name.
            NameMultiUnicode rgName = langDef.XmlWritingSystem.Name;
            int cws = rgName.Count;

            // If we don't have a name, use the IcuLocale rather than going without a name.
            // Otherwise it won't register as a language in en.txt/res.
            if (cws == 0)
            {
                StringWithWs sw = new StringWithWs(langDef.XmlWritingSystem.WritingSystem.IcuLocale, "en");
                rgName.Add(sw);
                cws = 1;
            }
            m_rgNames = new System.Collections.ArrayList(cws);
            for (int iws = 0; iws < cws; ++iws)
            {
                StringWithWs x = rgName[iws];
                m_rgNames.Add(x);
            }

            // TODO - dlh
            // Once collationElements are handled, something will have to be checked there
            // as the current implementation assumes that it's in the valid format.

            wsf.Shutdown();                     // This is (always) needed to balance creating the factory.
        }