Reads the input file in one pass. Nicer repetition of spaces in output (robertlipe).

This commit is contained in:
nanochess 2017-11-06 10:27:10 -06:00
parent 5e4600d5e2
commit f6ab2c4942

View File

@ -37,8 +37,8 @@ void request_space(FILE *output, int *current, int new, int force)
*/ */
while (1) { while (1) {
if (tabs == 0) { if (tabs == 0) {
fputc(' ', output); fprintf(output, "%*s", new - *current, "");
(*current)++; *current = new;
} else { } else {
fputc('\t', output); fputc('\t', output);
*current = (*current + tabs) / tabs * tabs; *current = (*current + tabs) / tabs * tabs;
@ -61,12 +61,8 @@ int main(int argc, char *argv[])
int align_comment; int align_comment;
FILE *input; FILE *input;
FILE *output; FILE *output;
char line[4096];
int input_lines;
int allocation; int allocation;
char *data; char *data;
int current_line;
int next_position;
char *p; char *p;
char *p1; char *p1;
char *p2; char *p2;
@ -198,43 +194,53 @@ int main(int argc, char *argv[])
/* /*
** Open input file, measure it and read it into buffer ** Open input file, measure it and read it into buffer
*/ */
input = fopen(argv[c], "r"); input = fopen(argv[c], "rb");
if (input == NULL) { if (input == NULL) {
fprintf(stderr, "Unable to open input file: %s\n", argv[c]); fprintf(stderr, "Unable to open input file: %s\n", argv[c]);
exit(1); exit(1);
} }
fprintf(stderr, "Processing %s...\n", argv[c]); fprintf(stderr, "Processing %s...\n", argv[c]);
input_lines = 0; fseek(input, 0, SEEK_END);
allocation = 0; allocation = ftell(input);
while (fgets(line, sizeof(line) - 1, input)) { data = malloc(allocation + sizeof(char));
allocation += strlen(line) + 1;
input_lines++;
}
fclose(input);
data = malloc(allocation);
if (data == NULL) { if (data == NULL) {
fprintf(stderr, "Unable to allocate memory\n"); fprintf(stderr, "Unable to allocate memory\n");
exit(1);
}
input = fopen(argv[c], "r");
if (input == NULL) {
fprintf(stderr, "Unable to open input file: %s\n", argv[c]);
exit(1);
}
current_line = 0;
next_position = 0;
while (fgets(line, sizeof(line) - 1, input)) {
if (current_line >= input_lines || next_position + strlen(line) + 1 > allocation) {
fprintf(stderr, "Buuurp! the source file grew!\n");
fclose(input); fclose(input);
exit(1); exit(1);
} }
strcpy(data + next_position, line); fseek(input, 0, SEEK_SET);
next_position += strlen(line) + 1; if (fread(data, sizeof(char), allocation, input) != allocation) {
current_line++; fprintf(stderr, "Something went wrong reading the input file\n");
fclose(input);
free(data);
exit(1);
} }
fclose(input); fclose(input);
/*
** Ease processing of input file
*/
request = 0;
p1 = data;
p2 = data;
while (p1 < data + allocation) {
if (*p1 == '\r') { /* Ignore \r characters */
p1++;
continue;
}
if (*p1 == '\n') {
p1++;
*p2++ = '\0'; /* Break line */
request = 1;
continue;
}
*p2++ = *p1++;
request = 0;
}
if (request == 0)
*p2++ = '\0'; /* Force line break */
allocation = p2 - data;
/* /*
** Now generate output file ** Now generate output file
*/ */
@ -245,7 +251,7 @@ int main(int argc, char *argv[])
exit(1); exit(1);
} }
p = data; p = data;
for (input_lines = 0; input_lines < current_line; input_lines++) { while (p < data + allocation) {
current_column = 0; current_column = 0;
p1 = p; p1 = p;
if (*p1 && !isspace(*p1) && *p1 != ';') { /* Label */ if (*p1 && !isspace(*p1) && *p1 != ';') { /* Label */
@ -276,7 +282,7 @@ int main(int argc, char *argv[])
request = start_operand; request = start_operand;
request_space(output, &current_column, request, 1); request_space(output, &current_column, request, 1);
p2 = p1; p2 = p1;
while (*p2 && *p2 != ';' && *p2 != '\r' && *p2 != '\n') { while (*p2 && *p2 != ';') {
if (*p2 == '"') { if (*p2 == '"') {
p2++; p2++;
while (*p2 && *p2 != '"') while (*p2 && *p2 != '"')
@ -311,7 +317,7 @@ int main(int argc, char *argv[])
request = start_mnemonic; request = start_mnemonic;
request_space(output, &current_column, request, 0); request_space(output, &current_column, request, 0);
p2 = p1; p2 = p1;
while (*p2 && *p2 != '\r' && *p2 != '\n') while (*p2)
p2++; p2++;
while (p2 > p1 && isspace(*(p2 - 1))) while (p2 > p1 && isspace(*(p2 - 1)))
p2--; p2--;