-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbulk.c
186 lines (139 loc) · 3.24 KB
/
bulk.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <fcntl.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include "bulk.h"
#include "cell.h"
#include "encoding.h"
#include "toy.h"
Bulk*
new_bulk() {
Bulk *bulk;
bulk = GC_MALLOC(sizeof(Bulk));
ALLOC_SAFE(bulk);
memset(bulk, 0, sizeof(Bulk));
bulk->line = 1;
return bulk;
}
int
bulk_load_file(Bulk *bulk, const char *file, int encoder) {
int fd;
struct stat statbuff;
int size;
char *readbuff;
encoder_error_info *error_info;
Cell *encbuff;
if (NULL == bulk) return 0;
if (-1 == (fd = open(file, O_RDONLY))) return 0;
if (-1 == fstat(fd, &statbuff)) goto error;
size = statbuff.st_size;
error_info = GC_MALLOC(sizeof(encoder_error_info));
ALLOC_SAFE(error_info);
memset(error_info, 0, sizeof(encoder_error_info));
bulk->length = size;
bulk->allocsize = (size+1)*sizeof(wchar_t);
bulk->pos = 0;
bulk->line = 1;
readbuff = GC_MALLOC_ATOMIC(size+1);
ALLOC_SAFE(readbuff);
if (-1 == read_size(fd, readbuff, size)) goto error;
readbuff[size] = 0;
encbuff = decode_raw_to_unicode(new_cell(to_wchar(readbuff)), encoder, error_info);
if (NULL == encbuff) goto error;
bulk->data = cell_get_addr(encbuff);
bulk->length = cell_get_length(encbuff);
close(fd);
return 1;
error:
close(fd);
bulk->length = 0;
bulk->allocsize = 0;
bulk->pos = 0;
bulk->line = 1;
bulk->data = 0;
return 0;
}
int
bulk_set_string(Bulk *bulk, const wchar_t *str) {
int len;
if (NULL == bulk) return 0;
if (NULL == str) return 0;
len = wcslen(str);
bulk->data = GC_MALLOC_ATOMIC((len)*sizeof(wchar_t));
ALLOC_SAFE(bulk->data);
bulk->length = len;
bulk->allocsize = (len)*sizeof(wchar_t);
bulk->pos = 0;
bulk->line = 1;
memcpy(bulk->data, str, len*sizeof(wchar_t));
return 1;
}
int
bulk_rewind(Bulk *bulk) {
if (NULL == bulk) return 0;
if (NULL == bulk->data) return 0;
bulk->pos = 0;
bulk->line = 1;
return 1;
}
int
bulk_getchar(Bulk *bulk) {
int c;
if (NULL == bulk) return EOF;
if (EOF == bulk_is_eof(bulk)) return EOF;
c = bulk->data[bulk->pos];
if (IS_NEWLINE(c)) {
bulk->line++;
}
bulk->pos++;
return c;
}
int
bulk_ungetchar(Bulk *bulk) {
if (NULL == bulk) return EOF;
if (bulk->pos > 0) bulk->pos--;
if (IS_NEWLINE(bulk->data[bulk->pos])) {
bulk->line--;
}
return 1;
}
int
bulk_is_eof(Bulk *bulk) {
if (NULL == bulk) return EOF;
if (NULL == bulk->data) return EOF;
if ((bulk->pos) >= (bulk->length)) return EOF;
return 1;
}
int
bulk_get_length(Bulk *bulk) {
if (NULL == bulk) return EOF;
return bulk->length;
}
int
bulk_get_position(Bulk *bulk) {
if (NULL == bulk) return EOF;
return bulk->pos;
}
int
bulk_set_position(Bulk *bulk, int pos) {
if (NULL == bulk) return 0;
if (EOF == bulk_is_eof(bulk)) return EOF;
if (pos >= bulk->length) {
bulk->pos = bulk->length;
return EOF;
}
bulk->pos = pos;
return 1;
}
wchar_t*
bulk_get_addr(Bulk *bulk) {
if (NULL == bulk) return 0;
return bulk->data;
}
int
bulk_get_line(Bulk *bulk) {
if (NULL == bulk) return 0;
return bulk->line;
}