Subversion Repositories Kolibri OS

Rev

Rev 5098 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #include <string.h>
  2. #include <stdio.h>
  3.  
  4. typedef int conv_t;
  5. typedef unsigned int ucs4_t;
  6.  
  7. typedef int iconv_t;
  8.  
  9.  
  10. /* Return code if invalid input after a shift sequence of n bytes was read.
  11.    (xxx_mbtowc) */
  12. #define RET_SHIFT_ILSEQ(n)  (-1-2*(n))
  13. /* Return code if invalid. (xxx_mbtowc) */
  14. #define RET_ILSEQ           RET_SHIFT_ILSEQ(0)
  15. /* Return code if only a shift sequence of n bytes was read. (xxx_mbtowc) */
  16. #define RET_TOOFEW(n)       (-2-2*(n))
  17.  
  18. /* Return code if invalid. (xxx_wctomb) */
  19. #define RET_ILUNI      -1
  20. /* Return code if output buffer is too small. (xxx_wctomb, xxx_reset) */
  21. #define RET_TOOSMALL   -2
  22.  
  23.  
  24. #define CP866 0
  25. #define CP1251 1
  26. #define CP1252 2
  27. #define KOI8_RU 3
  28. #define ISO8859_5 4
  29. #define UTF_8 5
  30.  
  31.  
  32. int strcmp (const char* a, const char* b)
  33. {
  34.         return (*a && *b && (*a == *b)) ? ((*(a+1) || *(b+1)) ? (strcmp(a+1, b+1)) : (0)) : ((*a > *b) ? (1) : (-1));
  35. }
  36.  
  37.  
  38.  
  39.  
  40. #include "cp866.h"
  41. #include "cp1251.h"
  42. #include "cp1252.h"
  43. #include "koi8_ru.h"
  44. #include "iso8859_5.h"
  45. #include "utf8.h"
  46.  
  47.  
  48. int encoding(const char *what) {
  49.        
  50.         if (!strcmp(what,"CP866")) return CP866;
  51.         if (!strcmp(what,"CP1251")) return CP1251;
  52.         if (!strcmp(what,"CP1252")) return CP1252;
  53.         if (!strcmp(what,"KOI8-RU")) return KOI8_RU;
  54.         if (!strcmp(what,"ISO8859-5")) return ISO8859_5;
  55.         if (!strcmp(what,"UTF-8")) return UTF_8;
  56.         return -1;
  57.        
  58. }
  59.  
  60.  
  61. iconv_t iconv_open(const char *tocode, const char *fromcode) {
  62.         int to, from;
  63.         if ((to=encoding(tocode))==-1) return -1;
  64.         if ((from=encoding(fromcode))==-1) return -1;
  65.         to=to<<16&0xFFFF0000;
  66.         from=from&0xFFFF;
  67.         return to+from;
  68.                
  69. }
  70.  
  71. size_t iconv(iconv_t cd, const char **inbuf, size_t *inbytesleft,
  72.         char **outbuf, size_t *outbytesleft)
  73. {
  74.         int n, to, from, count1,count2;
  75.         int pwc, converted,written;
  76.         int (*mbtowc)(conv_t, ucs4_t *, const unsigned char *, int);
  77.         int (*wctomb)(conv_t, ucs4_t *, const unsigned char *, int);
  78.        
  79.         char *str;
  80.         str=*outbuf;
  81.        
  82.         from=cd>>16;
  83.         to=cd&0xFFFF;
  84.                        
  85.         switch (from)
  86.         {
  87.                 case CP866: mbtowc=cp866_mbtowc; break;
  88.                 case CP1251: mbtowc=cp1251_mbtowc; break;
  89.                 case CP1252: mbtowc=cp1252_mbtowc; break;
  90.                 case ISO8859_5: mbtowc=iso8859_5_mbtowc; break;
  91.                 case KOI8_RU: mbtowc=koi8_ru_mbtowc; break;
  92.                 case UTF_8: mbtowc=utf8_mbtowc; break;
  93.                 default: return -2;
  94.         }
  95.                
  96.         switch (to)
  97.         {
  98.                 case CP866: wctomb=cp866_wctomb; break;
  99.                 case CP1251: wctomb=cp1251_wctomb; break;
  100.                 case CP1252: wctomb=cp1252_wctomb; break;
  101.                 case ISO8859_5: wctomb=iso8859_5_wctomb; break;
  102.                 case KOI8_RU: wctomb=koi8_ru_wctomb; break;
  103.                 case UTF_8: wctomb=utf8_wctomb; break;
  104.                 default: return -3;
  105.         }
  106.        
  107.         count1=0;
  108.         count2=0;
  109.                
  110.         while ( *inbytesleft>0 && *outbytesleft>1)
  111.         {
  112.                 n=1;
  113.                
  114.                 do {
  115.                 //converted= (utf8_mbtowc)(0,&pwc,((*inbuf)+count1),n);
  116.                 //      printf("%d\n",n);
  117.                 converted= (mbtowc)(0,&pwc,((*inbuf)+count1),n);
  118.                
  119.                 n++;
  120.                 }       while (converted==RET_TOOFEW(0));
  121.                
  122.                 if (converted<0) return -10;
  123.                 //written=  (cp866_wctomb)(0,str+count2,pwc,1);
  124.                 written=  (wctomb)(0,str+count2,pwc,1);
  125.                 if (written<0) written=0;//return -11;
  126.                                
  127.                 //printf("Conv:%d Wri:%d In:%d Out:%d UTF:%x UCS:%x 866:%s\n",converted, written, *inbytesleft,*outbytesleft,*((*inbuf)+count1),pwc, str);
  128.                
  129.                 (*inbytesleft)-=converted;
  130.                 (*outbytesleft)-=written;
  131.                 count1+=converted;
  132.                 count2+=written;
  133.         }
  134.         *(str+count2)='\0';
  135.                        
  136.         if (*inbytesleft>0 && *outbytesleft==0) return -12;
  137.         return 0;
  138. }
  139.  
  140.  
  141. /*
  142. int main()
  143. {
  144.         char *s;// ="вертолет";
  145.         char *z;
  146.         //unsigned int pwc;
  147.         iconv_t cd;
  148.         int in, out;
  149.        
  150.                 FILE *infile;
  151.                 char *fname = "file.txt";
  152.            
  153.                 infile = fopen(fname,"r");
  154.        
  155.         fseek(infile, 0, SEEK_END);
  156.         size_t file_size = ftell(infile);
  157.         rewind(infile);
  158.  
  159.         //printf ("LOL\n");
  160.  
  161.         char *buffer = (char*)malloc(file_size * sizeof(char));
  162.         if (buffer == NULL)
  163.         {
  164.                 fclose(infile);
  165.                 printf("Error allocating %d bytes.\n", file_size * sizeof(char));
  166.                 return -1;
  167.         }
  168.         size_t bytes_read = fread(buffer, sizeof(char), file_size, infile);
  169.         if (bytes_read != file_size)
  170.         {
  171.                 printf("Have read only %d bytes of %d.\n", bytes_read, file_size);
  172.                 free(buffer);
  173.                 fclose(infile);
  174.                 return -1;
  175.         }
  176.        
  177.         in=strlen(buffer);
  178.         z=malloc(in+1);
  179.        
  180.         out=in+1;
  181.         cd=iconv_open("CP1251","CP866");
  182. //      printf("%x\n",cd);
  183.         int t;
  184.         t=iconv(cd, &buffer, &in, &z, &out);
  185.         printf("\nResult: %d", t);
  186.         puts(z);
  187.         //for (;s<s+strlen(s);s++) {cp866_mbtowc (0,  &pwc, s, 1);printf("%c=%u\n",*s,pwc);}
  188. }
  189. */
  190.  
  191.  
  192. typedef struct
  193. {
  194.         char *name;
  195.         void *f;
  196. } export_t;
  197.  
  198. char szStart[]           = "START";
  199. char szVersion[]         = "version";
  200. char sziconv_open[]    = "iconv_open";
  201. char sziconv[]   = "iconv";
  202.  
  203. export_t EXPORTS[] __asm__("EXPORTS") =
  204. {
  205.         { szStart,       (void*)0x0 },
  206.         { szVersion,     (void*)0x00010001 },
  207.         { sziconv_open,  iconv_open    },
  208.         { sziconv,       iconv   },
  209.         { NULL,          NULL },
  210. };
  211.