]> code.citadel.org Git - citadel.git/blobdiff - webcit/html2html.c
* fix pointer magic
[citadel.git] / webcit / html2html.c
index 609ebe038e4294810b323d8e0b7a915d46e82813..7d3e597ae2b07a0229e491f1855b51c6ce14995a 100644 (file)
@@ -82,7 +82,7 @@ void extract_charset_from_meta(char *charset, char *meta_http_equiv, char *meta_
  *
  * \param supplied_charset the input charset as declared in the MIME headers
  */
-void output_html(char *supplied_charset, int treat_as_wiki, int msgnum) {
+void output_html(const char *supplied_charset, int treat_as_wiki, int msgnum, StrBuf *Source, StrBuf *Target) {
        char buf[SIZ];
        char *msg;
        char *ptr;
@@ -108,21 +108,23 @@ void output_html(char *supplied_charset, int treat_as_wiki, int msgnum) {
        size_t obuflen;               /**< Length of output buffer              */
        char *osav;                   /**< Saved pointer to output buffer       */
 #endif
+       if (Target == NULL)
+               Target = WC->WBuf;
 
        safestrncpy(charset, supplied_charset, sizeof charset);
        msg = strdup("");
        sprintf(new_window, "<a target=\"%s\" href=", TARGET);
 
-       while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
+       if (Source == NULL) while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
                line_length = strlen(buf);
                buffer_length = content_length + line_length + 2;
                ptr = realloc(msg, buffer_length);
                if (ptr == NULL) {
-                       wprintf("<b>");
-                       wprintf(_("realloc() error! couldn't get %d bytes: %s"),
+                       StrBufAppendPrintf(Target, "<b>");
+                       StrBufAppendPrintf(Target, _("realloc() error! couldn't get %d bytes: %s"),
                                buffer_length + 1,
                                strerror(errno));
-                       wprintf("</b><br /><br />\n");
+                       StrBufAppendPrintf(Target, "</b><br /><br />\n");
                        while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
                                /** flush */
                        }
@@ -135,6 +137,12 @@ void output_html(char *supplied_charset, int treat_as_wiki, int msgnum) {
                strcpy(&msg[content_length], "\n");
                content_length += 1;
        }
+       else {
+               content_length = StrLength(Source);
+               free(msg);
+               msg = (char*) ChrPtr(Source);/* TODO: remove cast */
+               buffer_length = content_length;
+       }
 
        /** Do a first pass to isolate the message body */
        ptr = msg + 1;
@@ -227,6 +235,10 @@ void output_html(char *supplied_charset, int treat_as_wiki, int msgnum) {
                strcpy(msg, msgstart);
        }
 
+       /** Now go through the message, parsing tags as necessary. */
+       converted_msg = NewStrBufPlain(NULL, content_length + 8192);
+
+
        /** Convert foreign character sets to UTF-8 if necessary. */
 #ifdef HAVE_ICONV
        if ( (strcasecmp(charset, "us-ascii"))
@@ -234,25 +246,37 @@ void output_html(char *supplied_charset, int treat_as_wiki, int msgnum) {
           && (strcasecmp(charset, ""))
        ) {
                lprintf(9, "Converting %s to UTF-8\n", charset);
-               ic = ctdl_iconv_open("UTF-8", charset);
+               ctdl_iconv_open("UTF-8", charset, &ic);
                if (ic == (iconv_t)(-1) ) {
                        lprintf(5, "%s:%d iconv_open() failed: %s\n",
                                __FILE__, __LINE__, strerror(errno));
                }
        }
-       if (ic != (iconv_t)(-1) ) {
-               ibuf = msg;
-               ibuflen = content_length;
-               obuflen = content_length + (content_length / 2) ;
-               obuf = (char *) malloc(obuflen);
-               osav = obuf;
-               iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
-               content_length = content_length + (content_length / 2) - obuflen;
-               osav[content_length] = 0;
-               free(msg);
-               msg = osav;
-               iconv_close(ic);
+       if  (Source == NULL) {
+               if (ic != (iconv_t)(-1) ) {
+                       ibuf = msg;
+                       ibuflen = content_length;
+                       obuflen = content_length + (content_length / 2) ;
+                       obuf = (char *) malloc(obuflen);
+                       osav = obuf;
+                       iconv(ic, &ibuf, &ibuflen, &obuf, &obuflen);
+                       content_length = content_length + (content_length / 2) - obuflen;
+                       osav[content_length] = 0;
+                       free(msg);
+                       msg = osav;
+                       iconv_close(ic);
+               }
+       }
+       else {
+               if (ic != (iconv_t)(-1) ) {
+                       StrBuf *Buf = NewStrBufPlain(NULL, StrLength(Source) + 8096);;
+                       StrBufConvert(Source, Buf, &ic);
+                       FreeStrBuf(&Buf);
+                       iconv_close(ic);
+                       msg = (char*)ChrPtr(Source); //TODO: get rid of this.
+               }
        }
+               
 #endif
 
        /**
@@ -263,10 +287,8 @@ void output_html(char *supplied_charset, int treat_as_wiki, int msgnum) {
         *      terminated now.
         */
 
-       /** Now go through the message, parsing tags as necessary. */
-       converted_msg = NewStrBufPlain(NULL, content_length + 8192);
        if (converted_msg == NULL) {
-               wprintf("Error %d: %s<br />%s:%d", errno, strerror(errno), __FILE__, __LINE__);
+               StrBufAppendPrintf(Target, "Error %d: %s<br />%s:%d", errno, strerror(errno), __FILE__, __LINE__);
                goto BAIL;
        }
 
@@ -448,14 +470,14 @@ void output_html(char *supplied_charset, int treat_as_wiki, int msgnum) {
        /**     output_length = content_length;                         */
 
        /** Output our big pile of markup */
-       StrBufAppendBuf(WC->WBuf, converted_msg, 0);
+       StrBufAppendBuf(Target, converted_msg, 0);
 
 BAIL:  /** A little trailing vertical whitespace... */
-       wprintf("<br /><br />\n");
+       StrBufAppendPrintf(Target, "<br /><br />\n");
 
        /** Now give back the memory */
-       if (converted_msg != NULL) free(converted_msg);
-       if (msg != NULL) free(msg);
+       FreeStrBuf(&converted_msg);
+       if ((msg != NULL) && (Source == NULL)) free(msg);
 }
 
 /*@}*/