chiark / gitweb /
Infrastructure: Split the files into subdirectories.
[mLib] / mem / pool-file.c
CommitLineData
d94be366 1/* -*-c-*-
2 *
d94be366 3 * File handles in resource pools
4 *
5 * (c) 2000 Straylight/Edgeware
6 */
7
d4efbcd9 8/*----- Licensing notice --------------------------------------------------*
d94be366 9 *
10 * This file is part of the mLib utilities library.
11 *
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
d4efbcd9 16 *
d94be366 17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
d4efbcd9 21 *
d94be366 22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
d94be366 28/*----- Header files ------------------------------------------------------*/
29
30#include <stdio.h>
31
32#include "pool.h"
33
34/*----- Main code ---------------------------------------------------------*/
35
36/* --- @pool_fopen@ --- *
37 *
38 * Arguments: @pool *p@ = pointer to a pool
39 * @const char *file@ = name of the file to open
40 * @const char *how@ = string specifying opening parameters
41 *
42 * Returns: A pointer to a pool resource containing an open file handle,
43 * or null if the file open filed.
44 *
45 * Use: Opens a file so that it will be freed again when a pool is
46 * destroyed.
47 */
48
49static void pf_destroy(pool_resource *r) { pool_fclose((pool_file *)r); }
50
51pool_file *pool_fopen(pool *p, const char *file, const char *how)
52{
53 FILE *fp;
54 pool_file *pf;
55
56 if ((fp = fopen(file, how)) == 0)
57 return (0);
58 pf = pool_alloc(p, sizeof(pool_file));
59 POOL_ADD(p, &pf->r, pf_destroy);
60 pf->fp = fp;
61 return (pf);
62}
63
64/* --- @pool_fclose@ --- *
65 *
66 * Arguments: @pool_file *pf@ = pointer to a file resource
67 *
68 * Returns: The response from the @fclose@ function.
69 *
70 * Use: Closes a file. It is not an error to close a file multiple
71 * times.
72 */
73
74int pool_fclose(pool_file *pf)
75{
76 if (!pf->r.destroy)
77 return (0);
78 pf->r.destroy = 0;
79 return (fclose(pf->fp));
80}
81
82/*----- That's all, folks -------------------------------------------------*/