File: bcache.c

package info (click to toggle)
mpsql 2.1-2
  • links: PTS
  • area: non-free
  • in suites: potato
  • size: 3,528 kB
  • ctags: 4,886
  • sloc: ansic: 35,184; makefile: 3,761; sh: 44
file content (369 lines) | stat: -rw-r--r-- 10,526 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*  -*- c -*-  */
/* -------------------------------------------------------------------- *
**  copyright (c) 1995 ipvr stuttgart and thomas harrer
** -------------------------------------------------------------------- *
**
**  libhelp
**
**  a comprehensive hypertext help system for OSF/Motif(tm) applications. 
**  based on libhtmlw from NCSA Mosaic(tm) version 2.4
**
**  written by thomas harrer
**  e-mail: Thomas.Harrer@rus.uni-stuttgart.de
**  
** -------------------------------------------------------------------- *
*h  $Id: bcache.c,v 1.7 1995/06/28 12:59:30 thomas Exp $
** -------------------------------------------------------------------- *
**
*h  module:		bcache.c
**
**  contents:		buffer cache implementation
**
**  interface:		functions
**			* bcache_find
**			* bcache_insert
**			* bcache_free
**			* bcache_current
**			* bcache_flush
**
** -------------------------------------------------------------------- *
**  license and copying issues:
**
**  this software is free; you can redistribute it and/or modify it 
**  under terms similar to the gnu general public license (version 1 
**  or any later version published by the free software foundation). 
**  see the file Licence for more details.
**
**  this program is distributed in the hope that it will be useful,
**  but without any warranty; without even the implied warranty of
**  merchantability or fitness for a particular purpose.  
** -------------------------------------------------------------------- */
/* -------------------------------------------------------------------- *
*g  include files
** -------------------------------------------------------------------- */
#include "helpp.h"
#include "buffer.h"
#include "bcache.h"
#include "util.h"
#include "path.h"

/* -------------------------------------------------------------------- *
*g  module identification
** -------------------------------------------------------------------- */
#ifdef RCSID
static char rcsid [] =
    "$Id: bcache.c,v 1.7 1995/06/28 12:59:30 thomas Exp $";
#endif /* RCSID */

/* -------------------------------------------------------------------- *
*g  global type:	bcache_t
**			buffer cache.
** -------------------------------------------------------------------- */
typedef struct bcache_s
{
    char*		filename;      /* file from reference		*/
    char*		path;	       /* the preferred path.           */
    buffer_t*		buf;	       /* the buffer.			*/
    struct bcache_s*	next;
    
} bcache_t;

/* -------------------------------------------------------------------- *
*p  private prototypes
** -------------------------------------------------------------------- */
static void bcache_free (bcache_t*);

/* -------------------------------------------------------------------- *
*g  global variables
** -------------------------------------------------------------------- */
static bcache_t* the_bcache = NULL;    
static int	bcache_len = 0;	      	

/* -------------------------------------------------------------------- *
*p  procedure-name:	bcache_current
**
**  purpose:		returns the current path and filename or NULL.
**			the returned buffer must be freed by the caller.
** -------------------------------------------------------------------- *
**  returns:	        if we have a file and a pathname, we concatenate
**			them. if we have either a file or a path, we return 
**			it. else we return NULL.
** -------------------------------------------------------------------- */
char*
bcache_current (void)
{
    char* current_path = NULL;	/* scope: return value. */

    execute ("bcache_current");
    
    /* if we have the bcache we return the value, else NULL.  */
    if (the_bcache) {

	char* current = the_bcache->filename;

	if (current) {
	    /* absolute names don't have path's added.  */
	    if ((current[0] == '.') || (current[0] == '/')) {

		checked_strdup (current_path, current);

	    } else  {

		/* relative names are appended to the path.  */
		char* path = the_bcache->path;
	    
		if (path) {
		    int len = c_strlen (current) + c_strlen (path) + 10;
		    checked_malloc (current_path, len, char);
		    c_strcpy (current_path, path);
		    c_strcat (current_path, current);
		} else  {
		    checked_strdup (current_path, current);
		}
	    }

	} else  { 
	    
	    if (the_bcache->path) {
		checked_strdup (current_path, the_bcache->path);
	    }
	}
    }
    
    return current_path;
}

