Add vp_aprints
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Mon, 28 Apr 2014 18:14:06 +0000 (19:14 +0100)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Fri, 2 May 2014 13:57:49 +0000 (14:57 +0100)
src/include/libradius.h
src/lib/print.c

index 1e5ba8a..1f33dc8 100644 (file)
@@ -453,6 +453,7 @@ char                *vp_aprinttype(TALLOC_CTX *ctx, PW_TYPE type);
 char           *vp_aprint(TALLOC_CTX *ctx, VALUE_PAIR const *vp);
 size_t         vp_prints_value_json(char *out, size_t outlen, VALUE_PAIR const *vp);
 size_t         vp_prints(char *out, size_t outlen, VALUE_PAIR const *vp);
+char           *vp_aprints(TALLOC_CTX *ctx, VALUE_PAIR const *vp);
 void           vp_print(FILE *, VALUE_PAIR const *);
 void           vp_printlist(FILE *, VALUE_PAIR const *);
 #define                fprint_attr_val vp_print
index 775547a..90de6ac 100644 (file)
@@ -909,6 +909,40 @@ size_t vp_prints(char *out, size_t outlen, VALUE_PAIR const *vp)
        return (outlen - freespace);
 }
 
+/** Print one attribute and value to a string
+ *
+ * Print a VALUE_PAIR in the format:
+@verbatim
+       <attribute_name>[:tag] <op> <value>
+@endverbatim
+ * to a string.
+ *
+ * @param vp to print.
+ * @return a talloced buffer with the attribute operator and value.
+ */
+char *vp_aprints(TALLOC_CTX *ctx, VALUE_PAIR const *vp)
+{
+       char const      *token = NULL;
+       char            *pair, *value;
+
+       if (!vp || !vp->da) return 0;
+
+       VERIFY_VP(vp);
+
+       if ((vp->op > T_OP_INVALID) && (vp->op < T_TOKEN_LAST)) {
+               token = vp_tokens[vp->op];
+       } else {
+               token = "<INVALID-TOKEN>";
+       }
+
+       value = vp_aprints(ctx, vp);
+       pair = vp->da->flags.has_tag ?
+              talloc_asprintf(ctx, "%s:%d %s %s", vp->da->name, vp->tag, token, value) :
+              talloc_asprintf(ctx, "%s %s %s", vp->da->name, token, value);
+       talloc_free(value);
+
+       return pair;
+}
 
 /** Print one attribute and value to FP
  *