~ubuntu-branches/ubuntu/precise/hime/precise

« back to all changes in this revision

Viewing changes to src/gtab-use-count.c

  • Committer: Package Import Robot
  • Author(s): Yao Wei (魏銘廷)
  • Date: 2012-01-14 00:24:08 UTC
  • Revision ID: package-import@ubuntu.com-20120114002408-e79gagbeg1rt8npv
Tags: upstream-0.9.9
Import upstream version 0.9.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2010 Edward Der-Hua Liu, Hsin-Chu, Taiwan
 
2
 *
 
3
 * This library is free software; you can redistribute it and/or
 
4
 * modify it under the terms of the GNU Lesser General Public
 
5
 * License as published by the Free Software Foundation; either
 
6
 * version 2.1 of the License, or (at your option) any later version.
 
7
 *
 
8
 * This library is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
 * Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public
 
14
 * License along with this library; if not, write to the Free Software
 
15
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
16
 */
 
17
 
 
18
#include "hime.h"
 
19
#include "gtab.h"
 
20
 
 
21
typedef struct {
 
22
  int use_count;
 
23
  u_char bytes, flag;
 
24
} GTAB_USE_CNT;
 
25
 
 
26
static char gtab_use_count_file[]="gtab-use-count2";
 
27
static FILE *fp_gtab_use_count;
 
28
 
 
29
static void init_fp()
 
30
{
 
31
  if (!fp_gtab_use_count) {
 
32
    char fname[128];
 
33
    get_hime_user_fname(gtab_use_count_file, fname);
 
34
 
 
35
    if (!(fp_gtab_use_count=fopen(fname, "rb+"))) {
 
36
      if (!(fp_gtab_use_count=fopen(fname, "wb+"))) {
 
37
        dbg("cannot write to %s\n", fname);
 
38
        return;
 
39
      }
 
40
    }
 
41
  }
 
42
}
 
43
 
 
44
 
 
45
void inc_gtab_use_count(char *s)
 
46
{
 
47
  init_fp();
 
48
 
 
49
  int bytes = strlen(s);
 
50
 
 
51
  GTAB_USE_CNT c;
 
52
  rewind(fp_gtab_use_count);
 
53
 
 
54
//  dbg("zzz '%s' bytes:%d\n", s, bytes);
 
55
//  dbg("inc %d\n", ftell(fp_gtab_use_count));
 
56
  while (!feof(fp_gtab_use_count)) {
 
57
    char tt[512];
 
58
    bzero(&c, sizeof(c));
 
59
    fread(&c, sizeof(c), 1, fp_gtab_use_count);
 
60
    if (c.bytes != bytes)
 
61
      continue;
 
62
    fread(tt, bytes, 1, fp_gtab_use_count);
 
63
 
 
64
    if (memcmp(tt, s, bytes))
 
65
      continue;
 
66
 
 
67
//    long ofs = ftell(fp_gtab_use_count);
 
68
//    dbg("aa %d ofs:%d sz:%d\n", c.use_count, ofs, sizeof(c));
 
69
    fseek(fp_gtab_use_count, - (sizeof(c)+bytes) , SEEK_CUR);
 
70
//    dbg("bb %d ofs:%d\n", c.use_count, ftell(fp_gtab_use_count));
 
71
 
 
72
    c.use_count++;
 
73
    fwrite(&c, sizeof(c), 1, fp_gtab_use_count);
 
74
    fflush(fp_gtab_use_count);
 
75
    return;
 
76
  }
 
77
 
 
78
//  int fofs = ftell(fp_gtab_use_count);
 
79
//  dbg("fofs: %d\n", fofs);
 
80
 
 
81
#if 0
 
82
  int delta = fofs % sizeof(GTAB_USE_CNT);
 
83
  if (delta) // avoid incomplete write
 
84
    fseek(fp_gtab_use_count, - delta, SEEK_CUR);
 
85
#endif
 
86
 
 
87
  bzero(&c, sizeof(c));
 
88
  c.use_count = 1;
 
89
  c.bytes = bytes;
 
90
  fwrite(&c, sizeof(c), 1, fp_gtab_use_count);
 
91
  fwrite(s, bytes, 1, fp_gtab_use_count);
 
92
  fflush(fp_gtab_use_count);
 
93
}
 
94
 
 
95
 
 
96
int get_gtab_use_count(char *s)
 
97
{
 
98
  int bytes = strlen(s);
 
99
  init_fp();
 
100
 
 
101
//  dbg("get_gtab_use_count %s\n", s);
 
102
 
 
103
  GTAB_USE_CNT c;
 
104
  rewind(fp_gtab_use_count);
 
105
  while (!feof(fp_gtab_use_count)) {
 
106
    fread(&c, sizeof(c), 1, fp_gtab_use_count);
 
107
    if (c.bytes != bytes)
 
108
      continue;
 
109
    char tt[512];
 
110
    fread(tt, bytes, 1, fp_gtab_use_count);
 
111
 
 
112
    if (!memcmp(tt, s, bytes)) {
 
113
//      dbg("count found %s %d\n", s, c.use_count);
 
114
      return c.use_count;
 
115
    }
 
116
  }
 
117
 
 
118
//  dbg("not found\n");
 
119
 
 
120
  return 0;
 
121
}