Introduce yield_codec(), similar to the original audio_yield_codecs()
[Rockbox.git] / tools / rdf2binary.c
blobcfafcb21a1cec3d815fd42673757fd3dfb14165b
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Miika Pekkarinen
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
21 This tool converts the rdf file to the binary data used in the dict plugin.
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <string.h>
28 #include <stdio.h>
30 /* maximum word lenght, has to be the same in dict.c */
31 #define WORDLEN 32
33 /* struckt packing */
34 #ifdef __GNUC__
35 #define STRUCT_PACKED __attribute__((packed))
36 #else
37 #define STRUCT_PACKED
38 #pragma pack (push, 2)
39 #endif
42 struct word
44 char word[WORDLEN];
45 long offset;
46 } STRUCT_PACKED;
48 /* convert offsets here, not on device. */
49 long reverse (long N) {
50 unsigned char B[4];
51 B[0] = (N & 0x000000FF) >> 0;
52 B[1] = (N & 0x0000FF00) >> 8;
53 B[2] = (N & 0x00FF0000) >> 16;
54 B[3] = (N & 0xFF000000) >> 24;
55 return ((B[0] << 24) | (B[1] << 16) | (B[2] << 8) | (B[3] << 0));
59 int main()
61 FILE *in, *idx_out, *desc_out;
62 struct word w;
63 char buf[10000];
64 long cur_offset = 0;
66 in = fopen("dict.preparsed", "r");
67 idx_out = fopen("dict.index", "wb");
68 desc_out = fopen("dict.desc", "wb");
70 if (in == NULL || idx_out == NULL || desc_out == NULL)
72 fprintf(stderr, "Error: Some files couldn't be opened\n");
73 return 1;
76 while (fgets(buf, sizeof buf, in) != NULL)
78 /* It is safe to use strtok here */
79 const char *word = strtok(buf, "\t");
80 const char *desc = strtok(NULL, "\t");
82 if (word == NULL || desc == NULL)
84 fprintf(stderr, "Parse error!\n");
85 fprintf(stderr, "word: %s\ndesc: %s\n", word, desc);
87 return 2;
90 /* We will null-terminate the words */
91 strncpy(w.word, word, WORDLEN - 1);
92 w.offset = reverse(cur_offset);
93 fwrite(&w, sizeof(struct word), 1, idx_out);
95 while (1)
97 int len = strlen(desc);
98 cur_offset += len;
99 fwrite(desc, len, 1, desc_out);
101 desc = strtok(NULL, "\t");
102 if (desc == NULL)
103 break ;
105 cur_offset++;
106 fwrite("\n", 1, 1, desc_out);
111 return 0;