Ejemplo n.º 1
0
bool Keys::SelectKeys( vector<Char *> arr )
{
    // Проверка на комментарии
    int all = CheckComment( arr );
    if( all == -1 )
        all = arr.size();

    // Проверяем на дерективы препроцессора
    if( CheckDerective( arr, all ) )
        return true;

    // Проверка на строку
    commas.clear();
    CheckCommas( arr, all );

    // Обработка оставшегося массива
    int pos,left;
    string str  = GetString( arr, all );
    string str2;

    // До конца ключевых символов
    for( unsigned int i = 0; i < keys.size(); i++ )
    {
        CheckLast( arr, all, keys[i].str );

        // Проверка всех допустимых символов
        for( unsigned int j = 0; j < keys[i].crn.length(); j++ )
        {
            str2 = str;
            pos  = 0;
            left = 0;

            // Пока есть ключевые слова
            while( pos >= 0 )
            {
                pos = str2.find( keys[i].str + keys[i].crn[j] );

                // Если позиция найденна
                if( pos >= 0 )
                {
                    int posReal = GetPos( pos + left, arr );

                    if( !CheckPosCommas( posReal ) )
                    {
                        // Задание параметров
                        KeysArrayPos newKord;
                        newKord.begin = posReal;
                        newKord.end   = newKord.begin + keys[i].str.length();
                        newKord.font  = 1;
                        newKord.color = BLUE;
                        keysArrPos.push_back( newKord );
                    }

                    // Обрезка строки
                    str2.erase(0, pos + keys[i].str.length() + 1 );
                    left += pos + keys[i].str.length() + 1;
                }
            }
        }
    }

    if( !keysArrPos.size() )
        return false;

    return true;
}
Ejemplo n.º 2
0
// Checks if text is valid and sets each part
// of the todo; Returns true if it was a valid Todo
bool Todo::IsValidAndParse(string& errorMessage)
{
	string		text = Text;
	int			indexOfText = 0;
	Tag			aTag;
	vector<Tag> listOfTags;

	// Move past any leading space characters
	RemoveStartWhitespace(text);
	RemoveEndWhitespace(text);

	int	todoLength = text.length();	// For performance
	
	// May not start with following chars, invalid format
	if (text[0] == '{' || text[0] == '}' || text[0] == ')' || text[0] == ']')
	{
		errorMessage = "Invalid format.";
		return false;
	}

	// 1. Check for priority tag
	if ( text[indexOfText] == '(' )
	{
		int closedParenthesisIndex = NOT_FOUND;
		for (int i = 0; i < int(text.size()); i++)
			if ( text[i] == ')' )
				closedParenthesisIndex = i;
		
		// Invalid if no matching parenthesis or if closed is last char;
		// todo cannot contain only a priority
		if (closedParenthesisIndex == NOT_FOUND || closedParenthesisIndex == text.size()-1)
		{
			errorMessage = "Invalid format";
			return false;
		}

		char aPriority = toupper(text[indexOfText+1]);
		if ( CheckForValidPriority(aPriority) )
		{
			Priority = aPriority;
			indexOfText += 3;
		}
		else 
		{
			errorMessage =  "Priority tag '";
			errorMessage += text[indexOfText+1];
			errorMessage += "' is an invalid character. ";
			return false;
		}
	}

	// 2. Get due date
	int i = indexOfText;
	int openBracketIndex = NOT_FOUND;
	for ( ; i < todoLength && openBracketIndex == NOT_FOUND; i++)
		if (text[i] == '[')
			openBracketIndex = i;

	if (openBracketIndex != NOT_FOUND)
	{
		int closedBracketIndex = NOT_FOUND;
		for ( ; i < todoLength && closedBracketIndex == NOT_FOUND; i++)
			if (text[i] == ']')
				closedBracketIndex = i;

		if ( closedBracketIndex == NOT_FOUND || closedBracketIndex == text.size()-1 )
		{
			errorMessage = "Invalid date format, no closed bracket found. ";
			return false;
		}

		string dateText = text.substr(openBracketIndex+1, closedBracketIndex-openBracketIndex-1);
		if ( !ValidDate(dateText) )
		{
			errorMessage = "Invalid date format. ";
			return false;
		}

		DueDate.Set(dateText);
		indexOfText = closedBracketIndex+1;
	}

	MovePastWhitespace(text, indexOfText);

	// 3. Get Text of the todo
	i = indexOfText;
	int openBraceIndex = NOT_FOUND;
	for ( ; i < todoLength && openBraceIndex == NOT_FOUND; i++)
		if (text[i] == '{')
			openBraceIndex = i;

	// If no open brace found, set Text as everything remaining in the input text
	if (openBraceIndex == NOT_FOUND)
	{
		Text = text.substr(indexOfText);
		RemoveEndWhitespace(Text);
		// check for invalid chars
		if (!CheckForInvalidCharacters(Text))
		{
			errorMessage = "Invalid characters found.";
			return false;
		}
		return true;
	}
	// Otherwise, '{' should mark the beginning of tag list, so set
	// Text as everything up to that index
	else
	{
		Text = text.substr(indexOfText, i-indexOfText-1);
		RemoveEndWhitespace(Text);
		indexOfText = i;
		if (!CheckForInvalidCharacters(Text))
		{
			errorMessage = "Invalid characters found in tags.";
			return false;
		}
	}

	// 4. Get tags
	int closedBraceIndex;
	openBraceIndex = indexOfText-1;
	closedBraceIndex = NOT_FOUND;

	// - Find closed brace; marks end of tag list
	for (i = indexOfText+1; i < todoLength; i++)
		if ( text[i] == '}' )
			closedBraceIndex = i;

	// - todo is invalid if there is nothing between the braces
	// or if no closed brace is found
	if ( closedBraceIndex == NOT_FOUND || openBraceIndex == closedBraceIndex-1 )
	{
		errorMessage = "Invalid tag format. ";
		return false;
	}

	// invalid if nonspace chars are found after the closed brace;
	// nothing should come after the tags, if any
	for (int i = closedBraceIndex+1; i < todoLength; i++)
		if (text[i] != ' ')
		{
			errorMessage = "No characters may follow the closed brace. ";
			return false;
		}
		// - tags is substring from open brace to closed brace; includes all tags
		string tags = text.substr(openBraceIndex+1, closedBraceIndex-openBraceIndex-1);

		// Check to see if only spaces exist between braces
		int lastNonSpaceCharacter = NOT_FOUND;
		for (int i = 0; i < int(tags.length()); i++)
			if (tags[i] != ' ')
				lastNonSpaceCharacter = i;

		if (lastNonSpaceCharacter == NOT_FOUND || !CheckCommas(tags))
		{
			errorMessage = "Invalid tag format. ";
			return false;
		}
		// - Look for last comma
		int lastCommaIndex = NOT_FOUND;
		for (int pos = 0; pos < int(tags.length()); pos++)
			if ( tags[pos] == ',' )
				lastCommaIndex = pos;

		// - If no comma found, there is only one tag
		if ( lastCommaIndex == NOT_FOUND )
		{
			aTag = tags.substr(0);
			listOfTags.push_back(aTag);
			MyTags = listOfTags;
		}
		else
		{
			int previousCommaIndex = NOT_FOUND;
			int nextCommaIndex = NOT_FOUND;
			for (int pos = 0; pos < int(tags.size()) || nextCommaIndex == NOT_FOUND; pos += previousCommaIndex)
			{
				Tag aTag;
				string temp;
				nextCommaIndex = NOT_FOUND;
				// Find index of the next ','			
				if (previousCommaIndex == NOT_FOUND)
				{
					for (int i = pos; i < int(tags.size()); i++)
						if (tags[i] == ',')
							nextCommaIndex = i;
				}
				else
					for (int i = previousCommaIndex+1; i < int(tags.size()); i++)
						if (tags[i] == ',')
							nextCommaIndex = i;

				// If no previous comma was found yet, substring = characters from beginning of 
				// tags until next comma
				if ( previousCommaIndex == NOT_FOUND && nextCommaIndex != NOT_FOUND )
					temp = tags.substr(0, nextCommaIndex);
				// else substring = characters from previous until next comma
				else if ( previousCommaIndex != NOT_FOUND && nextCommaIndex != NOT_FOUND )
					temp = tags.substr(previousCommaIndex + 1, nextCommaIndex - previousCommaIndex - 1);
				// else substring = characters from previous comma until end of tags
				else
				{
					temp = tags.substr(previousCommaIndex+1);
					nextCommaIndex = tags.size();				// to exit the loop
				}

				// Remove any spaces from beginning and end of tag text
				RemoveEndWhitespace(temp);
				RemoveStartWhitespace(temp);

				// Insert aTag into vector listOfTags
				aTag = temp;
				listOfTags.push_back(aTag);

				// Look for next comma after current nextCommaIndex
				previousCommaIndex = nextCommaIndex;
			}
			MyTags = listOfTags;
		}
		return true;
}