-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgapbuffer.c
More file actions
162 lines (100 loc) · 3.02 KB
/
gapbuffer.c
File metadata and controls
162 lines (100 loc) · 3.02 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* gapbuffer.c */
/* A simple gapbuffer implementation. */
/* This code is released to the public domain. */
/* "Share and enjoy..." ;) */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>
char *slice(char *array, int first, int last)
{
char *retarray = (char*) malloc(last-first);
/* Do the array slice */
strncpy(retarray, array+first, last);
return retarray;
free(retarray);
retarray = NULL;
}
/* A pointer to move around the text */
/* This works! Very handy to move around our text. */
/* It returns the text from the letter pointed at */
/* to the end */
char *ptr(char *text, int pos)
{
return &text[pos];
}
/* Subtracting two pointers gives the number of */
/* array elements between them. We use that here. */
int index(char *ptr1, char *ptr2)
{
int retval = ptr2 - ptr1;
return retval;
}
/* Print the text */
void print_text(char *text)
{
printf("Text is %s \n", text);
}
/* Return the first text buffer */
char *buf1(char *text, int pos)
{
char *buffer1 = slice(text, 0, pos);
return buffer1;
}
/* Return the second text buffer */
char *buf2(char *text, int pos)
{
char *buffer2 = slice(text, pos, strlen(text) );
return buffer2;
}
/* Return the cursor position */
void cursorpos()
{
}
/* Create a gap in the middle of the text. */
void makegap(char *text, int pos)
{
}
/* Remove the gap (before moving it). */
void removegap(char *text)
{
}
/* Move the cursor (and the gap). */
void movepos(char *text, int num)
{
}
/* Test the code. */
int main()
{
/* Create a text string to work with. */
char *text = "Mary had a little lamb, its fleece was white as snow." ;
print_text(text);
char *buffer1 = buf1(text, 14);
char *buffer2 = buf2(text, 14);
printf("Buffer1: %s \n", buffer1);
printf("Buffer2: %s \n", buffer2);
char *test = ptr("obsidian", 3);
printf("Ptr: %s \n", test);
char *foo = "obsidian";
/* Create another pointer by adding to foo. */
char *bar = foo + 3;
/* Get the first part of the text */
char baz[4];
strncpy(baz, foo, 3);
/* Null-terminate the text */
baz[3]=0 ;
/* Get the middle of the string */
/* "*foo" is "obsidian". */
/* "*bar" is *foo + 3. "idian" in other words. */
char middle[4];
strncpy(middle, bar, 3);
middle[3]=0;
int myindex = index(foo, bar);
printf("Index: %d \n", myindex);
printf("Foo: %s \n", foo);
printf("Bar: %s \n", bar);
printf("Baz: %s \n", baz);
printf("Middle: %s \n", middle);
return 0;
}