Example #1
0
/**
 * Does name2 contain name1?
 * @param m the matrix in question
 * @param name1 the name that may be inside
 * @param name2 the name that may be outside
 * @return the number of times name1 is inside
 */
int matrix_inside( matrix *m, UChar *name1, UChar *name2 )
{
    int index1 = hashset_get(m->lookup, name1)-1;
    int index2 = hashset_get(m->lookup, name2)-1;
    if ( index1 == -1 || index2 == -1 )
        return 0;
    else
        return m->cells[m->n_props*index1+index2];
}
Example #2
0
/**
 * Record that a property is inside another
 * @param m the matrix in question
 * @param prop1 the property that is inside
 * @param prop2 the outer property
 */
void matrix_record( matrix *m, UChar *prop1, UChar *prop2 )
{
    int index1 = hashset_get( m->lookup, prop1 )-1;
    int index2 = hashset_get( m->lookup, prop2 )-1;
    if ( index1 != -1 && index2 != -1 )
    {
        m->cells[m->n_props*index1+index2]++;
    }
}
Example #3
0
/**
 * Forbid property 1 to be inside property 2. Do this after matrix init
 * @param m the matrix in question
 * @param prop1 this can't be inside prop2
 * @param prop2 the outer property that may NOT contain prop1
 */
static void matrix_forbid( matrix *m, UChar *prop1, UChar *prop2 )
{
    if ( m->inited )
    {
        int index1 = hashset_get(m->lookup, prop1)-1;
        int index2 = hashset_get(m->lookup, prop2)-1;
        int cell_index = m->n_props*index1+index2;
        m->cells[cell_index] = 0;
    }
    else
        warning("initialise matrix first\n");
}
/**
 * Allow property 1 to be inside property 2. Do this after matrix init
 * @param m the matrix in question
 * @param prop1 this can be inside prop2
 * @param prop2 the outer property that may contain prop1
 */
static void matrix_allow( matrix *m, char *prop1, char *prop2 )
{
    if ( m->inited )
    {
        int index1 = hashset_get(m->lookup, prop1)-1;
        int index2 = hashset_get(m->lookup, prop2)-1;
        int cell_index = m->n_props*index1+index2;
        if ( m->cells[cell_index]==0 )
            m->cells[cell_index] = 1;
    }
    else
        warning("initialise matrix first\n");
}
Example #5
0
bool hstr_regexp_match(
		HstrRegexp *hstrRegexp,
		const char *regexp,
		const char *text,
		regmatch_t *match,
		char *errorMessage,
		const size_t errorMessageSize)
{
	regex_t* compiled;
	if(hashset_contains(&hstrRegexp->cache,regexp)) {
		compiled=hashset_get(&hstrRegexp->cache, regexp);
	} else {
		compiled=malloc(sizeof(regex_t));
		int compilationFlags=(hstrRegexp->caseSensitive?0:REG_ICASE);
		int compilationStatus=regcomp(compiled, regexp, compilationFlags);
		if(!compilationStatus) {
			hashset_put(&hstrRegexp->cache, hstr_strdup(regexp), compiled);
		} else {
			regerror(compilationStatus, compiled, errorMessage, errorMessageSize);
			free(compiled);
			return false;
		}
	}

	int matches=REGEXP_MATCH_BUFFER_SIZE;
	regmatch_t matchPtr[REGEXP_MATCH_BUFFER_SIZE];
	int matchingFlags=0;
	int matchingStatus=regexec(compiled, text, matches, matchPtr, matchingFlags);
	if(!matchingStatus) {
		if(matchPtr[0].rm_so != -1) {
			match->rm_so=matchPtr[0].rm_so;
			match->rm_eo=matchPtr[0].rm_eo;
			return true;
		}
	}
	return false;
}
Example #6
0
HistoryItems *get_prioritized_history()
{
	using_history();

	char *historyFile = get_history_file_name();
	if(read_history(historyFile)!=0) {
		fprintf(stderr, "\nUnable to read history file from '%s'!\n",historyFile);
		exit(EXIT_FAILURE);
	}
	HISTORY_STATE *historyState=history_get_history_state();

	if(historyState->length > 0) {
		HashSet rankmap;
		hashset_init(&rankmap);

		HashSet blacklist;
		int i;
		hashset_init(&blacklist);
		for(i=0; i<4; i++) {
			hashset_add(&blacklist, commandBlacklist[i]);
		}

		RadixSorter rs;
		radixsort_init(&rs, 100000);

		RankedHistoryItem *r;
		RadixItem *radixItem;
        HIST_ENTRY **historyList=history_list();
        char **rawHistory=malloc(sizeof(char*) * historyState->length);
        int rawOffset=historyState->length-1;
		char *line;
		for(i=0; i<historyState->length; i++, rawOffset--) {
			line=historyList[i]->line;
			rawHistory[rawOffset]=line;
			if(hashset_contains(&blacklist, line)) {
				continue;
			}
			if((r=hashset_get(&rankmap, line))==NULL) {
				r=malloc(sizeof(RankedHistoryItem));
				r->rank=HISTORY_RANKING_FUNCTION(0, i, strlen(line));
				r->item=historyList[i]->line;

				hashset_put(&rankmap, line, r);

				radixItem=malloc(sizeof(RadixItem));
				radixItem->key=r->rank;
				radixItem->data=r;
				radixItem->next=NULL;
				radixsort_add(&rs, radixItem);
			} else {
				radixItem=radix_cut(&rs, r->rank, r);

				assert(radixItem);

				if(radixItem) {
					r->rank=HISTORY_RANKING_FUNCTION(r->rank, i, strlen(line));
					radixItem->key=r->rank;
					radixsort_add(&rs, radixItem);
				}
			}
		}

		DEBUG_RADIXSORT();

		RadixItem **prioritizedRadix=radixsort_dump(&rs);
		prioritizedHistory=malloc(sizeof(HistoryItems));
		prioritizedHistory->count=rs.size;
		prioritizedHistory->items=malloc(rs.size * sizeof(char*));
		prioritizedHistory->raw=rawHistory;
		for(i=0; i<rs.size; i++) {
			if(prioritizedRadix[i]->data) {
				prioritizedHistory->items[i]=((RankedHistoryItem *)(prioritizedRadix[i]->data))->item;
			}
			free(prioritizedRadix[i]->data);
			free(prioritizedRadix[i]);
		}

		radixsort_destroy(&rs);

		return prioritizedHistory;
	} else {
		return NULL;
	}
}