Пример #1
0
void
hook (GMatchInfo *minfo, gpointer user_data)
{
    GString *str = (GString *)user_data;

    g_string_append_printf (str, "FROM THE HOOK<br/><br/>\n");

    while (g_match_info_matches (minfo))
    {
        guint n = g_match_info_get_match_count (minfo);
        g_string_append_printf (str, "Match count: %d<br/><br/>\n", n);
        gchar *word = g_match_info_fetch (minfo, 0);
        g_string_append_printf (str, "Found: %s<br/><br/>\n", word);
        g_free (word);

        guint i;
        for (i = 1; i < n; i++)
        {
            gchar *word = g_match_info_fetch (minfo, i);
            g_string_append_printf (str, "sub %d: %s<br/><br/>\n", i, word);
            g_free (word);
        }

        if (n > 1)
        {
            word = g_match_info_fetch_named (minfo, "controller");
            g_string_append_printf (str, "sub named controller: %s<br/><br/>\n", word);
            g_free (word);
        }

        g_match_info_next (minfo, NULL);
    }
}
Пример #2
0
Reader *tokenize(char *line) {
    GRegex *regex;
    GMatchInfo *matchInfo;
    GError *err = NULL;

    Reader *reader = reader_new();

    regex = g_regex_new ("[\\s ,]*(~@|[\\[\\]{}()'`~@]|\"(?:[\\\\].|[^\\\\\"])*\"|;.*|[^\\s \\[\\]{}()'\"`~@,;]*)", 0, 0, &err);   
    g_regex_match (regex, line, 0, &matchInfo);

    if (err != NULL) {
        fprintf(stderr, "Tokenize error: %s\n", err->message);
        return NULL;
    }
    
    while (g_match_info_matches(matchInfo)) {
        gchar *result = g_match_info_fetch(matchInfo, 1);
        if (result[0] != '\0' && result[0] != ';') {
            reader_append(reader, result);
        }
        g_match_info_next(matchInfo, &err);
    }
    g_match_info_free(matchInfo);
    g_regex_unref(regex);
    if (reader->array->len == 0) {
        reader_free(reader);
        return NULL;
    } else {
        return reader;
    }
}
Пример #3
0
GPtrArray* getCVEUrls(const string &changelog)
{
    GPtrArray *cve_urls = g_ptr_array_new();

    // Regular expression to find cve references
    GRegex *regex;
    GMatchInfo *match_info;
    regex = g_regex_new("CVE-\\d{4}-\\d{4,}",
                        G_REGEX_CASELESS,
                        G_REGEX_MATCH_NEWLINE_ANY,
                        0);
    g_regex_match (regex, changelog.c_str(), G_REGEX_MATCH_NEWLINE_ANY, &match_info);
    while (g_match_info_matches(match_info)) {
        gchar *cve = g_match_info_fetch (match_info, 0);
        gchar *cveLink;

        cveLink = g_strdup_printf("http://web.nvd.nist.gov/view/vuln/detail?vulnId=%s", cve);
        g_ptr_array_add(cve_urls, (gpointer) cveLink);

        g_free(cve);
        g_match_info_next(match_info, NULL);
    }
    g_match_info_free(match_info);
    g_regex_unref(regex);

    // NULL terminate
    g_ptr_array_add(cve_urls, NULL);

    return cve_urls;
}
Пример #4
0
void latex_analyse_errors(GuLatex* lc)
{
  gchar* result = NULL;
  GError* err = NULL;
  GRegex* match_str = NULL;
  GMatchInfo* match_info;

  if (!(match_str = g_regex_new(":(\\d+):", G_REGEX_DOTALL, 0, &err))) {
    slog(L_ERROR, "g_regex_new (): %s\n", err->message);
    g_error_free(err);
    return;
  }

  if (lc->compilelog == NULL) printf("null\n");

  if (g_regex_match(match_str, lc->compilelog, 0, &match_info)) {
    gint count = 0;
    while (g_match_info_matches(match_info)) {
      if (count + 1 == BUFSIZ) break;
      result = g_match_info_fetch(match_info, 1);
      lc->errorlines[count++] = atoi(result);
      g_free(result);
      g_match_info_next(match_info, NULL);
    }
  }
  if (!lc->errorlines[0])
    lc->errorlines[0] = -1;
  g_match_info_free(match_info);
  g_regex_unref(match_str);
}
Пример #5
0
static void
linkify_cb (GtkTextBuffer *buf, GRegex *regex)
{
  gchar *text;
  GtkTextIter start, end;
  GMatchInfo *match;

  gtk_text_buffer_get_bounds (buf, &start, &end);
  text = gtk_text_buffer_get_text (buf, &start, &end, FALSE);

  gtk_text_buffer_remove_all_tags (buf, &start, &end);

  if (g_regex_match (regex, text, G_REGEX_MATCH_NOTEMPTY, &match))
    {
      do
	{
	  gint sp, ep, spos, epos;

	  g_match_info_fetch_pos (match, 0, &sp, &ep);

	  /* positions are in bytes, not character, so here we must normalize it*/
	  spos = g_utf8_pointer_to_offset (text, text + sp);
	  epos = g_utf8_pointer_to_offset (text, text + ep);

	  gtk_text_buffer_get_iter_at_offset (buf, &start, spos);
	  gtk_text_buffer_get_iter_at_offset (buf, &end, epos);

	  gtk_text_buffer_apply_tag (buf, tag, &start, &end);
	}
      while (g_match_info_next (match, NULL));
    }
  g_match_info_free (match);

  g_free(text);
}
Пример #6
0
static GHashTable *
parse_schema (gchar *schema)
{
	GRegex *regex;
	GMatchInfo *match;
	GError *error = NULL;
	GHashTable *hash;

	hash = g_hash_table_new(g_str_hash, g_str_equal);
	regex = g_regex_new("(\\w+)=\"(.*?)\"", 0, 0, &error);

	if (error) {
		HASHFS_DEBUG("Failed to create regex: %s", error->message);

		g_error_free(error);

		return NULL;
	}

	g_regex_match(regex, schema, 0, &match);
	while (g_match_info_matches(match)) {
		gchar *key = g_match_info_fetch(match, 1);
		gchar *val = g_match_info_fetch(match, 2);

		g_hash_table_insert(hash, key, val);

		g_match_info_next(match, NULL);
	}

	g_match_info_free(match);
	g_regex_unref(regex);

	return hash;
}
Пример #7
0
GuSnippetInfo* snippets_parse (char* snippet) {
    gint i, start, end;
    GError* err = NULL;
    gchar** result = NULL;
    GRegex* regex = NULL;
    GMatchInfo* match_info = NULL;
    const gchar* holders[] = { "\\$([0-9]+)", "\\${([0-9]*):?([^}]*)}",
                               "\\$(FILENAME)", "\\${(FILENAME)}",
                               "\\$(BASENAME)", "\\${(BASENAME)}",
                               "\\$(SELECTED_TEXT)", "\\${(SELECTED_TEXT)}"
                               /* Anyway to combine these? */
                            };

    GuSnippetInfo* info = snippet_info_new (snippet);

    for (i = 0; i < sizeof(holders) / sizeof(holders[0]); ++i) {
        if (! (regex = g_regex_new (holders[i], G_REGEX_DOTALL, 0, &err))) {
            slog (L_ERROR, "g_regex_new (): %s\n", err->message);
            g_error_free (err);
            return info;
        }
        g_regex_match (regex, snippet, 0, &match_info);
        while (g_match_info_matches (match_info)) {
            result = g_match_info_fetch_all (match_info);
            g_match_info_fetch_pos (match_info, 0, &start, &end);

            /* Convert start, end to UTF8 offset */
            char* s_start = g_substr(snippet, 0, start);
            char* s_end = g_substr(snippet, 0, end);
            start = g_utf8_strlen(s_start, -1);
            end = g_utf8_strlen(s_end, -1);
            g_free(s_start);
            g_free(s_end);

            if (i < 2) {
                snippet_info_append_holder (info, atoi (result[1]), start,
                        end -start, result[2]);
            } else {
                snippet_info_append_holder (info, -1, start, end -start,
                        result[1]);
            }
            slog (L_DEBUG, "Placeholder: (%s, %s, %s)\n", result[0], result[1],
                    result[2]);
            g_match_info_next (match_info, NULL);
            g_strfreev (result);
        }
        g_regex_unref (regex);
        g_match_info_free (match_info);
    }
    info->einfo = g_list_sort (info->einfo, snippet_info_pos_cmp);
    info->einfo_sorted = g_list_copy (info->einfo);
    info->einfo_sorted = g_list_sort (info->einfo_sorted, snippet_info_num_cmp);

    return info;
}
Пример #8
0
static pcat_rcode parse_value( GMatchInfo *match_info, GError **err, pcat_reported_value rval )
{
    pcat_rcode rv = PCAT_SUCCESS;

    rval->name      = g_match_info_fetch( match_info, 1 );
    gchar *type_val = g_match_info_fetch( match_info, 2 );
    gchar *value    = g_match_info_fetch( match_info, 3 );
    if( rval->name == NULL || type_val == NULL || value == NULL )
    {
        if( type_val )   { g_free( type_val ); }
        if( rval->name ) { g_free( rval->name ); }
        if( value )      { g_free( value ); }
        return PCAT_ERROR;
    }
    
    if( 0 == strcmp( type_val, "REAL" ) )
    {
        rval->val.tag = PCAT_GENERIC_VALUE_REAL;
        int match = sscanf( value, "%lf", &rval->val._.r );
        if( match != 1 )
        {
            /* error */
        }
        rval->original_string = value;
    }
    else if( 0 == strcmp( type_val, "STRING" ) )
    {
        rval->val.tag = PCAT_GENERIC_VALUE_STRING;
        rval->val._.s = value;
        rval->original_string = value;
    }
    else if( 0 == strcmp( type_val, "INTEGRAL" ) )
    {
        rval->val.tag = PCAT_GENERIC_VALUE_INTEGRAL;
        int match = sscanf( value, "%"SCNi64, &rval->val._.i );
        if( match != 1 )
        {
            /* error */
        }
        rval->original_string = value;
    }
    else
    {
        rv = PCAT_ERROR;
        abort();
    }
    g_print( "Found: %s : %s = %s\n",
             rval->name, generic_value_tag_string( rval->val.tag ), rval->original_string );
    g_free( type_val );
    if( g_match_info_next( match_info, err ) )
    {
        rv = PCAT_ERROR;
    }
    return rv;
}
Пример #9
0
//
// Protected
//
bool
SearchSupport::find_all( const Glib::ustring &pattern )
{
    m_matches.clear();

    Glib::ustring text = m_buffer->get_text();

    GRegex *regex;
    GMatchInfo *match_info;
    GRegexCompileFlags compile_flags;
    GRegexMatchFlags match_options;
    GError *error = NULL;

    regex = g_regex_new( pattern.data(), 
                         compile_flags, 
                         match_options, &error );

    if ( error )
    {
        g_print("Error while creating regex: %s\n", error->message);
        return false;
    }

    
    if (!g_regex_match( regex, text.data(), 
                        match_options, &match_info ))
    {
        return false;
    }

    gint start, end;

    int idx=0;
    while (g_match_info_matches(match_info))
    {
        if (!g_match_info_fetch_pos( match_info, 0, &start, &end ))
        {
            m_matches.clear();
            return false;
        }

        MatchInfo *info = new MatchInfo();
        info->start_pos = start;
        info->end_pos = end;
        m_matches.push_back( info );

        g_match_info_next( match_info, NULL ); 
    }

    g_match_info_free( match_info );
    g_regex_unref( regex );

    return true;
}
Пример #10
0
static VALUE
rg_next(VALUE self)
{
    gboolean matched;
    GError *error = NULL;

    matched = g_match_info_next(_SELF(self), &error);

    if (error)
        RAISE_GERROR(error);

    return CBOOL2RVAL(matched);
}
Пример #11
0
static inline void scr_log_urls(const gchar *string)
{
  GMatchInfo *match_info;

  g_regex_match_full(url_regex, string, -1, 0, 0, &match_info, NULL);
  while (g_match_info_matches(match_info)) {
    gchar *url = g_match_info_fetch(match_info, 0);
    scr_print_logwindow(url);
    g_free(url);
    g_match_info_next(match_info, NULL);
  }
  g_match_info_free(match_info);
}
Пример #12
0
void
url_check_line (char *buf, int len)
{
	GRegex *re(void);
	GMatchInfo *gmi;
	char *po = buf;
	int i;

	/* Skip over message prefix */
	if (*po == ':')
	{
		po = strchr (po, ' ');
		if (!po)
			return;
		po++;
	}
	/* Allow only commands from the above list */
	for (i = 0; i < ARRAY_SIZE (commands); i++)
	{
		char *cmd = commands[i];
		int len = strlen (cmd);

		if (strncmp (cmd, po, len) == 0)
		{
			po += len;
			break;
		}
	}
	if (i == ARRAY_SIZE (commands))
		return;

	/* Skip past the channel name or user nick */
	po = strchr (po, ' ');
	if (!po)
		return;
	po++;

	g_regex_match(re_url(), po, 0, &gmi);
	while (g_match_info_matches(gmi))
	{
		int start, end;

		g_match_info_fetch_pos(gmi, 0, &start, &end);
		while (end > start && (po[end - 1] == '\r' || po[end - 1] == '\n'))
			end--;
		if (g_strstr_len (po + start, end - start, "://"))
			url_add(po + start, end - start);
		g_match_info_next(gmi, NULL);
	}
	g_match_info_free(gmi);
}
Пример #13
0
static void
scan_matches(GRegex *regex, const char *string, GPtrArray *dst)
{
    GMatchInfo *match_info;
    char *match;

    g_regex_match(regex, string, G_REGEX_MATCH_NOTEMPTY, &match_info);
    while (g_match_info_matches(match_info)) {
        match = g_match_info_fetch(match_info, 1);
        g_ptr_array_add(dst, match);
        g_match_info_next(match_info, NULL);
    }
    g_match_info_free(match_info);
}
void
tpaw_string_match_link (const gchar *text,
    gssize len,
    TpawStringReplace replace_func,
    TpawStringParser *sub_parsers,
    gpointer user_data)
{
  GRegex *uri_regex;
  GMatchInfo *match_info;
  gboolean match;
  gint last = 0;

  uri_regex = uri_regex_dup_singleton ();
  if (uri_regex == NULL)
    {
      tpaw_string_parser_substr (text, len, sub_parsers, user_data);
      return;
    }

  match = g_regex_match_full (uri_regex, text, len, 0, 0, &match_info, NULL);
  if (match)
    {
      gint s = 0, e = 0;

      do
        {
          g_match_info_fetch_pos (match_info, 0, &s, &e);

          if (s > last)
            {
              /* Append the text between last link (or the
               * start of the message) and this link */
              tpaw_string_parser_substr (text + last,
                  s - last,
                  sub_parsers,
                  user_data);
            }

          replace_func (text + s, e - s, NULL, user_data);

          last = e;
        } while (g_match_info_next (match_info, NULL));
    }

  tpaw_string_parser_substr (text + last, len - last, sub_parsers, user_data);

  g_match_info_free (match_info);
  g_regex_unref (uri_regex);
}
Пример #15
0
static void
parse_unsolicited (MMPortSerial *port, GByteArray *response)
{
    MMPortSerialAt *self = MM_PORT_SERIAL_AT (port);
    GSList *iter;

    /* Remove echo */
    if (self->priv->remove_echo)
        mm_port_serial_at_remove_echo (response);

    for (iter = self->priv->unsolicited_msg_handlers; iter; iter = iter->next) {
        MMAtUnsolicitedMsgHandler *handler = (MMAtUnsolicitedMsgHandler *) iter->data;
        GMatchInfo *match_info;
        gboolean matches;

        if (!handler->enable)
            continue;

        matches = g_regex_match_full (handler->regex,
                                      (const char *) response->data,
                                      response->len,
                                      0, 0, &match_info, NULL);
        if (handler->callback) {
            while (g_match_info_matches (match_info)) {
                handler->callback (self, match_info, handler->user_data);
                g_match_info_next (match_info, NULL);
            }
        }

        g_match_info_free (match_info);

        if (matches) {
            /* Remove matches */
            char *str;
            int result_len = response->len;

            str = g_regex_replace_eval (handler->regex,
                                        (const char *) response->data,
                                        response->len,
                                        0, 0,
                                        remove_eval_cb, &result_len, NULL);

            g_byte_array_remove_range (response, 0, response->len);
            g_byte_array_append (response, (const guint8 *) str, result_len);
            g_free (str);
        }
    }
}
Пример #16
0
// Parse NCBI GIs from refseq descriptions and store GI to refseq mapping in a hashtable.
//
// bam_header      Pointer to the header for one of the alignment BAM files.
// refseq_gids     Stored NCBI GI for each target sequence in the bam_header.
//
// Returns a GHashTable mapping reference sequence NCBI GI to BAM header index
// (i.e., an index into bam_header->target_name).
//
GHashTable *build_refseq_hash(bam_header_t *bam_header, int **refseq_gids, gboolean verbose)
{
  // mapping from target sequence GI to BAM header refseq index
  // NOTE: some target sequences appear more than once in the HMP files.  In those 
  // cases the BAM header refseq index will be the index of the _first_ one found 
  // with that GI.
  GHashTable *refseq_gi_ht = g_hash_table_new(g_int_hash, g_int_equal);

  // regex to parse GI from refseq/refseq name
  GRegex *refseq_gi_re = g_regex_new("^[A-Z\\d_]+\\|gi\\|(\\d+)\\|.*", 0, 0, NULL);
  GMatchInfo *match_info = NULL;
  int gid;
  // HACK: this array (refseq_inds) is not currently freed anywhere
  int *refseq_inds = g_malloc(sizeof(int) * bam_header->n_targets);
  *refseq_gids = g_malloc(sizeof(int) * bam_header->n_targets);
  int *refseq_gids_a = *refseq_gids;
  int i;

  for (i = 0;i < bam_header->n_targets; ++i) {
    refseq_inds[i] = i;
    // parse target sequence GI and insert it into the hash table
    if (g_regex_match(refseq_gi_re, bam_header->target_name[i], 0, &match_info)) {
      while (g_match_info_matches(match_info)) {
	gchar *gi_str = g_match_info_fetch(match_info, 1);
	sscanf(gi_str, "%d", &gid);
	g_free(gi_str);
	refseq_gids_a[i] = gid;

	// check for duplicate gids
	if (g_hash_table_lookup(refseq_gi_ht, &gid) != NULL) {
	  if (verbose) fprintf(stderr, "WARN - duplicate gid=%d found in target %d: %s\n", gid, i, bam_header->target_name[i]);
	} 
	// store mapping from gid to (first) target index (since there can be multiple targets with the same gi)
	else {
	  g_hash_table_insert(refseq_gi_ht, &refseq_gids_a[i], &refseq_inds[i]);
	}
	g_match_info_next(match_info, NULL);
      }
    }
    // if no GI can be parsed, generate a dummy one
    else {
      refseq_gids_a[i] = new_gi--;
    }
    g_match_info_free(match_info); 
  }
  g_regex_unref(refseq_gi_re);
  return refseq_gi_ht;
}
Пример #17
0
static guint8 *
get_tagged_byte_array (const gchar *uri,
                       GRegex      *filter_regex)
{
	guint8 *byte_array;
	gsize uri_len;
	GMatchInfo *match_info;
	gboolean no_match = TRUE;

	g_return_val_if_fail (uri != NULL, NULL);

	uri_len = strlen (uri);
	byte_array = g_malloc0 (uri_len + 1);
	byte_array[uri_len] = BYTE_ARRAY_END;

	if (g_regex_match (filter_regex, uri, 0, &match_info) == TRUE)
	{
		while (g_match_info_matches (match_info) == TRUE)
		{
			guint8 *p;
			gint match_len;
			gint start_pos;
			gint end_pos;

			if (g_match_info_fetch_pos (match_info, 0, &start_pos, &end_pos) == TRUE)
			{
				match_len = end_pos - start_pos;
				no_match = FALSE;

				p = (guint8 *)((gsize)byte_array + start_pos);
				memset (p, SELECTOR_TAG_MATCH, match_len);
			}

			g_match_info_next (match_info, NULL);
		}
	}

	g_match_info_free (match_info);

	if (no_match)
	{
		g_free (byte_array);
		return NULL;
	}

	return byte_array;
}
Пример #18
0
static GList *snr3_find_pcre(Tsnr3run *s3run, gchar *buffer) {
	Tlineinbuffer lib = {0,1};
	GList *results=NULL;
	GMatchInfo *match_info;

	g_regex_match(s3run->regex, buffer, 0, &match_info);
	while(g_match_info_matches(match_info)) {
		gint so, eo;
		guint line;
		g_match_info_fetch_pos(match_info,0,&so,&eo);
		line = calculate_line_in_buffer(&lib, buffer, so);
		results = g_list_prepend(results, new_result(line, buffer, so));
		g_match_info_next(match_info, NULL);
	}
	g_match_info_free(match_info);

	return results;
}
Пример #19
0
/* Search regular expression in text, return TRUE and matching part as start and
 * end integer position */
