//------------------------------------------------------ // Class: TChineseCalendar // Function: SetDate // Arguments: TChineseDate& // // Comments: this function sets the julian day value // in the base class from the given // TChineseDate // // Return: None //------------------------------------------------------ EXPORT_C TInt TChineseCalendar::SetDate(const TChineseDate &aDate) { TReal jD; TChineseDate ChinDate = aDate; if(ValidDate(ChinDate)==EFalse) { return KCalConInvalidDate; } ChineseToFixed(aDate,jD); iJulianDay = jD; return KErrNone; }
// Assign a due date void Todo::AddDueDate () { string dateText; cout << "Enter a due date [mm/dd format]: "; getline(cin, dateText); while ( !ValidDate(dateText) ) { cout << "Invlaid date format, enter another: "; getline(cin, dateText); } DueDate.Set(dateText); }
// 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; }