コード例 #1
0
ファイル: objnamedtuple.c プロジェクト: A-L-E-X/micropython
STATIC int namedtuple_find_field(const char *name, const char *namedef) {
    int id = 0;
    size_t len = strlen(name);
    while (namedef) {
        if (memcmp(name, namedef, len) == 0) {
            namedef += len;
            if (*namedef == 0 || is_end_tok(*namedef)) {
                return id;
            }
        }
        namedef = skip_to_next(namedef);
        id++;
    }

    return -1;
}
コード例 #2
0
ファイル: libircii.c プロジェクト: jduck/libircii
/*
 * convert a line supposedly from an ircII server into
 * some nice usable structured data
 * 
 * returns:
 * -1 on malformed data being passed in, and 1 on success
 */
int
ircii_convert(char *l, ircII *p)
{
   char *user, *host, *type, *to, *args, *q, *s, *t;
   
   if (!l)
     return 0;

   memset(p, 0, sizeof(ircII));

   if (!l || !*l || !(q = skip_space(l)))
     return -1;
   
   switch (*q)
     {
      case 'N':
	to = skip_to_next(q);
	if (!to || !*to)
	  return -1;
	if (strcmp(q, IRCII_STR_NOTICE) == 0)
	  {
	     if (!(args = skip_to_next(to)))
	       return -1;
	     p->type = IRCII_TM_NOTICE;
	     p->targ = to;
	     if (*args == ':')
	       p->msg = args + 1;
	     else
	       p->msg = args;
	     break;
	  }
	return -1;
	
      case 'E':
	args = skip_to_next(q);
	if (!args || !*args)
	  return -1;
	if (strcmp(q, IRCII_STR_ERROR) == 0)
	  {
	     p->type = IRCII_TM_ERROR;
	     if (args && *args == ':')
	       args++;
	     p->msg = args;
	     break;
	  }
	return -1;
	
      case 'P':
	args = skip_to_next(q);
	if (!args || !*args)
	  return -1;
	if (strcmp(q, IRCII_STR_PING) == 0)
	  {
	     p->type = IRCII_TM_PING;
	     if (args && *args == ':')
	       args++;
	     p->msg = args;
	     break;
	  }
	return -1;
	
      case ':':
	if (!(q+1) || !(*(q+1)))
	  return -1;
	p->from = q + 1;
	type = skip_to_next(p->from);
	user = index(q, '!');
	host = index(q, '@');
	if (user && *user && host && *host)
	  {
	     if (host <= user)
	       return -1;
	     *user++ = '\0';
	     *host++ = '\0';
	     p->user = user;
	     p->host = host;
	  }
	
	/* nasty string comparison stuff */
	to = skip_to_next(type);
	if (!to || !*to)
	  return -1;
	if (strcmp(type, IRCII_STR_PRIVMSG) == 0)
	  {
	     if (!(args = skip_to_next(to)))
	       return -1;
	     p->targ = to;
	     s = strchr(args, '\001');
	     t = strrchr(args, '\001');
	     if (s && t && s != t)
	       {
		  *t = '\0';
		  p->type = IRCII_TM_CTCP;
		  p->msg = s;
	       }
	     else if (*args == ':' && (args+1) && *(args+1))
	       {
		  p->type = IRCII_TM_PRIVMSG;
		  p->msg = args+1;
	       }
	     else
	       {
		  p->type = IRCII_TM_PRIVMSG;
		  p->msg = args;
	       }
	  }
	else if (strcmp(type, IRCII_STR_NOTICE) == 0)
	  {
	     if (!(args = skip_to_next(to)))
	       return -1;
	     p->targ = to;
	     s = strchr(args, '\001');
	     t = strrchr(args, '\001');
	     if (s && t && s != t)
	       {
		  *t = '\0';
		  p->type = IRCII_TM_CTCP_REPLY;
		  p->msg = s;
	       }
	     else
	       {
		  p->type = IRCII_TM_NOTICE;
		  if (*args == ':' && (args+1) && *(args+1))
		    args++;
		  p->msg = args;
	       }
	  }
	else if (strcmp(type, IRCII_STR_MODE) == 0)
	  {
	     if (!(args = skip_to_next(to)))
	       return -1;
	     p->targ = to;
	     p->type = IRCII_TM_MODE;
	     p->msg = args;
	  }
	else if (strcmp(type, IRCII_STR_JOIN) == 0)
	  {
	     p->type = IRCII_TM_JOIN;
	     p->targ = *to == ':' ? to+1 : to;
	  }
	else if (strcmp(type, IRCII_STR_PART) == 0)
	  {
	     p->type = IRCII_TM_PART;
	     p->targ = *to == ':' ? to+1 : to;
	  }
	else if (strcmp(type, IRCII_STR_QUIT) == 0)
	  {
	     p->type = IRCII_TM_QUIT;
	     p->msg = *to == ':' ? to+1 : to;
	  }
	else if (strcmp(type, IRCII_STR_PONG) == 0)
	  {
	     p->type = IRCII_TM_PONG;
	     p->msg = *to == ':' ? to+1 : to;
	  }
	else if (strcmp(type, IRCII_STR_NICK) == 0)
	  {
	     p->type = IRCII_TM_NICK;
	     p->msg = *to == ':' ? to+1 : to;
	  }
	else if (isdigit(*type) && atoi(type) > 0)
	  {
	     p->type = IRCII_TM_NUMERIC;
	     p->numeric = atoi(type);
	     if (!(args = skip_to_next(to)))
	       return -1;
	     if (*args == ':' && (args+1) && *(args+1))
	       args++;
	     p->msg = args;
	     p->targ = to;
	  }
	else
	  return -1;
	break;
	
      default:
	printf("Unknown message beginning: %s\n", q);
	return -1;
     }
   return 1;
}