/Users/maurits/Documents/studie/afstuderen/biosphere/common/io.c

Go to the documentation of this file.
00001 /*
00002  * Author: MA Hartman
00003  * Date: mar 31, 2007
00004  * 
00005  * Function:
00006  * General I/O functions for use with memory pools.
00007  * 
00008  * License information:
00009  * 
00010  * Copyright (c) 2006 Maurits Hartman
00011  *
00012  * Permission is hereby granted, free of charge, to any person
00013  * obtaining a copy of this software and associated documentation
00014  * files (the "Software"), to deal in the Software without
00015  * restriction, including without limitation the rights to use,
00016  * copy, modify, merge, publish, distribute, sublicense, and/or sell
00017  * copies of the Software, and to permit persons to whom the
00018  * Software is furnished to do so, subject to the following
00019  * conditions:
00020  * 
00021  * The above copyright notice and this permission notice shall be
00022  * included in all copies or substantial portions of the Software.
00023  * 
00024  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
00026  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
00028  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
00029  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00030  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00031  * OTHER DEALINGS IN THE SOFTWARE.
00032  * 
00033  */
00034  
00035  #include "io.h"
00036  
00037  #include <apr_file_io.h>
00038  #include <apr_strings.h>
00039  #include <string.h>
00040  
00041  
00042  bs_status read_file_into_buf(const char *fname, char **buf,
00043                 bs_uint64 *len, apr_pool_t *mp)
00044 {
00045         apr_status_t rv;
00046         apr_file_t *fp;
00047         char *collect = NULL, tmp[1024];
00048         apr_size_t length;
00049         
00050         /* Try to open the file for reading: */
00051         rv = apr_file_open(&fp, fname, APR_FOPEN_READ, APR_OS_DEFAULT, mp);
00052         if (rv != APR_SUCCESS) return BS_ERROR;
00053         
00054         /* Read until the end */
00055         *len = 0;
00056         memset(tmp, '\0', sizeof(tmp));
00057         while (1) {
00058                 length = sizeof(tmp);
00059                 rv = apr_file_read(fp, tmp, &length);
00060                 if (rv != APR_SUCCESS || length == 0) break;
00061                 
00062                 if (collect == NULL) collect = apr_pstrdup(mp, tmp); /* First iteration */
00063                 else collect = apr_pstrcat(mp, collect, tmp, NULL);
00064                 *len += length;
00065         }
00066         
00067         *buf = collect;
00068         apr_file_close(fp);
00069         return BS_OK;
00070 }
00071 
00072 
00073 bs_status write_buf_into_file(const char *fname, const char *buf,
00074                 apr_pool_t *mp)
00075 {
00076         apr_status_t rv;
00077         apr_file_t *fp;
00078         apr_size_t length;
00079         
00080         /* Try to open the file for writing: */
00081         rv = apr_file_open(&fp, fname,
00082                         APR_FOPEN_READ|APR_FOPEN_WRITE, APR_OS_DEFAULT, mp);
00083         if (rv != APR_SUCCESS) return BS_ERROR;
00084         
00085         /* Write buffer to file: */
00086         length = strlen(buf);
00087         rv = apr_file_write(fp, buf, &length);
00088         if (rv != APR_SUCCESS || length == 0) return BS_ERROR;
00089         
00090         /* Close file and return: */
00091         apr_file_close(fp);
00092         return BS_OK;
00093 }

Generated on Tue Jul 17 09:50:52 2007 for Bio-SPHERE by  doxygen 1.5.1