home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c      Meh, in C you gotta define EVERYTHING      243,242 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 242,011 of 243,242   
   Michael Sanders to James Kuyper   
   Re: Unicode...   
   19 Nov 25 19:22:52   
   
   From: porkchop@invalid.foo   
      
   On Wed, 19 Nov 2025 09:08:10 -0500, James Kuyper wrote:   
      
   > If your targets include Linux Mint, there's a chance the locale names   
   > might be similar to those on my Ubuntu Linux system - but I'm no expert   
   > on the differences between Linux distributions. If so, you should make   
   > the "UTF" search case-insensitive, and make the '-' optional, which   
   > would add considerable complexity to what is currently a very simple   
   > routine.   
      
   Thanks for the info. Cases-insensitive routines were posted in this   
   sub-thread yesterday. But I'll repost it here.   
      
   James straight up, I'm simply trying to learn. I appreciate your   
   comments but I need examples of what to do - that's how my mind works...   
      
   /*   
    * Robust UTF-8 capability test?   
    *   
    * This test checks:   
    *   1. Locale reports UTF-8   
    *   2. Wide-character conversion works (mbrtowc)   
    *   3. Terminal accepts UTF-8 output (optional: write test char)   
    *   
    * Result returned:   
    *   0 = No UTF-8 support detected   
    *   1 = UTF-8 *likely* supported   
    */   
      
   #include    
   #include    
   #include    
   #include    
   #include    
      
   /* return 1 if UTF-8 capable, else 0 */   
      
   int utf8_capable(void) {   
       /* 1 check locale */   
       const char *loc = setlocale(LC_CTYPE, "");   
       if (!loc) return 0;   
       if (!strstr(loc, "UTF-8") && !strstr(loc, "utf8")) return 0;   
      
       /* 2 check UTF-8 decoding with mbrtowc */   
       {   
           const char *test = "✓"; /* E2 9C 93 */   
           wchar_t wc = 0;   
           mbstate_t st;   
           memset(&st, 0, sizeof(st));   
      
           size_t n = mbrtowc(&wc, test, strlen(test), &st);   
      
           if (n == (size_t)-1 || n == (size_t)-2) return 0;  /* decode error */   
           if (wc == 0) return 0;  /* returned null? impossible for ✓ */   
       }   
      
       /* 3 ensure terminal accepts UTF-8 by writing */   
       if (fwrite("✓", 3, 1, stdout) != 1) {   
           return 0; /* write failed — suppress error message, just say no */   
       }   
      
       return 1;   
   }   
      
   int main(void) {   
       if (utf8_capable()) printf("\nUTF-8 OK\n");   
       else printf("\nNOT UTF-8\n");   
       return 0;   
   }   
      
   --   
   :wq   
   Mike Sanders   
      
      
      
      
      
   --   
   :wq   
   Mike Sanders   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca