[AGENT++] patch to add an optional parameter to Oidx::as_string(boolean withLength=TRUE)

Claus Klein claus.klein at arcormail.de
Wed Aug 17 22:56:22 CEST 2011


Perhaps, that would be the better solution:

=== modified file 'agent++/include/agent_pp/snmp_pp_ext.h'
--- old/agent++/include/agent_pp/snmp_pp_ext.h	2010-10-31 08:30:19 +0000
+++ new/agent++/include/agent_pp/snmp_pp_ext.h	2011-08-17 20:26:04 +0000
@@ -286,9 +286,12 @@
  	 * interpreted as one char. Thus, all subidentifiers must be
  	 * between 0 and 255.
  	 *
+	 * @param withoutLength
+	 *    if TRUE there will be no preceeding subid containing
+	 *    the length of the string
  	 * @return An OctetStr.
  	 */
-	NS_SNMP OctetStr	as_string() const;
+	NS_SNMP OctetStr	as_string(boolean withoutLength=FALSE) const;

  	/**
  	 * Return an object identifier from a string. The first

=== modified file 'agent++/src/snmp_pp_ext.cpp'
--- old/agent++/src/snmp_pp_ext.cpp	2010-10-31 08:30:19 +0000
+++ new/agent++/src/snmp_pp_ext.cpp	2011-08-17 20:50:16 +0000
@@ -728,10 +728,15 @@
  	return smival.value.oid.ptr[smival.value.oid.len-1];
  }

-OctetStr Oidx::as_string() const
+OctetStr Oidx::as_string(boolean withoutLength) const
  {
  	OctetStr str;
-	for (int i=0; i<(int)len(); i++) {
+
+	int i = 0;
+	// check if the len is implied and should be ignored!
+	if (withoutLength && len() > 0 && len() == ((*this)[0] + 1))
+		i++;    // first oid seems to be the len
+	for (; i<(int)len(); i++) {
  		str += (unsigned char)(*this)[i];
  	}
  	return str;

On 17.08.2011, at 21:55, Claus Klein wrote:

> Hi Frank,
>
> I would like to be able to get the original string back from an Oidx
> without the default added strlen value.
>
> As example, it should be possible to build an Oidx from a string and
> back with full control of the containing or implied strlen:
>
>     const char * firstTestString = "context";
>     const char * otherTestString = "XY";
>     const char * testOidString = "1.3.6.1.6.3.13.1.2.1";
>     Agentpp::Oidx IpaddressIndex("127.0.0.1");
>
>     Agentpp::Oidx StringIndex =
> Agentpp::Oidx::from_string(firstTestString, true);  // add the len,
> withLength = true
>     assert(std::string(firstTestString) ==
> StringIndex.as_string(false).get_printable()); // get back without
> length!
>
>     Agentpp::Oidx ImpliedStringIndex =
> Agentpp::Oidx::from_string(firstTestString, false);  // without
> Length, implied!
>     assert(std::string(firstTestString) ==
> ImpliedStringIndex.as_string(true).get_printable()); //NOTE: the
> asymmetry!
>
>     Agentpp::Oidx OidIndex(testOidString);
>     assert(std::string(testOidString) == OidIndex.get_printable());
>
> In my Opinion, it would be better to have the default parameter set to
> FALSE, but than the change would break the
> existing APi.
>
> With regards,
> Claus
>
> Here is my patch to fix it:
> === modified file 'agent++/include/agent_pp/snmp_pp_ext.h'
> --- old/agent++/include/agent_pp/snmp_pp_ext.h	2010-10-31 08:30:19  
> +0000
> +++ new/agent++/include/agent_pp/snmp_pp_ext.h	2011-08-17 06:27:20  
> +0000
> @@ -286,9 +286,12 @@
>  	 * interpreted as one char. Thus, all subidentifiers must be
>  	 * between 0 and 255.
>  	 *
> +	 * @param withExplicitLength
> +	 *    if FALSE there will be no preceeding subid containing
> +	 *    the length of the string
>  	 * @return An OctetStr.
>  	 */
> -	NS_SNMP OctetStr	as_string() const;
> +	NS_SNMP OctetStr	as_string(boolean withLenght=TRUE) const;
>
>  	/**
>  	 * Return an object identifier from a string. The first
>
> === modified file 'agent++/src/snmp_pp_ext.cpp'
> --- old/agent++/src/snmp_pp_ext.cpp	2010-10-31 08:30:19 +0000
> +++ new/agent++/src/snmp_pp_ext.cpp	2011-08-17 06:55:27 +0000
> @@ -728,10 +728,14 @@
>  	return smival.value.oid.ptr[smival.value.oid.len-1];
>  }
>
> -OctetStr Oidx::as_string() const
> +OctetStr Oidx::as_string(boolean withLength) const
>  {
>  	OctetStr str;
> -	for (int i=0; i<(int)len(); i++) {
> +
> +	int i = 0;
> +	if (!withLength)
> +		i++;
> +	for (; i<(int)len(); i++) {
>  		str += (unsigned char)(*this)[i];
>  	}
>  	return str;
>
> _______________________________________________
> AGENTPP mailing list
> AGENTPP at agentpp.org
> http://lists.agentpp.org/mailman/listinfo/agentpp




More information about the AGENTPP mailing list