Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "document.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int makeBlankParagraph(Paragraph *p);
- int makeBlankLine(Paragraph *p, int line);
- int shiftParagraphsByOne(Document *doc, int paragraph_number);
- int shiftLinesByOne (Document *doc, int paragraph_number, int line_number);
- void removeStringTrailingNewline(char *str);
- int init_document(Document *doc, const char *name) {
- int p, l;
- char lines[MAX_PARAGRAPH_LINES][MAX_STR_SIZE + 1];
- if (doc == NULL || name == NULL || MAX_STR_SIZE < strlen(name)) {
- return FAILURE;
- }
- strcpy(doc->name, name);
- doc->number_of_paragraphs = 0;
- for(p = 0; p < MAX_PARAGRAPHS; p++) {
- makeBlankParagraph(&doc->paragraphs[p]);
- }
- return SUCCESS;
- }
- int reset_document(Document *doc) {
- if (doc == NULL) {
- return FAILURE;
- }
- doc->number_of_paragraphs = 0;
- return SUCCESS;
- }
- int print_document(Document *doc) {
- int p, l;
- if (doc == NULL) {
- return FAILURE;
- }
- printf("Document name: \"%s\"\n", doc->name);
- printf("Number of Paragraphs: %d\n", doc->number_of_paragraphs);
- for(p = 0; p < doc->number_of_paragraphs; p++) {
- /*printf("P: %d L: %d\n", p, doc->paragraphs[p].number_of_lines);*/
- for(l = 0; l < doc->paragraphs[p].number_of_lines; l++) {
- if(!is_blank_line(doc->paragraphs[p].lines[l])) {
- printf("%s\n", doc->paragraphs[p].lines[l]);
- }/* else {
- printf("Condition Failed at (indexes) %d %d\n", p, l);
- }*/
- }
- if(p < doc->number_of_paragraphs - 1) printf("\n");
- }
- return SUCCESS;
- }
- int print_document_detail(Document *doc) {
- int p, l;
- if (doc == NULL) {
- return FAILURE;
- }
- printf("Document name: \"%s\"\n", doc->name);
- printf("Number of Paragraphs: %d\n", doc->number_of_paragraphs);
- for(p = 0; p < doc->number_of_paragraphs; p++) {
- /*printf("P: %d L: %d\n", doc->number_of_paragraphs, doc->paragraphs[p].number_of_lines);*/
- for(l = 0; l < doc->paragraphs[p].number_of_lines; l++) {
- if(!is_blank_line(doc->paragraphs[p].lines[l])) {
- printf("Paragraph %d: %s\n", p, doc->paragraphs[p].lines[l]);
- }/* else {
- printf("Condition Failed at (indexes) %d %d\n", p, l);
- }*/
- }
- if(p < doc->number_of_paragraphs - 1) printf("\n");
- }
- return SUCCESS;
- }
- int add_paragraph_after(Document *doc, int paragraph_number) {
- if (doc == NULL || doc->number_of_paragraphs >= MAX_PARAGRAPHS || paragraph_number > MAX_PARAGRAPHS || paragraph_number > doc->number_of_paragraphs) {
- return FAILURE;
- }
- /*printf("\nBefore adding paragraph after %d \n", paragraph_number);*/
- if(paragraph_number < doc->number_of_paragraphs) {
- /*printf("\nGoing to shift because Paragraph Number: %d < number_of_paragraphs: %d \n", paragraph_number, doc->number_of_paragraphs);*/
- shiftParagraphsByOne(doc, paragraph_number);
- }
- doc->number_of_paragraphs++;
- /*printf("\nAfter adding paragraph after %d \n", paragraph_number);*/
- return SUCCESS;
- }
- int add_line_after(Document *doc, int paragraph_number, int line_number, const char *new_line) {
- if (doc == NULL || doc->number_of_paragraphs > MAX_PARAGRAPHS || paragraph_number > MAX_PARAGRAPHS
- || paragraph_number <= 0 || line_number > MAX_PARAGRAPH_LINES
- || doc->paragraphs[paragraph_number].number_of_lines > MAX_PARAGRAPH_LINES) {
- return FAILURE;
- }
- if(line_number < doc->paragraphs[paragraph_number - 1].number_of_lines) {
- shiftLinesByOne(doc, paragraph_number, line_number);
- }
- strcpy(doc->paragraphs[paragraph_number - 1].lines[line_number], new_line);
- /*printf("Copied \"%s\" to (indexes) %d %d\n", doc->paragraphs[paragraph_number - 1].lines[line_number], paragraph_number - 1, line_number);*/
- doc->paragraphs[paragraph_number - 1].number_of_lines++;
- return SUCCESS;
- }
- int get_number_lines_paragraph(Document *doc, int paragraph_number, int *number_of_lines) {
- if (doc == NULL || number_of_lines == NULL || paragraph_number > MAX_PARAGRAPHS) {
- return FAILURE;
- }
- number_of_lines = (int *) doc->paragraphs[paragraph_number - 1].number_of_lines;
- return SUCCESS;
- }
- int append_line(Document *doc, int paragraph_number, const char *new_line) {
- if (doc == NULL || doc->number_of_paragraphs > MAX_PARAGRAPHS || paragraph_number > MAX_PARAGRAPHS
- || paragraph_number <= 0 || doc->paragraphs[paragraph_number].number_of_lines > MAX_PARAGRAPH_LINES) {
- return FAILURE;
- }
- return add_line_after(doc, paragraph_number, doc->paragraphs[paragraph_number - 1].number_of_lines, new_line);
- }
- int remove_line(Document *doc, int paragraph_number, int line_number) {
- int index = line_number - 1;
- /*index denotes the index of the line in the array*/
- if (doc == NULL || paragraph_number > doc->number_of_paragraphs
- || line_number > doc->paragraphs[paragraph_number - 1].number_of_lines) {
- return FAILURE;
- }
- makeBlankLine(&doc->paragraphs[paragraph_number - 1], line_number);
- for(index = line_number - 1; index < MAX_PARAGRAPH_LINES - 1; index++) {
- strcpy(doc->paragraphs[paragraph_number - 1].lines[index], doc->paragraphs[paragraph_number - 1].lines[index + 1]);
- }
- return SUCCESS;
- }
- int load_document(Document *doc, char data[][MAX_STR_SIZE + 1], int data_lines) {
- int line = 0, parNum = 1;
- if (doc == NULL || data == NULL || data_lines <= 0) {
- return FAILURE;
- }
- doc->number_of_paragraphs = 1;
- for (line = 0; line < data_lines; line++) {
- /*printf("ParNum %d at line %d\n", parNum, line);*/
- if (is_blank_line(data[line])) {
- parNum++;
- doc->number_of_paragraphs++;
- } else {
- append_line(doc, parNum, data[line]);
- }
- }
- return SUCCESS;
- }
- int replace_text(Document *doc, const char *target, const char *replacement) {
- char result[MAX_STR_SIZE+1], *currentLine, *strstrRes;
- int p = 0, l = 0, oldIndex = 0, resultIndex = 0, cnt = 0;
- int tarLen = strlen(target);
- int repLen = strlen(replacement);
- if (doc == NULL) return FAILURE;
- for (p = 0; p < doc->number_of_paragraphs; p++) {
- memset(result, '\0', (MAX_STR_SIZE + 1)*sizeof(char));
- for (l = 0; l < doc->paragraphs[p].number_of_lines; l++) {
- memset(result, '\0', (MAX_STR_SIZE + 1)*sizeof(char));
- /*printf("P: %d L: %d\n", p, l);*/
- currentLine = doc->paragraphs[p].lines[l];
- resultIndex = 0;
- for (oldIndex = 0; currentLine[oldIndex] != '\0'; oldIndex++) {
- /*printf("Current Line: %s Result: %s\n", currentLine, result);
- printf("Str Res: %s\n", strstr(¤tLine[oldIndex], target));*/
- strstrRes = strstr(¤tLine[oldIndex], target);
- if (strstrRes == ¤tLine[oldIndex]) {
- /*printf("Case 1\n");*/
- strcat(&result[oldIndex], replacement);
- resultIndex += repLen;
- oldIndex += tarLen - 1;
- } else {
- /*printf("Case 2\n");
- printf("Transferring: %c from index %d to index %d", currentLine[oldIndex], oldIndex, resultIndex);*/
- result[resultIndex] = currentLine[oldIndex];
- resultIndex++;
- }
- }
- strcpy(currentLine, result);
- }
- }
- return SUCCESS;
- }
- int highlight_text(Document *doc, const char *target) {
- char copy[MAX_STR_SIZE+1] = HIGHLIGHT_START_STR;
- if (doc == NULL) return FAILURE;
- strcat(copy, target);
- strcat(copy, HIGHLIGHT_END_STR);
- return replace_text(doc, target, copy);
- }
- int remove_text(Document *doc, const char *target) {
- if (doc == NULL) return FAILURE;
- return replace_text(doc, target, "");
- }
- /*Shifts all the paragraphs right of the p_index (inclusive) right by 1*/
- int shiftParagraphsByOne (Document *doc, int p_index) {
- int i = doc->number_of_paragraphs;
- Paragraph temp;
- if(i == MAX_PARAGRAPHS) i--;
- /*printf("p: %d, i: %d\n", paragraph_number, i);*/
- for(i; i > p_index; i--) {
- /*printf("p: %d, i: %d\n", p, i);*/
- doc->paragraphs[i] = doc->paragraphs[i-1];
- /*printf("After nudging index %d to the right\n", i-1);*/
- }
- doc->paragraphs[p_index].number_of_lines = 0;
- strcpy(doc->paragraphs[p_index].lines[0], "\0");
- /*printf("Shifted Paragraphs from %d\n", p_index);*/
- return 0;
- }
- int shiftLinesByOne (Document *doc, int paragraph_number, int line_number) {
- int line = (line == 0) ? 0 : line_number - 1;
- char temp[MAX_STR_SIZE + 1] = "\0";
- strcpy(temp, doc->paragraphs[paragraph_number - 1].lines[line]);
- strcpy(doc->paragraphs[line].lines[line], "\0");
- line++;
- for(line; line < MAX_PARAGRAPH_LINES - 1; line++) {
- strcpy(doc->paragraphs[paragraph_number - 1].lines[line], temp);
- strcpy(temp, doc->paragraphs[paragraph_number - 1].lines[line + 1]);
- }
- /*printf("Shifted Lines from %d", line_number);*/
- return 0;
- }
- int makeBlankParagraph(Paragraph *p) {
- p->number_of_lines = 0;
- memset(p->lines, '\0', (MAX_PARAGRAPH_LINES)*(MAX_STR_SIZE + 1)*sizeof(char));
- return 0;
- }
- int load_file(Document *doc, const char *filename) {
- FILE *input;
- int parNum = 1;
- char line[MAX_STR_SIZE + 1];
- if (doc == NULL || filename == NULL || doc->number_of_paragraphs > MAX_PARAGRAPHS
- || (input = fopen(filename, "r")) == NULL) {
- exit(EXIT_FAILURE);
- return FAILURE;
- }
- add_paragraph_after(doc, 0);
- parNum = 1;
- while (fgets(line, MAX_STR_SIZE + 1, input) != NULL) {
- /*printf("parNum: %d\n", parNum);*/
- /*printf("\nline: \"%s\"\n", line);*/
- if (is_blank_line(line)) {
- /*printf("was blank ");*/
- if(doc->number_of_paragraphs == MAX_PARAGRAPHS) {
- break;
- } else {
- add_paragraph_after(doc, parNum);
- parNum++;
- }
- } else {
- removeStringTrailingNewline(line);
- /*printf("Appending to par %d: \"%s\"\n", parNum, line);*/
- append_line(doc, parNum, line);
- }
- }
- fclose(input);
- return SUCCESS;
- }
- int save_document(Document *doc, const char *filename) {
- FILE *output_stream;
- char line[MAX_STR_SIZE + 1];
- int p, l;
- if (doc == NULL || filename == NULL || (output_stream = fopen(filename, "w")) == NULL) {
- exit(EXIT_FAILURE);
- return FAILURE;
- }
- for(p = 0; p < doc->number_of_paragraphs; p++) {
- /*printf("P: %d L: %d\n", doc->number_of_paragraphs, doc->paragraphs[p].number_of_lines);*/
- for(l = 0; l < doc->paragraphs[p].number_of_lines; l++) {
- if(strcmp(doc->paragraphs[p].lines[l], "\0")) {
- fputs(doc->paragraphs[p].lines[l], output_stream);
- fputs("\n", output_stream);
- } /*else {
- printf("Condition Failed at (indexes) %d %d\n", p, l);
- }*/
- }
- fputs("\n", output_stream);
- }
- fclose(output_stream);
- return SUCCESS;
- }
- /*line is one greater that its index*/
- int makeBlankLine(Paragraph *p, int line) {
- memset(p->lines[line - 1], '\0', (MAX_STR_SIZE + 1)*sizeof(char));
- return 0;
- }
- void removeStringTrailingNewline(char *str) {
- int length = strlen(str);
- if (str == NULL)
- return;
- if (str[length-1] == '\n')
- str[length-1] = '\0';
- }
- int is_blank_line(char* line) {
- int index = 0, length = strlen(line);
- for(index = 0; index < length; index++) {
- if(!isspace(line[index])) return 0;
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment