1
/* Copyright (C) 2010 Edward Der-Hua Liu, Hsin-Chu, Taiwan
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.
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.
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
26
static char gtab_use_count_file[]="gtab-use-count2";
27
static FILE *fp_gtab_use_count;
31
if (!fp_gtab_use_count) {
33
get_hime_user_fname(gtab_use_count_file, fname);
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);
45
void inc_gtab_use_count(char *s)
49
int bytes = strlen(s);
52
rewind(fp_gtab_use_count);
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)) {
59
fread(&c, sizeof(c), 1, fp_gtab_use_count);
62
fread(tt, bytes, 1, fp_gtab_use_count);
64
if (memcmp(tt, s, bytes))
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));
73
fwrite(&c, sizeof(c), 1, fp_gtab_use_count);
74
fflush(fp_gtab_use_count);
78
// int fofs = ftell(fp_gtab_use_count);
79
// dbg("fofs: %d\n", fofs);
82
int delta = fofs % sizeof(GTAB_USE_CNT);
83
if (delta) // avoid incomplete write
84
fseek(fp_gtab_use_count, - delta, SEEK_CUR);
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);
96
int get_gtab_use_count(char *s)
98
int bytes = strlen(s);
101
// dbg("get_gtab_use_count %s\n", s);
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)
110
fread(tt, bytes, 1, fp_gtab_use_count);
112
if (!memcmp(tt, s, bytes)) {
113
// dbg("count found %s %d\n", s, c.use_count);
118
// dbg("not found\n");