Command* CommandFactory::createInstance(Client* from, const string& msg) {
    Command* cmd = NULL;

    if(msg[0] == '/') {
        if (_matches(msg, "/list")) {
            cmd = new ListCommand(from);
        } else if (_matches(msg, "/msg [[:alnum:]]\\+ [[:alnum:]]\\+")) {
            size_t name_start = msg.find(' ')+1;
            size_t text_start = msg.find(' ', name_start);
            string name = msg.substr(name_start, text_start - name_start);
            string text = msg.substr(text_start+1);
            cmd = new PrivateMessage(from, name, text);
        } else if (_matches(msg, "/nick [[:alnum:]]\\+")) {
            size_t nick_start = msg.find(' ')+1;
            size_t nick_end = msg.find(' ', nick_start);
            if (nick_end == string::npos) {
                nick_end = msg.length()-1;
            }
            string nick = msg.substr(nick_start, nick_end - nick_start);
            cmd = new NickCommand(from, nick);
        } else {
            cmd = new UnknownCommand(from);
        }
    } else {
        cmd = new PublicMessage(from, msg);
    }
    return cmd;
}
Exemple #2
0
	vector<string> anagram::matches(const vector<string> words)
	{
		vector<string> ret;

		for (string word : words)
		{
			if (_matches(word))
			{
				ret.push_back(word);
			}
		}
		return ret;
	}
Exemple #3
0
static void
find_func (gpointer data, gpointer user_data)
{
  DiaObject *obj = data;
  SearchData *sd = (SearchData *)user_data;
  
  if (!sd->found) {
    if (_matches (obj, sd)) {
      if (!sd->first)
        sd->first = obj;
      if (obj == sd->last)
        sd->seen_last = TRUE;
      else if (sd->seen_last) {
        sd->found = obj;
      }
    }
  }
}
Exemple #4
0
    bool TypeMatchExpression::_matches( const StringData& path,
                                        const MatchableDocument* doc,
                                        MatchDetails* details ) const {

        FieldRef fieldRef;
        fieldRef.parse( path );

        bool traversedArray = false;
        size_t idxPath = 0;
        BSONElement e = doc->getFieldDottedOrArray( fieldRef, &idxPath, &traversedArray );

        string rest = fieldRef.dottedField( idxPath + 1 );

        if ( e.type() != Array ) {
            return matchesSingleElement( e );
        }

        BSONObjIterator i( e.Obj() );
        while ( i.more() ) {
            BSONElement x = i.next();
            bool found = false;
            if ( rest.size() == 0 ) {
                found = matchesSingleElement( x );
            }
            else if ( x.isABSONObj() ) {
                BSONMatchableDocument doc( x.Obj() );
                found = _matches( rest, &doc, details );
            }

            if ( found ) {
                if ( details && details->needRecord() ) {
                    // this block doesn't have to be inside the _allHaveToMatch handler
                    // but this matches the old semantics
                    details->setElemMatchKey( x.fieldName() );
                }
                return true;
            }
        }

        return false;
    }
Exemple #5
0
 bool TypeMatchExpression::matches( const BSONObj& doc, MatchDetails* details ) const {
     return _matches( _path, doc, details );
 }
Exemple #6
0
 bool TypeMatchExpression::matches( const MatchableDocument* doc, MatchDetails* details ) const {
     return _matches( _path, doc, details );
 }
