-
Notifications
You must be signed in to change notification settings - Fork 5
My version of the program. #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Bibliocar-ng
wants to merge
9
commits into
mazarf:master
Choose a base branch
from
Bibliocar-ng:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
dac1b37
Update line.c
Bibliocar-ng a2d0ddf
Update line.h
Bibliocar-ng 022f4cc
Update page.c
Bibliocar-ng 22cf99a
Update page.h
Bibliocar-ng f0ced53
Update text.c
Bibliocar-ng f5a4080
Update text.h
Bibliocar-ng e50d6ee
Merge branch 'mazarf:master' into master
Bibliocar-ng 6980d1c
Add files via upload
Bibliocar-ng 3000e98
Update README.md
Bibliocar-ng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,50 +1,55 @@ | ||
| #include "line.h" | ||
|
|
||
|
|
||
| void init_line(LINE *s) | ||
| { | ||
| s->size = LINE_SIZE; | ||
| s->line = (char *)malloc(LINE_SIZE * sizeof(char)); | ||
| s->line[0] = '\0'; | ||
| } // init_line | ||
|
|
||
| s->line = malloc(LINE_SIZE * sizeof(char)); | ||
| if(s->line != NULL) | ||
| { | ||
| s->size = LINE_SIZE; | ||
| s->line[0] = '\0'; | ||
| } | ||
| } //init_line | ||
|
|
||
| // Insert char into string. | ||
| void insert_char(LINE *s, char c, int index) | ||
| void add_char(LINE *s, char c) | ||
| { | ||
| int i; | ||
|
|
||
| if(strlen(s->line) >= s->size - 2) expand(s); | ||
|
|
||
| for(i = strlen(s->line); i >= index; i--) | ||
| s->line[i + 1] = s->line[i]; | ||
|
|
||
| s->line[index] = c; | ||
| } // insert | ||
|
|
||
| insert_char(s, c, strlen(s->line)); | ||
| } | ||
|
|
||
| // Insert char into string. | ||
| void insert_char(LINE *s, char c, int index){ | ||
| char temp[2]; | ||
| temp[0] = c; | ||
| temp[1] = '\0'; | ||
| insert_string(s, temp, index); | ||
| } // insert char using insert_string() | ||
|
|
||
| void remove_char(LINE *s, int index) | ||
| void insert_string(LINE *s, char *in, int index) | ||
| { | ||
| int i; | ||
| int len = strlen(s->line); | ||
| for(i = index; i < len; i++) | ||
| s->line[i] = s->line[i + 1]; | ||
| } // remove_char | ||
| int line_ln = strlen(s->line); | ||
| int in_ln = strlen(in); | ||
| if(line_ln + in_ln >= s->size) expand(s, in_ln); | ||
| if(line_ln + in_ln >= s->size) return; | ||
|
|
||
| size_t append_ln = line_ln - index + 1; | ||
|
|
||
| memmove(s->line + index + in_ln, s->line + index, append_ln * sizeof(char)); | ||
| memcpy(s->line + index, in, (in_ln) * sizeof(char)); | ||
| } // insert string | ||
|
|
||
| // expands size of line | ||
| void expand(LINE *s) | ||
| void expand(LINE *s, int ln) | ||
| { | ||
| int new_size = s->size * 2; | ||
| char *temp = (char *)malloc(new_size * sizeof(char)); | ||
| strcpy(temp, s->line); | ||
| free(s->line); | ||
| s->line = temp; | ||
| s->size = new_size; | ||
| } // expand | ||
|
|
||
| void add_char(LINE *s, char c) | ||
| { | ||
| insert_char(s, c, strlen(s->line)); | ||
| size_t new_size = s->size * 2 + ln; | ||
| char *p = realloc(s->line, new_size * sizeof(char)); | ||
| if(p != NULL) | ||
| { | ||
| s->size = new_size; | ||
| s->line = p; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| void remove_char(LINE *s, int index){ | ||
| size_t append_ln = strlen(s->line) - index; | ||
| memmove(s->line + index, s->line + index + 1, append_ln * sizeof(char)); | ||
| } // remove_char | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,97 +1,96 @@ | ||
| #include "page.h" | ||
|
|
||
|
|
||
| #include "page.h" | ||
| void file_too_big(PAGE *p, int size); | ||
| void init_page(PAGE *p, char *filename, int size) | ||
| { | ||
| p->text = (LINE *)malloc(size * sizeof(LINE)); | ||
|
|
||
| int i; | ||
| for(i = 0; i < size; i++) | ||
| { | ||
| init_line(p->text + i); | ||
| } | ||
| p->text = malloc(size * sizeof(LINE)); | ||
| if(p->text == NULL) file_too_big(p, 0); | ||
|
|
||
| int i; | ||
| for(i = 0; i < size; i++) | ||
| { | ||
| init_line(p->text + i); | ||
| if(p->text + i == NULL) file_too_big(p, i); | ||
| } | ||
| strcpy(p->filename, filename); | ||
| p->numlines = 0; | ||
| p->size = size; | ||
| p->numlines = size; | ||
| p->size = size; | ||
|
|
||
|
|
||
| } // init_page | ||
|
|
||
| void dest_page(PAGE *p) | ||
| { | ||
| int i; | ||
| for(i = 0; i < p->numlines; i++) | ||
| { | ||
| free(p->text[i].line); // maybe replace with dest_line() | ||
| } | ||
| free(p->text); | ||
| int i; | ||
| for(i = 0; i < p->numlines; i++) | ||
| { | ||
| free((p->text + i)->line); // maybe replace with dest_line() | ||
| } | ||
| free(p->text); | ||
| } // dest_page | ||
|
|
||
|
|
||
|
|
||
| // WARNING: Expansion function implemented but not tested | ||
| void insert_line(PAGE *p, int index) | ||
| { | ||
| if( p->numlines >= p->size ) expand_page(p); | ||
|
|
||
| LINE newline; | ||
| init_line(&newline); | ||
| newline.line[0] = '\0'; | ||
|
|
||
| int i; | ||
|
|
||
| for(i = p->numlines - 1; i >= index; i--) | ||
| p->text[i + 1] = p->text[i]; | ||
|
|
||
| p->text[index] = newline; | ||
| (p->numlines)++; | ||
| int insert_line(PAGE *p, int index) | ||
| { | ||
| if(p->numlines + 1 >= p->size) expand_page(p); | ||
| if(p->numlines + 1 >= p->size) return 0; | ||
|
|
||
| size_t lines = p->numlines - index; | ||
| memmove(&p->text[index + 1].line, &p->text[index].line, | ||
| lines * sizeof(LINE)); | ||
|
|
||
| LINE newline; | ||
| init_line(&newline); | ||
| p->text[index] = newline; | ||
| p->numlines++; | ||
| return 1; | ||
| } // insert_line | ||
|
|
||
|
|
||
|
|
||
| void remove_line(PAGE *p, int index) | ||
| { | ||
| if( p->numlines > 1 ) | ||
| { | ||
| free(p->text[index].line); | ||
|
|
||
| int i; | ||
| for(i = index; i < p->numlines - 1; i++) | ||
| { | ||
| p->text[i] = p->text[i + 1]; | ||
| } | ||
| (p->numlines)--; | ||
| } | ||
| size_t lines; | ||
|
|
||
| free(p->text[index].line); | ||
| lines = p->numlines - index - 1; | ||
| memmove(&p->text[index].line, &p->text[index + 1].line, | ||
| lines * sizeof(LINE)); | ||
| p->numlines--; | ||
| } // remove_line | ||
|
|
||
|
|
||
| void expand_page(PAGE *p) | ||
| { | ||
| int newsize = p->size * 2; | ||
| LINE *newline = malloc(newsize * sizeof(LINE)); | ||
|
|
||
| int i; | ||
| for(i = 0; i < p->size; i++) // copy old lines | ||
| newline[i] = p->text[i]; | ||
| for(i = p->size; i < newsize; i++) // init new lines | ||
| init_line(newline + i); | ||
|
|
||
| p->text = newline; | ||
| p->size = newsize; | ||
| } // expand_page | ||
|
|
||
| int newsize = p->size * 2 + 1; | ||
| LINE *newline = realloc(p->text, newsize * sizeof(LINE)); | ||
|
|
||
| if(newline != NULL) | ||
| { | ||
| p->size = newsize; | ||
| p->text = newline; | ||
| } | ||
| } | ||
|
|
||
| // NOTE: This moves the cursor to the end of the displayed text | ||
| void print_page(const PAGE *p, int start, int end) | ||
| { | ||
| int i, line; | ||
| for(i = start, line = 0; i < p->numlines && i < end; i++, line++) | ||
| { | ||
| move(line, 0); | ||
| clrtoeol(); | ||
| printw(" %s", p->text[i].line); | ||
| } | ||
| if(start < end) | ||
| int i, line; | ||
| for(i = start; i <= end; i++) | ||
| { | ||
| move(line, 0); | ||
| clrtoeol(); // if we deleted a line this may be necessary | ||
| move(line-1, 1); | ||
| line = i - start; | ||
| move(line, 0); | ||
| clrtoeol(); | ||
| if(i < p->numlines) | ||
| printw(" %s", p->text[i].line); | ||
| } | ||
| refresh(); | ||
| refresh(); | ||
| } // print_page | ||
|
|
||
|
|
||
| void file_too_big(PAGE *p, int last_line) | ||
| { | ||
| p->size = last_line; | ||
| dest_page(p); | ||
| exit(EXIT_FAILURE); | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -120,3 +120,4 @@ int prompt_yesno(const char *message) | |
| return !choice; // yes is 0 | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move the curly brace to its own line