~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina6/tina-libs/tina/file/fileUtil_io.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /**********
  2  * 
  3  * Copyright (c) 2003, Division of Imaging Science and Biomedical Engineering,
  4  * University of Manchester, UK.  All rights reserved.
  5  * 
  6  * Redistribution and use in source and binary forms, with or without modification, 
  7  * are permitted provided that the following conditions are met:
  8  * 
  9  *   . Redistributions of source code must retain the above copyright notice, 
 10  *     this list of conditions and the following disclaimer.
 11  *    
 12  *   . Redistributions in binary form must reproduce the above copyright notice,
 13  *     this list of conditions and the following disclaimer in the documentation 
 14  *     and/or other materials provided with the distribution.
 15  * 
 16  *   . Neither the name of the University of Manchester nor the names of its
 17  *     contributors may be used to endorse or promote products derived from this 
 18  *     software without specific prior written permission.
 19  * 
 20  * 
 21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  * POSSIBILITY OF SUCH DAMAGE.
 32  *
 33  **********
 34  * 
 35  * Program :    TINA
 36  * File    :  $Source: /home/tina/cvs/tina-libs/tina/file/fileUtil_io.c,v $
 37  * Date    :  $Date: 2003/09/22 16:09:02 $
 38  * Version :  $Revision: 1.2 $
 39  * CVS Id  :  $Id: fileUtil_io.c,v 1.2 2003/09/22 16:09:02 tony Exp $
 40  *
 41  * Author  :  Legacy TINA
 42  *
 43  * Notes   :  Generic functions to handle files.
 44  * 
 45  *
 46  *********
 47 */
 48 
 49 #include "fileUtil_io.h"
 50 
 51 #if HAVE_CONFIG_H
 52   #include <config.h>
 53 #endif
 54 
 55 #include <stdio.h>
 56 #include <string.h>
 57 #include <tina/file/file_AiffDef.h>
 58 #include <tina/sys/sysDef.h>
 59 #include <tina/sys/sysPro.h>
 60 #include <tina/image/imgPro.h>
 61 #include <tina/image/imgDef.h>
 62 
 63 
 64 /* Close file. On failure, give error message and return false. */
 65 Bool    fclose_2(FILE * stream, const char *pathname)
 66 {
 67     Bool    closed_ok = true;
 68 
 69     if (fclose(stream) == EOF)
 70     {
 71         errorf(non_fatal, "Can't close file: %s\n", pathname);
 72         closed_ok = false;
 73     }
 74     return closed_ok;
 75 }
 76 
 77 
 78 /* Read 'count' items of size 'size' from stream into contiguous buffer
 79  * 'data' (alloc'd by this fn). Return buffer. On failure, give error
 80  * message and return NULL. */
 81 void   *fread_2(const unsigned size, const unsigned count, FILE * stream,
 82                         const char *pathname)
 83 {
 84     void   *data = ralloc(size * count);
 85 
 86     if (count != fread(data, size, count, stream))
 87     {
 88         errorf(non_fatal, "Can't read data: %s\n", pathname);
 89         rfree(data);
 90         data = NULL;
 91     }
 92     return data;
 93 }
 94 
 95 /* Read stream into 'nfields' fields whose sizes and addresses are
 96  * given in field_info. Returns number of bytes read. On failure, give
 97  * error message and return 0. */
 98 unsigned fread_fields(Field_info * field_info, const unsigned nfields, FILE * stream, const char *pathname)
 99 {
100     unsigned nbytes = 0;
101     unsigned i;
102 
103     for (i = 0; i < nfields; i++)
104     {
105         if (field_info[i].count !=
106             fread(field_info[i].address,
107                   field_info[i].size,
108                   field_info[i].count, stream))
109         {
110             errorf(non_fatal, "Can't read fields: %s\n", pathname);
111             nbytes = 0;
112             break;
113         } else
114         {
115             nbytes += field_info[i].size * field_info[i].count;
116         }
117     }
118     return nbytes;
119 }
120 
121 /* Read image data by rows (possibly non-contiguous offset rows) from
122  * stream. Imregion & vtype describe data 'data'. Returns true (read
123  * ok)/false. On failure give error message.  NB Imrect data space must
124  * have been alloc'd. */
125 Bool    fread_imrect_data(const Imrect * imrect, FILE * stream, const char *pathname)
126 {
127     Bool    read_ok = true;
128 
129     if (imrect)
130     {
131         Imregion *imregion = imrect->region;
132         int     lx = imregion->lx;
133         int     ux = imregion->ux;
134         int     ly = imregion->ly;
135         int     uy = imregion->uy;
136         int     y;
137         unsigned ncols = ux - lx;
138         unsigned size = var_size(imrect->vtype);
139 
140         for (y = ly; y < uy; ++y)
141         {
142             void   *row = NULL;
143 
144             IM_PIX_GETP(imrect, y, lx, row);
145             if (fread(row, size, ncols, stream) != ncols)
146             {
147                 errorf(non_fatal, "fwrite_imrect_data: Can't read data: %s\n",
148                        pathname);
149                 read_ok = false;
150                 break;
151             }
152         }
153     } else
154     {
155         errorf(non_fatal, "No image to write to: %s\n", pathname);
156         read_ok = false;
157     }
158     return read_ok;
159 }
160 
161 /* Read padding (after data in header, to fill to standard size) and
162  * discard. On failure, give error message and return false */
163 Bool    fread_padding(const unsigned size, FILE * stream, const char *pathname)
164 {
165     Bool    read_ok = true;
166     void   *data = ralloc(size);
167 
168     if (1 != fread(data, size, 1, stream))
169     {
170         errorf(non_fatal, "Can't read padding: %s\n", pathname);
171         read_ok = false;
172     }
173     rfree(data);
174     return read_ok;
175 }
176 
177 /* Write 'count' items of 'size' from  contiguous buffer 'data' to
178  * stream. On failure, give error message and return false. */
179 Bool    fwrite_2(const void *data, const unsigned size, const unsigned count,
180                          FILE * stream, const char *pathname)
181 {
182     Bool    write_ok = true;
183 
184     if (count != fwrite(data, size, count, stream))
185     {
186         errorf(non_fatal, "Can't write data: %s\n", pathname);
187         write_ok = false;
188     }
189     return write_ok;
190 }
191 
192 /* Write 'nfields' fields whose sizes and addresses are given in
193  * field_info. Returns number of bytes read. On failure, give error
194  * message and return 0. */
195 unsigned fwrite_fields(Field_info * field_info, const unsigned nfields, FILE * stream, const char *pathname)
196 {
197     unsigned          i;
198     unsigned nbytes = 0;
199 
200     for (i = 0; i < nfields; i++)
201     {
202         if (field_info[i].count !=
203             fwrite(field_info[i].address,
204                    field_info[i].size,
205                    field_info[i].count, stream))
206         {
207             errorf(non_fatal, "Can't write fields: %s\n", pathname);
208             nbytes = 0;
209             break;
210         } else
211         {
212             nbytes += field_info[i].size * field_info[i].count;
213         }
214     }
215     return nbytes;
216 }
217 
218 /* Write image data by rows (possibly non-contiguous and offset) to
219  * stream. Imregion & vtype describe data 'data'. On failure, give
220  * error message and return false. */
221 Bool    fwrite_imrect_data(const Imrect * imrect, FILE * stream, const char *pathname)
222 {
223     Bool    write_ok = true;
224 
225     if (imrect)
226     {
227         Imregion *imregion = imrect->region;
228         int     lx = imregion->lx;
229         int     ux = imregion->ux;
230         int     ly = imregion->ly;
231         int     uy = imregion->uy;
232         int     y;
233         unsigned size = var_size(imrect->vtype);
234         unsigned ncols = ux - lx;
235 
236         for (y = ly; y < uy; y++)
237         {
238             void   *row = NULL;
239 
240             IM_PIX_GETP(imrect, y, lx, row);
241             if (fwrite((char *) row, size, ncols, stream) != ncols)
242             {
243                 errorf(non_fatal, "fwrite_imrect_data: Can't write data: %s\n",
244                        pathname);
245                 write_ok = false;
246                 break;
247             }
248         }
249     } else
250     {
251         errorf(non_fatal, "No image to write to: %s\n", pathname);
252         write_ok = false;
253     }
254     return write_ok;
255 }
256 
257 /* Write padding (after data in header, to fill to standard size) On
258  * failure, give error message and return false */
259 Bool    fwrite_padding(const unsigned size, FILE * stream, const char
260                               *pathname)
261 {
262     Bool    write_ok = true;
263     void   *data = ralloc(size);
264 
265     if (1 != fwrite(data, size, 1, stream))
266     {
267         errorf(non_fatal, "Can't write padding: %s\n", pathname);
268         write_ok = false;
269     }
270     rfree(data);
271     return write_ok;
272 }
273 
274 /* Open file (with error message on failure). Returns stream (NULL on
275  * failure) */
276 FILE   *fopen_2(const char *pathname, const char *mode)
277 {
278     FILE   *stream = fopen(pathname, mode);
279 
280     if (!stream)
281     {
282         errorf(non_fatal, "Can't open file: %s\n", pathname);
283     }
284     return stream;
285 }
286 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.