-
Notifications
You must be signed in to change notification settings - Fork 0
/
text.c
115 lines (107 loc) · 3.35 KB
/
text.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* "text.c" - Gabriel Bauer (@ToCodeABluejay)
*
*Copyright (c) 2021 Gabriel Bauer (@ToCodeABluejay)
*
*Permission is hereby granted, free of charge, to any person obtaining a copy
*of this software and associated documentation files (the "Software"), to deal
*in the Software without restriction, including without limitation the rights
*to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
*copies of the Software, and to permit persons to whom the Software is
*furnished to do so, subject to the following conditions:
*
*The above copyright notice and this permission notice shall be included in all
*copies or substantial portions of the Software.
*
*THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
*SOFTWARE.
*/
#include "edit.h"
int get_line_number_pos(int line, char *text)
/*Returns the position of the line-nth line
*in a character array 'text'
*/
{
long long i, j;
if (line == 0)
return 0;
else
{
for(i=0, j=0; text[i]!='\0'; i++)
{
if (text[i]=='\n')
j++;
if (text[i]=='\n' && j==line)
break;
}
if (text[i]!='\0')
return i+1;
else
return EOF;
}
}
int get_end_of_line(int start, char *text)
/*From a given start position in a char array,
*this function returns the position of the end
*of the given line at that position, whether the
*line is the end of the array (aka '\0') or the
*start of a new line (aka '\n')
*/
{
int i;
for (i = start; text[i]!='\0' && text[i]!='\n'; i++);
return i;
}
unsigned long long array_size(int k)
/*Base-2 dynamic array size function. It takes a length k
*and finds the closest base of two that is equal to or
*larger than the given k value - used to determine malloc()
*sizes for text-content in a dynamic setting
*/
{
unsigned len = (unsigned)floor(log(k)/log(2));
return pow(2,len+1);
}
void ins_char(int pos, char k, char *str)
/*The basic functionality of most of the editor
*relies on this function. It is simply used to
*insert a character into the text from the given
*cursor position.
*/
{
if(strlen(str)==array_size(strlen(str)))
/*If we have reached the end of the dynamic array,
*we make a copy of its contents and then resize
*the original to be double the previous size, copy
*the contents back in.
*/
{
char *resize = (char*) calloc(1, strlen(str));
strcpy(resize, str);
str = realloc(str,array_size(strlen(str))*2);
strcpy(str, resize);
}
//Insert the character at the given position
char *cpy = (char*) calloc(1, array_size(strlen(str)));
strncpy(cpy, str, pos);
cpy[pos] = k;
strcat(cpy, str+pos);
strcpy(str, cpy);
}
void del_char(struct Window *w, struct Cursor *c)
//Deletes a given character
{
key_left(w,c);
for(int pos=c->abs;w->contents[pos]!='\0'; pos++) w->contents[pos]=w->contents[pos+1];
}
void del_line(int line, char *text)
//Deletes a given line
{
int start = get_line_number_pos(line, text);
int diff = get_end_of_line(start, text)-start+1;
for(;text[start]!='\0'; start++) text[start]=text[start+diff];
}