/* -------------------------------------------------------------------- *
*p  procedure-name:	bcache_find
**
**  purpose:		returns a cached buffer or NULL
** -------------------------------------------------------------------- *
**  args:		filename
**  return type:	buffer_t*	
**  side effect:	the current path is added to the help path.
**  error handling.:	returns NULL.
** -------------------------------------------------------------------- */
buffer_t*	
bcache_find (/* i  */ char* ref)
{
    /* local data  */
    buffer_t* return_buffer = NULL; /* scope: return value */
    char* current_path = NULL;	/* pointer to the current path. */
    
    execute ("bcache_find");
    
    if (the_bcache) {

	if (ref) {

	    /* local data  */
	    /* scope: local  */
	    char* filename = get_file_and_strip_path (ref);
	    /* scope: local  */
	    char* pathname = get_path (ref);
	    
	    bcache_trace ("ref: %s", ref);
	    bcache_trace ("\tfile: %s", filename);
	    bcache_trace ("\tpath: %s", pathname);

	    if ((!filename) || (filename[0] != '\0')) {

		bcache_t*	bc = the_bcache;
		bcache_t*	pre = NULL;
		
		/*
		 *  we traverse the cache and compare file and
		 *  path names.
		 */
		while (bc) {

		    /*
		     *  logic: if we have no filename we just compare 
		     *  with the path. else we compare file and path.
		     */
		    
		    bcache_trace ("* filename: %s\n", filename);
		    bcache_trace ("* bc->filename: %s\n", bc->filename);
		    bcache_trace ("* pathname: %s\n", pathname);
		    bcache_trace ("* bc->path: %s\n", bc->path);
		    
		    if ((!filename) && (!bc->filename)) {
			if (((pathname) && (bc->path))
			    && (0 == c_strcmp (bc->path, pathname))) {
			    break;
			}			
		    }
		    if (((filename) && (bc->filename))
			&& (0 == c_strcmp (bc->filename, filename))) {
			if (((pathname) && (bc->path))
			    && (0 == c_strcmp (bc->path, pathname))) {
			    break;
			} else  {
			    break;
			}
		    }
		    pre = bc;
		    bc = bc->next;
		}

		/* bc is NULL if nothing was found.  */
		if (bc) {

		    return_buffer = bc->buf; /* the desired buffer.  */
		    current_path = bc->path;
		    
		    bcache_trace ("found %s", bc->filename);
		    bcache_trace ("path %s\n", bc->path);

		    /* we move the element to the first position.  */
		    if (pre) {

			pre->next = bc->next;
			bc->next = the_bcache;
			the_bcache = bc;

			bcache_trace ("moved %s on top\n", bc->filename);
		    }
		}

	    } else {

		/* reference to the current buffer.  */
		if (ref[0] == '#') {
		    return_buffer = the_bcache->buf;
		    current_path = the_bcache->path;
		    bcache_trace ("current_doc: %s ", the_bcache->filename);
		    bcache_trace ("path: %s\n", the_bcache->path);
		}
	    }

	    /* these strings are allocated.  */
	    if (pathname) checked_free (pathname);
	    if (filename) checked_free (filename);
	}
    }
    path_add_current (current_path);
    return return_buffer;
}

/* -------------------------------------------------------------------- *
*p  procedure-name:	bcache_insert
**
**  purpose:		inserts a buffer into the cache.
** -------------------------------------------------------------------- *
**  args:		current_path is a pointer to an allocated string.
**			bcache_free is responsible to free it.
**  side effect:	the current path is added to the help path.
** -------------------------------------------------------------------- */
void
bcache_insert (/* i  */ char* 		ref,
	       /* i  */ char*		current_path,
	       /* i  */ buffer_t* 	buf)
{
    execute ("bcache_insert");

    /* if the cache is full, we remove the last element.  */
    if (bcache_len >= BCACHE_SIZE) {
	
	/* we unline the last element.  */
	bcache_t* bc = the_bcache;      /* != NULL here. */
	bcache_t* pre = NULL;
	
	while (bc->next) {
	    pre = bc;
	    bc = bc->next;
	}
	
	if (pre) { 
	    pre->next = NULL;
	} else {
	    the_bcache = NULL;	       /* possible ? */
	}
	bcache_trace ("freed %s\n", bc->filename);
	bcache_free (bc);
	bcache_len--;
    } 

    /* begin  */ {

	/* local data  */
	bcache_t* 	new;
	checked_calloc (new, 1, bcache_t); /* scoppe -> bcache_free */

	if (ref) {
	    /* scope -> bcache_free  */
	    new->filename = get_file_and_strip_path (ref);
	} else  {
	    new->filename = NULL;
	}
	
	new->buf = buf;		/* is valid and not NULL */
	new->path = current_path; /* was allocated by read_html_file */
	new->next = the_bcache;
	the_bcache = new;
	bcache_len++;	
	bcache_trace ("added %s ", new->filename);
	bcache_trace ("path %s ", new->path);
	bcache_trace ("(cache = %d)\n", bcache_len);

	/* side effect: we add the current path to the help path.  */
	/* but we make a copy.  */
	path_add_current (current_path);
    }
}

/* -------------------------------------------------------------------- *
*p  procedure-name:	bcache_free
**
**  purpose:		free's the requested cache element.
** -------------------------------------------------------------------- */
static void
bcache_free (bcache_t* bc)
{
    execute ("bcache_free");
    
    if (bc) {
	if (bc->filename) checked_free (bc->filename);
	if (bc->path) checked_free (bc->path);
	if (bc->buf) bf_free (bc->buf);
	checked_free (bc);
    } else  {
	fprintf (stderr, "Error: called bcache_free with no arg\n");
    }
}

/* -------------------------------------------------------------------- *
*p  procedure-name:	bcache_flush
**
**  purpose:		releases all cached memory.
** -------------------------------------------------------------------- */
void
bcache_flush (void)
{
    bcache_t* bc = the_bcache;
    
    execute ("bcache_flush");

    while (bc) {

	bcache_t* bcn = bc->next;
	bcache_free (bc);
	bcache_len--;
	bc = bcn;
    }
    if (bcache_len != 0) {
	fprintf (stderr, "inconsistency in buffer cache (flush)\n");
	exit (EXIT_FAILURE);
    }
    the_bcache = NULL;
}

/* -------------------------------------------------------------------- *
*l  emacs:
**  local variables:
**  mode:		c
**  outline-regexp:	"\*[HGPLT]"
**  comment-column:	32
**  eval:		(outline-minor-mode t)
**  end:
** -------------------------------------------------------------------- */