Exemplo n.º 1
0
void WordGenerator (CXMLElement *pCmdLine)
{
    int i;

    //	Load input file

    CString sFilespec = pCmdLine->GetAttribute(CONSTLIT("input"));
    if (sFilespec.IsBlank())
    {
        printf("ERROR: input filename expected.\n");
        return;
    }

    CFileReadBlock InputFile(sFilespec);
    if (InputFile.Open() != NOERROR)
    {
        printf("ERROR: Unable to open file: %s\n", sFilespec.GetASCIIZPointer());
        return;
    }

    //	"Novel" means that we only generate words that are not
    //	in the input file.

    bool bNovelWordsOnly = pCmdLine->GetAttributeBool(NOVEL_ATTRIB);

    //	Build up a word generator

    CMarkovWordGenerator Generator;
    TMap<CString, DWORD> InputWords;

    //	Read each line of the file

    char *pPos = InputFile.GetPointer(0);
    char *pEndPos = pPos + InputFile.GetLength();
    while (pPos < pEndPos)
    {
        //	Skip whitespace

        while (pPos < pEndPos && (strIsWhitespace(pPos) || *pPos < ' '))
            pPos++;

        //	Parse the line

        char *pStart = pPos;
        while (pPos < pEndPos && *pPos != '\r' && *pPos != '\n' && *pPos >= ' ')
            pPos++;

        CString sWord(pStart, pPos - pStart);

        //	Add the word to the generator

        if (!sWord.IsBlank())
        {
            Generator.AddSample(strTrimWhitespace(sWord));

            //	If we are looking for novel words we need to keep a map
            //	of all words in the input file.

            if (bNovelWordsOnly)
                InputWords.Insert(sWord);
        }
    }

    //	If we have a count, then output a list of random words

    int iCount;
    if (pCmdLine->FindAttributeInteger(COUNT_ATTRIB, &iCount))
    {
        if (iCount > 0)
        {
            TArray<CString> Result;
            Generator.GenerateUnique(iCount, &Result);

            for (i = 0; i < Result.GetCount(); i++)
                if (InputWords.Find(Result[i]))
                {
                    Result.Delete(i);
                    i--;
                }

            Result.Sort();

            for (i = 0; i < Result.GetCount(); i++)
                printf("%s\n", Result[i].GetASCIIZPointer());
        }
    }

    //	Otherwise, output the generator as XML

    else
    {
        CMemoryWriteStream Output;
        if (Output.Create() != NOERROR)
        {
            printf("ERROR: Out of memory.\n");
            return;
        }

        if (Generator.WriteAsXML(&Output) != NOERROR)
        {
            printf("ERROR: Unable to output generator as XML.\n");
            return;
        }

        Output.Write("\0", 1);
        printf(Output.GetPointer());
    }
}
Exemplo n.º 2
0
bool CHexeMarkupEvaluator::ProcessHeader (SHTTPRequestCtx &Ctx, CDatum dResult)

//	ProcessHeader
//
//	Outputs the given header

	{
	//	Check for error

	if (dResult.IsError())
		{
		m_Output.Write(strPattern(ERR_PROCESSING_HEADER, dResult.AsString()));
		return true;
		}

	//	Processing depends on result type

	switch (dResult.GetBasicType())
		{
		case CDatum::typeNil:
			return true;

		//	If this is a string or anything else, we expect both field and value
		//	are in the same string and we need to parse it.

		default:
			{
			CString sData = dResult.AsString();

			//	Parse into field and value

			char *pPos = sData.GetParsePointer();
			while (strIsWhitespace(pPos))
				pPos++;

			//	Look for the field name

			char *pStart = pPos;
			while (*pPos != ':' && *pPos != '\0')
				pPos++;

			CString sField(pStart, pPos - pStart);
			if (sField.IsEmpty())
				{
				m_Output.Write(strPattern(ERR_NO_HEADER_FIELD, sData));
				return true;
				}

			//	Look for the value

			CString sValue;
			if (*pPos == ':')
				{
				pPos++;
				while (strIsWhitespace(pPos))
					pPos++;

				sValue = CString(pPos);
				}

			//	Done

			CHTTPMessage::SHeader *pNewHeader = m_Headers.Insert();
			pNewHeader->sField = sField;
			pNewHeader->sValue = sValue;
			}
		}

	return true;
	}
Exemplo n.º 3
0
bool CTimeDate::Parse (const CString &sFormat, const CString &sValue, CString *retsError)

