Пример #1
0
nserror utf8_to_local_encoding(const char *string, size_t len, char **result)
{
	const char *encname = "ISO-8859-1";

#ifdef __amigaos4__
	LONG charset;

	charset = GetDiskFontCtrl(DFCTRL_CHARSET);
	encname = (const char *) ObtainCharsetInfo(DFCS_NUMBER, charset, DFCS_MIMENAME);
#endif

	return utf8_to_enc(string,encname,len,result);
}
Пример #2
0
static void getSystemCodeset(struct LibraryHeader *lib)
{
  struct codeset *foundCodeset = NULL;

  ENTER();

  // before we go any query the system via locale.library (which
  // might not be so accurate) we try different other means of
  // finding the codeset/charset of the system
  #if defined(__amigaos4__)
  if(foundCodeset == NULL)
  {
    LONG default_charset = GetDiskFontCtrl(DFCTRL_CHARSET);
    char *charset = (char *)ObtainCharsetInfo(DFCS_NUMBER, default_charset, DFCS_MIMENAME);

    foundCodeset = codesetsFind(&lib->codesets, charset);

    D(DBF_STARTUP, "%s system default codeset: '%s' (diskfont)", foundCodeset ? "found" : "not found", charset);
  }
  #endif

  if(foundCodeset == NULL)
  {
    char codepage[40];

    codepage[0] = '\0';
    if(GetVar("CODEPAGE", codepage, sizeof(codepage), 0) > 0)
    {
      D(DBF_STARTUP, "trying ENV:CODEPAGE '%s'", codepage);
      foundCodeset = codesetsFind(&lib->codesets, codepage);
    }

    D(DBF_STARTUP, "%s system default codeset: '%s' (ENV:CODEPAGE)", foundCodeset ? "found" : "did not find",
                                                                     foundCodeset ? foundCodeset->name : "?");
  }

  #if defined(__MORPHOS__)
  if(foundCodeset == NULL)
  {
    /* If CODEPAGE did not work (maybe user deleted it or something) we try a keymap instead. This only works
     * in MorphOS 2 and only if an old Amiga keymap is not used.
     */
    if(foundCodeset == NULL)
    {
      struct Library *KeymapBase;

      if((KeymapBase = OpenLibrary("keymap.library", 51)) != NULL)
      {
        /* Since we requested V51 it is either V51 or newer */
        if(KeymapBase->lib_Version > 51 || KeymapBase->lib_Revision >= 4)
        {
          CONST_STRPTR codepage;

          #ifndef GetKeyMapCodepage
          #define GetKeyMapCodepage(__p0) LP1(78, CONST_STRPTR , GetKeyMapCodepage, CONST struct KeyMap *, __p0, a0, , KEYMAP_BASE_NAME, 0, 0, 0, 0, 0, 0)
          #endif

          if((codepage = GetKeyMapCodepage(NULL)) != NULL)
          {
            foundCodeset = codesetsFind(&lib->codesets, codepage);

            D(DBF_STARTUP, "%s system default codeset: '%s' (keymap)", foundCodeset ? "found" : "did not find",
                                                                       foundCodeset ? foundCodeset->name : "?");
          }
        }

        CloseLibrary(KeymapBase);
      }
    }
  }
  #endif

  // if we still do not have our default charset we try to load
  // it from and environment variable ENVARC:CHARSET
  if(foundCodeset == NULL)
  {
    char charset[80];

    charset[0] = '\0';
    if(GetVar("CHARSET", charset, sizeof(charset), 0) > 0)
    {
      D(DBF_STARTUP, "trying ENV:CHARSET '%s'", charset);
      foundCodeset = codesetsFind(&lib->codesets, charset);
    }

    D(DBF_STARTUP, "%s system default codeset: '%s' (ENV:CHARSET)", foundCodeset ? "found" : "did not find",
                                                                    foundCodeset ? foundCodeset->name : "?");
  }

  // try the country code next
  if(foundCodeset == NULL)
  {
    struct Locale *defaultLocale;

    if((defaultLocale = OpenLocale(NULL)) != NULL)
    {
      int i;
      const struct loc *curLoc = NULL;
      BOOL found = FALSE;

      for(i=0;;i++)
      {
        curLoc = &locs[i];
        if(curLoc == NULL || curLoc->name == NULL)
          break;

        if(defaultLocale->loc_CountryCode == curLoc->carPlateCode || defaultLocale->loc_CountryCode == curLoc->iso3166Code)
        {
          D(DBF_STARTUP, "found country code for '%s'", curLoc->name);
          found = TRUE;
          break;
        }
      }

      if(found == TRUE)
        foundCodeset = codesetsFind(&lib->codesets, curLoc->codesetName);

      CloseLocale(defaultLocale);
    }

    D(DBF_STARTUP, "%s system default codeset: '%s' (locale/country)", foundCodeset ? "found" : "did not find",
                                                                       foundCodeset ? foundCodeset->name : "?");
  }

  // and if even the CHARSET environment variable didn't work
  // out we check the LANGUAGE env variable against our own
  // internal fallback list
  if(foundCodeset == NULL)
  {
    char language[80];

    language[0] = '\0';
    if(GetVar("LANGUAGE", language, sizeof(language), 0) > 0)
    {
      int i;
      const struct loc *curLoc = NULL;
      BOOL found = FALSE;

      for(i=0;;i++)
      {
        curLoc = &locs[i];
        if(curLoc == NULL || curLoc->name == NULL)
          break;

        if(Strnicmp(language, curLoc->name, curLoc->len) == 0)
        {
          D(DBF_STARTUP, "found language name for '%s'", curLoc->name);
          found = TRUE;
          break;
        }
      }

      if(found == TRUE)
        foundCodeset = codesetsFind(&lib->codesets, curLoc->codesetName);
    }

    D(DBF_STARTUP, "%s system default codeset: '%s' (ENV:LANGUAGE)", foundCodeset ? "found" : "did not find",
                                                                     foundCodeset ? foundCodeset->name : "?");
  }

  // and the very last check we do to find out the system's charset is
  // to use locale.library.
  if(foundCodeset == NULL)
  {
    struct Locale *locale;

    if((locale = OpenLocale(NULL)) != NULL)
    {
      int i;
      char *language = locale->loc_LanguageName;
      const struct loc *curLoc = NULL;
      BOOL found = FALSE;

      for(i=0;;i++)
      {
        curLoc = &locs[i];
        if(curLoc == NULL || curLoc->name == NULL)
          break;

        if(Strnicmp(language, curLoc->name, curLoc->len) == 0)
        {
          found = TRUE;
          break;
        }
      }

      CloseLocale(locale);

      if(found == TRUE)
        foundCodeset = codesetsFind(&lib->codesets, curLoc->codesetName);
    }

    D(DBF_STARTUP, "%s system default codeset: '%s' (locale/language)", foundCodeset ? "found" : "did not find",
                                                                        foundCodeset ? foundCodeset->name : "?");
  }

  // and if even that very last test didn't work out we
  // can just take the ISO-8859-1 charset as the fallback default
  if(foundCodeset == NULL)
    lib->systemCodeset = codesetsFind(&lib->codesets, "ISO-8859-1");
  else
    lib->systemCodeset = foundCodeset;

  D(DBF_STARTUP, "final system default codeset: '%s'", lib->systemCodeset ? lib->systemCodeset->name : "?");

  LEAVE();
}