Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

Working with cyrillics

Help
Mieczeslaw
2012-10-09
2013-04-25
  • Mieczeslaw
    Mieczeslaw
    2012-10-09

    Greetings everyone,

    I've tried using PDCurses in a small console game application and understood the general way it works. Still I can't get some of its features right. Say, I need reading some symbols from a text file then putting them on the screen. In plain old C this looked as follows:
    void logo()
    {
    FILE *in;
    char logo;

    clrscr();
    in = fopen("logo.txt", "rt");

    int i = 0, j = 0;
    while(!feof(in))
    {
    char buff = fgetc(in);
    if(buff == '\n')
    {
    j = 0;
    i++;
    }
    else
    {
    logo_ = buff;
    j++;
    }
    }
    fclose(in);

                 for(i=0; i<16; i++)
                 {
                          for(int j=0; j<59; j++)
                          {
                                  gotoxy(j, i);
                                  printf("%c", logo);
                          }
    }

    Using PDCurses I try writing this:
    void logo()
    {
    FILE *in;
    cchar_t logo;

    initscr();
    start_color();
    wclear(stdscr);
    init_pair(1, COLOR_WHITE, COLOR_BLACK);

    attron(COLOR_PAIR(1) | WA_BOLD);

    in = fopen("logo.txt", "r");

    int i = 0, j = 0;
    cchar_t buff;
    while(WEOF != (buff = fgetwc(in)))
    {
    if(buff == L'\n')
    {
    j = 0;
    i++;
    }
    else
    {
    logo = buff;
    j++;
    }
    }
    fclose(in);

                 for(i=0; i<16; i++)
                 {
                         for(j=0; j<59; j++)
                         {
                                  move(i, j);
                                  add_wch(&logo);
                          }
                 }
    wrefresh(stdscr);

    getch();

    endwin();

    return 0;

    }

    The file is now saved in UTF-8. Still for the non-ASCII characters (for example, cyrillics) I get wrong output (which is twice as long as the rows in the file). So, could anyone please show me the right way to work with cyrillics?_

     
  • Mieczeslaw
    Mieczeslaw
    2012-10-09

    Sorry, the site recognizes  as a tag for italics. Of course there should be

    logo[i][j]
    
     
  • Mieczeslaw
    Mieczeslaw
    2012-10-09

    logo , I mean)

     
  • Is your locale set to a UTF-8 locale? If not, fgetwc() will not work as you intend. If it is, it still may not work.

    Personally, I'd probably read in a line at a time with fgets(), then either print it with printw() (this assumes PDCurses is compiled with the UTF8=Y option, or else the locale is UTF-8), or manually decode it to a wide-character string, and then do the add_wch() thing.

     
  • Mieczeslaw
    Mieczeslaw
    2012-10-10

    Thanks for the fast reply!
    I'm not quite sure about the default locale in Windows but both

    setlocale(LC_ALL, "");
    

    and

    setlocale(LC_ALL, "Russian");
    

    give me the same result - just changed the look of the symbols in the output (and the output is still twice as long as is needed - say, for a three-letters word I get 6 characters on the screen).
    And yes, I compiled PDCurses from source with UTF8=Y enabled.

     
  • Well yes, they would give the same result, since neither of those is a UTF-8 locale. Windows' default locale is never UTF-8, sadly.

     
  • Mieczeslaw
    Mieczeslaw
    2012-10-10

    I tried

    setlocale(LC_ALL, "ru_RU.UTF8");
    

    also. The output didn't change.