//	Parse
//
//	Parse a date of the given format.

	{
	//	Internet format:
	//
	//	Sun, 06 Nov 1994 08:49:37 GMT

	if (strEquals(sFormat, FORMAT_INTERNET))
		{
		char *pPos = sValue.GetASCIIZPointer();
		char *pPosEnd = pPos + sValue.GetLength();

		//	Skip leading whitespace

		while (strIsWhitespace(pPos))
			pPos++;

		//	Skip day of week

		while (pPos < pPosEnd
				&& (*pPos < '0' || *pPos > '9'))
			pPos++;

		if (pPos >= pPosEnd)
			goto InvalidValue;

		//	Day

		bool bFail;
		m_Time.wDay = strParseInt(pPos, 0, &pPos, &bFail);
		if (bFail || m_Time.wDay < 1 || m_Time.wDay > 31)
			goto InvalidValue;

		pPos++;
		if (pPos >= pPosEnd)
			goto InvalidValue;

		//	Month

		if (*pPos == 'A')
			{
			pPos++;
			if (*pPos == 'p')
				{
				m_Time.wMonth = 4;
				pPos += 3;
				}
			else if (*pPos == 'u')
				{
				m_Time.wMonth = 8;
				pPos += 3;
				}
			else
				goto InvalidValue;
			}
		else if (*pPos == 'D')
			{
			m_Time.wMonth = 12;
			pPos += 4;
			}
		else if (*pPos == 'F')
			{
			m_Time.wMonth = 2;
			pPos += 4;
			}
		else if (*pPos == 'J')
			{
			pPos++;
			if (*pPos == 'a')
				{
				m_Time.wMonth = 1;
				pPos += 3;
				}
			else if (*pPos == 'u')
				{
				pPos++;
				if (*pPos == 'l')
					{
					m_Time.wMonth = 7;
					pPos += 2;
					}
				else if (*pPos == 'n')
					{
					m_Time.wMonth = 6;
					pPos += 2;
					}
				else
					goto InvalidValue;
				}
			else
				goto InvalidValue;
			}
		else if (*pPos == 'M')
			{
			pPos++;
			if (*pPos == 'a')
				{
				pPos++;
				if (*pPos == 'r')
					{
					m_Time.wMonth = 3;
					pPos += 2;
					}
				else if (*pPos == 'y')
					{
					m_Time.wMonth = 5;
					pPos += 2;
					}
				else
					goto InvalidValue;
				}
			else
				goto InvalidValue;
			}
		else if (*pPos == 'N')
			{
			m_Time.wMonth = 11;
			pPos += 4;
			}
		else if (*pPos == 'O')
			{
			m_Time.wMonth = 10;
			pPos += 4;
			}
		else if (*pPos == 'S')
			{
			m_Time.wMonth = 9;
			pPos += 4;
			}
		else
			goto InvalidValue;

		if (pPos >= pPosEnd)
			goto InvalidValue;

		//	Year

		m_Time.wYear = strParseInt(pPos, 0, &pPos, &bFail);
		if (bFail || m_Time.wYear < 1)
			goto InvalidValue;

		pPos++;
		if (pPos >= pPosEnd)
			goto InvalidValue;

		//	Hour

		m_Time.wHour = strParseInt(pPos, 0, &pPos, &bFail);
		if (bFail || m_Time.wHour > 23)
			goto InvalidValue;

		pPos++;
		if (pPos >= pPosEnd)
			goto InvalidValue;

		//	Minute

		m_Time.wMinute = strParseInt(pPos, 0, &pPos, &bFail);
		if (bFail || m_Time.wMinute > 59)
			goto InvalidValue;

		pPos++;
		if (pPos >= pPosEnd)
			goto InvalidValue;

		//	Second

		m_Time.wSecond = strParseInt(pPos, 0, &pPos, &bFail);
		if (bFail || m_Time.wSecond > 59)
			goto InvalidValue;

		//	Millisecond is not stored

		m_Time.wMilliseconds = 0;

		//	We don't store day of week

		m_Time.wDayOfWeek = 0xffff;

		//	Done

		return true;

InvalidValue:

		if (retsError) *retsError = strPatternSubst(CONSTLIT("Invalid date value: %s"), sValue);
		return false;
		}
	else
		{
		if (retsError) *retsError = strPatternSubst(CONSTLIT("Unsupported date format: %s."), sFormat);
		return false;
		}
	}
Exemplo n.º 4
0
inline bool IsDelimiterChar (char *pPos, bool bIsSpecialAttrib = false) { return (*pPos == '\0' || *pPos == ',' || *pPos == ';' || (!bIsSpecialAttrib && strIsWhitespace(pPos))); }