Skip to contentSkip to conversation

sensors: print the degree sign in the terminal's current

PATCH New Aurelien Jarno 2 participants 3 messages May 19, 2005

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sensors: print the degree sign in the terminal's current

Hi all,

To print the degree sign, sensors lookups the current locale, outputs it in UTF-8 if the locale is in UTF-8, and in latin-1 otherwise. However some people are using other locales. Moreover, it is rather irritating for people using a Japanese locale, as the character code 176 is a start of sequence of characters for EUC terminals.

The attached patch uses iconv to convert the degree sign in an UTF-8 format to the current locale.

Aurelien

Signature · 62 lines
  .''`.  Aurelien Jarno	              GPG: 1024D/F1BCDB73
 : :' :  Debian GNU/Linux developer | Electrical Engineering Student 
 `. `'   [email protected]         | [email protected]
   `-    people.debian.org/~aurel32 | www.aurel32.net
-------------- next part --------------
Index: prog/sensors/main.c
=================================RCS file: /home/cvs/lm_sensors2/prog/sensors/main.c,v
retrieving revision 1.90
diff -u -1 -b -p -r1.90 main.c
--- lm_sensors2.orig/prog/sensors/main.c	29 Feb 2004 18:43:53 -0000	1.90
+++ lm_sensors2/prog/sensors/main.c	21 Mar 2004 00:51:01 -0000
@@ -26,2 +26,3 @@
 #include <langinfo.h>
+#include <iconv.h>
 
@@ -156,2 +157,34 @@ void close_config_file(void)
 
+void set_degstr(void)
+{
+  /* Size hardcoded for better performance. Don't forget to count \0 !!! */
+  size_t deg_utf8_size = 4;
+  char *deg_utf8_celsius = "??C";
+  char *deg_utf8_farenheit = "??F";
+  size_t nconv;
+  size_t degstr_size = sizeof(degstr);
+  char *degstr_ptr = degstr;
+  
+  iconv_t cd = iconv_open(nl_langinfo(CODESET), "UTF-8");
+  if (cd = (iconv_t) -1)
+    goto conv_error;
+  
+  if (fahrenheit) 
+    nconv = iconv(cd, &deg_utf8_farenheit, &deg_utf8_size, &degstr_ptr, &degstr_size);
+  else
+    nconv = iconv(cd, &deg_utf8_celsius, &deg_utf8_size, &degstr_ptr, &degstr_size);
+  iconv_close(cd);
+  
+  if (nconv = (size_t) -1)
+    goto conv_error;
+
+  return;
+  
+conv_error:
+  if (fahrenheit) 
+    strcpy(degstr, " F");
+  else
+    strcpy(degstr, " C");
+}
+
 int main (int argc, char *argv[])
@@ -255,11 +288,3 @@ int main (int argc, char *argv[])
   /* build the degrees string */
-  if (strcmp(nl_langinfo(CODESET), "UTF-8") = 0)
-    sprintf(degstr, "%c%c", 0xc2, 0xb0);
-  else
-    sprintf(degstr, "%c", 176);
-  if (fahrenheit) {
-    strcat(degstr, "F");
-  } else {
-    strcat(degstr, "C");
-  }
+  set_degstr();
#1 lore raw

PATCH 1/1 sensors: print the degree sign in the terminal's current

Quoted 7 lines · Aurelien Jarno

Hi all,

To print the degree sign, sensors lookups the current locale, outputs it in UTF-8 if the locale is in UTF-8, and in latin-1 otherwise. However some people are using other locales. Moreover, it is rather irritating for people using a Japanese locale, as the character code 176 is a start of sequence of characters for EUC terminals.

Please find attached a new patch, I hope less ugly than the previous one. I made it using the advice Jean Delvare give me on IRC.

Aurelien

Signature · 53 lines
  .''`.  Aurelien Jarno	              GPG: 1024D/F1BCDB73
 : :' :  Debian GNU/Linux developer | Electrical Engineering Student 
 `. `'   [email protected]         | [email protected]
   `-    people.debian.org/~aurel32 | www.aurel32.net
-------------- next part --------------
Index: prog/sensors/main.c
=================================RCS file: /home/cvs/lm_sensors2/prog/sensors/main.c,v
retrieving revision 1.90
diff -u -1 -b -p -r1.90 main.c
--- lm_sensors2.orig/prog/sensors/main.c	29 Feb 2004 18:43:53 -0000	1.90
+++ lm_sensors2/prog/sensors/main.c	21 Mar 2004 12:15:33 -0000
@@ -26,2 +26,3 @@
 #include <langinfo.h>
+#include <iconv.h>
 
@@ -156,2 +157,25 @@ void close_config_file(void)
 
+static void set_degstr(void)
+{
+  /* Size hardcoded for better performance. Don't forget to count \0 !!! */
+  size_t deg_latin1_size = 3;
+  char *deg_latin1_text[2] = {"\260C", "\260F"};
+  const char *deg_default_text[2] = {" C", " F"};
+  size_t nconv;
+  size_t degstr_size = sizeof(degstr);
+  char *degstr_ptr = degstr;
+
+  iconv_t cd = iconv_open(nl_langinfo(CODESET), "ISO-8859-1");
+  if (cd != (iconv_t) -1) {
+    nconv = iconv(cd, &(deg_latin1_text[fahrenheit]), &deg_latin1_size, &degstr_ptr, &degstr_size);
+    iconv_close(cd);
+    
+    if (nconv > 0) 
+      return;	   
+  }
+
+  /* There was an error during the conversion, use the default text */
+  strcpy(degstr, deg_default_text[fahrenheit]);
+}
+
 int main (int argc, char *argv[])
@@ -255,11 +279,3 @@ int main (int argc, char *argv[])
   /* build the degrees string */
-  if (strcmp(nl_langinfo(CODESET), "UTF-8") = 0)
-    sprintf(degstr, "%c%c", 0xc2, 0xb0);
-  else
-    sprintf(degstr, "%c", 176);
-  if (fahrenheit) {
-    strcat(degstr, "F");
-  } else {
-    strcat(degstr, "C");
-  }
+  set_degstr();
Applied
#2 lore raw

PATCH 1/1 sensors: print the degree sign in the terminal's

In reply to Aurelien Jarno
Quoted 2 lines

Please find attached a new patch, I hope less ugly than the previous one. I made it using the advice Jean Delvare give me on IRC.

Thanks, applied with the fix we discussed on IRC.

Signature · 2 lines
Jean Delvare
http://www.ensicaen.ismra.fr/~delvare/