Advertisement
Guest User

blah

a guest
Nov 30th, 2011
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <assert.h>
  6. #include "uvic_formatter.h"
  7.  
  8. #define MAX_LINES 1000
  9. #define MAX_LINE_LENGTH 1000
  10. #define MODE_UNFILLED 0
  11. #define MODE_FILLED 1
  12. #define TRUE 1
  13. #define FALSE 0
  14.  
  15. /*
  16.  * Prototypes
  17.  */
  18.  
  19. void chomp(char line[]);
  20. void output_word(char *);
  21. void output_margin();
  22. void flush_line(char *);
  23.  
  24.  
  25. /*
  26.  * Globals
  27.  */
  28.  
  29. int page_width = 0;
  30. int margin_width = 0;
  31. int mode = MODE_UNFILLED;
  32. int newline_pending = FALSE;
  33. int output_line_length = 0;
  34. int line_count=1;
  35. char **result = NULL;
  36.  
  37. char **format_file(FILE *infile) {
  38.     char buffer[MAX_LINE_LENGTH+1];
  39.     char *t;
  40.     int temp;  
  41.  
  42.     assert (infile != NULL);
  43.     result = (char **)realloc (result,sizeof(char *) * line_count);
  44.     while (fgets(buffer, MAX_LINE_LENGTH, infile)) {
  45.         chomp(buffer);
  46.         if (strncmp(buffer, "?pagewidth ", 11) == 0) {
  47.             sscanf(buffer, "?pagewidth %d", &page_width);
  48.             mode = MODE_FILLED;
  49.             continue;
  50.         } else if (strncmp(buffer, "?margin ", 7) == 0) {
  51.             if (buffer[8] == '+' || buffer[8] == '-'){
  52.                 sscanf(buffer, "?margin %d", &temp);
  53.                 margin_width = margin_width + temp;
  54.             } else {
  55.                 sscanf(buffer, "?margin %d", &margin_width);
  56.             }
  57.  
  58.             if (margin_width <= 0){
  59.                 margin_width = 0;
  60.             } else if (page_width - margin_width < 20){
  61.                 margin_width = page_width - 20;
  62.             }
  63.             continue;
  64.         } else if (strcmp(buffer, "?mode filled") == 0 ) {
  65.             mode = MODE_FILLED;
  66.             continue;
  67.         } else if (strcmp(buffer, "?mode unfilled") == 0 ) {
  68.             mode = MODE_UNFILLED;  
  69.             continue;
  70.         }
  71.         /* If we reach this far, we have a regular line. */
  72.         if (mode == MODE_UNFILLED) {
  73.             flush_line(buffer);
  74.             continue;
  75.         }
  76.         /* If we reach this far, we have a regular line that
  77.          * requires formatting. Is it a blank line? If so, we
  78.          * need to flush the previous line and insert the
  79.          * blank line itself.
  80.          */
  81.         if (buffer[0] == '\0') {
  82.             flush_line("");
  83.         }
  84.  
  85.         t = strtok(buffer, " ");
  86.         while (t) {
  87.             output_word(t);
  88.             t = strtok(NULL, " ");
  89.         }
  90.     }
  91.     flush_line(NULL);
  92.  
  93.  
  94.  
  95.  
  96.     /*just temporary*/
  97.     return result;
  98. }
  99.  
  100.  
  101. char **format_lines(char **lines, int num_lines) {
  102.     char **result = NULL;
  103.  
  104. #ifdef DEBUG
  105.     result = (char **)malloc(sizeof(char *) * 2);
  106.     if (result == NULL) {
  107.         return NULL;
  108.     }
  109.  
  110.     result[0] = (char *)malloc(sizeof(char) * 80);
  111.     if (result[0] == NULL) {
  112.         return NULL;
  113.     }
  114.     strncpy(result[0], "(machine-like voice) EXTERMINATE THEM!", 79);
  115.  
  116.     result[1] = (char *)malloc(sizeof(char) * 2);
  117.     if (result[1] == NULL) {
  118.         return NULL;
  119.     }
  120.     result[1][0] = '\0';
  121. #endif
  122.  
  123.     return result;
  124. }
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132. /*
  133.  * Assumes the newline characters to be removed are
  134.  * at the end the line (i.e., '\n' in between
  135.  * regular chars will not be removed).
  136.  */
  137. void chomp(char line[]) {
  138.  
  139.     assert (line != NULL);
  140.  
  141.     if (strlen(line) == 0) {
  142.         return;
  143.     }
  144.  
  145.     while (line[strlen(line)-1] == '\n') {
  146.         line[strlen(line)-1] = '\0';
  147.     }
  148.  
  149.     return;
  150. }
  151.  
  152.  
  153.  
  154.  
  155.  
  156. /*
  157.  * The purpose of this function is to format the words that have
  158.  * been read so far, have not yet been output, and which consitute
  159.  * (in effect) the end of a paragraph.
  160.  */
  161.  
  162. void flush_line(char *line) {
  163.     if (newline_pending) {
  164.         line_count=line_count+1;
  165.         printf("1 \n");
  166.         result = (char **)realloc (result,sizeof(char *) * line_count);
  167.         newline_pending = FALSE;
  168.     }
  169.  
  170.     if (line != NULL && line[0] == '\0') {
  171.         line_count=line_count+1;
  172.         printf("2 \n");
  173.         result = (char **)realloc (result,sizeof(char *) * line_count);
  174.         output_line_length = 0;
  175.     } else if (line != NULL) {
  176.         output_margin();
  177.         if (result[line_count-1] == NULL){
  178.             printf("3 \n");
  179.             result[line_count-1]= (char *)realloc(result[line_count-1],(strlen(line)) * sizeof(char));
  180.         } else {
  181.             printf("4 \n");
  182.             result[line_count-1]= (char *)realloc(result[line_count-1],(strlen(result[line_count-1]) + strlen(line)) * sizeof(char));
  183.         }
  184.         strcat(result[line_count-1],line);
  185.         line_count=line_count+1;
  186.         printf("5 \n");
  187.         result = (char **)realloc (result,sizeof(char *) * line_count);
  188.         output_line_length = 0;
  189.     }
  190. }
  191.  
  192.  
  193. /*
  194.  * This function contains the line-break logic. Note that this
  195.  * function is called recursively in the case that a word causes
  196.  * a line break.
  197.  */
  198.  
  199. void output_word(char *word) {
  200.     if (newline_pending == FALSE) {
  201.         output_margin();
  202.         newline_pending = TRUE;
  203.         if (result[line_count-1] == NULL){
  204.             printf("6 \n");
  205.             result[line_count-1]= (char *)realloc(result[line_count-1],(strlen(word)) * sizeof(char));
  206.         } else {
  207.             printf("7 \n");
  208.             printf("1%s2 \n",result[line_count-1]);
  209.             printf("%s \n",word);
  210.             printf("%d \n",(strlen(result[line_count-1])+strlen(word))*sizeof(char));
  211.             result[line_count-1]= (char *)realloc(result[line_count-1],(strlen(result[line_count-1])+strlen(word))*sizeof(char));
  212.             printf("test \n");
  213.         }
  214.         strcat(result[line_count-1],word);
  215.         output_line_length = strlen(word);
  216.         return;
  217.     }
  218.  
  219.     if (output_line_length + 1 + strlen(word) <= page_width - margin_width) {
  220.         output_line_length += 1 + strlen(word);
  221.         if (result[line_count-1] == NULL){
  222.             printf("8 \n");
  223.             result[line_count-1]= (char *)realloc(result[line_count-1],(strlen(word)+1) * sizeof(char));
  224.         } else {
  225.             printf("9 \n");
  226.             result[line_count-1]= (char *)realloc(result[line_count-1],(strlen(result[line_count-1]) + strlen(word)+1) * sizeof(char));
  227.         }
  228.         strcat(result[line_count-1]," ");
  229.         strcat(result[line_count-1],word);
  230.     } else {
  231.         line_count=line_count+1;
  232.         printf("10 \n");
  233.         result = (char **)realloc (result,sizeof(char *) * line_count);
  234.         newline_pending = FALSE;
  235.         output_word(word);
  236.     }
  237. }
  238.  
  239.  
  240. void output_margin() {
  241.     int i;
  242.  
  243.     if (mode == MODE_UNFILLED) {
  244.         return;
  245.     }
  246.  
  247.     for (i = 0; i < margin_width; i++) {
  248.         if (result[line_count-1]==NULL){
  249.             printf("11 \n");
  250.             result[line_count-1]= (char *)realloc(result[line_count-1],strlen(" ") * sizeof(char));
  251.         } else {
  252.             printf("12 \n");
  253.             result[line_count-1]= (char *)realloc(result[line_count-1],(strlen(result[line_count-1]) + strlen(" ")) * sizeof(char));
  254.         }
  255.         strcat(result[line_count-1]," ");
  256.     }
  257. }
  258.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement