Change a field into a value. This will be tough given how we output lines. So, we might have to try a few things. Start with simply expanding/contracting the string to put in the new value.
| 295 | // So, we might have to try a few things. |
| 296 | // Start with simply expanding/contracting the string to put in the new value. |
| 297 | void changeFieldSplitLine(splitLine_t * line, int fnum, char * newValue) |
| 298 | { |
| 299 | // What we do will depend on the lengths of the two strings. |
| 300 | // So, start by calculaing these only once. |
| 301 | char * fp = line->fields[fnum]; |
| 302 | int oldLen = strlen(fp); |
| 303 | int newLen = strlen(newValue); |
| 304 | // Now see if we need to first change the length of the whole line. |
| 305 | int move = newLen - oldLen; |
| 306 | if (move != 0) |
| 307 | { |
| 308 | // This should never happen, but to be robust we need to check. |
| 309 | // It is messy to fix it, as all the field ptrs will now be wrong. |
| 310 | if ((size_t)(line->bufLen + move) >= line->maxBufLen) |
| 311 | { |
| 312 | resizeSplitLine(line, line->bufLen + move); |
| 313 | fp = line->fields[fnum]; |
| 314 | } |
| 315 | // Calculate the size of the tail that is still needed. |
| 316 | int distance = 1 + line->bufLen - (fp - line->buffer) - oldLen; |
| 317 | // Do the copy. |
| 318 | memmove(fp+newLen, fp+oldLen, distance); |
| 319 | // Correct the total length of the buffer. |
| 320 | line->bufLen += move; |
| 321 | // We need to correct the other ptrs as well. |
| 322 | for (int i=fnum+1; i<line->numFields; i++) line->fields[i] += move; |
| 323 | } |
| 324 | // Copy in the new value. |
| 325 | memcpy(fp, newValue, newLen); |
| 326 | } |
| 327 | |
| 328 | void addTag(splitLine_t * line, const char * header, const char * val) |
| 329 | { |
no test coverage detected