static gboolean
search_regex_in_text (const gchar* search_entry, const gchar* editor_text, gboolean search_forward, gint * start_pos, gint * end_pos)
{
	GRegex * regex;
	GMatchInfo *match_info;
	gboolean found;
	GError * err = NULL;

	regex = g_regex_new (search_entry, 0, 0, &err);
	if (err)
	{
		g_message ("%s",err->message);
		g_error_free (err);
		g_regex_unref(regex);
		return FALSE;
	}

	found = g_regex_match (regex, editor_text, 0, &match_info);

	if (found)
	{
		if (search_forward)
			g_match_info_fetch_pos(match_info, 0, start_pos, end_pos);
		else
		{
			do
			{
				g_match_info_fetch_pos(match_info, 0, start_pos, end_pos);
			}
			while (g_match_info_next(match_info, NULL));
		}

		*start_pos = g_utf8_pointer_to_offset(editor_text, &editor_text[*start_pos]);
		*end_pos = g_utf8_pointer_to_offset(editor_text, &editor_text[*end_pos]);
	}

	if (regex)
		g_regex_unref(regex);
	if (match_info)
		g_match_info_free (match_info);

	return found;

}
Пример #20
0
static void
playsound(gchar **str, gint which)
{
    GMatchInfo *match_info;
    const gchar *list = NULL;
    gchar **candidates = NULL, **candidate = NULL;

    list = purple_prefs_get_string(which ? OPT_USERLIST_SENDER :
                                   OPT_USERLIST_RECIPIENT);
    g_return_if_fail(list != NULL);
    if(strstr(list, DEFAULT_LIST))
        return;

    candidates = g_strsplit_set(list, " ,:;", 0);
    g_return_if_fail(candidates != NULL);

    g_regex_match(regp[which], *str, 0, &match_info);
    while(g_match_info_matches(match_info)) {
        gchar *user = NULL;
        if(which == RECIPIENT)
            user = g_match_info_fetch(match_info, 2);
        else if(which == SENDER)
            user = g_match_info_fetch(match_info, 2);
        twitter_debug("user = %s\n", user);

        for(candidate = candidates; *candidate; candidate++) {
            if(!strcmp(*candidate, ""))
                continue;
            twitter_debug("candidate = %s\n", *candidate);
            if(!strcmp(user, *candidate)) {
                twitter_debug("match. play sound\n");
                purple_sound_play_event(purple_prefs_get_int
                                        (which ? OPT_SOUNDID_SENDER :
                                         OPT_SOUNDID_RECIPIENT), NULL);
                break;
            }
        }
        g_free(user);
        g_match_info_next(match_info, NULL);
    }
    g_strfreev(candidates);
    g_match_info_free(match_info);
}
static void
parse_unsolicited (MMSerialPort *port, GByteArray *response)
{
    MMAtSerialPort *self = MM_AT_SERIAL_PORT (port);
    MMAtSerialPortPrivate *priv = MM_AT_SERIAL_PORT_GET_PRIVATE (self);
    GSList *iter;

    for (iter = priv->unsolicited_msg_handlers; iter; iter = iter->next) {
        MMAtUnsolicitedMsgHandler *handler = (MMAtUnsolicitedMsgHandler *) iter->data;
        GMatchInfo *match_info;
        gboolean matches;

        matches = g_regex_match_full (handler->regex,
                                      (const char *) response->data,
                                      response->len,
                                      0, 0, &match_info, NULL);
        if (handler->callback) {
            while (g_match_info_matches (match_info)) {
                handler->callback (self, match_info, handler->user_data);
                g_match_info_next (match_info, NULL);
            }
        }

        g_match_info_free (match_info);

        if (matches) {
            /* Remove matches */
            char *str;
            int result_len = response->len;

            str = g_regex_replace_eval (handler->regex,
                                        (const char *) response->data,
                                        response->len,
                                        0, 0,
                                        remove_eval_cb, &result_len, NULL);

            g_byte_array_remove_range (response, 0, response->len);
            g_byte_array_append (response, (const guint8 *) str, result_len);
            g_free (str);
        }
    }
}
Пример #22
0
static int 
regexp_match(struct objlist *obj,N_VALUE *inst,N_VALUE *rval,int argc,char **argv)
{
  struct oregexp_local *local;
  GMatchInfo *info;
  int r;
  char *str, **match;

  str = (char *) argv[2];

  rval->i = 0;

  _getobj(obj, "_local", inst, &local);
  if (local->regexp == NULL) {
    return 1;
  }

  del_array_element(local->array);

  if (str == NULL || str[0] == '\0') {
    return 1;
  }

  info = NULL;
  r = g_regex_match(local->regexp, str, 0, &info);
  if (! r) {
    g_match_info_free(info);
    return 1;
  }

  while (g_match_info_matches(info)) {
    match = g_match_info_fetch_all(info);
    arrayadd(local->array, &match);
    g_match_info_next(info, NULL);
  }
  g_match_info_free(info);

  rval->i = arraynum(local->array);

  return 0;
}
Пример #23
0
static int
do_an_re(const char *word, int *start, int *end, int *type)
{
	typedef struct func_s {
		GRegex *(*fn)(void);
		int type;
	} func_t;
	func_t funcs[] =
	{
		{ re_url, WORD_URL },
		{ re_email, WORD_EMAIL },
		{ re_channel, WORD_CHANNEL },
		{ re_host6, WORD_HOST6 },
		{ re_host, WORD_HOST },
		{ re_path, WORD_PATH },
		{ re_nick, WORD_NICK }
	};

	GMatchInfo *gmi;
	int k;

	for (k = 0; k < sizeof funcs / sizeof (func_t); k++)
	{
		g_regex_match (funcs[k].fn(), word, 0, &gmi);
		if (!g_match_info_matches (gmi))
		{
			g_match_info_free (gmi);
			continue;
		}
		while (g_match_info_matches (gmi))
		{
			g_match_info_fetch_pos (gmi, 0, start, end);
			g_match_info_next (gmi, NULL);
		}
		g_match_info_free (gmi);
		*type = funcs[k].type;
		return TRUE;
	}

	return FALSE;
}
Пример #24
0
static gboolean is_match(const gchar * filename,
                        const gchar * re)
{
	GRegex * regex;
	GMatchInfo * match_info;

	regex = g_regex_new(re,0,0,NULL);
	g_regex_match(regex,filename,0,&match_info);

	while(g_match_info_matches(match_info))
	{
		gchar * word = g_match_info_fetch (match_info, 0);
		/* g_print ("Found: %s\n", word); */
		g_free (word);
		g_match_info_next (match_info, NULL);
		return TRUE;

	}
	g_match_info_free(match_info);
	g_regex_unref(regex);
return FALSE;
}
Пример #25
0
int regex_match_count(char *buff, int buff_size, const gchar * regex_string, GRegexCompileFlags compile_flags)
{
  int matches = 0;

  //create regex
  GError *regex_error = NULL;
  GMatchInfo *match_info = NULL;
  GRegex *regex = g_regex_new(regex_string, compile_flags, (GRegexMatchFlags) 0, &regex_error);

  if (regex_error != NULL)
  {
    debug_log("regex error code was: %s", regex_error->message);
    g_error_free(regex_error);
  } else
  {
    g_regex_match_full(regex, buff, buff_size, 0, (GRegexMatchFlags) 0, &match_info, &regex_error);
    if (regex_error != NULL)
    {
      debug_log("regex error code was: %s", regex_error->message);
      g_error_free(regex_error);
    }
    while (g_match_info_matches(match_info))
    {
      gchar *word = g_match_info_fetch(match_info, 0);

      //debug_log("Regex matched: : %s", word);
      g_free(word);
      g_match_info_next(match_info, NULL);
      matches++;
    }
    //free match info
    g_match_info_free(match_info);
  }

  //free regex
  g_regex_unref(regex);

  return matches;
}
Пример #26
0
static gboolean
regex_match (const GRegex *re, const char *word, int *start, int *end)
{
	GMatchInfo *gmi;

	g_regex_match (re, word, 0, &gmi);
	
	if (!g_match_info_matches (gmi))
	{
		g_match_info_free (gmi);
		return FALSE;
	}
	
	while (g_match_info_matches (gmi))
	{
		g_match_info_fetch_pos (gmi, 0, start, end);
		g_match_info_next (gmi, NULL);
	}
	
	g_match_info_free (gmi);
	
	return TRUE;
}
Пример #27
0
PangoAttrList *token_match_get_pango_attr ( GRegex **tokens, const char *input, PangoAttrList *retv )
{
    // Do a tokenized match.
    if ( tokens ) {
        for ( int j = 0; tokens[j]; j++ ) {
            GMatchInfo *gmi = NULL;
            g_regex_match ( (GRegex *) tokens[j], input, G_REGEX_MATCH_PARTIAL, &gmi );
            while ( g_match_info_matches ( gmi ) ) {
                int            start, end;
                g_match_info_fetch_pos ( gmi, 0, &start, &end );
                PangoAttribute *pa  = pango_attr_underline_new ( PANGO_UNDERLINE_SINGLE );
                PangoAttribute *pa2 = pango_attr_weight_new ( PANGO_WEIGHT_BOLD );
                pa2->start_index = pa->start_index = start;
                pa2->end_index   = pa->end_index = end;
                pango_attr_list_insert ( retv, pa );
                pango_attr_list_insert ( retv, pa2 );
                g_match_info_next ( gmi, NULL );
            }
            g_match_info_free ( gmi );
        }
    }
    return retv;
}
static gchar *
get_file (const gchar *text,
	  const gchar *regex_place)
{
	GRegex *regex;
	GMatchInfo *match_info;
	gchar *word = NULL;

	regex = g_regex_new (regex_place, 0, 0, NULL);
	g_regex_match (regex, text, 0, &match_info);
	while (g_match_info_matches (match_info))
	{
		g_free (word);

		word = g_match_info_fetch (match_info, 0);

		g_match_info_next (match_info, NULL);
	}
	g_match_info_free (match_info);
	g_regex_unref (regex);

	return word;
}
Пример #29
0
void
misc_macro_find(const GRegex *regex, const gchar *string, GHashTable *table)
{
    GMatchInfo *info;

    g_regex_match(regex, string, 0, &info);

    while (g_match_info_matches(info))
    {
        gchar *name = g_match_info_fetch(info, 1);

        if (!g_hash_table_lookup_extended(table, name, NULL, NULL))
        {
            g_hash_table_insert(table, name, NULL);
        }

        g_free(name);

        g_match_info_next(info, NULL);
    }

    g_match_info_free(info);
}
Пример #30
0
int twitter_url_len_diff(gchar *msg, unsigned int target_len)
{
	int url_len_diff = 0;

	static GRegex *regex = NULL;
	GMatchInfo *match_info;

	if (regex == NULL)
		regex = g_regex_new("(^|\\s)(http(s)?://[^\\s$]+)", 0, 0, NULL);
	
	g_regex_match(regex, msg, 0, &match_info);
	while (g_match_info_matches(match_info)) {
		gchar *url = g_match_info_fetch(match_info, 2);
		url_len_diff += target_len - g_utf8_strlen(url, -1);
		/* Add another character for https://t.co/... URLs */
		if (g_match_info_fetch(match_info, 3) != NULL)
			url_len_diff += 1;
		g_free(url);
		g_match_info_next(match_info, NULL);
	}
	g_match_info_free(match_info);

	return url_len_diff;
}