|
34 | 34 | * Public Functions |
35 | 35 | ****************************************************************************/ |
36 | 36 |
|
| 37 | +/**************************************************************************** |
| 38 | + * Name: nx_vasprintf |
| 39 | + * |
| 40 | + * Description: |
| 41 | + * This function is similar to vsprintf, except that it dynamically |
| 42 | + * allocates a string (as with kmm_malloc) to hold the output, instead of |
| 43 | + * putting the output in a buffer you allocate in advance. The ptr |
| 44 | + * argument should be the address of a char * object, and a successful |
| 45 | + * call to vasprintf stores a pointer to the newly allocated string at that |
| 46 | + * location. |
| 47 | + * |
| 48 | + * Returned Value: |
| 49 | + * The returned value is the number of characters allocated for the buffer, |
| 50 | + * or less than zero if an error occurred. Usually this means that the |
| 51 | + * buffer could not be allocated. |
| 52 | + * |
| 53 | + ****************************************************************************/ |
| 54 | + |
| 55 | +int nx_vasprintf(FAR char **ptr, FAR const IPTR char *fmt, va_list ap) |
| 56 | +{ |
| 57 | + struct lib_outstream_s nulloutstream; |
| 58 | + struct lib_memoutstream_s memoutstream; |
| 59 | + |
| 60 | + /* On some architectures, va_list is really a pointer to a structure on |
| 61 | + * the stack. And the va_arg builtin will modify that instance of va_list. |
| 62 | + * Since vasprintf traverse the parameters in the va_list twice, the |
| 63 | + * va_list will be altered in this first cases and the second usage will |
| 64 | + * fail. This is a known issue with x86_64. |
| 65 | + */ |
| 66 | + |
| 67 | +#ifdef va_copy |
| 68 | + va_list ap2; |
| 69 | +#endif |
| 70 | + FAR char *buf; |
| 71 | + int nbytes; |
| 72 | + |
| 73 | + DEBUGASSERT(ptr != NULL && fmt != NULL); |
| 74 | + |
| 75 | +#ifdef va_copy |
| 76 | + va_copy(ap2, ap); |
| 77 | +#endif |
| 78 | + |
| 79 | + /* First, use a nullstream to get the size of the buffer. The number |
| 80 | + * of bytes returned may or may not include the null terminator. |
| 81 | + */ |
| 82 | + |
| 83 | + lib_nulloutstream(&nulloutstream); |
| 84 | + lib_vsprintf(&nulloutstream, fmt, ap); |
| 85 | + |
| 86 | + /* Then allocate a buffer to hold that number of characters, adding one |
| 87 | + * for the null terminator. |
| 88 | + */ |
| 89 | + |
| 90 | + buf = kmm_malloc(nulloutstream.nput + 1); |
| 91 | + if (buf == NULL) |
| 92 | + { |
| 93 | +#ifdef va_copy |
| 94 | + va_end(ap2); |
| 95 | +#endif |
| 96 | + return ERROR; |
| 97 | + } |
| 98 | + |
| 99 | + /* Initialize a memory stream to write into the allocated buffer. The |
| 100 | + * memory stream will reserve one byte at the end of the buffer for the |
| 101 | + * null terminator and will not report this in the number of output bytes. |
| 102 | + */ |
| 103 | + |
| 104 | + lib_memoutstream(&memoutstream, buf, nulloutstream.nput + 1); |
| 105 | + |
| 106 | + /* Then let lib_vsprintf do it's real thing */ |
| 107 | + |
| 108 | +#ifdef va_copy |
| 109 | + nbytes = lib_vsprintf(&memoutstream.common, fmt, ap2); |
| 110 | + va_end(ap2); |
| 111 | +#else |
| 112 | + nbytes = lib_vsprintf(&memoutstream.common, fmt, ap); |
| 113 | +#endif |
| 114 | + |
| 115 | + /* Return a pointer to the string to the caller. NOTE: the memstream put() |
| 116 | + * method has already added the NUL terminator to the end of the string |
| 117 | + * (not included in the nput count). |
| 118 | + */ |
| 119 | + |
| 120 | + DEBUGASSERT(nbytes < 0 || nbytes == nulloutstream.nput); |
| 121 | + |
| 122 | + if (nbytes < 0) |
| 123 | + { |
| 124 | + kmm_free(buf); |
| 125 | + return ERROR; |
| 126 | + } |
| 127 | + |
| 128 | + *ptr = buf; |
| 129 | + return nbytes; |
| 130 | +} |
| 131 | + |
37 | 132 | /**************************************************************************** |
38 | 133 | * Name: vasprintf |
39 | 134 | * |
|
52 | 147 | * |
53 | 148 | ****************************************************************************/ |
54 | 149 |
|
| 150 | +#undef vasprintf |
55 | 151 | int vasprintf(FAR char **ptr, FAR const IPTR char *fmt, va_list ap) |
56 | 152 | { |
57 | 153 | struct lib_outstream_s nulloutstream; |
|
0 commit comments