Exemple #7
0
bool LeafMatchExpression::_matches( const FieldRef& fieldRef,
                                    const MatchableDocument* doc,
                                    MatchDetails* details ) const {

    bool traversedArray = false;
    size_t idxPath = 0;
    BSONElement e = doc->getFieldDottedOrArray( fieldRef, &idxPath, &traversedArray );

    if ( e.type() != Array || traversedArray ) {
        return matchesSingleElement( e );
    }

    string rest = fieldRef.dottedField( idxPath + 1 );
    StringData next;
    bool nextIsNumber = false;
    if ( rest.size() > 0 ) {
        next = fieldRef.getPart( idxPath + 1 );
        nextIsNumber = isAllDigits( next );
    }

    BSONObjIterator i( e.Obj() );
    while ( i.more() ) {
        BSONElement x = i.next();

        bool found = false;
        if ( rest.size() == 0 ) {
            found = matchesSingleElement( x );
        }
        else if ( x.type() == Object ) {
            FieldRef myFieldRef;
            myFieldRef.parse( rest );
            BSONMatchableDocument myDoc( x.Obj() );
            found = _matches( myFieldRef, &myDoc, NULL );
        }


        if ( !found && nextIsNumber && next == x.fieldName() ) {
            string reallyNext = fieldRef.dottedField( idxPath + 2 );
            if ( reallyNext.size() == 0 ) {
                found = matchesSingleElement( x );
            }
            else if ( x.isABSONObj() ) {
                // TODO: this is slow
                FieldRef myFieldRef;
                myFieldRef.parse( "x." + reallyNext );
                BSONObjBuilder b;
                b.appendAs( x, "x" );
                BSONObj temp = b.obj();
                BSONMatchableDocument myDoc( temp );
                found = _matches( myFieldRef, &myDoc, NULL );
            }
        }

        if ( found ) {
            if ( !_allHaveToMatch ) {
                if ( details && details->needRecord() ) {
                    // this block doesn't have to be inside the _allHaveToMatch handler
                    // but this matches the old semantics
                    details->setElemMatchKey( x.fieldName() );
                }
                return true;
            }
        }
        else if ( _allHaveToMatch ) {
            return false;
        }
    }

    if ( rest.size() > 0 ) {
        // we're supposed to have gone further down
        return false;
    }

    return matchesSingleElement( e );
}
Exemple #8
0
bool LeafMatchExpression::matches( const MatchableDocument* doc, MatchDetails* details ) const {
    return _matches( _fieldRef, doc, details );
}
Exemple #9
0
static gint
fnr_respond (GtkWidget *widget, gint response_id, gpointer data)
{
  const gchar *search = gtk_entry_get_text (g_object_get_data (G_OBJECT (widget), "search-entry")); 
  const gchar *replace;
  DDisplay *ddisp = (DDisplay*)data;
  SearchData sd = { 0, };
  sd.diagram = ddisp->diagram;
  sd.flags =  gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON ( 
                  g_object_get_data (G_OBJECT (widget), "match-case"))) ? MATCH_CASE : 0;
  sd.flags |= gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON ( 
                  g_object_get_data (G_OBJECT (widget), "match-word"))) ? MATCH_WORD : 0;
  sd.flags |= gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON ( 
		  g_object_get_data (G_OBJECT (widget), "match-all-properties"))) ? MATCH_ALL_PROPERTIES : 0;
  

  switch (response_id) {
  case RESPONSE_FIND :
    sd.key = search;
    sd.last = g_object_get_data (G_OBJECT (widget), "last-found");
    if (!_matches (sd.last, &sd))
      sd.last = NULL; /* reset if we start a new search */
    diagram_remove_all_selected (ddisp->diagram, TRUE);
    data_foreach_object (ddisp->diagram->data, find_func, &sd);
    /* remember it */
    sd.last = sd.found ? sd.found : sd.first;
    g_object_set_data (G_OBJECT (widget), "last-found", sd.last);
    if (sd.last) {
      if (dia_object_get_parent_layer(sd.last) != ddisp->diagram->data->active_layer) {
        /* can only select objects in the active layer */
        data_set_active_layer(ddisp->diagram->data, dia_object_get_parent_layer(sd.last));
        diagram_add_update_all(ddisp->diagram);
        diagram_flush(ddisp->diagram);
      }
      diagram_select (ddisp->diagram, sd.last);
      ddisplay_present_object (ddisp, sd.last);
    }
    break;
  case RESPONSE_REPLACE :
    replace = gtk_entry_get_text (g_object_get_data (G_OBJECT (widget), "replace-entry"));
    sd.key = search;
    sd.last = g_object_get_data (G_OBJECT (widget), "last-found");
    if (!_matches (sd.last, &sd)) {
      sd.last = NULL; /* reset if we start a new search */
      data_foreach_object (ddisp->diagram->data, find_func, &sd);
    }
    sd.last = sd.found ? sd.found : sd.first;
    g_object_set_data (G_OBJECT (widget), "last-found", sd.last);
    if (sd.last) {
      _replace (sd.last, &sd, replace);
      undo_set_transactionpoint(ddisp->diagram->undo);
    }
    g_object_set_data (G_OBJECT (widget), "last-found", sd.last);
    break;
  case RESPONSE_REPLACE_ALL :
    replace = gtk_entry_get_text (g_object_get_data (G_OBJECT (widget), "replace-entry"));
    sd.key = search;
    sd.last = g_object_get_data (G_OBJECT (widget), "last-found");
    do {
      if (!_matches (sd.last, &sd)) {
        sd.last = NULL; /* reset if we start a new search */
	sd.first = NULL;
        data_foreach_object (ddisp->diagram->data, find_func, &sd);
      }
      sd.last = sd.found ? sd.found : sd.first;
      if (sd.last)
        if (!_replace (sd.last, &sd, replace))
	  sd.last = NULL;
    } while (sd.last);
    g_object_set_data (G_OBJECT (widget), "last-found", sd.last);
    undo_set_transactionpoint(ddisp->diagram->undo);
    break;
  default:
    gtk_widget_hide (widget);
  }
  return 0;
}