Ejemplo n.º 1
0
NoteCollection NoteStore::search(std::string term) {
    
    std::transform(term.begin(), term.end(), term.begin(), ::tolower);
    
    std::string word = "";
    std::vector<std::string> words;
    
    for (unsigned int i=0; i<term.length(); i++) {
        char c = term[i];
        if (c != ' ') {
            word += c;
        } else {
            if (word.length() > 0) {
                words.push_back(word);
            }
            word = "";
        }
    }
    if (word.length() > 0) {
        words.push_back(word);
    }
    
    NoteCollection noteCollection;
    Note note;
    std::string guid;
    
    for ( auto it = this->noteDatabase.begin(); it != this->noteDatabase.end(); ++it ) {
        
        guid = it->first;
        note = it->second;
        
        bool matchesAll = true;
        
        for (unsigned int i=0; i<words.size(); i++) {
            std::string keyword = words[i];
            long wildcardAt = keyword.find_first_of('*');
            
            if (strcmp(keyword.substr(0, 4).c_str(), "tag:") == 0) {
                if (!note.hasTag(keyword.substr(4,keyword.length()-4),wildcardAt-4)) {
                    matchesAll = false;
                    break;
                }
            } else if (strcmp(keyword.substr(0, 8).c_str(), "created:") == 0) {
                if (!note.createdOnOrAfter(keyword.substr(8,keyword.length()-8))) {
                    matchesAll = false;
                    break;
                }
            } else {
                if (!note.hasKeyword(keyword, wildcardAt)) {
                    matchesAll = false;
                    break;
                }
            }
        }
        
        if (matchesAll) {
            noteCollection.addNote(note);
        }
    }
    
    noteCollection.sortByDate();
    return noteCollection;
    
}