Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include <assert.h>
- #include "uvic_formatter.h"
- #define MAX_LINES 1000
- #define MAX_LINE_LENGTH 1000
- #define MODE_UNFILLED 0
- #define MODE_FILLED 1
- #define TRUE 1
- #define FALSE 0
- /*
- * Prototypes
- */
- void chomp(char line[]);
- void output_word(char *);
- void output_margin();
- void flush_line(char *);
- /*
- * Globals
- */
- int page_width = 0;
- int margin_width = 0;
- int mode = MODE_UNFILLED;
- int newline_pending = FALSE;
- int output_line_length = 0;
- int line_count=1;
- char **result = NULL;
- char **format_file(FILE *infile) {
- char buffer[MAX_LINE_LENGTH+1];
- char *t;
- int temp;
- assert (infile != NULL);
- result = (char **)realloc (result,sizeof(char *) * line_count);
- while (fgets(buffer, MAX_LINE_LENGTH, infile)) {
- chomp(buffer);
- if (strncmp(buffer, "?pagewidth ", 11) == 0) {
- sscanf(buffer, "?pagewidth %d", &page_width);
- mode = MODE_FILLED;
- continue;
- } else if (strncmp(buffer, "?margin ", 7) == 0) {
- if (buffer[8] == '+' || buffer[8] == '-'){
- sscanf(buffer, "?margin %d", &temp);
- margin_width = margin_width + temp;
- } else {
- sscanf(buffer, "?margin %d", &margin_width);
- }
- if (margin_width <= 0){
- margin_width = 0;
- } else if (page_width - margin_width < 20){
- margin_width = page_width - 20;
- }
- continue;
- } else if (strcmp(buffer, "?mode filled") == 0 ) {
- mode = MODE_FILLED;
- continue;
- } else if (strcmp(buffer, "?mode unfilled") == 0 ) {
- mode = MODE_UNFILLED;
- continue;
- }
- /* If we reach this far, we have a regular line. */
- if (mode == MODE_UNFILLED) {
- flush_line(buffer);
- continue;
- }
- /* If we reach this far, we have a regular line that
- * requires formatting. Is it a blank line? If so, we
- * need to flush the previous line and insert the
- * blank line itself.
- */
- if (buffer[0] == '\0') {
- flush_line("");
- }
- t = strtok(buffer, " ");
- while (t) {
- output_word(t);
- t = strtok(NULL, " ");
- }
- }
- flush_line(NULL);
- /*just temporary*/
- return result;
- }
- char **format_lines(char **lines, int num_lines) {
- char **result = NULL;
- #ifdef DEBUG
- result = (char **)malloc(sizeof(char *) * 2);
- if (result == NULL) {
- return NULL;
- }
- result[0] = (char *)malloc(sizeof(char) * 80);
- if (result[0] == NULL) {
- return NULL;
- }
- strncpy(result[0], "(machine-like voice) EXTERMINATE THEM!", 79);
- result[1] = (char *)malloc(sizeof(char) * 2);
- if (result[1] == NULL) {
- return NULL;
- }
- result[1][0] = '\0';
- #endif
- return result;
- }
- /*
- * Assumes the newline characters to be removed are
- * at the end the line (i.e., '\n' in between
- * regular chars will not be removed).
- */
- void chomp(char line[]) {
- assert (line != NULL);
- if (strlen(line) == 0) {
- return;
- }
- while (line[strlen(line)-1] == '\n') {
- line[strlen(line)-1] = '\0';
- }
- return;
- }
- /*
- * The purpose of this function is to format the words that have
- * been read so far, have not yet been output, and which consitute
- * (in effect) the end of a paragraph.
- */
- void flush_line(char *line) {
- if (newline_pending) {
- line_count=line_count+1;
- printf("1 \n");
- result = (char **)realloc (result,sizeof(char *) * line_count);
- newline_pending = FALSE;
- }
- if (line != NULL && line[0] == '\0') {
- line_count=line_count+1;
- printf("2 \n");
- result = (char **)realloc (result,sizeof(char *) * line_count);
- output_line_length = 0;
- } else if (line != NULL) {
- output_margin();
- if (result[line_count-1] == NULL){
- printf("3 \n");
- result[line_count-1]= (char *)realloc(result[line_count-1],(strlen(line)) * sizeof(char));
- } else {
- printf("4 \n");
- result[line_count-1]= (char *)realloc(result[line_count-1],(strlen(result[line_count-1]) + strlen(line)) * sizeof(char));
- }
- strcat(result[line_count-1],line);
- line_count=line_count+1;
- printf("5 \n");
- result = (char **)realloc (result,sizeof(char *) * line_count);
- output_line_length = 0;
- }
- }
- /*
- * This function contains the line-break logic. Note that this
- * function is called recursively in the case that a word causes
- * a line break.
- */
- void output_word(char *word) {
- if (newline_pending == FALSE) {
- output_margin();
- newline_pending = TRUE;
- if (result[line_count-1] == NULL){
- printf("6 \n");
- result[line_count-1]= (char *)realloc(result[line_count-1],(strlen(word)) * sizeof(char));
- } else {
- printf("7 \n");
- printf("1%s2 \n",result[line_count-1]);
- printf("%s \n",word);
- printf("%d \n",(strlen(result[line_count-1])+strlen(word))*sizeof(char));
- result[line_count-1]= (char *)realloc(result[line_count-1],(strlen(result[line_count-1])+strlen(word))*sizeof(char));
- printf("test \n");
- }
- strcat(result[line_count-1],word);
- output_line_length = strlen(word);
- return;
- }
- if (output_line_length + 1 + strlen(word) <= page_width - margin_width) {
- output_line_length += 1 + strlen(word);
- if (result[line_count-1] == NULL){
- printf("8 \n");
- result[line_count-1]= (char *)realloc(result[line_count-1],(strlen(word)+1) * sizeof(char));
- } else {
- printf("9 \n");
- result[line_count-1]= (char *)realloc(result[line_count-1],(strlen(result[line_count-1]) + strlen(word)+1) * sizeof(char));
- }
- strcat(result[line_count-1]," ");
- strcat(result[line_count-1],word);
- } else {
- line_count=line_count+1;
- printf("10 \n");
- result = (char **)realloc (result,sizeof(char *) * line_count);
- newline_pending = FALSE;
- output_word(word);
- }
- }
- void output_margin() {
- int i;
- if (mode == MODE_UNFILLED) {
- return;
- }
- for (i = 0; i < margin_width; i++) {
- if (result[line_count-1]==NULL){
- printf("11 \n");
- result[line_count-1]= (char *)realloc(result[line_count-1],strlen(" ") * sizeof(char));
- } else {
- printf("12 \n");
- result[line_count-1]= (char *)realloc(result[line_count-1],(strlen(result[line_count-1]) + strlen(" ")) * sizeof(char));
- }
- strcat(result[line_count-1]," ");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement