[Solved] Sending some byte at time


Here is a solution in C. Hopefully I understood your question.

void send_substr(
    const char * str,
    size_t len,
    const size_t bytes_at_a_time,
    void (*sender)(const char *)
    )
/*
   sender() must check the char * manually for
   null termination or call strlen()

   for Unicode just change all size_t to unsigned long
   and all const char * to const wchar_t * (POSIX)
   or LPCWSTR (Win32)
 */
{
  size_t i, index_to_end, tail;

  //for C99 (gcc)
  char ret[bytes_at_a_time];

  //for C89 (Visual C++)
  //char * ret = (char *) malloc(sizeof(char)*bytes_at_a_time);

  tail = len % bytes_at_a_time;
  index_to_end = len - tail;

  for(i = 0; i < index_to_end; i += bytes_at_a_time)
  {
    memcpy(ret, str+i, bytes_at_a_time);
    *(ret + bytes_at_a_time) = '\0';
    (*sender)(ret);
  }
  memcpy(ret, str+index_to_end, tail);
  *(ret + tail) = '\0';
  (*sender)(ret);
  //for C89
  //free(ret);
}

void print_substr(const char * substr)
{
  while(*substr != '\0')
  {
    putchar(*substr);
    substr++;
  }
  putchar('\n');
}

int main()
{
  char test[] = "hello world, i'm happy to meet you all."
    " Let be friends or maybe more, but nothing less";
  send_substr(test, sizeof(test)/sizeof(*test), 5, &print_substr);

  return 0;
}

3

solved Sending some byte at time