Changeset 2387

Show
Ignore:
Timestamp:
03/21/04 19:57:09 (9 years ago)
Author:
khali
Message:

Print the degree symbol in the terminal's current locale. Original

patch by Aurelien Jarno.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lm-sensors/trunk/prog/sensors/main.c

    r2328 r2387  
    2525#include <locale.h> 
    2626#include <langinfo.h> 
     27#include <iconv.h> 
    2728 
    2829#include "lib/sensors.h"  
     
    155156} 
    156157 
     158static void set_degstr(void) 
     159{ 
     160  /* Size hardcoded for better performance. 
     161     Don't forget to count the trailing \0! */ 
     162  size_t deg_latin1_size = 3; 
     163  char *deg_latin1_text[2] = {"\260C", "\260F"}; 
     164  const char *deg_default_text[2] = {" C", " F"}; 
     165  size_t nconv; 
     166  size_t degstr_size = sizeof(degstr); 
     167  char *degstr_ptr = degstr; 
     168 
     169  iconv_t cd = iconv_open(nl_langinfo(CODESET), "ISO-8859-1"); 
     170  if (cd != (iconv_t) -1) { 
     171    nconv = iconv(cd, &(deg_latin1_text[fahrenheit]), &deg_latin1_size, 
     172                  &degstr_ptr, &degstr_size); 
     173    iconv_close(cd); 
     174     
     175    if (nconv != (size_t) -1) 
     176      return;       
     177  } 
     178 
     179  /* There was an error during the conversion, use the default text */ 
     180  strcpy(degstr, deg_default_text[fahrenheit]); 
     181} 
     182 
    157183int main (int argc, char *argv[]) 
    158184{ 
     
    254280 
    255281  /* build the degrees string */ 
    256   if (strcmp(nl_langinfo(CODESET), "UTF-8") == 0) 
    257     sprintf(degstr, "%c%c", 0xc2, 0xb0); 
    258   else 
    259     sprintf(degstr, "%c", 176); 
    260   if (fahrenheit) { 
    261     strcat(degstr, "F"); 
    262   } else { 
    263     strcat(degstr, "C"); 
    264   } 
     282  set_degstr(); 
    265283 
    266284  if(do_the_real_work(&error)) {