| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: pool-file.c,v 1.1 2000/07/16 12:28:48 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 | /*----- Revision history --------------------------------------------------* |
| 31 | * |
| 32 | * $Log: pool-file.c,v $ |
| 33 | * Revision 1.1 2000/07/16 12:28:48 mdw |
| 34 | * Support for resource pools, based on the Apache model. |
| 35 | * |
| 36 | */ |
| 37 | |
| 38 | /*----- Header files ------------------------------------------------------*/ |
| 39 | |
| 40 | #include <stdio.h> |
| 41 | |
| 42 | #include "pool.h" |
| 43 | |
| 44 | /*----- Main code ---------------------------------------------------------*/ |
| 45 | |
| 46 | /* --- @pool_fopen@ --- * |
| 47 | * |
| 48 | * Arguments: @pool *p@ = pointer to a pool |
| 49 | * @const char *file@ = name of the file to open |
| 50 | * @const char *how@ = string specifying opening parameters |
| 51 | * |
| 52 | * Returns: A pointer to a pool resource containing an open file handle, |
| 53 | * or null if the file open filed. |
| 54 | * |
| 55 | * Use: Opens a file so that it will be freed again when a pool is |
| 56 | * destroyed. |
| 57 | */ |
| 58 | |
| 59 | static void pf_destroy(pool_resource *r) { pool_fclose((pool_file *)r); } |
| 60 | |
| 61 | pool_file *pool_fopen(pool *p, const char *file, const char *how) |
| 62 | { |
| 63 | FILE *fp; |
| 64 | pool_file *pf; |
| 65 | |
| 66 | if ((fp = fopen(file, how)) == 0) |
| 67 | return (0); |
| 68 | pf = pool_alloc(p, sizeof(pool_file)); |
| 69 | POOL_ADD(p, &pf->r, pf_destroy); |
| 70 | pf->fp = fp; |
| 71 | return (pf); |
| 72 | } |
| 73 | |
| 74 | /* --- @pool_fclose@ --- * |
| 75 | * |
| 76 | * Arguments: @pool_file *pf@ = pointer to a file resource |
| 77 | * |
| 78 | * Returns: The response from the @fclose@ function. |
| 79 | * |
| 80 | * Use: Closes a file. It is not an error to close a file multiple |
| 81 | * times. |
| 82 | */ |
| 83 | |
| 84 | int pool_fclose(pool_file *pf) |
| 85 | { |
| 86 | if (!pf->r.destroy) |
| 87 | return (0); |
| 88 | pf->r.destroy = 0; |
| 89 | return (fclose(pf->fp)); |
| 90 | } |
| 91 | |
| 92 | /*----- That's all, folks -------------------------------------------------*/ |