Replaced get_line() with new implementation which is faster, less buggy!

This commit is contained in:
Bobbi Webber-Manners 2020-07-29 17:28:35 -04:00
parent 24f022f0bd
commit d6841c517c
5 changed files with 88 additions and 128 deletions

View File

@ -99,41 +99,28 @@ void readconfigfile(void) {
}
/*
* Read a text file a line at a time leaving the line in linebuf[]
* Read a text file a line at a time
* Returns number of chars in the line, or -1 if EOF.
* Expects Apple ][ style line endings (CR) and does no conversion
* fp - file to read from
* reset - if 1 then just reset the buffer and return
* writep - Pointer to buffer into which line will be written
*/
int16_t get_line(FILE *fp, uint8_t reset) {
int16_t get_line(FILE *fp, char *writep) {
static uint16_t rd = 0; // Read
static uint16_t wt = 0; // Write
uint8_t found = 0;
uint16_t j = 0;
uint16_t i;
if (reset) {
rd = wt = 0;
return 0;
}
static uint16_t end = 0; // End of valid data in buf
uint16_t i = 0;
while (1) {
while (rd < wt) {
linebuf[j++] = buf[rd++];
if (linebuf[j - 1] == '\r') {
found = 1;
break;
}
if (rd == end) {
end = fread(buf, 1, READSZ, fp);
rd = 0;
}
linebuf[j] = '\0';
if (rd == wt) // Empty buf
rd = wt = 0;
if (found) {
return j;
if (end == 0)
return -1; // EOF
writep[i++] = buf[rd++];
if (writep[i - 1] == '\r') {
writep[i] = '\0';
return i;
}
if (feof(fp)) {
return -1;
}
i = fread(&buf[wt], 1, READSZ - wt, fp);
wt += i;
}
}
@ -246,10 +233,9 @@ void attach(char *fname) {
if (!destfp)
error(ERR_FATAL, "Can't open TMPFILE");
get_line(fp, 1); // Reset buffer
printf("Copying email content ... "); // Space is for spinner to eat
size = 0;
while ((chars = get_line(fp, 0)) != -1) {
while ((chars = get_line(fp, linebuf)) != -1) {
size += chars;
if (linebuf[0] == '\r')
break;
@ -262,7 +248,7 @@ void attach(char *fname) {
fprintf(destfp, "--a2forever\r");
fprintf(destfp, "Content-Type: text/plain; charset=US-ASCII\r");
fprintf(destfp, "Content-Transfer-Encoding: 7bit\r\r");
while ((chars = get_line(fp, 0)) != -1) {
while ((chars = get_line(fp, linebuf)) != -1) {
size += chars;
fputs(linebuf, destfp);
spinner(size, 0);

View File

@ -503,33 +503,26 @@ void update_highlighted(void) {
*/
int16_t get_line(FILE *fp, uint8_t reset, char *writep, uint32_t *pos) {
static uint16_t rd = 0; // Read
static uint16_t wt = 0; // Write
uint8_t found = 0;
uint16_t j = 0;
uint16_t i;
static uint16_t end = 0; // End of valid data in buf
uint16_t i = 0;
if (reset) {
rd = wt = 0;
rd = end = 0;
*pos = 0;
return 0;
}
while (1) {
while (rd < wt) {
writep[j++] = buf[rd++];
++(*pos);
if (writep[j - 1] == '\r') {
found = 1;
break;
}
if (rd == end) {
end = fread(buf, 1, READSZ, fp);
rd = 0;
}
if (end == 0)
return -1; // EOF
writep[i++] = buf[rd++];
++(*pos);
if (writep[i - 1] == '\r') {
writep[i] = '\0';
return i;
}
writep[j] = '\0';
if (rd == wt) // Empty buf[]
rd = wt = 0;
if (found)
return j;
if (feof(fp))
return -1;
i = fread(&buf[wt], 1, READSZ - wt, fp);
wt += i;
}
}
@ -936,7 +929,7 @@ restart:
case ENC_B64:
//chars = decode_base64(writep);
{
uint16_t i = 0, j = 0;
uint8_t i = 0, j = 0;
while (writep[i] != '\r') {
writep[j++] = b[writep[i]] << 2 | b[writep[i + 1]] >> 4;
if (writep[i + 2] != '=')

View File

@ -310,36 +310,29 @@ void readconfigfile(void) {
}
/*
* Read a text file a line at a time leaving the line in linebuf[]
* Read a text file a line at a time
* Returns number of chars in the line, or -1 if EOF.
* Converts line endings from CRLF -> CR (Apple ][ style)
* Expects CRLF line endings (CRLF) and converts to Apple II (CR) convention
* fp - file to read from
* writep - Pointer to buffer into which line will be written
*/
int16_t get_line(FILE *fp) {
int16_t get_line(FILE *fp, char *writep) {
static uint16_t rd = 0; // Read
static uint16_t wt = 0; // Write
uint8_t found = 0;
uint16_t j = 0;
uint16_t i;
static uint16_t end = 0; // End of valid data in buf
uint16_t i = 0;
while (1) {
while (rd < wt) {
linebuf[j++] = buf[rd++];
// The following line is safe because of linebuf_pad[]
if ((linebuf[j - 1] == '\n') && (linebuf[j - 2] == '\r')) {
found = 1;
break;
}
if (rd == end) {
end = fread(buf, 1, READSZ, fp);
rd = 0;
}
linebuf[j] = '\0';
if (rd == wt) // Empty buf[]
rd = wt = 0;
if (found) {
linebuf[j - 1] = '\0'; // Remove LF from end
return j - 1;
if (end == 0)
return -1; // EOF
writep[i++] = buf[rd++];
// The following line is safe because of linebuf_pad[]
if ((writep[i - 1] == '\n') && (writep[i - 2] == '\r')) {
writep[i - 1] = '\0'; // Remove LF
return i - 1;
}
if (feof(fp))
return -1;
i = fread(&buf[wt], 1, READSZ - wt, fp);
wt += i;
}
}
@ -433,7 +426,7 @@ void update_inbox(uint16_t nummsgs) {
hdrs.skipbytes = 0; // Just in case it doesn't get set
hdrs.status = 'N';
hdrs.tag = ' ';
while ((chars = get_line(fp)) != -1) {
while ((chars = get_line(fp, linebuf)) != -1) {
if (headers) {
headerchars += chars;
if (!strncmp(linebuf, "Date: ", 6)) {

View File

@ -44,33 +44,29 @@ void error_exit() {
}
/*
* Read a text file a line at a time leaving the line in linebuf[]
* Read a text file a line at a time
* Returns number of chars in the line, or -1 if EOF.
* Converts line endings from CRLF -> CR (Apple ][ style)
* Expects Apple ][ style line endings (CR) and does no conversion
* fp - file to read from
* writep - Pointer to buffer into which line will be written
* pos - position in file is updated via this pointer
*/
int16_t get_line(FILE *fp) {
int16_t get_line(FILE *fp, char *writep) {
static uint16_t rd = 0; // Read
static uint16_t wt = 0; // Write
uint8_t found = 0;
uint16_t j = 0;
uint16_t i;
static uint16_t end = 0; // End of valid data in buf
uint16_t i = 0;
while (1) {
while (rd < wt) {
linebuf[j++] = buf[rd++];
if (linebuf[j - 1] == '\r') {
found = 1;
break;
}
if (rd == end) {
end = fread(buf, 1, READSZ, fp);
rd = 0;
}
if (end == 0)
return -1; // EOF
writep[i++] = buf[rd++];
if (writep[i - 1] == '\r') {
writep[i] = '\0';
return i;
}
linebuf[j] = '\0';
if (rd == wt) // Empty buf[]
rd = wt = 0;
if (found)
return j;
if (feof(fp))
return -1;
i = fread(&buf[wt], 1, READSZ - wt, fp);
wt += i;
}
}
@ -192,7 +188,7 @@ void repair_mailbox(void) {
hdrs.skipbytes = 0; // Just in case it doesn't get set
hdrs.status = 'R';
hdrs.tag = ' ';
while ((chars = get_line(fp)) != -1) {
while ((chars = get_line(fp, linebuf)) != -1) {
if (headers) {
headerchars += chars;
if (!strncmp(linebuf, "Date: ", 6)) {

View File

@ -104,41 +104,33 @@ void spinner(uint32_t sz, uint8_t final) {
}
/*
* Read a text file a line at a time leaving the line in linebuf[]
* Read a text file a line at a time
* Returns number of chars in the line, or -1 if EOF.
* Expects Apple ][ style line endings (CR) and does no conversion
* fp - file to read from
* reset - if 1 then just reset the buffer and return
* writep - Pointer to buffer into which line will be written
*/
int16_t get_line(FILE *fp, uint8_t reset) {
int16_t get_line(FILE *fp, uint8_t reset, char *writep) {
static uint16_t rd = 0; // Read
static uint16_t wt = 0; // Write
uint8_t found = 0;
uint16_t j = 0;
uint16_t i;
static uint16_t end = 0; // End of valid data in buf
uint16_t i = 0;
if (reset) {
rd = wt = 0;
rd = end = 0;
return 0;
}
while (1) {
while (rd < wt) {
linebuf[j++] = buf[rd++];
if (linebuf[j - 1] == '\r') {
found = 1;
break;
}
if (rd == end) {
end = fread(buf, 1, READSZ, fp);
rd = 0;
}
linebuf[j] = '\0';
if (rd == wt) // Empty buf
rd = wt = 0;
if (found) {
return j;
if (end == 0)
return -1; // EOF
writep[i++] = buf[rd++];
if (writep[i - 1] == '\r') {
writep[i] = '\0';
return i;
}
if (feof(fp)) {
return -1;
}
i = fread(&buf[wt], 1, READSZ - wt, fp);
wt += i;
}
}
@ -169,11 +161,11 @@ bool w5100_tcp_send_recv(char* sendbuf, char* recvbuf, size_t length,
int16_t len;
filesize = 0;
len = get_line(fp, 1); // Reset buffer
len = get_line(fp, 1, linebuf); // Reset buffer
while (cont) {
len = get_line(fp, 0);
len = get_line(fp, 0, linebuf);
pos = 0;
if (len == -1) {
@ -444,8 +436,8 @@ void update_sent_mbox(char *name) {
hdrs.skipbytes = 0; // Just in case it doesn't get set
hdrs.status = 'N';
hdrs.tag = ' ';
get_line(fp, 1); // Reset buffer
while ((chars = get_line(fp, 0)) != -1) {
get_line(fp, 1, linebuf); // Reset buffer
while ((chars = get_line(fp, 0, linebuf)) != -1) {
if (headers) {
headerchars += chars;
if (!strncmp(linebuf, "Date: ", 6)) {
@ -577,7 +569,7 @@ void main(int argc, char *argv[]) {
strcpy(recipients, "");
while (1) {
if ((get_line(fp, 0) == -1) || (linecount == 20)) {
if ((get_line(fp, 0, linebuf) == -1) || (linecount == 20)) {
if (strlen(recipients) == 0) {
printf("No recipients (To or Cc) in %s. Skipping msg.\n", d->d_name);
goto skiptonext;