Example #1
0
	//strstr(BM法)用補助関数:パターンが1文字専用
	const char* strstrbm1(const char* str, const char pattern, std::function<bool(const char*, const char*)> found_it)
	{
		//検索開始
		const std::size_t str_len = GASHA_ strlen(str);
		const char* str_end_p = str + str_len;
		const char* str_p = str;
		while (str_p < str_end_p)
		{
			const char str_c = *str_p;
			if (str_c == pattern)
			{
				if (found_it(str_p, str))
					return str_p;
			}
			//パターンの途中の文字が不一致
			++str_p;
		}
		return nullptr;
	}
int scan_file(const char *filename, int use_or, int search_len, char *search_for[])
{
    char *line = calloc(MAX_LINE, 1);
    FILE *file = fopen(filename, "r");
    int found_count = 0;
    int i = 0;

    check_mem(line);
    check(file, "Failed to open file: %s", filename);

    // read each line of the file and search that line for the contents
    while(fgets(line, MAX_LINE-1, file) != NULL)
    {
        for(i = 0; i < search_len; i++) {
            if(strcasestr(line, search_for[i]) != NULL) {
                debug("file: %s, line: %s, search: %s", filename, line, search_for[i]);
                found_count++;
            }
        }

        if(found_it(use_or, found_count, search_len)) {
            printf("%s\n", filename);
            break;
        } else {
            found_count = 0;
        }
    }


    free(line);
    fclose(file);
    return 0;

error:
    if(line) free(line);
    if(file) fclose(file);

    return -1;
}
Example #3
0
/**
 * Writes a new "inkscape:path-effect" string to xml, where the old_lpeobjects are substituted by the new ones.
 *  Note that this method messes up the item's \c PathEffectList.
 */
void SPLPEItem::replacePathEffects( std::vector<LivePathEffectObject const *> const &old_lpeobjs,
                                    std::vector<LivePathEffectObject const *> const &new_lpeobjs )
{
    HRefList hreflist;
    for (PathEffectList::const_iterator it = this->path_effect_list->begin(); it != this->path_effect_list->end(); ++it)
    {
        LivePathEffectObject const * current_lpeobj = (*it)->lpeobject;
        std::vector<LivePathEffectObject const *>::const_iterator found_it(std::find(old_lpeobjs.begin(), old_lpeobjs.end(), current_lpeobj));

        if ( found_it != old_lpeobjs.end() ) {
            std::vector<LivePathEffectObject const *>::difference_type found_index = std::distance (old_lpeobjs.begin(), found_it);
            const gchar * repr_id = new_lpeobjs[found_index]->getRepr()->attribute("id");
            gchar *hrefstr = g_strdup_printf("#%s", repr_id);
            hreflist.push_back( std::string(hrefstr) );
            g_free(hrefstr);
        }
        else {
            hreflist.push_back( std::string((*it)->lpeobject_href) );
        }
    }

    std::string r = hreflist_write_svg(hreflist);
    this->getRepr()->setAttribute("inkscape:path-effect", r.c_str());
}
Example #4
0
//strstr(BM法)
const char* strstrbm(const char* str, const char* pattern, std::function<bool(const char*, const char*)> found_it)
{
//nullチェックしない
//	if (!str || !pattern)
//		return 0;
	//patternの長さに基づいて、処理を振り分ける
	if (*pattern == '\0')//パターンが0文字の時
		return str;
	if (*(pattern + 1) == '\0')//パターンが1文字の時
	{
		if (*str == '\0')
			return nullptr;
		return _private::strstrbm1(str, *pattern, found_it);
	}
	const std::size_t pattern_len = GASHA_ strlen(pattern);
	const std::size_t str_len = GASHA_ strlen(str);
	if (str_len < pattern_len)
		return nullptr;
	//パターン内の文字ごとに、照合失敗時のスキップ文字数を記録
	int skip[256];
	for (std::size_t i = 0; i < 256; ++i)
		skip[i] = static_cast<int>(pattern_len);
	for (std::size_t i = 0; i < pattern_len; ++i)
		skip[static_cast<unsigned char>(pattern[i])] = static_cast<int>(pattern_len) - static_cast<int>(i) - 1;
	//検索開始
	const std::size_t pattern_term = pattern_len - 1;
	const char* pattern_term_p = pattern + pattern_term;
	const char* pattern_term_1_p = pattern_term_p - 1;
	const char pattern_term_c = *pattern_term_p;
	const char* str_end_p = str + str_len;
	const char* str_p = str + pattern_len - 1;
	while (str_p < str_end_p)
	{
		const char str_c = *str_p;
		if (str_c == pattern_term_c)
		{
			//パターンの末尾の文字が一致 ... パターンを照合する
			const char* _str_p = str_p - 1;
			const char* _pattern_p = pattern_term_1_p;
			char _str_c = *_str_p;
			while (true)
			{
				if (_str_c != *_pattern_p)//パターン不一致
					break;
				if (_pattern_p == pattern)//パターン検出
				{
					if (found_it(_str_p, str))
						return _str_p;
					break;
				}
				--_pattern_p;
				--_str_p;
				_str_c = *_str_p;
			}
			//パターンの途中の文字が不一致 ... パターンの中に次の文字が見つかる位置まで移動
			_str_c = *(++str_p);
			const int _skip = skip[static_cast<unsigned char>(_str_c)];
			str_p += _skip;
		}
		else
		{
			//パターンの末尾の文字が不一致 ... パターンの中に str_c が見つかる位置まで移動
			const int _skip = skip[static_cast<unsigned char>(str_c)];
			str_p += _skip;
		}
	}
	return nullptr;
}