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

Linux Cross Reference
Tina4/src/file/matrix/matrix_io.c

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

  1 /* matrix_io.c
  2 
  3    Functions to read/write to/from files/matrices
  4 
  5    Author: A.Lacey
  6    Date  : 2-4-93;  version 1.0
  7            6.12.95; version 2.0
  8    Update: S.Crossley
  9    Date  : 9-5-96; version 3.0
 10 */
 11 
 12 #include <stdio.h>
 13 #include <tina/sys.h>
 14 #include <tina/sysfuncs.h>
 15 #include <tina/math.h>
 16 #include <tina/mathfuncs.h>
 17 
 18 #ifndef MAT_HEAD_DEFINED
 19 #define MAT_HEAD_DEFINED
 20 
 21 typedef struct mat_head
 22 {
 23     Vartype vtype;
 24     Matrix_shape shape;
 25     int m, n;
 26 } Mat_hdr;  
 27 
 28 #endif /* MAT_HEAD_DEFINED */
 29 
 30 #define SFPLIMIT 50
 31 
 32 static int isave_matrix(Matrix *mp, FILE *fp)
 33 {
 34   int i, j, val;
 35   
 36   for (i=0; i<mp->m; i++)
 37     {
 38       for (j=0; j<mp->n; j++)
 39         {
 40           val = mp->el.int_v[i][j];
 41           if(!fprintf(fp, "%d\t", val))
 42             return (-1);
 43         }
 44       
 45       fprintf(fp, "\n");
 46     }
 47   
 48   return (0);    
 49 }
 50 
 51 static int fsave_matrix(Matrix *mp, FILE *fp)
 52 {
 53   int    i, j;
 54   float val;
 55   
 56   for (i=0; i<mp->m; i++)
 57     {
 58       for (j=0; j<mp->n; j++)
 59         {
 60           val = mp->el.float_v[i][j];
 61           if(!fprintf(fp, "%f\t", val))
 62             return (-1);
 63         }
 64       
 65       fprintf(fp, "\n");
 66     }
 67   
 68   return (0);    
 69 }
 70 
 71 static int dsave_matrix(Matrix *mp, FILE *fp)
 72 {
 73   int i, j;
 74   double val;
 75   
 76   for (i=0; i<mp->m; i++)
 77     {
 78       for (j=0; j<mp->n; j++)
 79         {
 80           val = mp->el.double_v[i][j];
 81           if (!fprintf(fp, "%f\t", val))
 82             return (-1);
 83         }
 84       
 85       fprintf(fp, "\n");
 86     }
 87   
 88   return (0);    
 89 }
 90 
 91 static int iload_matrix(Matrix *mp, FILE *fp)
 92 {
 93   int i, j;
 94   int val;
 95   
 96   for (i=0; i<mp->m; i++)
 97     for (j=0; j<mp->n; j++)
 98       {
 99         if(!fscanf(fp, " %d ", &val))
100           return (-1);
101         mp->el.int_v[i][j] = val;
102       }
103   
104   return (0);   
105 }
106 
107 static int fload_matrix(Matrix *mp, FILE *fp)
108 {
109   int i, j;
110   float val;
111   
112   for (i = 0; i < mp->m; i++)
113     for (j = 0; j < mp->n; j++)
114       {
115         if(!fscanf(fp, " %f ", &val))
116           return (-1);
117         mp->el.float_v[i][j] = val;
118       }
119   
120   return (0);   
121 }
122 
123 static int dload_matrix(Matrix *mp, FILE *fp)
124 {
125   int i, j;
126   double val;
127   
128   for (i=0; i<mp->m; i++)
129     for (j=0; j<mp->n; j++)
130       {
131         if(!fscanf(fp, " %lf ", &val))
132           return(-1);
133         mp->el.double_v[i][j] = val;
134       }
135   
136   return (0);   
137 }
138 
139 static Mat_hdr *mat_hdr_alloc(Mat_hdr *data)
140 {
141   unsigned int size;
142 
143   if (data != NULL)
144     return (NULL);
145 
146   size = sizeof(Mat_hdr);
147   if ((data = ralloc(size)) == NULL)
148     {
149       error(" memory allocation failure ", non_fatal);
150       return(NULL);
151     }
152 
153   return(data);
154 }
155 
156 static int mat_hdr_free(Mat_hdr *data)
157 {
158   if (data == NULL)
159     return (1);
160 
161   rfree(data);
162   return (0);
163 }
164 
165 static int sfpto_eol(FILE *fp)
166 {
167   char c = 0;
168   int count = 0;
169 
170   while (c!='\n' && count<SFPLIMIT)
171     {
172       c = getc(fp);
173       count++;
174     }
175 
176   if (count < SFPLIMIT)
177     return (0);
178   
179   return (1);   
180 }
181 
182 static Mat_hdr *matrix_read_hdr(char *fname)
183 {
184   Mat_hdr *mhdr = NULL;
185   FILE *fptr;
186   int status;
187 
188 
189   if (fname==NULL)
190     return (NULL);
191   
192   mhdr = mat_hdr_alloc(mhdr);
193   
194   if ((fptr=fopen(fname, "r")) == NULL)
195     {
196       error("matrix_read : cannot open file", warning);
197       mat_hdr_free(mhdr);
198       return (NULL);
199     }
200   
201   status = fscanf(fptr, " %d ; %d ; %d ; %d", &(mhdr->m), 
202                   &(mhdr->n), &(mhdr->shape), &(mhdr->vtype));
203   
204   fclose(fptr);
205   
206   if (status!=4)
207     {
208       error("matrix_read : header corruted", warning);
209       mat_hdr_free(mhdr);
210       return (NULL);
211     }
212   
213   return (mhdr);
214 }
215 
216 static int matrix_write_hdr(Mat_hdr *hdr, char *fname)
217 {
218   FILE *fptr;
219   char message[256];
220   
221   if (fname==NULL)
222     return (1);
223   
224   if ((fptr=fopen(fname, "w")) == NULL)
225     {
226       sprintf(message, "Couldn't create file %s", fname);
227       error(message, warning);
228       return (2);
229     }
230   
231   fprintf(fptr, "%d ; %d ; %d ; %d ;\n", hdr->m, hdr->n,
232           hdr->shape, hdr->vtype);
233   
234   fclose(fptr);
235   return (-1); 
236         
237 }
238 
239 Matrix *matrix_read_file(char *fname)
240 {
241   Mat_hdr *mh;
242   Matrix *mptr;
243   FILE *fptr;
244   int status = 0;
245   
246   if (fname==NULL)
247     return (NULL);
248   
249   if ((mh = matrix_read_hdr(fname)) == NULL)
250     return (NULL);
251   
252   mptr = matrix_alloc( mh->m, mh->n, mh->shape, mh->vtype);
253   mat_hdr_free(mh);
254   
255   if ((fptr = fopen(fname, "r")) == NULL)
256     {
257       error("matrix_read : cannot open file", warning);
258       return (NULL);
259     }
260   
261   if (!sfpto_eol(fptr))
262     {
263       switch (mptr->vtype)
264         {
265         case int_v:
266           status = iload_matrix(mptr, fptr);
267           break;
268         case float_v:
269           status = fload_matrix(mptr, fptr);
270           break;
271         case double_v:
272           status = dload_matrix(mptr, fptr);
273           break;
274         }
275     }
276   fclose(fptr);
277   
278   if (status == -1)
279     {
280       matrix_free(mptr);
281       return (NULL);
282     }
283 
284   return (mptr);
285 }
286 
287 Matrix *matrix_write_file(Matrix *mptr, char *fname)
288 {
289   Mat_hdr   *mh = NULL; 
290   FILE      *fptr;
291   int        status = 0;
292 
293   if (mptr == NULL || fname == NULL)
294     return (NULL);
295   
296   mh = mat_hdr_alloc(mh);
297   
298   mh->m = mptr->m;
299   mh->n = mptr->n;
300   mh->shape = mptr->shape;
301   mh->vtype = mptr->vtype;
302   
303   if (matrix_write_hdr(mh, fname) > -1)
304     return (NULL);
305   
306   mat_hdr_free(mh);
307   
308   if ((fptr = fopen(fname, "a")) == NULL)
309     {
310       error("matrix_write : cannot open file", warning);
311       return (NULL);
312     }
313   
314   switch (mptr->vtype)
315     {
316     case int_v:
317       status = isave_matrix(mptr, fptr);
318       break;
319     case float_v:
320       status = fsave_matrix(mptr, fptr);
321       break;
322     case double_v:
323       status = dsave_matrix(mptr, fptr);
324       break;
325     }
326   
327   fclose(fptr);
328   
329   if (status == -1)
330     return (NULL);
331   
332   return (mptr);        
333 }
334 
335 
336 
337 

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