Subject Re: [firebird-support] UDF - another way?
Author Ivan Prenosil
> I'd rather implement this as an SP than a UDF, can anyone see a efficient way
> to do so?

Current implementation of SUBSTRING function does not allow
using variables for FROM and FOR clauses, so you can't do
such things using SP only.

Ivan


>
> char* EXPORT fn_titlecase(char* s)
> {
> char *buffer = (char *)malloc(256);
> short j = 0;
> char *pos1;
>
> while (*s == ' ') /* skip leading blanks */
> s++;
>
> *s = toupper(*s); /* uppercase first char */
>
> while (*s) /* copy the rest */
> {
> buffer[j++] = *s++;
> }
>
> buffer[j] = '\0';
>
> /* convert any character that follows a space and first char */
> for (pos1 = buffer, pos1 = strstr(pos1, " "); pos1 != NULL; pos1 =
> strstr(pos1, " "))
> {
> pos1 = pos1 + 1;
> *pos1 = toupper(*pos1);
> }
>
> /* convert character following special strings */
>
> for (pos1 = buffer, pos1 = strstr(pos1, "-"); pos1 != NULL; pos1 =
> strstr(pos1, "-"))
> {
> pos1 = pos1 + 1;
> *pos1 = toupper(*pos1);
> }
> for (pos1 = buffer, pos1 = strstr(pos1, "'"); pos1 != NULL; pos1 =
> strstr(pos1, "'"))
> {
> pos1 = pos1 + 1;
> if ( (pos1 - 3) < buffer || *(pos1 - 3) == ' ') /* only if
> preceeded by space, letter */
> *pos1 = toupper(*pos1);
> }
> for (pos1 = buffer, pos1 = strstr(pos1, "("); pos1 != NULL; pos1 =
> strstr(pos1, "("))
> {
> pos1 = pos1 + 1;
> *pos1 = toupper(*pos1);
> }
> for (pos1 = buffer, pos1 = strstr(pos1, "Mc"); pos1 != NULL; pos1 =
> strstr(pos1, "Mc"))
> {
> pos1 = pos1 + 2;
> *pos1 = toupper(*pos1);
> }
>
> buffer [strlen(s)-1] = '\0';
> return buffer;
> }