#include #include "reverse.h" #if (WITH_ARRAY) #ifndef MAX_INPUT_SIZE #define MAX_INPUT_SIZE 1024 #endif static char lines[MAX_INPUT_SIZE][MAX_LINE_SIZE]; static int line_count; void reverse_init() { line_count = 0; } int reverse_add_line(const char * l) { if (line_count < MAX_INPUT_SIZE) { int i; for(i = 0; i < MAX_LINE_SIZE - 1 && l[i]; ++i) lines[line_count][i] = l[i]; lines[line_count][i] = 0; ++line_count; return 1; } else { return 0; } } void reverse_print() { int i; for(i = line_count; i > 0; --i) printf("%s", lines[i - 1]); } void reverse_shutdown() { line_count = 0; } #else #include struct line { char chars[MAX_LINE_SIZE]; struct line * next; }; static struct line * first_line = 0; void reverse_init() { first_line = 0; } int reverse_add_line(const char * l) { struct line * new_line = malloc(sizeof(struct line)); if (!new_line) return 0; int i; for(i = 0; i < MAX_LINE_SIZE - 1 && l[i] != 0; ++i) new_line->chars[i] = l[i]; new_line->chars[i] = 0; new_line->next = first_line; first_line = new_line; return 1; } void reverse_print() { struct line * l; for(l = first_line; l != 0; l = l->next) printf("%s", l->chars); } void reverse_shutdown() { while (first_line) { struct line * next = first_line->next; free(first_line); first_line = next; } } #endif