/* This file is part of mailfrom filter. -*- c -*- Copyright (C) 2006 Sergey Poznyakoff This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ MF_DEFUN(toupper, STRING, STRING string) { char *s = MF_COPY_STRING(string); char *p; for (p = s; *p; p++) *p = toupper(*p); MF_RETURN(s); } END MF_DEFUN(tolower, STRING, STRING string) { char *s = MF_COPY_STRING(string); char *p; for (p = s; *p; p++) *p = tolower(*p); MF_RETURN(s); } END MF_DEFUN(length, NUMBER, STRING string) { MF_RETURN(strlen(string)); } END MF_DEFUN(substring, STRING, STRING string, NUMBER start, NUMBER end) { long len = strlen(string); char *s; if (end < 0) end = len - 1; if (end < start) { long t = end; end = start; start = t; } MF_ASSERT(start < len && end < len, mf_range, "argument out of range"); len = end - start + 1; s = MF_ALLOC_HEAP(len + 1); memcpy(s, string + start, len); s[len] = 0; MF_RETURN(s); } END MF_DEFUN(substr, STRING, STRING string, NUMBER start, OPTIONAL, NUMBER nbytes) { long len = strlen(string); char *s; MF_ASSERT(start >= 0, mf_range, "argument out of range"); if (nbytes == 0) nbytes = len - start; s = MF_ALLOC_HEAP(nbytes + 1); memcpy(s, string + start, nbytes); s[nbytes] = 0; MF_RETURN(s); } END MF_DEFUN(index, NUMBER, STRING str, STRING sub) { char *p = strstr(str, sub); MF_RETURN(p ? p - str : -1); } END MF_DEFUN(rindex, NUMBER, STRING str, STRING sub) { int slen, xlen, rc; char *p, *s, *x; char *temp; slen = strlen(str); xlen = strlen(sub); temp = MF_ALLOC_HEAP_TEMP(xlen + slen + 2); s = temp; x = s + slen + 1; /* Reverse str */ #define REV(v,s,l) \ l = strlen(s); \ v[l] = 0; \ for (p = v + l - 1; p >= v; p--, s++) \ *p = *s; REV(s,str,slen); REV(x,sub,xlen); p = strstr(s, x); if (p) rc = slen - (p - s + xlen); else rc = -1; MF_RETURN(rc); } END MF_DEFUN(revstr, STRING, STRING str) { int len = strlen(str); char *p; char *s = MF_ALLOC_HEAP(len + 1); s[len] = 0; for (p = s + len - 1; p >= s; p--, str++) *p = *str; MF_RETURN(s); } END MF_INIT