Hallo,
ich hab eine für mich sehr nützliche Sache gefunden in einem C Quelltext, diese hätte ich super gerne in Delphi, nur das übersteigt meine Fähigkeiten bei weitem.
Könnte jemand das ganze vielleict nach Delphi übersetzen das wär mehr als genial!
Code:
int match_pattern(const char *text, const char *search, int case_sensitive, int strict)
{
if (search && *search)
{
#ifdef __GNUC__
const char *stack[strlen(text) + 2];
#else
const char** stack= g_malloc(sizeof(char*) * ((unsigned int) strlen(text) + 2));
#endif
int stackp= 0;
const char *search_begin= search;
const char *tend= text+g_utf8_strlen(text, (gssize)strlen(text));
const char *send= search+g_utf8_strlen(text, (gssize)strlen(search));
gunichar (*normcase)(gunichar)= case_sensitive ? dummy : g_unichar_toupper;
while (*search && *text)
{
switch (*search)
{
case '*':
search= g_utf8_next_char(search);
if (*search != '?')
{
gunichar norm_search= normcase(*search);
stack[stackp++]= search;
while (*text && normcase(g_utf8_get_char_validated(text, (gssize)(tend - text))) != norm_search)
text= g_utf8_next_char(text);
break;
}
case '?':
search= g_utf8_next_char(search);
text= g_utf8_next_char(text);
break;
default:
if (normcase(g_utf8_get_char_validated(text, (gssize)(tend-search))) != normcase(g_utf8_get_char_validated(search, (gssize)(send-search))))
{
if (stackp==0)
{
return 0;
}
else
{
// backtrack
search= stack[--stackp];
while (*text && *search && normcase(g_utf8_get_char_validated(text, (gssize)(tend-text))) != normcase(g_utf8_get_char_validated(search, (gssize)(send-search))))
text= g_utf8_next_char(text);
}
}
else
{
search= g_utf8_next_char(search);
text= g_utf8_next_char(text);
}
break;
}
}
#ifndef __GNUC__
g_free(stack);
#endif
if (strict)
{
if (!*search)
{
if (search > search_begin && g_utf8_prev_char(search) && *g_utf8_prev_char(search) == '*')
return 1;
return *text ? 0 : 1;
}
else
return 0;
}
else
{
// If both search string and text string are at end then we found a match.
if (!*search && !*text)
return 1;
else
{
// If the search string still contains characters then text was exhausted already.
// In this case we match only if search contains one or more '*'.
if (!*search)
return 0;
else
{
while (*search == '*')
++search;
return !*search;
};
};
};
}
else
return -1;
}
Naja sofern das halt überhaupt möglich ist...
Vielen Dank
s!