예제 #1
0
        // Group: Loading Functions
        // __________________________________________________________________________


        /* Function: Load
         * Loads the information in <Languages.nd> into a <Config> object, returning whether it was successful.  If it was not
         * config will be null.
         */
        public bool Load(Path filename, out Config config)
        {
            BinaryFile file = new BinaryFile();

            try
            {
                if (file.OpenForReading(filename, "2.2") == false)
                {
                    config = null;
                    return(false);
                }
                else
                {
                    config = new Config();

                    // [String: Language Name]
                    // [[Language Attributes]]
                    // ...
                    // [String: null]

                    string languageName = file.ReadString();

                    while (languageName != null)
                    {
                        Language language = new Language(languageName);

                        // [Int32: ID]
                        // [Byte: Type]
                        // [String: Simple Identifier]
                        // [Byte: Enum Values]
                        // [Byte: Case Sensitive (1 or 0)]
                        // [String: Member Operator Symbol]
                        // [String: Line Extender Symbol]

                        language.ID = file.ReadInt32();

                        byte type = file.ReadByte();
                        if (Enum.IsDefined(typeof(Language.LanguageType), type))
                        {
                            language.Type = (Language.LanguageType)type;
                        }
                        else
                        {
                            config = null;
                            return(false);
                        }

                        language.SimpleIdentifier = file.ReadString();

                        byte enumValues = file.ReadByte();
                        if (Enum.IsDefined(typeof(Language.EnumValues), enumValues))
                        {
                            language.EnumValue = (Language.EnumValues)enumValues;
                        }
                        else
                        {
                            config = null;
                            return(false);
                        }

                        language.CaseSensitive = (file.ReadByte() == 1);

                        language.MemberOperator = file.ReadString();
                        language.LineExtender   = file.ReadString();

                        // [String: Line Comment Symbol] [] ... [String: null]
                        // [String: Opening Block Comment Symbol] [String: Closing Block Comment Symbol] [] [] ... [String: null]
                        // [String: Javadoc First Line Comment Symbol] [String: Javadoc Following Lines Comment Symbol [] [] ... [String: null]
                        // [String: Javadoc Opening Block Comment Symbol] [String: Javadoc Closing Block Comment Symbol] [] [] ... [String: null]
                        // [String: XML Line Comment Symbol] [] ... [String: null]

                        var lineCommentSymbols = ReadSymbolList(file);
                        if (lineCommentSymbols != null)
                        {
                            language.LineCommentSymbols = lineCommentSymbols;
                        }

                        var blockCommentSymbols = ReadBlockCommentSymbolsList(file);
                        if (blockCommentSymbols != null)
                        {
                            language.BlockCommentSymbols = blockCommentSymbols;
                        }

                        var javadocLineCommentSymbols = ReadLineCommentSymbolsList(file);
                        if (javadocLineCommentSymbols != null)
                        {
                            language.JavadocLineCommentSymbols = javadocLineCommentSymbols;
                        }

                        var javadocBlockCommentSymbols = ReadBlockCommentSymbolsList(file);
                        if (javadocBlockCommentSymbols != null)
                        {
                            language.JavadocBlockCommentSymbols = javadocBlockCommentSymbols;
                        }

                        var xmlLineCommentSymbols = ReadSymbolList(file);
                        if (xmlLineCommentSymbols != null)
                        {
                            language.XMLLineCommentSymbols = xmlLineCommentSymbols;
                        }

                        // Prototype Enders:
                        // [Int32: Comment Type ID]
                        // [Byte: Include Line Breaks (1 or 0)]
                        // [String: Prototype Ender Symbol] [] ... [String: null]
                        // ...
                        // [Int32: 0]

                        int commentTypeID = file.ReadInt32();

                        while (commentTypeID != 0)
                        {
                            bool includeLineBreaks = (file.ReadByte() == 1);
                            var  enderSymbols      = ReadSymbolList(file);

                            language.AddPrototypeEnders(new PrototypeEnders(commentTypeID, enderSymbols, includeLineBreaks));

                            commentTypeID = file.ReadInt32();
                        }

                        config.AddLanguage(language);

                        languageName = file.ReadString();
                    }

                    // [String: Alias] [Int32: Language ID] [] [] ... [String: Null]

                    string alias = file.ReadString();

                    while (alias != null)
                    {
                        int languageID = file.ReadInt32();
                        config.AddAlias(alias, languageID);

                        alias = file.ReadString();
                    }

                    // [String: File Extension] [Int32: Language ID] [] [] ... [String: Null]

                    string fileExtension = file.ReadString();

                    while (fileExtension != null)
                    {
                        int languageID = file.ReadInt32();
                        config.AddFileExtension(fileExtension, languageID);

                        fileExtension = file.ReadString();
                    }

                    // [String: Shebang String] [Int32: Language ID] [] [] ... [String: Null]

                    string shebangString = file.ReadString();

                    while (shebangString != null)
                    {
                        int languageID = file.ReadInt32();
                        config.AddShebangString(shebangString, languageID);

                        shebangString = file.ReadString();
                    }
                }
            }
            catch
            {
                config = null;
                return(false);
            }
            finally
            {
                file.Close();
            }

            return(true);
        }