~ubuntu-branches/ubuntu/wily/skksearch/wily

« back to all changes in this revision

Viewing changes to .pc/db4.3.diff/dic_db.c

  • Committer: Bazaar Package Importer
  • Author(s): Tatsuya Kinoshita
  • Date: 2011-04-18 00:03:10 UTC
  • mfrom: (5.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20110418000310-kp7xm00g7gtltscj
Tags: 0.0-18
* Migration for libdb5.1. (closes: #621434)
* debian/control:
  - Remove quilt from Build-Depends.
  - Update Standards-Version to 3.9.2.
* debian/rules: Don't include patchsys-quilt.mk.
* debian/skksearch.8: Typo fix.
* debian/README.Debian: Doc fix for Berkeley DB 5.x.
* debian/README.source: Removed.
* debian/copyright: Switch to the DEP-5 format.
* Switch to the dpkg-source 3.0 (quilt) format.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 1999 Hideki Sakurada
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; either version 2, or (at your option)
 
7
 * any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 */
 
14
 
 
15
#include <stdlib.h>
 
16
#include <errno.h>
 
17
#include <string.h>
 
18
#include <db.h>
 
19
#include "config.h"
 
20
#include "err.h"
 
21
#include "dic.h"
 
22
#include "dic_db.h"
 
23
 
 
24
/* BerkeleyDB environment */
 
25
int env_initialized = 0;
 
26
DB_ENV env;
 
27
void errcall(const char *, char *);
 
28
 
 
29
 
 
30
struct dic *dic_db_open(struct dic *d, char *path) {
 
31
  struct dic_db_internal *internal;
 
32
 
 
33
  if (!env_initialized) {
 
34
    env.db_errcall = errcall;
 
35
    if ((errno = db_appinit(NULL, NULL, &env, DB_USE_ENVIRON)) != 0) {
 
36
      err(LOG_ERR, "dic_db_open: db_appinit failed\n");
 
37
      exit(1);
 
38
    }
 
39
    env_initialized = 1;
 
40
  }
 
41
 
 
42
  if ((internal = malloc(sizeof(struct dic_db_internal))) == NULL) {
 
43
    err(LOG_ERR, "dic_db_open(%s): %s\n", path, strerror(errno));
 
44
    exit(1);
 
45
  }
 
46
  memset(internal, 0, sizeof(struct dic_db_internal));
 
47
 
 
48
  if ((errno =
 
49
       db_open(path, DB_UNKNOWN, DB_RDONLY, 0, &env, NULL, &(internal->db)))
 
50
      != 0) {
 
51
    err(LOG_ERR, "dic_db_open(%s): %s\n", path, strerror(errno));
 
52
    exit(1);
 
53
  }
 
54
  d->internal = (void *)internal;
 
55
  d->search = dic_db_search;
 
56
  d->close = dic_db_close;
 
57
  return d;
 
58
  }
 
59
 
 
60
char *dic_db_search(struct dic *d, char *keystr, int keylen) {
 
61
  struct dic_db_internal *internal = (struct dic_db_internal *)(d->internal);
 
62
  DB *db = internal->db;
 
63
  DBT key;
 
64
  DBT data;
 
65
 
 
66
  key.data = keystr;
 
67
  key.size = keylen;
 
68
  key.flags = 0;
 
69
  data.data = d->buf;
 
70
  data.size = 0;
 
71
  data.ulen = DIC_BUFSIZE - 1;  /* -1 for '\0' */
 
72
  data.flags = DB_DBT_USERMEM;
 
73
 
 
74
  if ((errno = db->get(db, NULL, &key, &data, 0)) == DB_NOTFOUND) {
 
75
    return NULL;
 
76
  } else if (errno == 0) {      /* found */
 
77
    *((char *)(data.data) + data.size) = '\0';
 
78
    return (char *)(data.data);
 
79
  } else {
 
80
    err(LOG_WARNING, "dic_db_search: %s (may be too long entry)\n",
 
81
        strerror(errno));
 
82
    return NULL;
 
83
  }
 
84
}
 
85
 
 
86
 
 
87
int dic_db_close(struct dic *d) {
 
88
  DB *db = ((struct dic_db_internal *)(d->internal))->db;
 
89
  if ((errno = db->close(db, 0)) != 0) {
 
90
    err(LOG_ERR, "dic_db_close: %s\n", strerror(errno));
 
91
    exit(1);
 
92
  }
 
93
  free(d->internal);
 
94
  free(d);
 
95
  return 0;
 
96
}
 
97
 
 
98
 
 
99
void errcall(const char *foo, char *message) {
 
100
  err(LOG_ERR, "dic_db: %s\n", message);
 
101
}