Browse Source

fix first line bug

master 0.1.0
alistair 3 years ago
parent
commit
632bb6ef66
  1. 16
      cowsay.c

16
cowsay.c

@ -9,6 +9,8 @@
#include "cosmopolitan.h" #include "cosmopolitan.h"
#endif #endif
int replace_tabs(char **rtext);
int read_file(FILE* in, char **out) { int read_file(FILE* in, char **out) {
int size_of_buffer = 20; int size_of_buffer = 20;
char *buffer = calloc(size_of_buffer, sizeof(char)); char *buffer = calloc(size_of_buffer, sizeof(char));
@ -127,8 +129,9 @@ char *read_stdin(void) {
return NULL; return NULL;
} else { } else {
strip_excess_char(&text, '\n'); strip_excess_char(&text, '\n');
strip_excess_char(&text, '\t'); replace_tabs(&text);
strip_excess_char(&text, ' '); //strip_excess_char(&text, '\t');
//strip_excess_char(&text, ' ');
strip_tailing_whitespace(text); strip_tailing_whitespace(text);
return text; return text;
} }
@ -163,7 +166,7 @@ void insert_char(char **str, char c, int p) {
void wrap_text(char *text, int width) { void wrap_text(char *text, int width) {
char *last_break = NULL; char *last_break = NULL;
int current_line = 0; int current_line = 1;
int last_break_pos = 0; int last_break_pos = 0;
for (int i = 0; text[i] != '\0'; i++) { for (int i = 0; text[i] != '\0'; i++) {
@ -174,8 +177,6 @@ void wrap_text(char *text, int width) {
current_line = 0; current_line = 0;
} }
char *breaks = "\t ";
// set whitespace to be break point // set whitespace to be break point
if ((text[i] == ' ') && current_line <= width + 2) { if ((text[i] == ' ') && current_line <= width + 2) {
last_break_pos = current_line; last_break_pos = current_line;
@ -188,6 +189,7 @@ void wrap_text(char *text, int width) {
last_break = NULL; last_break = NULL;
current_line = 0; current_line = 0;
} else if (current_line > width + 1) { } else if (current_line > width + 1) {
// normal break
*last_break = '\n'; *last_break = '\n';
last_break = NULL; last_break = NULL;
current_line = current_line - last_break_pos; current_line = current_line - last_break_pos;
@ -292,6 +294,10 @@ int replace_tabs(char **rtext) {
tab = strstr(tab, "\t"); tab = strstr(tab, "\t");
} }
if (numtabs == 0) {
return 0;
}
len += 3 * numtabs; len += 3 * numtabs;
char * result = malloc(len); char * result = malloc(len);

Loading…
Cancel
Save