Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 319 lines (295 sloc) 10.638 kb
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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
/*
* Copyright (c) 2010 Openmoko Inc.
*
* 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 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <ctype.h>
#include <stddef.h>
#include <stdlib.h>
#include <errno.h>

#include <grifo.h>

#include "ustring.h"
#include "search.h"
#include "wiki_info.h"
#include "search_fnd.h"
#include "lcd_buf_draw.h"
#include "btree.h"

extern int search_interrupted;
#define FND_BUF_COUNT 1024
#define MAX_FND_FILES 16

// FND_BUF_BLOCK_SIZE needs to be larger than NUMBER_OF_FIRST_PAGE_RESULTS * sizeof(TITLE_SEARCH)
#define FND_BUF_BLOCK_SIZE 4096
typedef struct __attribute__ ((packed)) _fnd_buf {
uint32_t offset;
uint16_t len;
int16_t prev_used_seq; // entry idx with the used sequence before this
int16_t next_used_seq; // entry idx with the used sequence after this
uint8_t wiki_id;
char buf[FND_BUF_BLOCK_SIZE];
} FND_BUF, *PFND_BUF;
PFND_BUF pFndBuf;
int nIdxFndBufFirstUsed;
int nIdxFndBufLastUsed;

typedef struct __attribute__ ((packed)) _PER_WIKI_INFO {
int bSearchFndInited;
unsigned int nFndCount;
int fdFnd[MAX_FND_FILES];
unsigned long offsetFndStart[MAX_FND_FILES];
unsigned long lenFnd[MAX_FND_FILES];
} PER_WIKI_INFO, *PPER_WIKI_INFO;
PPER_WIKI_INFO pPerWikiInfo;

long nUsedSeq = 1;
BTREE btree;

void init_search_fnd(void)
{
static int bFirstCall = 1;
int i;

if (bFirstCall)
{
btree_init(&btree, FND_BUF_COUNT, 0);
pFndBuf = (PFND_BUF)memory_allocate(sizeof(FND_BUF) * FND_BUF_COUNT, "searchfnd1");
nIdxFndBufFirstUsed = 0;
nIdxFndBufLastUsed = FND_BUF_COUNT - 1;
for (i = 0; i < FND_BUF_COUNT; i++)
{
pFndBuf[i].offset = 0;
pFndBuf[i].prev_used_seq = i - 1;
if (i == FND_BUF_COUNT - 1)
pFndBuf[i].next_used_seq = -1;
else
pFndBuf[i].next_used_seq = i + 1;
}
pPerWikiInfo = (PPER_WIKI_INFO)memory_allocate(sizeof(PER_WIKI_INFO) * get_wiki_count(), "searchfnd2");
for (i = 0; i < get_wiki_count(); i++)
{
pPerWikiInfo[i].bSearchFndInited = 0;
}
bFirstCall = 0;
}

if (!pPerWikiInfo[nCurrentWiki].bSearchFndInited)
{
pPerWikiInfo[nCurrentWiki].fdFnd[0] = file_open(get_wiki_file_path(nCurrentWiki, "wiki.fnd"), FILE_OPEN_READ);
init_bigram(pPerWikiInfo[nCurrentWiki].fdFnd[0]);
pPerWikiInfo[nCurrentWiki].bSearchFndInited = 1;
pPerWikiInfo[nCurrentWiki].offsetFndStart[0] = 0;
file_size(get_wiki_file_path(nCurrentWiki, "wiki.fnd"), &pPerWikiInfo[nCurrentWiki].lenFnd[0]);
for (i = 1; i < MAX_FND_FILES; i++)
{
char file_name[13];
sprintf(file_name, "wiki%d.fnd", i);
pPerWikiInfo[nCurrentWiki].fdFnd[i] = file_open(get_wiki_file_path(nCurrentWiki, file_name), FILE_OPEN_READ);
if (pPerWikiInfo[nCurrentWiki].fdFnd[i] < 0)
break;
pPerWikiInfo[nCurrentWiki].offsetFndStart[i] = pPerWikiInfo[nCurrentWiki].offsetFndStart[i - 1] +
pPerWikiInfo[nCurrentWiki].lenFnd[i - 1];
file_size(get_wiki_file_path(nCurrentWiki, file_name), &pPerWikiInfo[nCurrentWiki].lenFnd[i]);
}
pPerWikiInfo[nCurrentWiki].nFndCount = i;
}
}

int copy_fnd_to_buf(long offset, unsigned char *buf, int len)
{
int idxFndBuf;
int nCopyLen;
unsigned long blocked_offset;
PBTREE_ELEMENT pElement;
long nKey;

blocked_offset = ((offset - SIZE_BIGRAM_BUF) / FND_BUF_BLOCK_SIZE) * FND_BUF_BLOCK_SIZE + SIZE_BIGRAM_BUF;
nKey = (nCurrentWiki << 24) | blocked_offset;
pElement = btree_search(&btree, nKey);
if (pElement)
{
idxFndBuf = pElement->data_entry_idx;
}
else
{
BTREE_ELEMENT element;
unsigned int nFndIdx;
idxFndBuf = nIdxFndBufFirstUsed;
if (pFndBuf[idxFndBuf].offset)
{
long nKeyOld = (pFndBuf[idxFndBuf].wiki_id << 24) | pFndBuf[idxFndBuf].offset;
btree_delete(&btree, nKeyOld);
}
for (nFndIdx = 0; nFndIdx < pPerWikiInfo[nCurrentWiki].nFndCount; nFndIdx++)
{
if (pPerWikiInfo[nCurrentWiki].offsetFndStart[nFndIdx] <= blocked_offset &&
blocked_offset < pPerWikiInfo[nCurrentWiki].offsetFndStart[nFndIdx] +
pPerWikiInfo[nCurrentWiki].lenFnd[nFndIdx])
{
break;
}
}
pFndBuf[idxFndBuf].len = 0;
if (nFndIdx < pPerWikiInfo[nCurrentWiki].nFndCount)
{
file_lseek(pPerWikiInfo[nCurrentWiki].fdFnd[nFndIdx],
blocked_offset - pPerWikiInfo[nCurrentWiki].offsetFndStart[nFndIdx]);
pFndBuf[idxFndBuf].len = file_read(pPerWikiInfo[nCurrentWiki].fdFnd[nFndIdx],
&pFndBuf[idxFndBuf].buf[0], FND_BUF_BLOCK_SIZE);
}
if (nFndIdx >= pPerWikiInfo[nCurrentWiki].nFndCount || pFndBuf[idxFndBuf].len <= 0)
{
pFndBuf[idxFndBuf].offset = 0;
if (idxFndBuf != nIdxFndBufFirstUsed)
{
pFndBuf[pFndBuf[idxFndBuf].prev_used_seq].next_used_seq = pFndBuf[idxFndBuf].next_used_seq;
if (idxFndBuf != nIdxFndBufLastUsed)
pFndBuf[pFndBuf[idxFndBuf].next_used_seq].prev_used_seq = pFndBuf[idxFndBuf].prev_used_seq;
else
nIdxFndBufFirstUsed = pFndBuf[idxFndBuf].prev_used_seq;
pFndBuf[nIdxFndBufFirstUsed].prev_used_seq = idxFndBuf;
pFndBuf[idxFndBuf].next_used_seq = nIdxFndBufFirstUsed;
nIdxFndBufFirstUsed = idxFndBuf;
pFndBuf[idxFndBuf].prev_used_seq = -1;
}
return 0;
}
if (pFndBuf[idxFndBuf].len < FND_BUF_BLOCK_SIZE && nFndIdx < pPerWikiInfo[nCurrentWiki].nFndCount - 1)
{
file_lseek(pPerWikiInfo[nCurrentWiki].fdFnd[nFndIdx + 1], 0);
pFndBuf[idxFndBuf].len += file_read(pPerWikiInfo[nCurrentWiki].fdFnd[nFndIdx + 1],
&pFndBuf[idxFndBuf].buf[pFndBuf[idxFndBuf].len], FND_BUF_BLOCK_SIZE - pFndBuf[idxFndBuf].len);
}
pFndBuf[idxFndBuf].offset = blocked_offset;
element.key = nKey;
element.data_entry_idx = idxFndBuf;
element.subtree_node_idx = BTREE_INVALID_NODE_IDX;
btree_insert(&btree, &element);
}

if (pFndBuf[idxFndBuf].prev_used_seq < 0)
{
nIdxFndBufFirstUsed = pFndBuf[idxFndBuf].next_used_seq;
pFndBuf[nIdxFndBufFirstUsed].prev_used_seq = -1;
}
else if (idxFndBuf != nIdxFndBufLastUsed)
{
pFndBuf[pFndBuf[idxFndBuf].prev_used_seq].next_used_seq = pFndBuf[idxFndBuf].next_used_seq;
pFndBuf[pFndBuf[idxFndBuf].next_used_seq].prev_used_seq = pFndBuf[idxFndBuf].prev_used_seq;
}

if (idxFndBuf != nIdxFndBufLastUsed)
{
pFndBuf[idxFndBuf].prev_used_seq = nIdxFndBufLastUsed;
pFndBuf[idxFndBuf].next_used_seq = -1;
pFndBuf[nIdxFndBufLastUsed].next_used_seq = idxFndBuf;
nIdxFndBufLastUsed = idxFndBuf;
}

if (len > (int)(pFndBuf[idxFndBuf].len - (offset - pFndBuf[idxFndBuf].offset))) // the buf to be copied is separated into two blocks or end of file
nCopyLen = pFndBuf[idxFndBuf].len - (offset - pFndBuf[idxFndBuf].offset);
else
nCopyLen = len;

if (nCopyLen < 0)
nCopyLen = 0;
else
memcpy(buf, &pFndBuf[idxFndBuf].buf[offset - pFndBuf[idxFndBuf].offset], nCopyLen);

if (nCopyLen > 0 && nCopyLen < len)
nCopyLen += copy_fnd_to_buf(pFndBuf[idxFndBuf].offset + pFndBuf[idxFndBuf].len, &buf[nCopyLen], len - nCopyLen);
return nCopyLen;
}

long locate_previous_title_search(long offset_fnd)
{
long len_buf;
int nZeros;
unsigned int i;
unsigned char buf[sizeof(TITLE_SEARCH)];

if (offset_fnd > (long)sizeof(TITLE_SEARCH))
len_buf = sizeof(TITLE_SEARCH);
else
len_buf = offset_fnd;
offset_fnd -= len_buf;
copy_fnd_to_buf(offset_fnd, buf, len_buf);
i = len_buf - 1;
nZeros = 0;
while (i >= sizeof(uint32_t) && nZeros < 3)
{
if (!buf[i])
nZeros++;
i--;
}
i -= sizeof(uint32_t) - 1;
return offset_fnd + i;
}

void retrieve_titles_from_fnd(long offset_fnd, unsigned char *sTitleSearchOut, unsigned char *sTitleActual)
{
TITLE_SEARCH aTitleSearch[SEARCH_FND_SEQUENTIAL_SEARCH_THRESHOLD];
unsigned char sTitleSearch[MAX_TITLE_SEARCH];
int nTitleSearch = 0;
int bFound1 = 0;
int bFound2 = 0;
int i;
int lenDuplicated;

// Find the title that is fully spelled out.
// The repeated characters with the previous title at the beginning of the current title will be replace by
// a character whose binary value is the number of the repeated characters.
while ((!bFound1 || !bFound2) && offset_fnd >= SIZE_BIGRAM_BUF + (long)sizeof(uint32_t) && nTitleSearch < SEARCH_FND_SEQUENTIAL_SEARCH_THRESHOLD)
{
const unsigned char *p;

copy_fnd_to_buf(offset_fnd, (unsigned char *)&aTitleSearch[nTitleSearch], sizeof(TITLE_SEARCH));
p = aTitleSearch[nTitleSearch].sTitleSearch;
ustrncpy(sTitleSearch, p, MAX_TITLE_SEARCH);
sTitleSearch[MAX_TITLE_SEARCH - 1] = '\0';
//bigram_decode(sTitleSearch, p, MAX_TITLE_SEARCH);
p += ustrlen(aTitleSearch[nTitleSearch].sTitleSearch) + 1; // pointing to actual title
//bigram_decode(sTitleActual, p, MAX_TITLE_SEARCH);
ustrncpy(sTitleActual, p, MAX_TITLE_ACTUAL);
sTitleActual[MAX_TITLE_ACTUAL - 1] = '\0';
ustrcpy(aTitleSearch[nTitleSearch].sTitleSearch, sTitleSearch);
ustrcpy(aTitleSearch[nTitleSearch].sTitleActual, sTitleActual);
if (aTitleSearch[nTitleSearch].sTitleSearch[0] >= ' ')
bFound1 = 1;
if (aTitleSearch[nTitleSearch].sTitleActual[0] >= ' ')
bFound2 = 1;
if (!bFound1 || !bFound2)
offset_fnd = locate_previous_title_search(offset_fnd);
nTitleSearch++;
}

sTitleSearch[0] = '\0';
if (bFound1)
{
for (i = nTitleSearch - 1; i >= 0; i--)
{
if ((unsigned char)aTitleSearch[i].sTitleSearch[0] >= ' ')
{
ustrncpy(sTitleSearch, aTitleSearch[i].sTitleSearch, MAX_TITLE_SEARCH);
sTitleSearch[MAX_TITLE_SEARCH - 1] = '\0';
}
else if (sTitleSearch[0])
{
lenDuplicated = aTitleSearch[i].sTitleSearch[0] + 1;
memcpy(&sTitleSearch[lenDuplicated], &aTitleSearch[i].sTitleSearch[1], MAX_TITLE_SEARCH - lenDuplicated - 1);
sTitleSearch[MAX_TITLE_SEARCH - 1] = '\0';
}
}
}
bigram_decode(sTitleSearchOut, sTitleSearch, MAX_TITLE_SEARCH);

sTitleActual[0] = '\0';
if (bFound2)
{
for (i = nTitleSearch - 1; i >= 0; i--)
{
if ((unsigned char)aTitleSearch[i].sTitleActual[0] >= ' ')
{
ustrncpy(sTitleActual, aTitleSearch[i].sTitleActual, MAX_TITLE_ACTUAL);
sTitleActual[MAX_TITLE_ACTUAL - 1] = '\0';
}
else if (sTitleActual[0])
{
lenDuplicated = aTitleSearch[i].sTitleActual[0] + 1;
memcpy(&sTitleActual[lenDuplicated], &aTitleSearch[i].sTitleActual[1], MAX_TITLE_ACTUAL - lenDuplicated - 1);
sTitleActual[MAX_TITLE_ACTUAL - 1] = '\0';
}
}
}
}
Something went wrong with that request. Please try again.