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

Linux Cross Reference
Tina4/src/file/util/file_io.c

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

  1 /**@(#)Generic functions to handle files.
  2 */
  3 
  4 #include <stdio.h>
  5 #include <string.h>
  6 #include <tina/file.h>
  7 #include <tina/sys.h>
  8 #include <tina/sys_types.h>
  9 #include <tina/sysfuncs.h>
 10 
 11 /* Close file. On failure, give error message and return false. */
 12 Bool    fclose_2(FILE * stream, const char *pathname)
 13 {
 14     Bool    closed_ok = true;
 15 
 16     if (fclose(stream) == EOF)
 17     {
 18         errorf(non_fatal, "Can't close file: %s\n", pathname);
 19         closed_ok = false;
 20     }
 21     return closed_ok;
 22 }
 23 
 24 
 25 /* Read 'count' items of size 'size' from stream into contiguous buffer
 26  * 'data' (alloc'd by this fn). Return buffer. On failure, give error
 27  * message and return NULL. */
 28 void   *fread_2(const unsigned size, const unsigned count, FILE * stream,
 29                         const char *pathname)
 30 {
 31     void   *data = ralloc(size * count);
 32 
 33     if (count != fread(data, size, count, stream))
 34     {
 35         errorf(non_fatal, "Can't read data: %s\n", pathname);
 36         rfree(data);
 37         data = NULL;
 38     }
 39     return data;
 40 }
 41 
 42 /* Read stream into 'nfields' fields whose sizes and addresses are
 43  * given in field_info. Returns number of bytes read. On failure, give
 44  * error message and return 0. */
 45 unsigned fread_fields(Field_info * field_info, const unsigned nfields, FILE * stream, const char *pathname)
 46 {
 47     unsigned nbytes = 0;
 48     unsigned i;
 49 
 50     for (i = 0; i < nfields; i++)
 51     {
 52         if (field_info[i].count !=
 53             fread(field_info[i].address,
 54                   field_info[i].size,
 55                   field_info[i].count, stream))
 56         {
 57             errorf(non_fatal, "Can't read fields: %s\n", pathname);
 58             nbytes = 0;
 59             break;
 60         } else
 61         {
 62             nbytes += field_info[i].size * field_info[i].count;
 63         }
 64     }
 65     return nbytes;
 66 }
 67 
 68 /* Read image data by rows (possibly non-contiguous offset rows) from
 69  * stream. Imregion & vtype describe data 'data'. Returns true (read
 70  * ok)/false. On failure give error message.  NB Imrect data space must
 71  * have been alloc'd. */
 72 Bool    fread_imrect_data(const Imrect * imrect, FILE * stream, const char *pathname)
 73 {
 74     Bool    read_ok = true;
 75 
 76     if (imrect)
 77     {
 78         Imregion *imregion = imrect->region;
 79         int     lx = imregion->lx;
 80         int     ux = imregion->ux;
 81         int     ly = imregion->ly;
 82         int     uy = imregion->uy;
 83         int     y;
 84         unsigned ncols = ux - lx;
 85         unsigned size = var_size(imrect->vtype);
 86 
 87         for (y = ly; y < uy; ++y)
 88         {
 89             void   *row = NULL;
 90 
 91             IM_PIX_GETP(imrect, y, lx, row);
 92             if (fread(row, size, ncols, stream) != ncols)
 93             {
 94                 errorf(non_fatal, "fwrite_imrect_data: Can't read data: %s\n",
 95                        pathname);
 96                 read_ok = false;
 97                 break;
 98             }
 99         }
100     } else
101     {
102         errorf(non_fatal, "No image to write to: %s\n", pathname);
103         read_ok = false;
104     }
105     return read_ok;
106 }
107 
108 /* Read padding (after data in header, to fill to standard size) and
109  * discard. On failure, give error message and return false */
110 Bool    fread_padding(const unsigned size, FILE * stream, const char *pathname)
111 {
112     Bool    read_ok = true;
113     void   *data = ralloc(size);
114 
115     if (1 != fread(data, size, 1, stream))
116     {
117         errorf(non_fatal, "Can't read padding: %s\n", pathname);
118         read_ok = false;
119     }
120     rfree(data);
121     return read_ok;
122 }
123 
124 /* Write 'count' items of 'size' from  contiguous buffer 'data' to
125  * stream. On failure, give error message and return false. */
126 Bool    fwrite_2(const void *data, const unsigned size, const unsigned count,
127                          FILE * stream, const char *pathname)
128 {
129     Bool    write_ok = true;
130 
131     if (count != fwrite(data, size, count, stream))
132     {
133         errorf(non_fatal, "Can't write data: %s\n", pathname);
134         write_ok = false;
135     }
136     return write_ok;
137 }
138 
139 /* Write 'nfields' fields whose sizes and addresses are given in
140  * field_info. Returns number of bytes read. On failure, give error
141  * message and return 0. */
142 unsigned fwrite_fields(Field_info * field_info, const unsigned nfields, FILE * stream, const char *pathname)
143 {
144     unsigned          i;
145     unsigned nbytes = 0;
146 
147     for (i = 0; i < nfields; i++)
148     {
149         if (field_info[i].count !=
150             fwrite(field_info[i].address,
151                    field_info[i].size,
152                    field_info[i].count, stream))
153         {
154             errorf(non_fatal, "Can't write fields: %s\n", pathname);
155             nbytes = 0;
156             break;
157         } else
158         {
159             nbytes += field_info[i].size * field_info[i].count;
160         }
161     }
162     return nbytes;
163 }
164 
165 /* Write image data by rows (possibly non-contiguous and offset) to
166  * stream. Imregion & vtype describe data 'data'. On failure, give
167  * error message and return false. */
168 Bool    fwrite_imrect_data(const Imrect * imrect, FILE * stream, const char *pathname)
169 {
170     Bool    write_ok = true;
171 
172     if (imrect)
173     {
174         Imregion *imregion = imrect->region;
175         int     lx = imregion->lx;
176         int     ux = imregion->ux;
177         int     ly = imregion->ly;
178         int     uy = imregion->uy;
179         int     y;
180         unsigned size = var_size(imrect->vtype);
181         unsigned ncols = ux - lx;
182 
183         for (y = ly; y < uy; y++)
184         {
185             void   *row = NULL;
186 
187             IM_PIX_GETP(imrect, y, lx, row);
188             if (fwrite((char *) row, size, ncols, stream) != ncols)
189             {
190                 errorf(non_fatal, "fwrite_imrect_data: Can't write data: %s\n",
191                        pathname);
192                 write_ok = false;
193                 break;
194             }
195         }
196     } else
197     {
198         errorf(non_fatal, "No image to write to: %s\n", pathname);
199         write_ok = false;
200     }
201     return write_ok;
202 }
203 
204 /* Write padding (after data in header, to fill to standard size) On
205  * failure, give error message and return false */
206 Bool    fwrite_padding(const unsigned size, FILE * stream, const char
207                               *pathname)
208 {
209     Bool    write_ok = true;
210     void   *data = ralloc(size);
211 
212     if (1 != fwrite(data, size, 1, stream))
213     {
214         errorf(non_fatal, "Can't write padding: %s\n", pathname);
215         write_ok = false;
216     }
217     rfree(data);
218     return write_ok;
219 }
220 
221 /* Open file (with error message on failure). Returns stream (NULL on
222  * failure) */
223 FILE   *fopen_2(const char *pathname, const char *mode)
224 {
225     FILE   *stream = fopen(pathname, mode);
226 
227     if (!stream)
228     {
229         errorf(non_fatal, "Can't open file: %s\n", pathname);
230     }
231     return stream;
232 }
233 

~ [ 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.