Added note about 2.5 being Archos only
[kugel-rb.git] / apps / tagdb / file.c
blobde641fec384b672e27b297a6afb33cca4bf15ac6
1 #include "malloc.h" // realloc() and free()
2 #include <string.h> // strncasecmp()
4 #include "file.h"
6 // how is our flag organized?
7 #define FLAG ( 0xBF )
8 #define FLAG_VALID(flag) (flag == 0xBF)
10 static int do_resize(struct file_entry *e, const uint32_t name_len, const int zero_fill);
12 struct file_entry* new_file_entry(const uint32_t name_len) {
13 // Start my allocating memory
14 struct file_entry *e = (struct file_entry*)malloc(sizeof(struct file_entry));
15 if( e == NULL ) {
16 DEBUGF("new_file_entry: could not allocate memory\n");
17 return NULL;
20 // We begin empty
21 e->name = NULL;
22 e->size.name_len = 0;
24 e->hash = 0;
25 e->song = 0;
26 e->rundb = 0;
28 e->flag = FLAG;
30 // and resize to the requested size
31 if( do_resize(e, name_len, 1) ) {
32 free(e);
33 return NULL;
36 return e;
39 int file_entry_destruct(struct file_entry *e) {
40 assert(e != NULL);
41 assert(FLAG_VALID(e->flag));
43 free(e->name);
45 free(e);
47 return ERR_NONE;
50 static int do_resize(struct file_entry *e, const uint32_t name_len, const int zero_fill) {
51 void* temp;
53 assert(e != NULL);
54 assert(FLAG_VALID(e->flag));
56 if( name_len != e->size.name_len ) {
57 temp = realloc(e->name, name_len);
58 if(temp == NULL) {
59 DEBUGF("file_entry_resize: out of memory to resize name\n");
60 return ERR_MALLOC;
62 e->name = (char*)temp;
64 // if asked, fill it with zero's
65 if( zero_fill ) {
66 uint32_t i;
67 for(i=e->size.name_len; i<name_len; i++)
68 e->name[i] = (char)0x00;
71 e->size.name_len = name_len;
74 return ERR_NONE;
77 inline int file_entry_resize(struct file_entry *e, const uint32_t name_len) {
78 return do_resize(e, name_len, 1);
81 int file_entry_serialize(FILE *fd, const struct file_entry *e) {
82 assert(fd != NULL);
83 assert(e != NULL);
84 assert(FLAG_VALID(e->flag));
86 // First byte we write is a flag-byte to indicate this is a valid record
87 if( fwrite(&e->flag, 1, 1, fd) != 1 ) {
88 DEBUGF("file_entry_serialize: failed to write flag-byte\n");
89 return ERR_FILE;
92 // First we write the length of the name field
93 if( fwrite(&e->size.name_len, sizeof(e->size.name_len), 1, fd) != 1 ) {
94 DEBUGF("file_entry_serialize: failed to write name_len\n");
95 return ERR_FILE;
97 // now the name field itself
98 if( fwrite(e->name, 1, e->size.name_len, fd) != e->size.name_len ) {
99 DEBUGF("file_entry_serialize: failed to write name\n");
100 return ERR_FILE;
103 // hash field
104 if( fwrite(&e->hash, sizeof(e->hash), 1, fd) != 1 ) {
105 DEBUGF("file_entry_serialize: failed to write hash\n");
106 return ERR_FILE;
109 // song field
110 if( fwrite(&e->song, sizeof(e->song), 1, fd) != 1 ) {
111 DEBUGF("file_entry_serialize: failed to write song\n");
112 return ERR_FILE;
115 // rundb field
116 if( fwrite(&e->rundb, sizeof(e->rundb), 1, fd) != 1 ) {
117 DEBUGF("file_entry_serialize: failed to write rundb\n");
118 return ERR_FILE;
121 return ERR_NONE;
124 int file_entry_unserialize(struct file_entry **dest, FILE *fd) {
125 uint32_t length;
126 struct file_entry *e;
128 assert(dest != NULL);
129 assert(fd != NULL);
131 // Allocate memory
132 e = new_file_entry(0);
133 if( e == NULL ) {
134 DEBUGF("file_entry_unserialize: could not create new file_entry\n");
135 return ERR_MALLOC;
138 // First we read the length of the name field
139 if( fread(&length, sizeof(length), 1, fd) != 1 ) {
140 DEBUGF("file_entry_unserialize: failed to read name_len\n");
141 file_entry_destruct(e);
142 return ERR_FILE;
145 // allocate memory for the upcomming name-field
146 if( do_resize(e, length, 0) ) {
147 DEBUGF("file_entry_unserialize: failed to allocate memory for name\n");
148 file_entry_destruct(e);
149 return ERR_MALLOC;
152 // read it in
153 if( fread(e->name, 1, e->size.name_len, fd) != e->size.name_len ) {
154 DEBUGF("file_entry_unserialize: failed to read name\n");
155 file_entry_destruct(e);
156 return ERR_FILE;
159 // hash field
160 if( fread(&e->hash, sizeof(e->hash), 1, fd) != 1 ) {
161 DEBUGF("file_entry_unserialize: failed to read hash\n");
162 file_entry_destruct(e);
163 return ERR_FILE;
166 // song field
167 if( fread(&e->song, sizeof(e->song), 1, fd) != 1 ) {
168 DEBUGF("file_entry_unserialize: failed to read song\n");
169 file_entry_destruct(e);
170 return ERR_FILE;
173 // rundb field
174 if( fread(&e->rundb, sizeof(e->rundb), 1, fd) != 1 ) {
175 DEBUGF("file_entry_unserialize: failed to read rundb\n");
176 file_entry_destruct(e);
177 return ERR_FILE;
180 *dest = e;
181 return ERR_NONE;
184 int file_entry_write(FILE *fd, struct file_entry *e, struct file_size *s) {
185 uint32_t be32;
186 char pad = 0x00;
188 assert(fd != NULL);
189 assert(e != NULL);
190 assert(FLAG_VALID(e->flag));
192 // file name
193 if( fwrite(e->name, 1, e->size.name_len, fd) != e->size.name_len ) {
194 DEBUGF("file_entry_write: failed to write name\n");
195 return ERR_FILE;
197 // pad the rest
198 be32 = e->size.name_len; // abuse be32 as counter
199 while( s != NULL && s->name_len > be32) {
200 if( fwrite(&pad, 1, 1, fd) == 1 ) {
201 be32++;
202 } else {
203 DEBUGF("file_entry_write: failed to pad name\n");
204 return ERR_FILE;
208 // hash
209 be32 = BE32(e->hash);
210 if( fwrite(&be32, sizeof(be32), 1, fd) != 1 ) {
211 DEBUGF("file_entry_write: failed to write hash\n");
212 return ERR_FILE;
215 // song
216 be32 = BE32(e->song);
217 if( fwrite(&be32, sizeof(be32), 1, fd) != 1 ) {
218 DEBUGF("file_entry_write: failed to write song\n");
219 return ERR_FILE;
222 // rundb
223 be32 = BE32(e->rundb);
224 if( fwrite(&be32, sizeof(be32), 1, fd) != 1 ) {
225 DEBUGF("file_entry_write: failed to write rundb\n");
226 return ERR_FILE;
229 return ERR_NONE;
232 inline int file_entry_compare(const struct file_entry *a, const struct file_entry *b) {
233 assert(a != NULL);
234 assert(b != NULL);
235 return strncasecmp(a->name, b->name, (a->size.name_len <= b->size.name_len ? a->size.name_len : b->size.name_len) );
238 struct file_size* new_file_size() {
239 struct file_size *s;
240 s = (struct file_size*)malloc(sizeof(struct file_size));
241 if( s == NULL ) {
242 DEBUGF("new_file_size: failed to allocate memory\n");
243 return NULL;
245 s->name_len = 0;
247 return s;
250 inline uint32_t file_size_get_length(const struct file_size *size) {
251 assert(size != NULL);
252 return size->name_len + 12;
255 inline int file_size_max(struct file_size *s, const struct file_entry *e) {
256 assert(s != NULL);
257 assert(e != NULL);
258 assert(FLAG_VALID(e->flag));
259 s->name_len = ( s->name_len >= e->size.name_len ? s->name_len : e->size.name_len );
260 return ERR_NONE;
263 int file_size_destruct(struct file_size *s) {
264 assert(s != NULL);
265 // nothing to do...
266 free(s);
267 return ERR_NONE;