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