Beispiel #1
0
void gui_paste_from_clipboard(struct gui_window *g, int x, int y)
{
	/* This and the other clipboard code is heavily based on the RKRM examples */
	struct ContextNode *cn;
	ULONG rlen=0,error;
	struct CSet cset;
	LONG codeset = 0;
	char *clip;
	STRPTR readbuf = AllocVec(1024,MEMF_PRIVATE | MEMF_CLEAR);

	cset.CodeSet = 0;

	if(OpenIFF(iffh,IFFF_READ)) return;
	if(StopChunk(iffh,ID_FTXT,ID_CHRS)) return;
	if(StopChunk(iffh,ID_FTXT,ID_CSET)) return;

	while(1)
	{
		error = ParseIFF(iffh,IFFPARSE_SCAN);
		if(error == IFFERR_EOC) continue;
		else if(error) break;

		cn = CurrentChunk(iffh);

		if((cn)&&(cn->cn_Type == ID_FTXT)&&(cn->cn_ID == ID_CSET))
		{
			rlen = ReadChunkBytes(iffh,&cset,32);
			if(cset.CodeSet == 1) codeset = 106;
				else codeset = cset.CodeSet;
		}

		if((cn)&&(cn->cn_Type == ID_FTXT)&&(cn->cn_ID == ID_CHRS))
		{
			while((rlen = ReadChunkBytes(iffh,readbuf,1024)) > 0)
			{
				if(codeset == 0)
				{
					utf8_from_local_encoding(readbuf,rlen,&clip);
				}
				else
				{
					utf8_from_enc(readbuf,
						(const char *)ObtainCharsetInfo(DFCS_NUMBER,
										codeset, DFCS_MIMENAME),
						rlen, &clip);
				}

				browser_window_paste_text(g->shared->bw,clip,rlen,true);
			}
			if(rlen < 0) error = rlen;
		}
	}
	CloseIFF(iffh);
}
Beispiel #2
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);
}
Beispiel #3
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();
}
Beispiel #4
0
char *ami_clipboard_cat_collection(struct CollectionItem *ci, LONG codeset, size_t *text_length)
{
	struct CollectionItem *ci_new = NULL, *ci_next, *ci_curr = ci;
	size_t len = 0;
	char *text = NULL, *p;

	/* Scan the collected chunks to find out the total size.
	 * If they are not in UTF-8, convert the chunks first and create a new CollectionItem list.
	 */
	do {
		switch(codeset) {
			case 106:
				len += ci_curr->ci_Size;
			break;
			
			case 0:
				if(ci_new) {
					ci_next->ci_Next = AllocVecTags(sizeof(struct CollectionItem), AVT_ClearWithValue, 0, TAG_DONE);
					ci_next = ci_next->ci_Next;
				} else {
					ci_new = AllocVecTags(sizeof(struct CollectionItem), AVT_ClearWithValue, 0, TAG_DONE);
					ci_next = ci_new;
				}
				
				utf8_from_local_encoding(ci_curr->ci_Data, ci_curr->ci_Size, (char **)&ci_next->ci_Data);
				ci_next->ci_Size = strlen(ci_next->ci_Data);
				len += ci_next->ci_Size;
			break;

			default:
				if(ci_new) {
					ci_next->ci_Next = AllocVecTags(sizeof(struct CollectionItem), AVT_ClearWithValue, 0, TAG_DONE);
					ci_next = ci_next->ci_Next;
				} else {
					ci_new = AllocVecTags(sizeof(struct CollectionItem), AVT_ClearWithValue, 0, TAG_DONE);
					ci_next = ci_new;
				}
				
				utf8_from_enc(ci_curr->ci_Data,
						(const char *)ObtainCharsetInfo(DFCS_NUMBER,
										codeset, DFCS_MIMENAME),
					      ci_curr->ci_Size, (char **)&ci_next->ci_Data, NULL);
				ci_next->ci_Size = strlen(ci_next->ci_Data);
				len += ci_next->ci_Size;
			break;
		}
	} while (ci_curr = ci_curr->ci_Next);

	text = malloc(len);

	if(text == NULL) return NULL;

	/* p points to the end of the buffer. This is because the chunks are
	 * in the list in reverse order. */
	p = text + len;

	if(ci_new) {
		ci_curr = ci_new;
	} else {
		ci_curr = ci;
	}

	do {
		p -= ci_curr->ci_Size;
		memcpy(p, ci_curr->ci_Data, ci_curr->ci_Size);
		ci_next = ci_curr->ci_Next;
		
		if(ci_new) {
			free(ci_curr->ci_Data);
			FreeVec(ci_curr);
		}
	} while (ci_curr = ci_next);

	*text_length = len;
	return text;
}