Skip to contentSkip to conversation

libsensors4: sensors_snprintf_chip_name

PATCH In review Jean Delvare 2 participants 2 messages Aug 15, 2007 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 5/6 libsensors4: sensors_snprintf_chip_name

Add a new function to libsensors doing the opposite of sensors_parse_chip_name(). sensors_snprintf_chip_name() converts a chip name from its internal representation to a human readable string. So far, each user program had to reimplement this function.

---
 lib/access.c         |    2 +-
 lib/access.h         |    5 +++++
 lib/data.c           |   20 ++++++++++++++++++++
 lib/sensors.h        |    6 ++++++
 prog/sensord/sense.c |    8 ++------
 prog/sensors/main.c  |    9 ++-------
 6 files changed, 36 insertions(+), 14 deletions(-)
lib/data.c +20 -0
@@ -20,6 +20,7 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "access.h"
 #include "error.h"
 #include "data.h"
 #include "sensors.h"
@@ -154,6 +155,25 @@ ERROR:
   return -SENSORS_ERR_CHIP_NAME;
 }
 
+int sensors_snprintf_chip_name(char *str, size_t size,
+			       const sensors_chip_name *chip)
+{
+	if (sensors_chip_name_has_wildcards(chip))
+		return -SENSORS_ERR_WILDCARDS;
+
+	switch (chip->bus) {
+	case SENSORS_CHIP_NAME_BUS_ISA:
+		return snprintf(str, size, "%s-isa-%04x", chip->prefix,
+				chip->addr);
+	case SENSORS_CHIP_NAME_BUS_PCI:
+		return snprintf(str, size, "%s-pci-%04x", chip->prefix,
+				chip->addr);
+	default:
+		return snprintf(str, size, "%s-i2c-%d-%02x", chip->prefix,
+				chip->bus, chip->addr);
+	}
+}
+
 int sensors_parse_i2cbus_name(const char *name, int *res)
 {
   char *endptr;
lib/sensors.h +6 -0
@@ -60,6 +60,12 @@ extern void sensors_cleanup(void);
 extern int sensors_parse_chip_name(const char *orig_name,
                                    sensors_chip_name *res);
 
+/* Print a chip name from its internal representation. Note that chip should
+   not contain wildcard values! Return the number of characters printed on
+   success (same as snprintf), <0 on error. */
+int sensors_snprintf_chip_name(char *str, size_t size,
+			       const sensors_chip_name *chip);
+
 /* Compare two chips name descriptions, to see whether they could match.
    Return 0 if it does not match, return 1 if it does match. */
 extern int sensors_match_chip(const sensors_chip_name *chip1,
prog/sensors/main.c +2 -7
@@ -298,13 +298,8 @@ const char *sprintf_chip_name(const sens
   #define BUF_SIZE 200
   static char buf[BUF_SIZE];
 
-  if (name->bus = SENSORS_CHIP_NAME_BUS_ISA)
-    snprintf(buf, BUF_SIZE, "%s-isa-%04x", name->prefix, name->addr);
-  else if (name->bus = SENSORS_CHIP_NAME_BUS_PCI)
-    snprintf(buf, BUF_SIZE, "%s-pci-%04x", name->prefix, name->addr);
-  else
-    snprintf(buf, BUF_SIZE, "%s-i2c-%d-%02x", name->prefix, name->bus,
-             name->addr);
+  if (sensors_snprintf_chip_name(buf, BUF_SIZE, name) < 0)
+    return NULL;
   return buf;
 }
 
prog/sensord/sense.c +2 -6
@@ -74,12 +74,8 @@ static const char *
 chipName
 (const sensors_chip_name *chip) {
   static char buffer[256];
-  if (chip->bus = SENSORS_CHIP_NAME_BUS_ISA)
-    sprintf (buffer, "%s-isa-%04x", chip->prefix, chip->addr);
-  else if (chip->bus = SENSORS_CHIP_NAME_BUS_PCI)
-	    sprintf (buffer, "%s-pci-%04x", chip->prefix, chip->addr);
-  else
-    sprintf (buffer, "%s-i2c-%d-%02x", chip->prefix, chip->bus, chip->addr);
+  if (sensors_snprintf_chip_name(buffer, 256, chip) < 0)
+    return NULL;
   return buffer;
 }
 
lib/access.c +1 -1
@@ -133,7 +133,7 @@ sensors_lookup_feature_name(const sensor
 /* Check whether the chip name is an 'absolute' name, which can only match
    one chip, or whether it has wildcards. Returns 0 if it is absolute, 1
    if there are wildcards. */
-static int sensors_chip_name_has_wildcards(const sensors_chip_name *chip)
+int sensors_chip_name_has_wildcards(const sensors_chip_name *chip)
 {
 	if ((chip->prefix = SENSORS_CHIP_NAME_PREFIX_ANY) ||
 	    (chip->bus = SENSORS_CHIP_NAME_BUS_ANY) ||
lib/access.h +5 -0
@@ -29,6 +29,11 @@
 extern const sensors_chip_feature *sensors_lookup_feature_nr(const sensors_chip_name *chip,
                                                              int feature);
 
+/* Check whether the chip name is an 'absolute' name, which can only match
+   one chip, or whether it has wildcards. Returns 0 if it is absolute, 1
+   if there are wildcards. */
+int sensors_chip_name_has_wildcards(const sensors_chip_name *chip);
+
 sensors_feature_type sensors_feature_get_type(const char *name, int *nr);
 
 #endif /* def LIB_SENSORS_ACCESS_H */
Signature · 6 lines
Jean Delvare

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
#1 lore raw
Quoted 5 lines · Jean Delvare

Add a new function to libsensors doing the opposite of sensors_parse_chip_name(). sensors_snprintf_chip_name() converts a chip name from its internal representation to a human readable string. So far, each user program had to reimplement this function.

Looks good too, again I'll run some tests once things hit svn.

Regards,

Hans

Quoted 112 lines
---
 lib/access.c         |    2 +-
 lib/access.h         |    5 +++++
 lib/data.c           |   20 ++++++++++++++++++++
 lib/sensors.h        |    6 ++++++
 prog/sensord/sense.c |    8 ++------
 prog/sensors/main.c  |    9 ++-------
 6 files changed, 36 insertions(+), 14 deletions(-)
lib/data.c +20 -0
@@ -20,6 +20,7 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "access.h"
 #include "error.h"
 #include "data.h"
 #include "sensors.h"
@@ -154,6 +155,25 @@ ERROR:
   return -SENSORS_ERR_CHIP_NAME;
 }
 
+int sensors_snprintf_chip_name(char *str, size_t size,
+			       const sensors_chip_name *chip)
+{
+	if (sensors_chip_name_has_wildcards(chip))
+		return -SENSORS_ERR_WILDCARDS;
+
+	switch (chip->bus) {
+	case SENSORS_CHIP_NAME_BUS_ISA:
+		return snprintf(str, size, "%s-isa-%04x", chip->prefix,
+				chip->addr);
+	case SENSORS_CHIP_NAME_BUS_PCI:
+		return snprintf(str, size, "%s-pci-%04x", chip->prefix,
+				chip->addr);
+	default:
+		return snprintf(str, size, "%s-i2c-%d-%02x", chip->prefix,
+				chip->bus, chip->addr);
+	}
+}
+
 int sensors_parse_i2cbus_name(const char *name, int *res)
 {
   char *endptr;
lib/sensors.h +6 -0
@@ -60,6 +60,12 @@ extern void sensors_cleanup(void);
 extern int sensors_parse_chip_name(const char *orig_name,
                                    sensors_chip_name *res);
 
+/* Print a chip name from its internal representation. Note that chip should
+   not contain wildcard values! Return the number of characters printed on
+   success (same as snprintf), <0 on error. */
+int sensors_snprintf_chip_name(char *str, size_t size,
+			       const sensors_chip_name *chip);
+
 /* Compare two chips name descriptions, to see whether they could match.
    Return 0 if it does not match, return 1 if it does match. */
 extern int sensors_match_chip(const sensors_chip_name *chip1,
prog/sensors/main.c +2 -7
@@ -298,13 +298,8 @@ const char *sprintf_chip_name(const sens
   #define BUF_SIZE 200
   static char buf[BUF_SIZE];
 
-  if (name->bus = SENSORS_CHIP_NAME_BUS_ISA)
-    snprintf(buf, BUF_SIZE, "%s-isa-%04x", name->prefix, name->addr);
-  else if (name->bus = SENSORS_CHIP_NAME_BUS_PCI)
-    snprintf(buf, BUF_SIZE, "%s-pci-%04x", name->prefix, name->addr);
-  else
-    snprintf(buf, BUF_SIZE, "%s-i2c-%d-%02x", name->prefix, name->bus,
-             name->addr);
+  if (sensors_snprintf_chip_name(buf, BUF_SIZE, name) < 0)
+    return NULL;
   return buf;
 }
 
prog/sensord/sense.c +2 -6
@@ -74,12 +74,8 @@ static const char *
 chipName
 (const sensors_chip_name *chip) {
   static char buffer[256];
-  if (chip->bus = SENSORS_CHIP_NAME_BUS_ISA)
-    sprintf (buffer, "%s-isa-%04x", chip->prefix, chip->addr);
-  else if (chip->bus = SENSORS_CHIP_NAME_BUS_PCI)
-	    sprintf (buffer, "%s-pci-%04x", chip->prefix, chip->addr);
-  else
-    sprintf (buffer, "%s-i2c-%d-%02x", chip->prefix, chip->bus, chip->addr);
+  if (sensors_snprintf_chip_name(buffer, 256, chip) < 0)
+    return NULL;
   return buffer;
 }
 
lib/access.c +1 -1
@@ -133,7 +133,7 @@ sensors_lookup_feature_name(const sensor
 /* Check whether the chip name is an 'absolute' name, which can only match
    one chip, or whether it has wildcards. Returns 0 if it is absolute, 1
    if there are wildcards. */
-static int sensors_chip_name_has_wildcards(const sensors_chip_name *chip)
+int sensors_chip_name_has_wildcards(const sensors_chip_name *chip)
 {
 	if ((chip->prefix = SENSORS_CHIP_NAME_PREFIX_ANY) ||
 	    (chip->bus = SENSORS_CHIP_NAME_BUS_ANY) ||
lib/access.h +0 -0
@@ -29,6 +29,11 @@
 extern const sensors_chip_feature *sensors_lookup_feature_nr(const sensors_chip_name *chip,

lib/access.h line 39

Quoted 10 lines
lib/access.h +5 -0
                                                              int feature);
 
+/* Check whether the chip name is an 'absolute' name, which can only match
+   one chip, or whether it has wildcards. Returns 0 if it is absolute, 1
+   if there are wildcards. */
+int sensors_chip_name_has_wildcards(const sensors_chip_name *chip);
+
 sensors_feature_type sensors_feature_get_type(const char *name, int *nr);
 
 #endif /* def LIB_SENSORS_ACCESS_H */

_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors