Tomas Salfischberger | 52abc68 | 2005-05-02 15:05:07 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2005 Miika Pekkarinen |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version 2 |
| 15 | * of the License, or (at your option) any later version. |
Tomas Salfischberger | 52abc68 | 2005-05-02 15:05:07 +0000 | [diff] [blame] | 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | |
| 22 | /* |
| 23 | This tool converts the rdf file to the binary data used in the dict plugin. |
| 24 | */ |
| 25 | |
| 26 | #include <sys/types.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <fcntl.h> |
| 29 | #include <string.h> |
| 30 | #include <stdio.h> |
| 31 | |
| 32 | /* maximum word lenght, has to be the same in dict.c */ |
| 33 | #define WORDLEN 32 |
| 34 | |
Tomas Salfischberger | 08bccbfd | 2005-06-05 14:23:15 +0000 | [diff] [blame] | 35 | /* struckt packing */ |
| 36 | #ifdef __GNUC__ |
| 37 | #define STRUCT_PACKED __attribute__((packed)) |
| 38 | #else |
| 39 | #define STRUCT_PACKED |
| 40 | #pragma pack (push, 2) |
| 41 | #endif |
| 42 | |
| 43 | |
Tomas Salfischberger | 23028f5 | 2005-05-02 16:06:05 +0000 | [diff] [blame] | 44 | struct word |
| 45 | { |
| 46 | char word[WORDLEN]; |
| 47 | long offset; |
Tomas Salfischberger | 08bccbfd | 2005-06-05 14:23:15 +0000 | [diff] [blame] | 48 | } STRUCT_PACKED; |
Tomas Salfischberger | 52abc68 | 2005-05-02 15:05:07 +0000 | [diff] [blame] | 49 | |
Tomas Salfischberger | 23028f5 | 2005-05-02 16:06:05 +0000 | [diff] [blame] | 50 | /* convert offsets here, not on device. */ |
Tomas Salfischberger | 08bccbfd | 2005-06-05 14:23:15 +0000 | [diff] [blame] | 51 | long reverse (long N) { |
| 52 | unsigned char B[4]; |
| 53 | B[0] = (N & 0x000000FF) >> 0; |
| 54 | B[1] = (N & 0x0000FF00) >> 8; |
| 55 | B[2] = (N & 0x00FF0000) >> 16; |
| 56 | B[3] = (N & 0xFF000000) >> 24; |
| 57 | return ((B[0] << 24) | (B[1] << 16) | (B[2] << 8) | (B[3] << 0)); |
Tomas Salfischberger | 23028f5 | 2005-05-02 16:06:05 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Tomas Salfischberger | 08bccbfd | 2005-06-05 14:23:15 +0000 | [diff] [blame] | 60 | |
Tomas Salfischberger | 52abc68 | 2005-05-02 15:05:07 +0000 | [diff] [blame] | 61 | int main() |
| 62 | { |
Tomas Salfischberger | 6875417 | 2005-05-02 23:50:43 +0000 | [diff] [blame] | 63 | FILE *in, *idx_out, *desc_out; |
Tomas Salfischberger | 23028f5 | 2005-05-02 16:06:05 +0000 | [diff] [blame] | 64 | struct word w; |
| 65 | char buf[10000]; |
| 66 | long cur_offset = 0; |
| 67 | |
| 68 | in = fopen("dict.preparsed", "r"); |
Tomas Salfischberger | 6875417 | 2005-05-02 23:50:43 +0000 | [diff] [blame] | 69 | idx_out = fopen("dict.index", "wb"); |
| 70 | desc_out = fopen("dict.desc", "wb"); |
Tomas Salfischberger | 23028f5 | 2005-05-02 16:06:05 +0000 | [diff] [blame] | 71 | |
Miika Pekkarinen | 3875b57 | 2006-11-12 02:29:52 +0000 | [diff] [blame] | 72 | if (in == NULL || idx_out == NULL || desc_out == NULL) |
Tomas Salfischberger | 23028f5 | 2005-05-02 16:06:05 +0000 | [diff] [blame] | 73 | { |
| 74 | fprintf(stderr, "Error: Some files couldn't be opened\n"); |
| 75 | return 1; |
| 76 | } |
| 77 | |
| 78 | while (fgets(buf, sizeof buf, in) != NULL) |
| 79 | { |
| 80 | /* It is safe to use strtok here */ |
| 81 | const char *word = strtok(buf, "\t"); |
| 82 | const char *desc = strtok(NULL, "\t"); |
| 83 | |
| 84 | if (word == NULL || desc == NULL) |
| 85 | { |
| 86 | fprintf(stderr, "Parse error!\n"); |
| 87 | fprintf(stderr, "word: %s\ndesc: %s\n", word, desc); |
| 88 | |
| 89 | return 2; |
| 90 | } |
| 91 | |
| 92 | /* We will null-terminate the words */ |
| 93 | strncpy(w.word, word, WORDLEN - 1); |
Tomas Salfischberger | 08bccbfd | 2005-06-05 14:23:15 +0000 | [diff] [blame] | 94 | w.offset = reverse(cur_offset); |
Tomas Salfischberger | 6875417 | 2005-05-02 23:50:43 +0000 | [diff] [blame] | 95 | fwrite(&w, sizeof(struct word), 1, idx_out); |
Tomas Salfischberger | 23028f5 | 2005-05-02 16:06:05 +0000 | [diff] [blame] | 96 | |
| 97 | while (1) |
| 98 | { |
| 99 | int len = strlen(desc); |
| 100 | cur_offset += len; |
Tomas Salfischberger | 6875417 | 2005-05-02 23:50:43 +0000 | [diff] [blame] | 101 | fwrite(desc, len, 1, desc_out); |
Tomas Salfischberger | 23028f5 | 2005-05-02 16:06:05 +0000 | [diff] [blame] | 102 | |
| 103 | desc = strtok(NULL, "\t"); |
| 104 | if (desc == NULL) |
| 105 | break ; |
| 106 | |
| 107 | cur_offset++; |
Tomas Salfischberger | 6875417 | 2005-05-02 23:50:43 +0000 | [diff] [blame] | 108 | fwrite("\n", 1, 1, desc_out); |
Tomas Salfischberger | 23028f5 | 2005-05-02 16:06:05 +0000 | [diff] [blame] | 109 | |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return 0; |
Tomas Salfischberger | 52abc68 | 2005-05-02 15:05:07 +0000 | [diff] [blame] | 114 | } |
| 115 | |