Advertisement
trannus_aran

smart_append working!

Jan 21st, 2025 (edited)
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.74 KB | None | 0 0
  1. #include <string.h>
  2. #include "exercise.h"
  3.  
  4. int smart_append(TextBuffer* dest, const char* src) {
  5.   /* REMEMBER: Length != Size */
  6.   int src_len, remaining_space;
  7.   int current_buf_len;
  8.   int const MAX_BUF_SIZE = 64;
  9.   if (dest == NULL || src == NULL)
  10.     return 1;
  11.   src_len = strlen(src);
  12.   current_buf_len = strlen(dest->buffer);
  13.   /* Sub buffer len from max_size, acct'ing for '\0' */
  14.   remaining_space = MAX_BUF_SIZE - (current_buf_len + 1);
  15.   if (src_len > remaining_space) {
  16.     /* Cp as much as possible, update & return 1 */
  17.     strncat(dest->buffer, src, remaining_space);
  18.     dest->length = MAX_BUF_SIZE - 1;
  19.     return 1;  
  20.   } else {
  21.     strncat(dest->buffer, src, src_len);
  22.     dest->length += src_len;
  23.     return 0;
  24.   }
  25. }
  26.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement