internal static CfgHeader ConvertCfgHeader(StreamMarshaler streamHelper, bool includeAllGrammarData, bool loadSymbols, out CfgSerializedHeader cfgSerializedHeader)
        {
            cfgSerializedHeader = new CfgSerializedHeader(streamHelper.Stream);
            CfgHeader cfgHeader = default(CfgHeader);

            cfgHeader.FormatId            = cfgSerializedHeader.FormatId;
            cfgHeader.GrammarGUID         = cfgSerializedHeader.GrammarGUID;
            cfgHeader.langId              = cfgSerializedHeader.LangID;
            cfgHeader.pszGlobalTags       = cfgSerializedHeader.pszSemanticInterpretationGlobals;
            cfgHeader.cArcsInLargestState = cfgSerializedHeader.cArcsInLargestState;
            cfgHeader.rules = Load <CfgRule>(streamHelper, cfgSerializedHeader.pRules, cfgSerializedHeader.cRules);
            if (includeAllGrammarData || loadSymbols)
            {
                cfgHeader.pszSymbols = LoadStringBlob(streamHelper, cfgSerializedHeader.pszSymbols, cfgSerializedHeader.cchSymbols);
            }
            if (includeAllGrammarData)
            {
                cfgHeader.pszWords = LoadStringBlob(streamHelper, cfgSerializedHeader.pszWords, cfgSerializedHeader.cchWords);
                cfgHeader.arcs     = Load <CfgArc>(streamHelper, cfgSerializedHeader.pArcs, cfgSerializedHeader.cArcs);
                cfgHeader.tags     = Load <CfgSemanticTag>(streamHelper, cfgSerializedHeader.tags, cfgSerializedHeader.cTags);
                cfgHeader.weights  = Load <float>(streamHelper, cfgSerializedHeader.pWeights, cfgSerializedHeader.cArcs);
            }
            if (cfgSerializedHeader.pszWords < Marshal.SizeOf(typeof(CfgSerializedHeader)))
            {
                cfgHeader.ulRootRuleIndex = uint.MaxValue;
                cfgHeader.GrammarOptions  = GrammarOptions.KeyValuePairs;
                cfgHeader.BasePath        = null;
                cfgHeader.GrammarMode     = GrammarType.VoiceGrammar;
            }
            else
            {
                cfgHeader.ulRootRuleIndex = cfgSerializedHeader.ulRootRuleIndex;
                cfgHeader.GrammarOptions  = cfgSerializedHeader.GrammarOptions;
                cfgHeader.GrammarMode     = (GrammarType)cfgSerializedHeader.GrammarMode;
                if (includeAllGrammarData)
                {
                    cfgHeader.scripts = Load <CfgScriptRef>(streamHelper, cfgSerializedHeader.pScripts, cfgSerializedHeader.cScripts);
                }
                if (cfgSerializedHeader.cBasePath != 0)
                {
                    streamHelper.Stream.Position = (int)cfgSerializedHeader.pRules + cfgHeader.rules.Length * Marshal.SizeOf(typeof(CfgRule));
                    cfgHeader.BasePath           = streamHelper.ReadNullTerminatedString();
                }
            }
            CheckValidCfgFormat(cfgSerializedHeader, cfgHeader, includeAllGrammarData);
            return(cfgHeader);
        }
Beispiel #2
0
        internal static CfgHeader ConvertCfgHeader(StreamMarshaler streamHelper, bool includeAllGrammarData, bool loadSymbols, out CfgSerializedHeader cfgSerializedHeader)
        {
            cfgSerializedHeader = new CfgSerializedHeader(streamHelper.Stream);

            //
            //  Because in 64-bit code, pointers != sizeof(ULONG) we copy each member explicitly.
            //

            CfgHeader header = new();

            header.FormatId            = cfgSerializedHeader.FormatId;
            header.GrammarGUID         = cfgSerializedHeader.GrammarGUID;
            header.langId              = cfgSerializedHeader.LangID;
            header.pszGlobalTags       = cfgSerializedHeader.pszSemanticInterpretationGlobals;
            header.cArcsInLargestState = cfgSerializedHeader.cArcsInLargestState;

            // read all the common fields
            header.rules = Load <CfgRule>(streamHelper, cfgSerializedHeader.pRules, cfgSerializedHeader.cRules);

            if (includeAllGrammarData || loadSymbols)
            {
                header.pszSymbols = LoadStringBlob(streamHelper, cfgSerializedHeader.pszSymbols, cfgSerializedHeader.cchSymbols);
            }

            if (includeAllGrammarData)
            {
                header.pszWords = LoadStringBlob(streamHelper, cfgSerializedHeader.pszWords, cfgSerializedHeader.cchWords);
                header.arcs     = Load <CfgArc>(streamHelper, cfgSerializedHeader.pArcs, cfgSerializedHeader.cArcs);
                header.tags     = Load <CfgSemanticTag>(streamHelper, cfgSerializedHeader.tags, cfgSerializedHeader.cTags);
                header.weights  = Load <float>(streamHelper, cfgSerializedHeader.pWeights, cfgSerializedHeader.cArcs);
            }

            //We know that in SAPI 5.0 grammar format pszWords follows header immediately.
            if (cfgSerializedHeader.pszWords < Marshal.SizeOf <CfgSerializedHeader>())
            {
                //This is SAPI 5.0 and SAPI 5.1 grammar format
                header.ulRootRuleIndex = 0xFFFFFFFF;
                header.GrammarOptions  = GrammarOptions.KeyValuePairs;
                header.BasePath        = null;
                header.GrammarMode     = GrammarType.VoiceGrammar;
            }
            else
            {
                //This is SAPI 5.2 and beyond grammar format
                header.ulRootRuleIndex = cfgSerializedHeader.ulRootRuleIndex;
                header.GrammarOptions  = cfgSerializedHeader.GrammarOptions;
                header.GrammarMode     = (GrammarType)cfgSerializedHeader.GrammarMode;
                if (includeAllGrammarData)
                {
                    header.scripts = Load <CfgScriptRef>(streamHelper, cfgSerializedHeader.pScripts, cfgSerializedHeader.cScripts);
                }
                // The BasePath string is written after the rules - no offset is provided
                // Get the chars and build the string
                if (cfgSerializedHeader.cBasePath > 0)
                {
                    streamHelper.Stream.Position = (int)cfgSerializedHeader.pRules + (header.rules.Length * Marshal.SizeOf <CfgRule>());
                    header.BasePath = streamHelper.ReadNullTerminatedString();
                }
            }

            // Check the content - should be valid for both SAPI 5.0 and SAPI 5.2 grammars
            CheckValidCfgFormat(cfgSerializedHeader, header, includeAllGrammarData);

            return(header);
        }