rocky1oo

document.c

Mar 29th, 2020
1,121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.78 KB | None | 0 0
  1. #include "document.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. int makeBlankParagraph(Paragraph *p);
  7. int makeBlankLine(Paragraph *p, int line);
  8. int shiftParagraphsByOne(Document *doc, int paragraph_number);
  9. int shiftLinesByOne (Document *doc, int paragraph_number, int line_number);
  10. void removeStringTrailingNewline(char *str);
  11.  
  12. int init_document(Document *doc, const char *name) {
  13.     int p, l;
  14.     char lines[MAX_PARAGRAPH_LINES][MAX_STR_SIZE + 1];
  15.  
  16.     if (doc == NULL || name == NULL || MAX_STR_SIZE < strlen(name)) {
  17.         return FAILURE;
  18.     }
  19.    
  20.     strcpy(doc->name, name);
  21.     doc->number_of_paragraphs = 0;
  22.  
  23.     for(p = 0; p < MAX_PARAGRAPHS; p++) {
  24.         makeBlankParagraph(&doc->paragraphs[p]);
  25.     }
  26.     return SUCCESS;
  27. }
  28.  
  29. int reset_document(Document *doc) {
  30.     if (doc == NULL) {
  31.         return FAILURE;
  32.     }
  33.  
  34.     doc->number_of_paragraphs = 0;
  35.     return SUCCESS;
  36. }
  37.  
  38. int print_document(Document *doc) {
  39.     int p, l;
  40.     if (doc == NULL) {
  41.         return FAILURE;
  42.     }
  43.  
  44.     printf("Document name: \"%s\"\n", doc->name);
  45.     printf("Number of Paragraphs: %d\n", doc->number_of_paragraphs);
  46.    
  47.     for(p = 0; p < doc->number_of_paragraphs; p++) {
  48.         /*printf("P: %d L: %d\n", p, doc->paragraphs[p].number_of_lines);*/
  49.         for(l = 0; l < doc->paragraphs[p].number_of_lines; l++) {
  50.             if(!is_blank_line(doc->paragraphs[p].lines[l])) {
  51.                 printf("%s\n", doc->paragraphs[p].lines[l]);
  52.             }/* else {
  53.                 printf("Condition Failed at (indexes) %d %d\n", p, l);
  54.             }*/
  55.         }
  56.  
  57.         if(p < doc->number_of_paragraphs - 1) printf("\n");
  58.     }
  59.  
  60.     return SUCCESS;
  61.  
  62. }
  63.  
  64. int print_document_detail(Document *doc) {
  65.     int p, l;
  66.     if (doc == NULL) {
  67.         return FAILURE;
  68.     }
  69.  
  70.     printf("Document name: \"%s\"\n", doc->name);
  71.     printf("Number of Paragraphs: %d\n", doc->number_of_paragraphs);
  72.    
  73.     for(p = 0; p < doc->number_of_paragraphs; p++) {
  74.         /*printf("P: %d L: %d\n", doc->number_of_paragraphs, doc->paragraphs[p].number_of_lines);*/
  75.         for(l = 0; l < doc->paragraphs[p].number_of_lines; l++) {
  76.             if(!is_blank_line(doc->paragraphs[p].lines[l])) {
  77.                 printf("Paragraph %d: %s\n", p, doc->paragraphs[p].lines[l]);
  78.             }/* else {
  79.                 printf("Condition Failed at (indexes) %d %d\n", p, l);
  80.             }*/
  81.         }
  82.             if(p < doc->number_of_paragraphs - 1) printf("\n");
  83.     }
  84.  
  85.     return SUCCESS;
  86.  
  87. }
  88.  
  89. int add_paragraph_after(Document *doc, int paragraph_number) {
  90.         if (doc == NULL || doc->number_of_paragraphs >= MAX_PARAGRAPHS || paragraph_number > MAX_PARAGRAPHS || paragraph_number > doc->number_of_paragraphs) {
  91.             return FAILURE;
  92.         }
  93.  
  94.         /*printf("\nBefore adding paragraph after %d \n", paragraph_number);*/
  95.  
  96.         if(paragraph_number < doc->number_of_paragraphs) {
  97.             /*printf("\nGoing to shift because Paragraph Number: %d < number_of_paragraphs: %d \n", paragraph_number, doc->number_of_paragraphs);*/
  98.             shiftParagraphsByOne(doc, paragraph_number);
  99.         }
  100.  
  101.         doc->number_of_paragraphs++;
  102.         /*printf("\nAfter adding paragraph after %d \n", paragraph_number);*/
  103.         return SUCCESS;
  104.     }
  105.  
  106. int add_line_after(Document *doc, int paragraph_number, int line_number, const char *new_line) {
  107.     if (doc == NULL || doc->number_of_paragraphs > MAX_PARAGRAPHS || paragraph_number > MAX_PARAGRAPHS
  108.             || paragraph_number <= 0 || line_number > MAX_PARAGRAPH_LINES
  109.             || doc->paragraphs[paragraph_number].number_of_lines > MAX_PARAGRAPH_LINES) {
  110.             return FAILURE;
  111.     }
  112.  
  113.     if(line_number < doc->paragraphs[paragraph_number - 1].number_of_lines) {
  114.         shiftLinesByOne(doc, paragraph_number, line_number);
  115.     }
  116.  
  117.     strcpy(doc->paragraphs[paragraph_number - 1].lines[line_number], new_line);
  118.     /*printf("Copied \"%s\" to (indexes) %d %d\n", doc->paragraphs[paragraph_number - 1].lines[line_number], paragraph_number - 1, line_number);*/
  119.     doc->paragraphs[paragraph_number - 1].number_of_lines++;
  120.  
  121.     return SUCCESS;
  122. }
  123.  
  124. int get_number_lines_paragraph(Document *doc, int paragraph_number, int *number_of_lines) {
  125.     if (doc == NULL || number_of_lines == NULL || paragraph_number > MAX_PARAGRAPHS) {
  126.         return FAILURE;
  127.     }
  128.  
  129.     number_of_lines = (int *) doc->paragraphs[paragraph_number - 1].number_of_lines;
  130.     return SUCCESS;
  131. }
  132.  
  133. int append_line(Document *doc, int paragraph_number, const char *new_line) {
  134.     if (doc == NULL || doc->number_of_paragraphs > MAX_PARAGRAPHS || paragraph_number > MAX_PARAGRAPHS
  135.         || paragraph_number <= 0 || doc->paragraphs[paragraph_number].number_of_lines > MAX_PARAGRAPH_LINES) {
  136.         return FAILURE;
  137.     }
  138.  
  139.     return add_line_after(doc, paragraph_number, doc->paragraphs[paragraph_number - 1].number_of_lines, new_line);
  140. }
  141.  
  142. int remove_line(Document *doc, int paragraph_number, int line_number) {
  143.     int index = line_number - 1;
  144.     /*index denotes the index of the line in the array*/
  145.  
  146.     if (doc == NULL || paragraph_number > doc->number_of_paragraphs
  147.             || line_number > doc->paragraphs[paragraph_number - 1].number_of_lines) {
  148.         return FAILURE;
  149.     }
  150.  
  151.  
  152.     makeBlankLine(&doc->paragraphs[paragraph_number - 1], line_number);
  153.  
  154.  
  155.     for(index = line_number - 1; index < MAX_PARAGRAPH_LINES - 1; index++) {
  156.         strcpy(doc->paragraphs[paragraph_number - 1].lines[index], doc->paragraphs[paragraph_number - 1].lines[index + 1]);
  157.     }
  158.  
  159.  
  160.     return SUCCESS;
  161. }
  162.  
  163. int load_document(Document *doc, char data[][MAX_STR_SIZE + 1], int data_lines) {
  164.     int line = 0, parNum = 1;
  165.  
  166.     if (doc == NULL || data == NULL || data_lines <= 0) {
  167.         return FAILURE;
  168.     }
  169.  
  170.     doc->number_of_paragraphs = 1;
  171.     for (line = 0; line < data_lines; line++) {
  172.         /*printf("ParNum %d at line %d\n", parNum, line);*/
  173.         if (is_blank_line(data[line])) {
  174.             parNum++;
  175.             doc->number_of_paragraphs++;
  176.         } else {
  177.             append_line(doc, parNum, data[line]);
  178.         }
  179.     }
  180.  
  181.     return SUCCESS;
  182. }
  183.  
  184. int replace_text(Document *doc, const char *target, const char *replacement) {
  185.     char result[MAX_STR_SIZE+1], *currentLine, *strstrRes;
  186.     int p = 0, l = 0, oldIndex = 0, resultIndex = 0, cnt = 0;
  187.     int tarLen = strlen(target);
  188.     int repLen = strlen(replacement);
  189.  
  190.     if (doc == NULL) return FAILURE;
  191.    
  192.  
  193.     for (p = 0; p < doc->number_of_paragraphs; p++) {
  194.         memset(result, '\0', (MAX_STR_SIZE + 1)*sizeof(char));
  195.         for (l = 0; l < doc->paragraphs[p].number_of_lines; l++) {
  196.             memset(result, '\0', (MAX_STR_SIZE + 1)*sizeof(char));
  197.             /*printf("P: %d L: %d\n", p, l);*/
  198.             currentLine = doc->paragraphs[p].lines[l];
  199.             resultIndex = 0;
  200.  
  201.             for (oldIndex = 0; currentLine[oldIndex] != '\0'; oldIndex++) {
  202.                 /*printf("Current Line: %s Result: %s\n", currentLine, result);
  203.                 printf("Str Res: %s\n", strstr(&currentLine[oldIndex], target));*/
  204.  
  205.                 strstrRes = strstr(&currentLine[oldIndex], target);
  206.  
  207.                 if (strstrRes == &currentLine[oldIndex]) {
  208.                     /*printf("Case 1\n");*/
  209.                     strcat(&result[oldIndex], replacement);
  210.                     resultIndex += repLen;
  211.                     oldIndex += tarLen - 1;
  212.                 } else {
  213.                     /*printf("Case 2\n");
  214.                     printf("Transferring: %c from index %d to index %d", currentLine[oldIndex], oldIndex, resultIndex);*/
  215.                     result[resultIndex] = currentLine[oldIndex];
  216.                     resultIndex++;
  217.                 }
  218.             }
  219.             strcpy(currentLine, result);
  220.         }
  221.     }
  222.     return SUCCESS;
  223. }
  224.  
  225. int highlight_text(Document *doc, const char *target) {
  226.     char copy[MAX_STR_SIZE+1] = HIGHLIGHT_START_STR;
  227.     if (doc == NULL) return FAILURE;
  228.  
  229.     strcat(copy, target);
  230.     strcat(copy, HIGHLIGHT_END_STR);
  231.     return replace_text(doc, target, copy);
  232. }
  233.  
  234. int remove_text(Document *doc, const char *target) {
  235.     if (doc == NULL) return FAILURE;
  236.    
  237.     return replace_text(doc, target, "");
  238. }
  239.  
  240. /*Shifts all the paragraphs right of the p_index (inclusive) right by 1*/
  241. int shiftParagraphsByOne (Document *doc, int p_index) {
  242.     int i = doc->number_of_paragraphs;
  243.     Paragraph temp;
  244.  
  245.     if(i == MAX_PARAGRAPHS) i--;
  246.  
  247.     /*printf("p: %d, i: %d\n", paragraph_number, i);*/
  248.     for(i; i > p_index; i--) {
  249.         /*printf("p: %d, i: %d\n", p, i);*/
  250.         doc->paragraphs[i] = doc->paragraphs[i-1];
  251.         /*printf("After nudging index %d to the right\n", i-1);*/
  252.     }
  253.  
  254.     doc->paragraphs[p_index].number_of_lines = 0;
  255.     strcpy(doc->paragraphs[p_index].lines[0], "\0");
  256.     /*printf("Shifted Paragraphs from %d\n", p_index);*/
  257.  
  258.     return 0;
  259. }
  260.  
  261. int shiftLinesByOne (Document *doc, int paragraph_number, int line_number)  {
  262.     int line = (line == 0) ? 0 : line_number - 1;
  263.     char temp[MAX_STR_SIZE + 1] = "\0";
  264.  
  265.     strcpy(temp, doc->paragraphs[paragraph_number - 1].lines[line]);
  266.     strcpy(doc->paragraphs[line].lines[line], "\0");
  267.  
  268.     line++;
  269.  
  270.     for(line; line < MAX_PARAGRAPH_LINES - 1; line++) {
  271.         strcpy(doc->paragraphs[paragraph_number - 1].lines[line], temp);
  272.         strcpy(temp, doc->paragraphs[paragraph_number - 1].lines[line + 1]);
  273.     }
  274.  
  275.     /*printf("Shifted Lines from %d", line_number);*/
  276.  
  277.     return 0;
  278. }
  279.  
  280. int makeBlankParagraph(Paragraph *p) {
  281.     p->number_of_lines = 0;
  282.     memset(p->lines, '\0', (MAX_PARAGRAPH_LINES)*(MAX_STR_SIZE + 1)*sizeof(char));
  283.  
  284.     return 0;
  285. }
  286.  
  287. int load_file(Document *doc, const char *filename) {
  288.     FILE *input;
  289.     int parNum = 1;
  290.     char line[MAX_STR_SIZE + 1];
  291.  
  292.     if (doc == NULL || filename == NULL || doc->number_of_paragraphs > MAX_PARAGRAPHS
  293.         || (input = fopen(filename, "r")) == NULL) {
  294.         exit(EXIT_FAILURE);
  295.         return FAILURE;
  296.     }
  297.  
  298.     add_paragraph_after(doc, 0);
  299.     parNum = 1;
  300.  
  301.     while (fgets(line, MAX_STR_SIZE + 1, input) != NULL) {
  302.         /*printf("parNum: %d\n", parNum);*/
  303.         /*printf("\nline: \"%s\"\n", line);*/
  304.         if (is_blank_line(line)) {
  305.             /*printf("was blank ");*/
  306.             if(doc->number_of_paragraphs == MAX_PARAGRAPHS) {
  307.                 break;
  308.             } else {
  309.                 add_paragraph_after(doc, parNum);
  310.                 parNum++;
  311.             }
  312.         } else {
  313.             removeStringTrailingNewline(line);
  314.             /*printf("Appending to par %d: \"%s\"\n", parNum, line);*/
  315.             append_line(doc, parNum, line);
  316.         }
  317.     }
  318.  
  319.     fclose(input);
  320.  
  321.     return SUCCESS;
  322. }
  323.  
  324. int save_document(Document *doc, const char *filename) {
  325.     FILE *output_stream;
  326.     char line[MAX_STR_SIZE + 1];
  327.     int p, l;
  328.  
  329.     if (doc == NULL || filename == NULL || (output_stream = fopen(filename, "w")) == NULL) {
  330.         exit(EXIT_FAILURE);
  331.         return FAILURE;
  332.     }
  333.  
  334.     for(p = 0; p < doc->number_of_paragraphs; p++) {
  335.         /*printf("P: %d L: %d\n", doc->number_of_paragraphs, doc->paragraphs[p].number_of_lines);*/
  336.         for(l = 0; l < doc->paragraphs[p].number_of_lines; l++) {
  337.             if(strcmp(doc->paragraphs[p].lines[l], "\0")) {
  338.                 fputs(doc->paragraphs[p].lines[l], output_stream);
  339.                 fputs("\n", output_stream);
  340.             } /*else {
  341.                 printf("Condition Failed at (indexes) %d %d\n", p, l);
  342.             }*/
  343.             }
  344.        
  345.             fputs("\n", output_stream);
  346.         }
  347.  
  348.     fclose(output_stream);
  349.     return SUCCESS;
  350. }
  351.  
  352. /*line is one greater that its index*/
  353. int makeBlankLine(Paragraph *p, int line) {
  354.     memset(p->lines[line - 1], '\0', (MAX_STR_SIZE + 1)*sizeof(char));
  355.  
  356.     return 0;
  357. }
  358.  
  359. void removeStringTrailingNewline(char *str) {
  360.   int length = strlen(str);
  361.   if (str == NULL)
  362.     return;
  363.   if (str[length-1] == '\n')
  364.     str[length-1]  = '\0';
  365. }
  366.  
  367. int is_blank_line(char* line) {
  368.     int index = 0, length = strlen(line);
  369.    
  370.     for(index = 0; index < length; index++) {
  371.         if(!isspace(line[index])) return 0;
  372.     }
  373.  
  374.     return 1;
  375. }
Advertisement
Add Comment
Please, Sign In to add comment