[patch] use wchar_t wide characters in fcgi printf

Ronny Spiegel <[email protected]>
Newsgroups gmane.comp.web.fastcgi.devel
Message-ID <[email protected]>
Hi there,

I'm currently porting some FCGIs to unicode. I created an utf8 aware
string class that internally uses wchar_t* strings.

Libfcgi overwrites printf functions and is/was not able to deal with %ls
and %lc format strings.

Here is a patch that solves this problem and converts these strings to
multibyte strings. This is just a patch that does the converting, I do
not check if a wchar_t (or the wchar.h and wctype.h header files) exist
because I'm not so experienced in using automake / autoconf.

I hope it is useful nervertheless and will probably be included in any
further version.

Thanks for the audience :)

Ronny

___________________________________
fastcgi-developers mailing list
http://fastcgi.com/fastcgi-developers/
libfcgi-wchar.patch (text/x-patch, 4.3 KB)
*** fcgiapp.c	Thu Feb 10 15:46:15 2005
--- fcgiapp.c	Thu Feb 10 15:50:49 2005
***************
*** 24,29 ****
--- 24,31 ----
  #include <stdlib.h>
  #include <string.h>
  #include <sys/types.h>
+ #include <wchar.h>
+ #include <wctype.h>
  
  #include "fcgi_config.h"
  
***************
*** 415,420 ****
--- 417,423 ----
      int fastPath, n, auxBuffLen = 0, buffReqd, minWidth, precision, exp;
      char *auxBuffPtr = NULL;
      int streamCount = 0;
+     int dynamicBuffer = 0;
      char fmtBuff[FMT_BUFFLEN];
      char buff[PRINTF_BUFFLEN];
  
***************
*** 721,731 ****
                              buffCount = strlen(buffPtr);
                              break;
  	                case 'l':
!                             /*
!                              * XXX: Allowed by ISO C Amendment 1, but
!                              * many platforms don't yet support wint_t
!                              */
!                             goto ErrorReturn;
                      default:
                              goto ErrorReturn;
                      }
--- 724,740 ----
                              buffCount = strlen(buffPtr);
                              break;
  	                case 'l':
!                             {
!                               mbstate_t mbs = { 0 };
!                               wint_t wc = va_arg( arg, wint_t );
!                               size_t s = wcrtomb( buffPtr, (wchar_t) wc, &mbs );
!                               if( s && s != (size_t) -1 )
!                               {
!                                 buffPtr[s] = 0;
!                                 buffCount = s;
!                               }
!                             }
!                             break;         
                      default:
                              goto ErrorReturn;
                      }
***************
*** 744,754 ****
  			    break;
  	                case 'l':
                              /*
!                              * XXX: Don't know how to convert a sequence
!                              * of wide characters into a byte stream, or
!                              * even how to predict the buffering required.
                               */
!                             goto ErrorReturn;
                          default:
                              goto ErrorReturn;
                      }
--- 753,779 ----
  			    break;
  	                case 'l':
                              /*
!                              * convert a wide-character-string to a "normal" char string
!                              * and mark the buffer as dynamically allocated 
                               */
!                             { 
!                                 size_t s = 0;
!                                 wchar_t* wcarg = va_arg( arg, wchar_t* );
!                                 const wchar_t* srcptr = wcarg;
!                                 mbstate_t mbs = { 0 };
!                             
!                                 s = wcsrtombs( (char*) NULL, &srcptr, 0, &mbs );
!                                 if( s == (size_t) -1 )
!                                         goto ErrorReturn;
!   
!                                 buffPtr = (char*) malloc( (s + 1) * sizeof( char ) );
!                                 buffCount = s;
!                                 buffLen = buffCount + 1;
!                                 srcptr = wcarg;
!                                 wcsrtombs( buffPtr, &srcptr, s + 1, &mbs );
!                                 dynamicBuffer = 1;
!                             }
!                             break;
                          default:
                              goto ErrorReturn;
                      }
***************
*** 836,843 ****
--- 861,880 ----
          ASSERT(buffCount < buffLen);
          if(buffCount > 0) {
              if(FCGX_PutStr(buffPtr, buffCount, stream) < 0)
+             {
+                 if( dynamicBuffer )
+                 {
+                     dynamicBuffer = 0;
+                     free( buffPtr );
+                 }
                  goto ErrorReturn;
+ 	    }
              streamCount += buffCount;
+             if( dynamicBuffer )
+             {
+                 dynamicBuffer = 0;
+                 free( buffPtr );
+             }
          } else if(buffCount < 0) {
              goto ErrorReturn;
  	}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.