That is some mighty fat
C. While I am all for people using
perl in situations like this I have to respect my elders and come in to defend
C's ability to be compact when it wants to be.
char* crlf2ptag(char* str) {
char *ret, *pstr, *pret;
/* ok, this could be done better but it's only a string ;-) */
ret = (char*) malloc(strlen(str) << 2 + 8);
strncpy(ret, "<p>", 3);
pstr = str;
pret = &ret[3];
while(*pstr != '\0')
if(*pstr == '\r' && *(pstr+1) != '\0' && *(pstr+1) == '\n') {
strncpy(pret, "</p><p>", 7);
pret += 7;
pstr += 2;
} else
*pret++ = *pstr++;
ret[strlen(ret) - 3] = '\0';
return ret;
}
This compiles under cygwin's gcc 2.95 in win98 and I'd be surprised if there were problems elsewhere (make sure you include
string.h though!). I'm sure this could be golfed to heck with even more pointer trickery, but my point is that the as long as you are a competent craftsmen it doesn't really matter what tools you use.
HTH
_________
broquaint
update: now checks for \r and \n to appease abstracts and should allocate enough space for all but the most \r\n ridden strings.