void put_chars(char *chars, unsigned int size, gboolean crlf_auto) { char *characters; int pos; GString *buffer_tmp; gchar *in_buffer; buffer_tmp = g_string_new(chars); /* If the auto CR LF mode on, read the buffer to add \r before \n */ if(crlf_auto) { in_buffer=buffer_tmp->str; in_buffer += size; for(pos=size; pos>0; pos--) { in_buffer--; if(*in_buffer=='\n' && *(in_buffer-1) != '\r') { g_string_insert_c(buffer_tmp, pos-1, '\r'); size += 1; } if(*in_buffer=='\r' && *(in_buffer+1) != '\n') { g_string_insert_c(buffer_tmp, pos, '\n'); size += 1; } } chars = buffer_tmp->str; } if(buffer == NULL) { i18n_printf(_("ERROR : Buffer is not initialized !\n")); return; } if(size > BUFFER_SIZE) { characters = chars + (size - BUFFER_SIZE); size = BUFFER_SIZE; } else characters = chars; if((size + pointer) >= BUFFER_SIZE) { memcpy(current_buffer, characters, BUFFER_SIZE - pointer); chars = characters + BUFFER_SIZE - pointer; pointer = size - (BUFFER_SIZE - pointer); memcpy(buffer, chars, pointer); current_buffer = buffer + pointer; overlapped = 1; } else { memcpy(current_buffer, characters, size); pointer += size; current_buffer += size; } if(write_func != NULL) write_func(characters, size); g_string_free(buffer_tmp, 1); }
void put_chars(char *chars, unsigned int size, gboolean crlf_auto) { char *characters; /* If the auto CR LF mode on, read the buffer to add \r before \n */ if(crlf_auto) { /* BUFFER_RECEPTION*2 for worst case scenario, all \n or \r chars */ char out_buffer[BUFFER_RECEPTION*2]; int i, out_size = 0; for (i=0; i<size; i++) { if (chars[i] == '\r') { /* If the previous character was a CR too, insert a newline */ if (cr_received) { out_buffer[out_size] = '\n'; out_size++; } cr_received = 1; } else { if (chars[i] == '\n') { /* If we get a newline without a CR first, insert a CR */ if (!cr_received) { out_buffer[out_size] = '\r'; out_size++; } } else { /* If we receive a normal char, and the previous one was a CR insert a newline */ if (cr_received) { out_buffer[out_size] = '\n'; out_size++; } } cr_received = 0; } out_buffer[out_size] = chars[i]; out_size++; } chars = out_buffer; size = out_size; } if(buffer == NULL) { i18n_printf(_("ERROR : Buffer is not initialized !\n")); return; } if(size > BUFFER_SIZE) { characters = chars + (size - BUFFER_SIZE); size = BUFFER_SIZE; } else characters = chars; if((size + pointer) >= BUFFER_SIZE) { memcpy(current_buffer, characters, BUFFER_SIZE - pointer); chars = characters + BUFFER_SIZE - pointer; pointer = size - (BUFFER_SIZE - pointer); memcpy(buffer, chars, pointer); current_buffer = buffer + pointer; overlapped = 1; } else { memcpy(current_buffer, characters, size); pointer += size; current_buffer += size; } if(write_func != NULL) write_func(characters, size); }