/[ipchat]/ipchat/src/misc.c
ViewVC logotype

Diff of /ipchat/src/misc.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.6 by beatk, Fri Mar 25 18:37:47 2005 UTC revision 1.7 by julioabg, Wed Apr 13 19:19:54 2005 UTC
# Line 34  Line 34 
34  #include <sys/socket.h>  #include <sys/socket.h>
35  #include <netinet/in.h>  #include <netinet/in.h>
36  #include <arpa/inet.h>  #include <arpa/inet.h>
37    #include <iconv.h>
38    #include <langinfo.h>
39  #include "common.h"  #include "common.h"
40  #include "misc.h"  #include "misc.h"
41  #include "user_iface.h"  #include "user_iface.h"
42    
   
43  #define BUF_SIZE 512  #define BUF_SIZE 512
44    
45    iconv_t cur_to_utf8 = (iconv_t) -1;
46    iconv_t utf8_to_cur = (iconv_t) -1;
47            
48  char *  char *
49  home_path (char *file)  home_path (char *file)
50  {  {
# Line 411  ipv4_to_string (ip_t ip) Line 415  ipv4_to_string (ip_t ip)
415          in.s_addr = htonl (ip);          in.s_addr = htonl (ip);
416          return inet_ntoa (in);          return inet_ntoa (in);
417  }  }
418    
419    int
420    utf8_encode (char *src, size_t mlen, char *utf8s, size_t utf8smlen)
421    {
422            size_t utf8l, len, r;
423            
424            utf8l = utf8smlen;
425            len = strnlen (src, mlen);
426            if (len < mlen)
427                    if (src[len] == '\0')
428                            len++;
429            
430            if (cur_to_utf8 != (iconv_t) -1)  {
431                    while (len > 0 && utf8smlen > 0) {
432                            r = iconv(cur_to_utf8, &src, &len, &utf8s, &utf8smlen);
433                            if (r==(size_t)-1 && errno==EILSEQ && utf8smlen>0) {
434                                    *utf8s++ = '?';
435                                    src++;
436                                    len--;
437                                    utf8smlen--;
438                                    ui_output_err ("%d", utf8smlen);
439                            }
440                            else
441                                    break;
442                            
443                            iconv(cur_to_utf8, NULL, NULL, NULL, NULL);
444                    }
445                    
446                    utf8l-=utf8smlen;
447            }
448            else {
449                    if (len < utf8smlen)
450                            utf8l=len;
451                    else    
452                            utf8l=utf8smlen;
453                    strncpy (utf8s, src, utf8l);
454            }
455            
456            return utf8l;
457    }
458    
459    int
460    utf8_decode (char *src, size_t mlen, char *dest, size_t destmlen)
461    {
462            size_t destl, len, r;
463            
464            destl = destmlen;
465            len = strnlen (src, mlen);
466            if (len < mlen)
467                    if (src[len] == '\0')
468                            len++;
469    
470            if (utf8_to_cur != (iconv_t) -1) {
471                    while (len > 0 && destmlen > 0) {
472                            r = iconv(utf8_to_cur,&src, &len, &dest, &destmlen);
473                            if (r==(size_t)-1 && errno==EILSEQ && destmlen>0) {
474                                    *dest++ = '?';
475                                    src++;
476                                    len--;
477                                    destmlen--;
478                            }
479                            else
480                                    break;
481                            
482                    iconv(utf8_to_cur, NULL, NULL, NULL, NULL);
483                    }
484            
485                    destl-=destmlen;
486            }
487            else {
488                    if (len < destmlen)  
489                            destl=len;
490                    else
491                            destl=destmlen;
492                    strncpy (dest, src, destl);
493            }
494            
495            return destl;
496    }
497    
498    int
499    get_encoding (void)
500    {
501            utf8_to_cur = iconv_open (nl_langinfo (CODESET), "UTF-8");      
502            cur_to_utf8 = iconv_open ("UTF-8", nl_langinfo (CODESET));
503    
504            if (utf8_to_cur == (iconv_t) -1 || cur_to_utf8 == (iconv_t) -1)
505                    return ERR;
506            
507            return OK;
508    }
509    

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.7

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26