~ubuntu-branches/debian/sid/skksearch/sid

« back to all changes in this revision

Viewing changes to .pc/030_db4.3.patch/dic_db.c

  • Committer: Package Import Robot
  • Author(s): Tatsuya Kinoshita
  • Date: 2012-09-01 20:53:26 UTC
  • Revision ID: package-import@ubuntu.com-20120901205326-rmw3gwmmpn3tkx23
Tags: 0.0-21
* debian/rules: Revive the -Wall option for CFLAGS
* debian/control: Fix Vcs-Browser
* debian/patches/*: Revert the previous changes

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
 
}