-
Notifications
You must be signed in to change notification settings - Fork 0
/
book.c
281 lines (224 loc) · 4.84 KB
/
book.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include "book.h"
#include "epub.h"
#include "settings.h"
#include <stdlib.h>
#include <string.h>
ebook *books = NULL;
int max_books = 100;
int num_books = 0;
bool file_exists (char *filename)
{
return 0;
}
const char *get_file_extension (const char *filename)
{
int len = strlen(filename);
char *ext = malloc(5 * sizeof(char));
for (int i = 0; i < len; i ++)
{
for (int j = 1; j < 4; j ++)
{
ext[j - 1] = ext[j];
}
ext[3] = filename[i];
}
ext[4] = '\0';
return ext;
}
// TODO: check actual file internals to see if it's an epub, not just file-ending
book_type get_file_type (char *filename)
{
const char *ext = get_file_extension(filename);
if (strcmp(ext, "epub") == 0)
{
return EPUB;
}
if (strcmp(ext, "mobi") == 0)
{
return MOBI;
}
return INVALID;
}
void resize_library ()
{
if (books == NULL)
{
books = malloc(max_books * sizeof(ebook));
} else if (num_books >= max_books)
{
max_books *= 2;
// XXX TODO you are not fuckng copying over the old books
free(books);
books = malloc(max_books * sizeof(ebook));
}
}
ebook *new_book ()
{
ebook *book;
resize_library();
book = &books[num_books++];
book->id = num_books - 1;
book->body = NULL;
book->title = NULL;
return book;
}
// XXX only works for books at the end of the array
void remove_book (bookid id)
{
if (id == num_books - 1)
{
num_books --;
}
}
int load_book (char *filename, unsigned *id)
{
if (file_exists(filename) != 0)
{
printf("no such file %s\n", filename);
return 1;
}
book_type type = get_file_type(filename);
if (type == INVALID)
{
return 2;
}
ebook *book;
bookid bid;
int result = 0;
switch (type)
{
case EPUB:
book = new_book();
bid = book->id;
result = load_epub(filename, bid);
break;
case MOBI:
// load_mobi(filename, &book);
break;
default:
// can't load format / not an ebook
break;
}
if (result != 0)
{
remove_book(bid);
}
return 0;
}
int get_ebook (unsigned id, ebook *book)
{
return 1;
}
const char *book_title (bookid id)
{
return books[id].title;
}
int init_ebook (unsigned *id)
{
id = 0;
return 1;
}
int set_title (bookid id, const char *text)
{
if (text == NULL)
{
printf("book.c::set_title(): NULL text\n");
return 1;
}
dbgprintf("set_title: %d %s\n", id, text);
books[id].title = malloc(strlen(text) * sizeof(char));
strcpy(books[id].title, text);
return 0;
}
const char *get_title (bookid id)
{
if (id >= num_books)
{
return "INVALID TITLE";
}
return books[id].title;
}
/*
*
*/
int add_section (bookid book, char *text, unsigned length)
{
if (text == NULL)
{
dbgprintf("book.c::add_section(): no text\n");
return 1;
}
dbgprintf("add section book id: %d. len: %u\n", book, length);
if (books[book].body != NULL)
{
char *new;
int old_len = books[book].body_size;
int new_len = old_len + length;
new = malloc(new_len + 1);
memcpy(new, books[book].body, old_len);
memcpy(new + old_len, text, length);
free(books[book].body);
books[book].body = new;
books[book].body_size = new_len;
} else
{
books[book].body = malloc(length + 1);
strcpy(books[book].body, text);
books[book].body_size = length;
}
books[book].body[books[book].body_size] = '\0';
return 0;
}
int get_page (bookid book, char **text, int offset, int length)
{
int max_len;
ebook *ebook;
if (book >= num_books)
{
printf("book.c::get_page(): invalid book %d\n", book);
return 2;
}
ebook = &books[book];
if (ebook->body == NULL)
{
printf("book.c: requested book has no text.\n");
return 1;
}
max_len = strlen(ebook->body);
if (offset > max_len)
{
printf("book.c: requested page outside of book %d %d\n", max_len, offset);
return 2;
}
if (offset + length > max_len)
{
length = max_len - offset;
}
*text = ebook->body + offset;
// *text = malloc(length);
// strncpy(*text, ebook.body + offset, length);
return 0;
}
const char *get_body (bookid id)
{
if (id >= num_books)
{
printf("book.c::get_page(): invalid book %d\n", id);
return 0;
}
return books[id].body;
}
unsigned get_num_books ()
{
return num_books;
}
void books_cleanup ()
{
for (int i = 0; i < num_books; i ++)
{
free(books[i].title);
// free(books[i].author);
free(books[i].body);
}
free(books);
}