Add some more rpmtd access methods
authorPanu Matilainen <pmatilai@redhat.com>
Tue, 13 May 2008 15:09:35 +0000 (18:09 +0300)
committerPanu Matilainen <pmatilai@redhat.com>
Tue, 13 May 2008 15:09:35 +0000 (18:09 +0300)
- rpmtdGetUint() for 16 and 32 bit integer types, similar to
  rpmtdGetString() (equally usable with scalar types and arrays)
- rpmtdToString() which converts "any" header data into string presentation

lib/rpmtd.c
lib/rpmtd.h

index dfb8628..693d6b2 100644 (file)
@@ -82,6 +82,31 @@ int rpmtdNext(rpmtd td)
     return i;
 }
 
+uint16_t * rpmtdGetUint16(rpmtd td)
+{
+    uint16_t *res = NULL;
+
+    assert(td != NULL);
+
+    if (td->type == RPM_INT16_TYPE) {
+       int ix = (td->ix >= 0 ? td->ix : 0);
+       res = (uint16_t *) td->data + ix;
+    } 
+    return res;
+}
+
+uint32_t * rpmtdGetUint32(rpmtd td)
+{
+    uint32_t *res = NULL;
+
+    assert(td != NULL);
+
+    if (td->type == RPM_INT32_TYPE) {
+       int ix = (td->ix >= 0 ? td->ix : 0);
+       res = (uint32_t *) td->data + ix;
+    } 
+    return res;
+}
 const char * rpmtdGetString(rpmtd td)
 {
     const char *str = NULL;
@@ -98,6 +123,42 @@ const char * rpmtdGetString(rpmtd td)
     return str;
 }
 
+char *rpmtdToString(rpmtd td)
+{
+    char *res = NULL;
+
+    switch (td->type) {
+       case RPM_STRING_TYPE:
+       case RPM_STRING_ARRAY_TYPE: {
+           const char *s = rpmtdGetString(td);
+           if (s) {
+               res = xstrdup(s);
+           }
+           break;
+       }
+       case RPM_INT16_TYPE: {
+           uint16_t *num = rpmtdGetUint16(td);
+           if (num) {
+               rasprintf(&res, "%hd", *num);
+           }
+           break;
+       }
+       case RPM_INT32_TYPE: {
+           uint32_t *num = rpmtdGetUint32(td);
+           if (num) {
+               rasprintf(&res, "%d", *num);
+           }
+           break;
+       }
+       case RPM_BIN_TYPE: {
+           /* XXX TODO: convert to hex presentation */
+       }
+       default:
+           break;
+    }
+    return res;
+}
+
 int rpmtdFromArgv(rpmtd td, rpmTag tag, ARGV_t argv)
 {
     int count = argvCount(argv);
index d7fa5a7..01291a5 100644 (file)
@@ -79,6 +79,26 @@ int rpmtdInit(rpmtd td);
 int rpmtdNext(rpmtd td);
 
 /** \ingroup rpmtd
+ * Return uint16_t data from tag container.
+ * For scalar return type, just return pointer to the integer. On array
+ * types, return pointer to current iteration index. If the tag container
+ * is not for int16 type, NULL is returned.
+ * @param td           Tag data container
+ * @return             Pointer to uint16_t, NULL on error
+ */
+uint16_t * rpmtdGetUint16(rpmtd td);
+
+/** \ingroup rpmtd
+ * Return uint32_t data from tag container.
+ * For scalar return type, just return pointer to the integer. On array
+ * types, return pointer to current iteration index. If the tag container
+ * is not for int32 type, NULL is returned.
+ * @param td           Tag data container
+ * @return             Pointer to uint32_t, NULL on error
+ */
+uint32_t * rpmtdGetUint32(rpmtd td);
+
+/** \ingroup rpmtd
  * Return string data from tag container.
  * For string types, just return the string. On string array types,
  * return the string from current iteration index. If the tag container
@@ -89,6 +109,17 @@ int rpmtdNext(rpmtd td);
 const char * rpmtdGetString(rpmtd td);
 
 /** \ingroup rpmtd
+ * Return data from tag container in string presentation.
+ * Return malloced string presentation of current data in container,
+ * converting from integers etc as necessary. On array types, data from
+ * current iteration index is returned.
+ * @param td           Tag data container
+ * @return             String representation of current data (malloc'ed), 
+ *                     NULL on error
+ */
+char *rpmtdToString(rpmtd td);
+
+/** \ingroup rpmtd
  * Construct tag container from ARGV_t array.
  * Tag type is checked to be of string array type and array is checked
  * to be non-empty.