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.


In reply to Re: C vs perl by broquaint
in thread C vs perl by mandog

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.