Sample: cleaning up Inheritance
[io/quag.git] / libs / basekit / source / UArray_format.c
blobc7cc378b64cd4ded5e7b351aa4ed04dd065a1f41
1 /*
2 copyright: Steve Dekorte, 2006. All rights reserved.
3 license: See _BSDLicense.txt.
4 */
6 #include "UArray.h"
7 #include <string.h>
8 #include <stdarg.h>
9 #include <stdio.h>
10 #include <stddef.h>
12 UArray *UArray_newWithFormat_(const char *format, ...)
14 UArray *self;
15 va_list ap;
16 va_start(ap, format);
17 self = UArray_newWithVargs_(format, ap);
18 va_end(ap);
19 return self;
22 UArray *UArray_newWithVargs_(const char *format, va_list ap)
24 UArray *self = UArray_new();
25 UArray_fromVargs_(self, format,ap);
26 return self;
29 UArray *UArray_fromFormat_(UArray *self, const char *format, ...)
31 va_list ap;
32 va_start(ap, format);
33 UArray_fromVargs_(self, format, ap);
34 va_end(ap);
35 return self;
38 void UArray_fromVargs_(UArray *self, const char *format, va_list ap)
40 while (*format)
42 if (*format == '%')
44 format ++;
46 if (*format == 's')
48 char *s = va_arg(ap, char *);
49 if (!s) { printf("UArray_fromVargs_ missing param"); return; }
50 UArray_appendCString_(self, s);
52 else if (*format == 'i' || *format == 'd')
54 int i = va_arg(ap, int);
55 char s[100];
57 snprintf(s, 100, "%i", i);
58 UArray_appendCString_(self, s);
60 else if (*format == 'f')
62 double d = va_arg(ap, double);
63 char s[100];
65 snprintf(s, 100, "%f", d);
66 UArray_appendCString_(self, s);
68 else if (*format == 'p')
70 void *p = va_arg(ap, void *);
71 char s[100];
73 snprintf(s, 100, "%p", p);
74 UArray_appendCString_(self, s);
76 // new format command for a given number adding spaces
77 else if (*format == '#')
79 int n, i = va_arg(ap, int);
80 char *s = " ";
82 for (n = 0; n < i; n ++)
84 UArray_appendCString_(self, s);
88 else
90 char s[2];
92 snprintf(s, 2, "%c", *format);
93 UArray_appendCString_(self, s);
96 format ++;
100 UArray *UArray_asNewHexStringUArray(UArray *self)
102 size_t i, newSize = self->size * 2;
103 UArray *ba = UArray_new();
104 UArray_setSize_(ba, newSize);
106 for(i = 0; i < self->size; i ++)
108 int v = UArray_longAt_(self, i);
109 char *s = (char *)(ba->data + i * 2);
111 if (v < 16)
113 snprintf(s, newSize, "0%x", (int)v);
115 else
117 snprintf(s, newSize, "%x", (int)v);
121 return ba;