Hi,

  I wrote a test that don't even use IUP, just to test fopen with UTF-8. It
is attached. I found out that it worked using setlocale only in Visual
Studio 2017. It seems to be a new feature. I decide to describe this in the
IUP documentation:

---------------------------------------------------------------------------------------------

Notice that IUP, CD and IM libraries use the *fopen* based functions to
read and write files. In Windows *fopen* expects the filename string in the
*ANSI* encoding by default. If your filename, including the path, has
characters that can not be converted to ANSI, *fopen* will fail to open the
file. In Windows we could use *_wfopen* combined with UTF-8, but this is a
Microsoft only function and most of *fopen* usage in these libraries are in
portable modules. *This is an IUP limitation in Windows.*

The simple workaround is to not use special characters in folders or files
name in Windows... Legacy applications will also have the same problem.

Another option is to call:

 setlocale(LC_ALL, ".UTF8");

But it will work for *fopen* only in Visual Studio 2017 or newer Microsoft
compilers (*setlocale* will return NULL on other compilers). *fopen* will
successfully open the file if filename is an UTF-8 string, even with
special characters. So you will be able to set both UTF8MODE and
UTF8MODE_FILE to YES.

If you decide to use this feature, another interesting option is to set the
console code page to UTF-8 executing "chcp 65001" on the command line. This
will allow your *printf* output to be properly displayed when using UTF-8
strings. This feature actually works for all Microsoft compilers in
Windows, and for MingW, even when *setlocale* returns NULL. Notice that
some font packages must be installed for this to fully work for all
characters (for instance Chinese, Japanese and Korean, along with some
symbols too).

---------------------------------------------------------------------------------------------

  Yes, this is all an IUP limitation because its external API do not
support Unicode.

  I also fixed a bug in IupConfig to handle the case where the system
folder has special characters, but they can be converted to ANSI. I was not
doing that conversion. Just committed to the SVN.

Best,
Scuri


Em ter., 11 de fev. de 2020 às 22:14, Andrew Robinson <arobinso...@cox.net>
escreveu:

> Hi Antonio,
>
> The following code:
>
>  config = IupConfig();
>  IupSetAttribute(config, "APP_NAME", "xyz");
>  IupConfigLoad(config);
>
> only seems to work if the current directory has no atypical
> (non-English) characters in it, e.g. -- "E:\My\Files" vs "E:\My…\Files". I
> am using the English version of Windows with code page 1252. Iup crashes at
> IupConfigLoad within the function IupLineFileClose. The character "…"
> is Unicode codepoint 2026 (which translates to UTF-8 as 0xE2 0x80 0xA6).
>
> Regards,
> Andrew
>
/* this file is in UTF-8 encoding */

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>

/* Tested on:
vc10 - failed
vc11 - failed
vc12 - failed
vc14 - failed
vc15 - ok

chcp 65001 - to enable utf-8 for printf, even when setlocale returns NULL
*/

int main(int argc, char **argv)
{
  FILE* file;

  const char* en = "Hello world!";
  const char* ch = "你好世界";
  const char* gk = "γειά σου κόσμος";
  const char* jp = "こんにちは世界";
  const char* ko = "여보세요 세계";
  const char* pt = "Olá mundo!";
  const char* ru = "Здравствулте мир!";  
//  const char* ru2 = u8"Русский текст"; // Intersting way to code strings in newer compilers
  const char* filename = "D:\\_goodies\\iup\\unicode\\ɄΩαβń.txt";
  
//	char* res = setlocale(LC_ALL, "");
	char* res = setlocale(LC_ALL, ".UTF8");
	if (res == NULL) 
    puts("setlocale failed\n");	
	else 
    printf("Current locale: %s\n", res);
  
  printf("English   : %s\n", en); 
  printf("Chinese   : %s\n", ch); 
  printf("Greek     : %s\n", gk); 
  printf("Japanese  : %s\n", jp); 
  printf("Korean    : %s\n", ko); 
  printf("Portuguese: %s\n", pt); 
  printf("Russian   : %s\n", ru); 
  printf("Filename  : %s\n", filename); 

  file = fopen(filename, "r");
  if (!file)
    puts("fopen failed\n");
  else
  {
    int size;
    char* str;

    fseek(file, 0, SEEK_END);
    size = ftell(file);
    fseek(file, 0, SEEK_SET);

    /* allocate memory for the file contents + nul terminator */
    str = malloc(size + 1);
    /* read all data at once */
    fread(str, size, 1, file);
    /* set the nul terminator */
    str[size] = 0;

    printf("File contents  :\n%s", str);

    free(str);
    fclose(file);
  }

  (void)argc;
  (void)argv;
  return 0;
}
_______________________________________________
Iup-users mailing list
Iup-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/iup-users

Reply via email to