ossp-pkg/xds/xml-encode-int64.c 1.2 -> 1.3
--- xml-encode-int64.c 2001/07/23 16:40:38 1.2
+++ xml-encode-int64.c 2001/07/24 13:52:41 1.3
@@ -25,14 +25,17 @@
SUCH DAMAGE.
*/
-#include <stdio.h>
+#include <string.h>
#include <assert.h>
#include "internal.h"
int xml_encode_int64(xds_t* xds, void* engine_context, void* buffer, size_t buffer_size, va_list* args)
{
- int rc;
xds_int64_t value;
+ char buf[64];
+ size_t i, j;
+ char* p;
+ int negative;
/* Consistency checks. */
@@ -43,13 +46,42 @@
if (xds == NULL || buffer == NULL || buffer_size == 0 || args == NULL)
return XDS_ERR_INVALID_ARG;
- /* Format value into buffer. */
+ /* Format value into our buffer. */
value = va_arg(*args, xds_int64_t);
- rc = snprintf(buffer, buffer_size, "<int64>%lld</int64>", value);
- if (rc < 0)
- return buffer_size*2;
- assert(rc >= 15);
+ if (value < 0)
+ {
+ negative = XDS_TRUE;
+ value = 0 - value;
+ }
+ else
+ negative = XDS_FALSE;
+ i = 0;
+ do
+ {
+ unsigned char remainder = value % 10;
+ value = value / 10;
+ buf[i++] = '0' + remainder;
+ }
+ while (value != 0);
+ if (negative)
+ buf[i++] = '-';
+
+ /* Check the buffer size. */
+
+ if (buffer_size < 7 + 8 + i)
+ return 7 + 8 + i;
+
+ /* Write result into the buffer. */
+
+ p = buffer;
+ strcpy(p, "<int64>");
+ p += 7;
+ for (j = i; j > 0; )
+ {
+ *p++ = buf[--j];
+ }
+ strcpy(p, "</int64>");
- return rc;
+ return 7 + 8 + i;
}
|
|