source: trunk/gsdl3/src/packages/mg/lib/filestats.c@ 3745

Last change on this file since 3745 was 3745, checked in by mdewsnip, 21 years ago

Addition of MG package for search and retrieval

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 3.1 KB
Line 
1/**************************************************************************
2 *
3 * filestats.c -- Functions for keeping stats on file accesses
4 * Copyright (C) 1994 Neil Sharman
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 * $Id: filestats.c 3745 2003-02-20 21:20:24Z mdewsnip $
21 *
22 **************************************************************************/
23
24
25/*
26 $Log$
27 Revision 1.1 2003/02/20 21:14:16 mdewsnip
28 Addition of MG package for search and retrieval
29
30 Revision 1.1 1999/08/10 21:16:48 sjboddie
31 renamed mg-1.3d directory mg
32
33 Revision 1.1 1998/11/17 09:31:54 rjmcnab
34 *** empty log message ***
35
36 * Revision 1.1 1994/08/22 00:24:42 tes
37 * Initial placement under CVS.
38 *
39 */
40
41static char *RCSID = "$Id: filestats.c 3745 2003-02-20 21:20:24Z mdewsnip $";
42
43#include "sysfuncs.h"
44
45#include "memlib.h"
46#include "filestats.h"
47#include "netorder.h" /* [RPAP - Jan 97: Endian Ordering] */
48
49
50File *
51Fopen (char *name, char *mode, unsigned long magic)
52{
53 FILE *f;
54 File *F;
55 unsigned long m;
56 if (!(f = fopen (name, mode)))
57 return (NULL);
58 if (magic)
59 switch (*mode)
60 {
61 case 'r':
62 fread ((char *) &m, sizeof (m), 1, f);
63 NTOHUL(m); /* [RPAP - Jan 97: Endian Ordering] */
64 if (m != magic)
65 {
66 fclose (f);
67 return (NULL);
68 }
69 break;
70 case 'w':
71 HTONUL(magic); /* [RPAP - Jan 97: Endian Ordering] */
72 fwrite ((char *) &magic, sizeof (magic), 1, f);
73 }
74 if (!(F = Xmalloc (sizeof (File))))
75 {
76 fclose (f);
77 return (NULL);
78 }
79 F->pathname = Xstrdup (name);
80 F->name = strrchr (F->pathname, '/');
81 F->name = F->name ? F->name + 1 : F->pathname;
82 F->f = f;
83 F->Current.NumSeeks = F->Current.NumReads = F->Current.NumBytes = 0;
84 F->Cumulative = F->Current;
85 return (F);
86}
87
88
89int
90Fclose (File * F)
91{
92 int num;
93 if (!F)
94 return (0);
95 num = fclose (F->f);
96 if (F->pathname)
97 Xfree (F->pathname);
98 Xfree (F);
99 return (num);
100}
101
102size_t
103Fread (void *ptr, size_t size, size_t nitems, File * F)
104{
105 int num;
106 num = fread ((char *) ptr, size, nitems, F->f);
107 F->Current.NumReads++;
108 F->Current.NumBytes += num * size;
109 return (num);
110}
111
112int
113Fseek (File * F, long offset, int ptrname)
114{
115 int num;
116 num = fseek (F->f, offset, ptrname);
117 F->Current.NumSeeks++;
118 return (num);
119}
120
121void
122Rewind (File * F)
123{
124 rewind (F->f);
125 F->Current.NumSeeks++;
126}
127
128
129void
130ZeroFileStats (File * F)
131{
132 F->Cumulative.NumSeeks += F->Current.NumSeeks;
133 F->Cumulative.NumReads += F->Current.NumReads;
134 F->Cumulative.NumBytes += F->Current.NumBytes;
135 F->Current.NumSeeks = F->Current.NumReads = F->Current.NumBytes = 0;
136}
Note: See TracBrowser for help on using the repository browser.