Exemple #1
0
void favorites_add(FavoriteItems* favorites, char* newFavorite)
{
    if(favorites->count) {
        favorites->items=realloc(favorites->items, sizeof(char*) * ++favorites->count);
        favorites->items[favorites->count-1]=hstr_strdup(newFavorite);
        favorites_choose(favorites, newFavorite);
    } else {
        favorites->items=malloc(sizeof(char*));
        favorites->items[0]=hstr_strdup(newFavorite);
        favorites->count=1;
    }

    favorites_save(favorites);
    hashset_add(favorites->set, newFavorite);
}
Exemple #2
0
void favorites_get(FavoriteItems* favorites)
{
    if(!favorites->loaded) {
        char* fileName = favorites_get_filename();
        char* fileContent = NULL;
        if(access(fileName, F_OK) != -1) {
            long inputFileSize;

            FILE* inputFile = fopen(fileName, "rb");
            fseek(inputFile, 0, SEEK_END);
            inputFileSize = ftell(inputFile);
            rewind(inputFile);
            fileContent = malloc((inputFileSize + 1) * (sizeof(char)));
            if(!fread(fileContent, sizeof(char), inputFileSize, inputFile)) {
                if(ferror(inputFile)) {
                    exit(EXIT_FAILURE);
                }
            }
            fclose(inputFile);
            fileContent[inputFileSize] = 0;

            if(fileContent && strlen(fileContent)) {
                favorites->count = 0;
                char* p=strchr(fileContent,'\n');
                while (p!=NULL) {
                    favorites->count++;
                    p=strchr(p+1,'\n');
                }

                favorites->items = malloc(sizeof(char*) * favorites->count);
                favorites->count = 0;
                char* pb=fileContent, *pe, *s;
                pe=strchr(fileContent, '\n');
                while(pe!=NULL) {
                    *pe=0;
                    if(!hashset_contains(favorites->set,pb)) {
                        if(!favorites->skipComments || !(strlen(pb) && pb[0]=='#')) {
                            s=hstr_strdup(pb);
                            favorites->items[favorites->count++]=s;
                            hashset_add(favorites->set,s);
                        }
                    }
                    pb=pe+1;
                    pe=strchr(pb, '\n');
                }
                free(fileContent);
            }
        } else {
            // favorites file not found > favorites don't exist yet
            favorites->loaded=true;
        }
        free(fileName);
    }
}
Exemple #3
0
void testBlacklist() {
	const char* commandBlacklist[] = { "a","b","c","d","e" };
	HashSet blacklist;
	int i;
	hashset_init(&blacklist);
	for (i = 0; i < 5; i++) {
		hashset_add(&blacklist, commandBlacklist[i]);
	}
	for (i = 0; i < 5; i++) {
		printf("match %d\n", hashset_contains(&blacklist, hstr_strdup(commandBlacklist[i])));
	}
}
Exemple #4
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;
}