Ejemplo n.º 1
0
CString CTemplateFormater::Format()
{
	ASSERT(!m_strSource.IsEmpty());
	CString szTmpl = CString(m_strSource);
	int iIndex, iTableSize;
	CString szKey, szText;
	int iStartPos, iNextPos;
	
	// Translate the keyword table...
	iTableSize = ARRAY_SIZE(s_KeywordTable);
	iIndex     = 0;
	// Iterate through the table
	while (iIndex < iTableSize)
	{
		// What is the keyword?
		szKey = KEYWORD_BEGIN;
		szKey += s_KeywordTable[iIndex].szKeyword;
		szKey += KEYWORD_END;
		
		// Substitute the correct value for the keyword
		szTmpl.Replace(szKey, s_KeywordTable[iIndex].Proc());
		
		// Next
		iIndex++;
	};
	
	// Translate replaceble parameters
	CStringArray ParamTable;
	
	GetReplacebleParams(ParamTable, szTmpl);
	
	iTableSize = ParamTable.GetSize();
	iIndex = 0;
	
	while (iIndex < iTableSize)
	{
		// What is the keyword?
		szKey  = PARAM_BEGIN;
		szKey += ParamTable[iIndex];
		szKey += PARAM_END;
		
		// Substitute the correct value for the keyword
		szTmpl.Replace(szKey, GetParam(ParamTable[iIndex]));
		
		// Next
		iIndex++;
	}
	
	// Translate the format specifiers
	iTableSize = ARRAY_SIZE(s_FormatSpecTable);
	iIndex     = 0;
	// Iterate through the table
	while (iIndex < iTableSize)
	{
		// What is the specifier?
		szKey = FORMAT_BEGIN;
		szKey += s_FormatSpecTable[iIndex].szSpecifier;
		iStartPos = 0;
		
		// Replace each occurrence of the specifier with
		// the new value
		while (GetNextInstance(szTmpl, szKey, iStartPos))
		{
			// Extract the enclosed text
			szText = ExtractText(szTmpl, szKey, iStartPos, iNextPos);
			
			// Replace the specifier and enclosed text with the
			// updated version
			szTmpl = ReplaceSpecifier(szTmpl, iStartPos, iNextPos,
				s_FormatSpecTable[iIndex].Proc(szText));
			
			iStartPos = iNextPos;
		};
		
		// Next
		iIndex++;
	};
	
	return (szTmpl);
}
Ejemplo n.º 2
0
static int
ExtractTables (
    char                    *InputPathname,
    char                    *Signature,
    unsigned int            MinimumInstances)
{
    char                    Buffer[BUFFER_SIZE];
    FILE                    *InputFile;
    FILE                    *OutputFile = NULL;
    size_t                  BytesWritten;
    size_t                  TotalBytesWritten = 0;
    size_t                  BytesConverted;
    unsigned int            State = FIND_HEADER;
    unsigned int            FoundTable = 0;
    unsigned int            Instances = 0;
    unsigned int            ThisInstance;
    char                    ThisSignature[4];
    int                     Status = 0;


    /* Open input in text mode, output is in binary mode */

    InputFile = fopen (InputPathname, "rt");
    if (!InputFile)
    {
        printf ("Could not open %s\n", InputPathname);
        return (-1);
    }

    if (Signature)
    {
        /* Are there enough instances of the table to continue? */

        NormalizeSignature (Signature);

        Instances = CountTableInstances (InputPathname, Signature);
        if (Instances < MinimumInstances)
        {
            printf ("Table %s was not found in %s\n", Signature, InputPathname);
            Status = -1;
            goto CleanupAndExit;
        }

        if (Instances == 0)
        {
            goto CleanupAndExit;
        }
    }

    /* Convert all instances of the table to binary */

    while (fgets (Buffer, BUFFER_SIZE, InputFile))
    {
        switch (State)
        {
        case FIND_HEADER:

            /* Ignore empty lines and lines that start with a space */

            if ((Buffer[0] == ' ') ||
                (Buffer[0] == '\n'))
            {
                continue;
            }

            NormalizeSignature (Buffer);
            strncpy (ThisSignature, Buffer, 4);

            if (Signature)
            {
                /* Ignore signatures that don't match */

                if (strncmp (ThisSignature, Signature, 4))
                {
                    continue;
                }
            }

            /*
             * Get the instance number for this signature. Only the
             * SSDT and PSDT tables can have multiple instances.
             */
            ThisInstance = GetNextInstance (InputPathname, ThisSignature);

            /* Build an output filename and create/open the output file */

            if (ThisInstance > 0)
            {
                sprintf (Filename, "%4.4s%u.dat", ThisSignature, ThisInstance);
            }
            else
            {
                sprintf (Filename, "%4.4s.dat", ThisSignature);
            }

            OutputFile = fopen (Filename, "w+b");
            if (!OutputFile)
            {
                printf ("Could not open %s\n", Filename);
                Status = -1;
                goto CleanupAndExit;
            }

            State = EXTRACT_DATA;
            TotalBytesWritten = 0;
            FoundTable = 1;
            continue;

        case EXTRACT_DATA:

            /* Empty line or non-data line terminates the data */

            if ((Buffer[0] == '\n') ||
                (Buffer[0] != ' '))
            {
                fclose (OutputFile);
                OutputFile = NULL;
                State = FIND_HEADER;

                printf ("Acpi table [%4.4s] - %u bytes written to %s\n",
                    ThisSignature, (unsigned int) TotalBytesWritten, Filename);
                continue;
            }

            /* Convert the ascii data (one line of text) to binary */

            BytesConverted = ConvertLine (Buffer, Data);

            /* Write the binary data */

            BytesWritten = fwrite (Data, 1, BytesConverted, OutputFile);
            if (BytesWritten != BytesConverted)
            {
                printf ("Write error on %s\n", Filename);
                fclose (OutputFile);
                OutputFile = NULL;
                Status = -1;
                goto CleanupAndExit;
            }

            TotalBytesWritten += BytesConverted;
            continue;

        default:
            Status = -1;
            goto CleanupAndExit;
        }
    }

    if (!FoundTable)
    {
        printf ("Table %s was not found in %s\n", Signature, InputPathname);
    }


CleanupAndExit:

    if (OutputFile)
    {
        fclose (OutputFile);
        if (State == EXTRACT_DATA)
        {
            /* Received an EOF while extracting data */

            printf ("Acpi table [%4.4s] - %u bytes written to %s\n",
                ThisSignature, (unsigned int) TotalBytesWritten, Filename);
        }
    }

    fclose (InputFile);
    return (Status);
}