Add arraysize format
authorPanu Matilainen <pmatilai@redhat.com>
Tue, 20 May 2008 09:38:40 +0000 (12:38 +0300)
committerPanu Matilainen <pmatilai@redhat.com>
Wed, 21 May 2008 09:04:51 +0000 (12:04 +0300)
- it's a bit silly but avoids ugly special casing in formatting

lib/formats.c
lib/headerfmt.c
lib/rpmtd.h

index ade9f8f..cee6b9b 100644 (file)
@@ -586,6 +586,20 @@ static char * depflagsFormat(rpmtd td, char * formatPrefix)
 }
 
 /**
+ * Return tag container array size.
+ * @param td           tag data container
+ * @param formatPrefix sprintf format string
+ * @return             formatted string
+ */
+static char * arraysizeFormat(rpmtd td, char * formatPrefix)
+{
+    char *val = NULL;
+    strcat(formatPrefix, "u");
+    rasprintf(&val, formatPrefix, rpmtdCount(td));
+    return val;
+}
+
+/**
  * Retrieve mounted file system paths.
  * @param h            header
  * @retval td          tag data container
@@ -1047,5 +1061,6 @@ static const struct headerFormatFunc_s rpmHeaderFormats[] = {
     { RPMTD_FORMAT_DATE,       "date",         dateFormat },
     { RPMTD_FORMAT_DAY,                "day",          dayFormat },
     { RPMTD_FORMAT_SHESCAPE,   "shescape",     shescapeFormat },
+    { RPMTD_FORMAT_ARRAYSIZE,  "arraysize",    arraysizeFormat },
     { -1,                      NULL,           NULL }
 };
index e75f077..4d0964c 100644 (file)
@@ -35,7 +35,6 @@ struct sprintfTag_s {
     headerTagFormatFunction fmt;
     rpmTag tag;
     int justOne;
-    int arrayCount;
     char * format;
     char * type;
 };
@@ -342,7 +341,6 @@ static int parseFormat(headerSprintfArgs hsa, char * str,
 
            token->u.tag.format = start;
            token->u.tag.justOne = 0;
-           token->u.tag.arrayCount = 0;
 
            chptr = start;
            while (*chptr && *chptr != '{' && *chptr != '%') chptr++;
@@ -363,7 +361,7 @@ static int parseFormat(headerSprintfArgs hsa, char * str,
                start++;
            } else if (*start == '#') {
                token->u.tag.justOne = 1;
-               token->u.tag.arrayCount = 1;
+               token->u.tag.type = "arraysize";
                start++;
            }
 
@@ -387,7 +385,9 @@ static int parseFormat(headerSprintfArgs hsa, char * str,
                    return 1;
                }
                token->u.tag.type = chptr;
-           } else {
+           } 
+           /* default to string conversion if no formats found by now */
+           if (!token->u.tag.type) {
                token->u.tag.type = "string";
            }
            
@@ -637,7 +637,6 @@ static char * formatValue(headerSprintfArgs hsa, sprintfTag tag, int element)
     size_t need = 0;
     char * t, * te;
     char buf[20];
-    int countBuf;
     struct rpmtd_s tmp;
     rpmtd td;
 
@@ -650,14 +649,6 @@ static char * formatValue(headerSprintfArgs hsa, sprintfTag tag, int element)
        td = &tmp;      
     }
 
-    if (tag->arrayCount) {
-       countBuf = td->count;
-       tmp.data = &countBuf;
-       tmp.count = 1;
-       tmp.type = RPM_INT32_TYPE;
-       element = 0;
-       td = &tmp;
-    }
     td->ix = element; /* Ick, use iterators instead */
 
     (void) stpcpy( stpcpy(buf, "%"), tag->format);
@@ -743,7 +734,6 @@ static char * singleSprintf(headerSprintfArgs hsa, sprintfToken token,
        {
            rpmtd td = NULL;
            if (spft->type != PTOK_TAG ||
-               spft->u.tag.arrayCount ||
                spft->u.tag.justOne) continue;
 
            if (!(td = getData(hsa, spft->u.tag.tag))) {
index bc6ed95..c7b4562 100644 (file)
@@ -134,6 +134,7 @@ typedef enum rpmtdFormats_e {
     RPMTD_FORMAT_DATE          = 11,   /* date format (int32 types) */
     RPMTD_FORMAT_DAY           = 12,   /* day format (int32 types) */
     RPMTD_FORMAT_SHESCAPE      = 13,   /* shell escaped (any type) */
+    RPMTD_FORMAT_ARRAYSIZE     = 14,   /* size of contained array (any type) */
 } rpmtdFormats;
 
 /** \ingroup rpmtd