* appendStringInfoVA * * Attempt to format text data under the control of fmt (an sprintf-style * format string) and append it to whatever is already in str. If successful * return zero; if not (because there's not enough space), return an estimate * of the space needed, without modifying str. Typically the caller should * pass the return value to enlargeStringInfo() before trying again; s
| 146 | * that from here. |
| 147 | */ |
| 148 | int |
| 149 | appendStringInfoVA(StringInfo str, const char *fmt, va_list args) |
| 150 | { |
| 151 | int avail; |
| 152 | size_t nprinted; |
| 153 | |
| 154 | Assert(str != NULL); |
| 155 | |
| 156 | /* |
| 157 | * If there's hardly any space, don't bother trying, just fail to make the |
| 158 | * caller enlarge the buffer first. We have to guess at how much to |
| 159 | * enlarge, since we're skipping the formatting work. |
| 160 | */ |
| 161 | avail = str->maxlen - str->len; |
| 162 | if (avail < 16) |
| 163 | return 32; |
| 164 | |
| 165 | nprinted = pvsnprintf(str->data + str->len, (size_t) avail, fmt, args); |
| 166 | |
| 167 | if (nprinted < (size_t) avail) |
| 168 | { |
| 169 | /* Success. Note nprinted does not include trailing null. */ |
| 170 | str->len += (int) nprinted; |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | /* Restore the trailing null so that str is unmodified. */ |
| 175 | str->data[str->len] = '\0'; |
| 176 | |
| 177 | /* |
| 178 | * Return pvsnprintf's estimate of the space needed. (Although this is |
| 179 | * given as a size_t, we know it will fit in int because it's not more |
| 180 | * than MaxAllocSize.) |
| 181 | */ |
| 182 | return (int) nprinted; |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * appendStringInfoString |