home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / extra / ustdio / ufile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  7.4 KB  |  306 lines

  1. /*
  2. *******************************************************************************
  3. *                                                                             *
  4. * COPYRIGHT:                                                                  *
  5. *   (C) Copyright International Business Machines Corporation, 1998           *
  6. *   Licensed Material - Program-Property of IBM - All Rights Reserved.        *
  7. *   US Government Users Restricted Rights - Use, duplication, or disclosure   *
  8. *   restricted by GSA ADP Schedule Contract with IBM Corp.                    *
  9. *                                                                             *
  10. *******************************************************************************
  11. *
  12. * File ufile.c
  13. *
  14. * Modification History:
  15. *
  16. *   Date        Name        Description
  17. *   11/19/98    stephen     Creation.
  18. *   03/12/99    stephen     Modified for new C API.
  19. *   06/16/99    stephen     Changed T_LocaleBundle to u_locbund
  20. *   07/19/99    stephen     Fixed to use ucnv's default codepage.
  21. *******************************************************************************
  22. */
  23.  
  24. #include "uhash.h"
  25. #include "ustdio.h"
  26. #include "ufile.h"
  27. #include "uloc.h"
  28. #include "loccache.h"
  29.  
  30. #include <string.h>
  31. #include <stdlib.h>
  32.  
  33.  
  34. /* the data in the following two functions should REALLY be somewhere else */
  35. /* convert from a country code (only the first 2 chars are significant)  */
  36. /* to an IBM codepage */
  37.  
  38. /* thanks to http://czyborra.com/charsets/iso8859.htm for most of this info */
  39. static const char ufile_locale2codepage[][256] = {
  40.   "af", "latin-1",  /* Afrikaans */
  41.   "ar", "ibm-1256", /* arabic */
  42.  
  43.   "be", "ibm-915"  , /*  Byelorussian */
  44.   "bg", "ibm-915"  , /* Bulgarian */
  45.     
  46.   "ca", "latin-1"  , /* catalan */
  47.   "cs", "ibm-912"  , /* Czech */
  48.   
  49.   "da", "latin-1"  ,  /* danish */
  50.   "de", "latin-1"  ,  /* german */
  51.   
  52.   "el", "ibm-813"  , /* Greek */
  53.   "en", "latin-1"  ,  /* English */
  54.   "eo", "ibm-913"  , /* Esperanto */
  55.   "es", "latin-1"  ,  /* Spanish */
  56.   "eu", "latin-1"  , /* basque */
  57.   "et", "ibm-914"  , /* Estonian  */
  58.   
  59.   "fi", "latin-1"  ,  /* Finnish */
  60.   "fo", "latin-1"  ,  /* faroese */
  61.   "fr", "latin-1"  ,  /* French */
  62.   
  63.   "ga", "latin-1"  ,  /* Irish (Gaelic) */
  64.   "gd", "latin-1"  ,  /* Scottish */
  65.   
  66.   "hr", "ibm-912"  , /* Croatian */
  67.   "hu", "ibm-912"  , /* Hungarian */
  68.   
  69.   "in", "latin-1"  , /* Indonesian */
  70.   "is", "latin-1"  ,  /* Icelandic */
  71.   "it", "latin-1"  ,  /* Italian  */
  72.   "iw", "ibm-916", /* hebrew */
  73.   
  74.   "ja", "ibm-943", /* Japanese */
  75.   "ji", "ibm-916", /* Yiddish */
  76.   
  77.   "kl", "ibm-914", /* Greenlandic */
  78.   "ko", "ibm-949", /* korean  */
  79.   
  80.   "lt", "ibm-914", /* Lithuanian */
  81.   "lv", "ibm-914", /* latvian (lettish) */
  82.   
  83.   "mk", "ibm-915"  , /* Macedonian */
  84.   "mt", "ibm-1208"  , /* Maltese [UTF8] */
  85.   
  86.   "nl", "latin-1"  ,  /* dutch */
  87.   "no", "latin-1"  ,  /* Norwegian */
  88.   
  89.   "pl", "ibm-912"  , /* Polish */
  90.   "pt", "latin-1"  ,  /* Portugese */
  91.   
  92.   "rm", "latin-1"  ,  /* Rhaeto-romanic (??) */
  93.   "ro", "ibm-912"  , /* Romanian */
  94.   "ru", "ibm-878"  , /* Russian */
  95.   
  96.   "sk", "ibm-912"  , /* Slovak */
  97.  
  98.   "sl", "ibm-912"  , /* Slovenian */
  99.   "sq", "latin-1"  ,  /* albanian */
  100.   "sr", "ibm-915"  , /* Serbian */
  101.   "sv", "latin-1"  ,  /* Swedish */
  102.   "sw", "latin-1"  ,  /* Swahili */
  103.   
  104.   "th", "ibm-1208" , /* Thai - UTF8 */
  105.   
  106.   "tr", "ibm-920",  /* Turkish */
  107.   
  108.   "uk", "ibm-915"  , /* pre 1990 Ukranian (?) */
  109.   
  110.   "zh", "Big-5",  /* Chinese */
  111.   0,    0 
  112. };
  113.  
  114. static const char* 
  115. ufile_lookup_codepage(const char *locale)
  116.   int32_t i;
  117.   for(i = 0; ufile_locale2codepage[i][0]; i+= 2)
  118.     if( ! strncmp(ufile_locale2codepage[i], locale, 2))
  119.       return ufile_locale2codepage[i + 1];
  120.   return 0;
  121. }
  122.  
  123.  
  124. UFILE*
  125. u_fopen(const char    *filename,
  126.     const char    *perm,
  127.     const char    *locale,
  128.     const char    *codepage)
  129. {
  130.   UErrorCode     status = U_ZERO_ERROR;
  131.   bool_t     useSysCP = (locale == 0 && codepage == 0);
  132.   UFILE     *result = (UFILE*) malloc(sizeof(UFILE));
  133.   if(result == 0)
  134.     return 0;
  135.  
  136.   result->fFile = fopen(filename, perm);
  137.   if(result->fFile == 0) {
  138.     free(result);
  139.     return 0;
  140.   }
  141.  
  142.   result->fOwnFile = TRUE;
  143.  
  144.   /* if locale is 0, use the default */
  145.   if(locale == 0)
  146.     locale = uloc_getDefault();
  147.  
  148.   result->fBundle = u_loccache_get(locale);
  149.   if(result->fBundle == 0) {
  150.     fclose(result->fFile);
  151.     free(result);
  152.     return 0;
  153.   }
  154.  
  155.   result->fOwnBundle     = FALSE;
  156.   result->fUCPos     = result->fUCBuffer;
  157.   result->fUCLimit     = result->fUCBuffer;
  158.  
  159.   /* if the codepage is 0, use the default for the locale */
  160.   if(codepage == 0) {
  161.     codepage = ufile_lookup_codepage(locale);
  162.   
  163.     /* if the codepage is still 0, the default codepage will be used */
  164.   }
  165.   
  166.   /* if both locale and codepage are 0, use the system default codepage */
  167.   else if(useSysCP)
  168.     codepage = 0;
  169.  
  170.   result->fConverter = ucnv_open(codepage, &status);
  171.   if(U_FAILURE(status) || result->fConverter == 0) {
  172.     fclose(result->fFile);
  173.     free(result);
  174.     return 0;
  175.   }
  176.  
  177.   return result;
  178. }
  179.  
  180. UFILE*
  181. u_finit(FILE        *f,
  182.     const char    *locale,
  183.     const char    *codepage)
  184. {
  185.   UErrorCode     status         = U_ZERO_ERROR;
  186.   bool_t     useSysCP     = (locale == 0 && codepage == 0);
  187.   UFILE     *result     = (UFILE*) malloc(sizeof(UFILE));
  188.   if(result == 0)
  189.     return 0;
  190.  
  191.   result->fFile = f;
  192.   result->fOwnFile = FALSE;
  193.  
  194.   /* if locale is 0, use the default */
  195.   if(locale == 0)
  196.     locale = uloc_getDefault();
  197.  
  198.   result->fBundle = u_loccache_get(locale);
  199.   if(result->fBundle == 0) {
  200.     fclose(result->fFile);
  201.     free(result);
  202.     return 0;
  203.   }
  204.  
  205.   result->fOwnBundle     = FALSE;
  206.   result->fUCPos     = result->fUCBuffer;
  207.   result->fUCLimit     = result->fUCBuffer;
  208.  
  209.   /* if the codepage is 0, use the default for the locale */
  210.   if(codepage == 0) {
  211.     codepage = ufile_lookup_codepage(locale);
  212.   
  213.     /* if the codepage is still 0, the default codepage will be used */
  214.   }
  215.   
  216.   /* if both locale and codepage are 0, use the system default codepage */
  217.   else if(useSysCP)
  218.     codepage = 0;
  219.  
  220.   result->fConverter = ucnv_open(codepage, &status);
  221.   if(U_FAILURE(status) || result->fConverter == 0) {
  222.     fclose(result->fFile);
  223.     free(result);
  224.     return 0;
  225.   }
  226.  
  227.   return result;
  228. }
  229.  
  230. void
  231. u_fclose(UFILE *file)
  232. {
  233.   if(file->fOwnFile)
  234.     fclose(file->fFile);
  235.  
  236.   if(file->fOwnBundle)
  237.     u_locbund_delete(file->fBundle);
  238.  
  239.   ucnv_close(file->fConverter);
  240.  
  241.   free(file);
  242. }
  243.  
  244. FILE*
  245. u_fgetfile(    UFILE         *f)
  246. {
  247.   return f->fFile;
  248. }
  249.  
  250. const char*
  251. u_fgetlocale(    UFILE        *file)
  252. {
  253.   return file->fBundle->fLocale;
  254. }
  255.  
  256. int32_t
  257. u_fsetlocale(const char        *locale,
  258.          UFILE        *file)
  259. {
  260.   if(file->fOwnBundle)
  261.     u_locbund_delete(file->fBundle);
  262.  
  263.   file->fBundle     = u_loccache_get(locale);
  264.   file->fOwnBundle     = FALSE;
  265.  
  266.   return file->fBundle == 0 ? -1 : 0;
  267. }
  268.  
  269. const char*
  270. u_fgetcodepage(UFILE        *file)
  271. {
  272.   UErrorCode     status = U_ZERO_ERROR;
  273.   const char     *codepage;
  274.  
  275.   codepage = ucnv_getName(file->fConverter, &status); 
  276.   if(U_FAILURE(status)) return 0;
  277.   return codepage;
  278. }
  279.  
  280. int32_t
  281. u_fsetcodepage(    const char    *codepage,
  282.         UFILE        *file)
  283. {
  284.   UErrorCode status = U_ZERO_ERROR;
  285.  
  286.   /* if the codepage is 0, use the default for the locale */
  287.   if(codepage == 0) {
  288.     codepage = ufile_lookup_codepage(file->fBundle->fLocale);
  289.   
  290.     /* if the codepage is still 0, fall back on the default codepage */
  291.   }
  292.  
  293.   ucnv_close(file->fConverter);
  294.   file->fConverter = ucnv_open(codepage, &status);
  295.   if(U_FAILURE(status))
  296.     return -1;
  297.   return 0;
  298. }
  299.  
  300.  
  301. UConverter * u_fgetConverter(UFILE *file)
  302. {
  303.   return file->fConverter;
  304. }
  305.