2005-05-02 15:03:46 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 Tomas Salfischberger
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2005-05-02 15:03:46 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "plugin.h"
|
2010-06-08 11:07:36 +00:00
|
|
|
#include "lib/simple_viewer.h"
|
2005-05-02 15:03:46 +00:00
|
|
|
|
2010-08-24 14:30:46 +00:00
|
|
|
|
2006-01-15 18:20:18 +00:00
|
|
|
|
2010-06-08 11:07:36 +00:00
|
|
|
#define MIN_DESC_BUF_SIZE 0x400 /* arbitrary minimum size for description */
|
2005-05-02 15:03:46 +00:00
|
|
|
|
|
|
|
/* Some lenghts */
|
|
|
|
#define WORDLEN 32 /* has to be the same in rdf2binary.c */
|
|
|
|
|
2005-06-05 14:21:51 +00:00
|
|
|
/* Struct packing */
|
|
|
|
#ifdef __GNUC__
|
|
|
|
#define STRUCT_PACKED __attribute__((packed))
|
|
|
|
#else
|
|
|
|
#define STRUCT_PACKED
|
|
|
|
#pragma pack (push, 2)
|
|
|
|
#endif
|
|
|
|
|
2005-05-02 15:03:46 +00:00
|
|
|
/* The word struct :) */
|
|
|
|
struct stWord
|
|
|
|
{
|
|
|
|
char word[WORDLEN];
|
|
|
|
long offset;
|
2005-06-05 14:21:51 +00:00
|
|
|
} STRUCT_PACKED;
|
2005-05-02 15:03:46 +00:00
|
|
|
|
|
|
|
/* for endian problems */
|
2005-05-02 16:06:05 +00:00
|
|
|
#ifdef ROCKBOX_BIG_ENDIAN
|
2005-06-05 14:21:51 +00:00
|
|
|
#define reverse(x) x
|
2005-05-02 15:03:46 +00:00
|
|
|
#else
|
2005-06-05 14:21:51 +00:00
|
|
|
long reverse (long N) {
|
|
|
|
unsigned char B[4];
|
|
|
|
B[0] = (N & 0x000000FF) >> 0;
|
|
|
|
B[1] = (N & 0x0000FF00) >> 8;
|
|
|
|
B[2] = (N & 0x00FF0000) >> 16;
|
|
|
|
B[3] = (N & 0xFF000000) >> 24;
|
|
|
|
return ((B[0] << 24) | (B[1] << 16) | (B[2] << 8) | (B[3] << 0));
|
2005-05-02 15:03:46 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2005-06-05 14:21:51 +00:00
|
|
|
/* data files */
|
2009-05-27 20:31:07 +00:00
|
|
|
#define DICT_INDEX PLUGIN_APPS_DIR "/dict.index"
|
|
|
|
#define DICT_DESC PLUGIN_APPS_DIR "/dict.desc"
|
2005-06-05 14:21:51 +00:00
|
|
|
|
2005-05-02 15:03:46 +00:00
|
|
|
/* the main plugin function */
|
2009-01-16 10:34:40 +00:00
|
|
|
enum plugin_status plugin_start(const void* parameter)
|
2005-05-02 15:03:46 +00:00
|
|
|
{
|
|
|
|
char searchword[WORDLEN]; /* word to search for */
|
2005-05-02 23:09:21 +00:00
|
|
|
char *description; /* pointer to description buffer */
|
2005-05-02 15:03:46 +00:00
|
|
|
struct stWord word; /* the struct to read into */
|
|
|
|
int fIndex, fData; /* files */
|
|
|
|
int filesize, high, low, probe;
|
2010-06-08 11:07:36 +00:00
|
|
|
char *buffer;
|
|
|
|
size_t buffer_size;
|
2005-05-02 15:03:46 +00:00
|
|
|
|
|
|
|
/* plugin stuff */
|
|
|
|
(void)parameter;
|
|
|
|
|
2010-06-08 11:07:36 +00:00
|
|
|
/* allocate buffer. */
|
|
|
|
buffer = rb->plugin_get_buffer(&buffer_size);
|
|
|
|
if (buffer == NULL || buffer_size < MIN_DESC_BUF_SIZE)
|
2005-05-02 23:09:21 +00:00
|
|
|
{
|
2010-06-08 11:07:36 +00:00
|
|
|
DEBUGF("Err: Failed to allocate buffer.\n");
|
|
|
|
rb->splash(HZ*2, "Failed to allocate buffer.");
|
2005-05-02 23:09:21 +00:00
|
|
|
return PLUGIN_ERROR;
|
|
|
|
}
|
|
|
|
|
2010-06-08 11:07:36 +00:00
|
|
|
description = buffer;
|
2005-05-02 23:09:21 +00:00
|
|
|
|
2005-05-02 15:03:46 +00:00
|
|
|
/* "clear" input buffer */
|
|
|
|
searchword[0] = '\0';
|
|
|
|
|
2010-06-08 11:07:36 +00:00
|
|
|
/* get the word to search */
|
|
|
|
if (rb->kbd_input(searchword, sizeof(searchword)) < 0)
|
|
|
|
return PLUGIN_OK; /* input cancelled */
|
2005-05-02 15:03:46 +00:00
|
|
|
|
2005-06-05 14:21:51 +00:00
|
|
|
fIndex = rb->open(DICT_INDEX, O_RDONLY); /* index file */
|
2005-05-02 15:03:46 +00:00
|
|
|
if (fIndex < 0)
|
|
|
|
{
|
|
|
|
DEBUGF("Err: Failed to open index file.\n");
|
2007-03-16 21:56:08 +00:00
|
|
|
rb->splash(HZ*2, "Failed to open index.");
|
2005-05-02 15:03:46 +00:00
|
|
|
return PLUGIN_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
filesize = rb->filesize(fIndex); /* get filesize */
|
|
|
|
|
|
|
|
DEBUGF("Filesize: %d bytes = %d words \n", filesize,
|
2007-03-17 10:50:58 +00:00
|
|
|
(filesize / (int)sizeof(struct stWord)));
|
2005-05-02 15:03:46 +00:00
|
|
|
|
|
|
|
/* for the searching algorithm */
|
|
|
|
high = filesize / sizeof( struct stWord );
|
|
|
|
low = -1;
|
|
|
|
|
|
|
|
while (high - low > 1)
|
|
|
|
{
|
|
|
|
probe = (high + low) / 2;
|
|
|
|
|
|
|
|
/* Jump to word pointed by probe, and read it. */
|
|
|
|
rb->lseek(fIndex, sizeof(struct stWord) * probe, SEEK_SET);
|
|
|
|
rb->read(fIndex, &word, sizeof(struct stWord));
|
|
|
|
|
|
|
|
/* jump according to the found word. */
|
|
|
|
if (rb->strcasecmp(searchword, word.word) < 0)
|
|
|
|
{
|
|
|
|
high = probe;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
low = probe;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read in the word */
|
|
|
|
rb->lseek(fIndex, sizeof(struct stWord) * low, SEEK_SET);
|
|
|
|
rb->read(fIndex, &word, sizeof(struct stWord));
|
2010-06-08 11:07:36 +00:00
|
|
|
rb->close(fIndex);
|
2005-05-02 15:03:46 +00:00
|
|
|
|
|
|
|
/* Check if we found something */
|
|
|
|
if (low == -1 || rb->strcasecmp(searchword, word.word) != 0)
|
|
|
|
{
|
|
|
|
DEBUGF("Not found.\n");
|
2007-03-16 21:56:08 +00:00
|
|
|
rb->splash(HZ*2, "Not found.");
|
2005-05-02 15:03:46 +00:00
|
|
|
return PLUGIN_OK;
|
|
|
|
}
|
|
|
|
|
2007-03-17 09:54:28 +00:00
|
|
|
DEBUGF("Found %s at offset %ld\n", word.word, reverse(word.offset));
|
2005-05-02 15:03:46 +00:00
|
|
|
|
|
|
|
/* now open the description file */
|
2005-06-05 14:21:51 +00:00
|
|
|
fData = rb->open(DICT_DESC, O_RDONLY);
|
2005-05-02 15:03:46 +00:00
|
|
|
if (fData < 0)
|
|
|
|
{
|
|
|
|
DEBUGF("Err: Failed to open description file.\n");
|
2007-03-16 21:56:08 +00:00
|
|
|
rb->splash(HZ*2, "Failed to open descriptions.");
|
2005-05-02 15:03:46 +00:00
|
|
|
return PLUGIN_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* seek to the right offset */
|
2005-06-05 14:21:51 +00:00
|
|
|
rb->lseek(fData, (off_t)reverse(word.offset), SEEK_SET);
|
2005-05-02 15:03:46 +00:00
|
|
|
|
|
|
|
/* Read in the description */
|
2010-06-08 11:07:36 +00:00
|
|
|
rb->read_line(fData, description, buffer_size);
|
2005-05-02 15:03:46 +00:00
|
|
|
|
|
|
|
/* And print it to debug. */
|
|
|
|
DEBUGF("Description: %s\n", description);
|
|
|
|
|
2010-06-08 11:07:36 +00:00
|
|
|
rb->close(fData);
|
2005-05-02 15:03:46 +00:00
|
|
|
|
2010-06-08 11:07:36 +00:00
|
|
|
/* display description. */
|
|
|
|
view_text(searchword, description);
|
2005-05-02 15:03:46 +00:00
|
|
|
|
|
|
|
return PLUGIN_OK;
|
|
|
|
}
|