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

Linux Cross Reference
Tina6/tina-libs/tina/file/fileTs_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/fileTs_io.c,v $
 37  * Date    :  $Date: 2007/02/15 01:52:29 $
 38  * Version :  $Revision: 1.6 $
 39  * CVS Id  :  $Id: fileTs_io.c,v 1.6 2007/02/15 01:52:29 paul Exp $
 40  *
 41  * Author  :  Legacy TINA
 42  *
 43  * Notes :
 44  *
 45  *********
 46 */
 47 
 48 #include "fileTs_io.h"
 49 
 50 #if HAVE_CONFIG_H
 51   #include <config.h>
 52 #endif
 53 
 54 #include <stdio.h>
 55 #include <stdlib.h>
 56 #include <string.h>
 57 #include <unistd.h>
 58 #include <tina/sys/sysDef.h>
 59 #include <tina/file/file_TsDef.h>
 60 #include <tina/file/fileUtil_io.h>
 61 
 62 
 63 #define TS_MAGIC            0x80008000
 64 #define TS_MAGIC_NEED_SWAP  0x00800080
 65 
 66 /* sunpro: warning: initializer does not fit or is out of range: 0x80008000 */
 67 static unsigned int magic = TS_MAGIC;           /* static data! */
 68 
 69 void fwrite_ts(FILE * fp, List * lg, int c)
 70 {
 71         List *l;
 72         int rval;
 73 
 74         fwrite(&magic, sizeof(int), 1, fp);
 75         fwrite(&c, sizeof(int), 1, fp);
 76 
 77         for (l = lg; l; l = l->next)
 78                 rval = fwrite(&(l->type), sizeof(int), 1, fp);
 79 
 80         for (l = lg; l; l = l->next)
 81                 if (l->type > 0)
 82                         rval = fwrite(l->to, l->type, 1, fp);
 83 }
 84 
 85 
 86 void swrite_ts(int fd, List * lg, int c)
 87 {
 88         List *l;
 89         int rval;
 90 
 91         write(fd, &magic, sizeof(int));
 92         write(fd, &c, sizeof(int));
 93 
 94         for (l = lg; l; l = l->next)
 95         {
 96                 rval = write(fd, &(l->type), sizeof(int));
 97 #ifdef DEBUG
 98                 printf("sent %d bytes\n", rval);
 99 #endif
100         }
101 
102         for (l = lg; l; l = l->next)
103                 if (l->type > 0)
104                 {
105                         rval = write(fd, l->to, l->type);
106 #ifdef DEBUG
107                         printf("sent %d bytes\n", rval);
108 #endif
109                 }
110 }
111 
112 
113 List *fread_ts(FILE * fp, int *c)
114 {
115         List *l = NULL;
116         int *s, count, magic, rval;
117         void **d;
118 
119         rval = fread(&magic, sizeof(int), 1, fp);
120         set_swapping_ts((magic == TS_MAGIC_NEED_SWAP));
121         rval = fread(c, sizeof(int), 1, fp);
122 
123         word_swap((char *) c);
124         s = (int *) ralloc(sizeof(int) * (*c));
125         d = (void **) ralloc(sizeof(void *) * (*c));
126 
127         for (count = 0; count < *c; count++)
128         {
129                 rval = fread(s + count, sizeof(int), 1, fp);
130 
131                 word_swap((char *) (s + count));
132                 d[count] = s[count] > 0 ? (void *) ralloc(s[count]) : NULL;
133         }
134         for (count = *c - 1; count >= 0; count--)
135                 if (!d[count])
136                         d[count] = d[count - s[count]];
137         for (count = 0; count < *c; count++)
138         {
139                 l = ref_addtostart(l, d[count], s[count]);
140                 if (s[count] > 0)
141                 {
142                         rval = fread(l->to, l->type, 1, fp);
143                         if (rval != 1)
144                                 fprintf(stderr, "fread_ts: Error reading data item %i\n", count);
145                 }
146         }
147         rfree(s);
148         rfree(d);
149         return l;
150 }
151 
152 
153 static int              sock_read(int fd, int *data, size_t size)
154 {
155         int     red = 0, count = 0; 
156         size_t  total = 0;
157         
158         while((total < size) && count < 50)
159         {
160                 red = read(fd, data, (size_t)(size-total));     
161                 total += red;
162                 data += red;
163         }
164 
165         return(red);
166 }
167                                 
168 
169 
170 List *sread_ts(int fd, int *c)
171 {
172         List *l = NULL;
173         int *s, count, magic, rval;
174         void **d;
175 
176         rval = sock_read(fd, &magic, sizeof(int));
177 #ifdef DEBUG
178         printf("got %d bytes\n", rval);
179 #endif
180         set_swapping_ts((magic == TS_MAGIC_NEED_SWAP));
181         rval = sock_read(fd, c, sizeof(int));
182 #ifdef DEBUG
183         printf("got %d bytes\n", rval);
184 #endif
185 
186         word_swap((char *) c);
187         s = (int *) ralloc(sizeof(int) * (*c));
188         d = (void **) ralloc(sizeof(void *) * (*c));
189 
190         for (count = 0; count < *c; count++)
191         {
192                 rval = sock_read(fd, s + count, sizeof(int));
193 #ifdef DEBUG
194                 printf("got %d bytes\n", rval);
195 #endif
196 
197                 word_swap((char *) (s + count));
198                 d[count] = s[count] > 0 ? (void *) ralloc(s[count]) : NULL;
199         }
200         for (count = *c - 1; count >= 0; count--)
201                 if (!d[count])
202                         d[count] = d[count - s[count]];
203         for (count = 0; count < *c; count++)
204         {
205                 l = ref_addtostart(l, d[count], s[count]);
206                 if (s[count] > 0)
207                 {
208                         rval = sock_read(fd, l->to, l->type);
209 #ifdef DEBUG
210                         printf("got %d bytes\n", rval);
211 #endif
212                 }
213         }
214         rfree(s);
215         rfree(d);
216         return l;
217 }
218 
219 
220 void buffwrite_ts(char *mem, List * lg, int c)
221 {
222         List *l;
223         Varptr m;
224 
225         m.char_v = mem;
226         *(m.int_v++) = magic;
227         *(m.int_v++) = c;
228         for (l = lg; l; l = l->next)
229                 *(m.int_v++) = l->type;
230         for (l = lg; l; l = l->next)
231                 if (l->type > 0)
232                         memcpy(m.char_v, l->to, l->type), m.char_v += l->type;
233 }
234 
235 
236 List *buffread_ts(char *mem, int *c)
237 {
238         List *l = NULL;
239         Varptr m;
240         int *s=NULL, count, magic=0;
241         void **d=NULL;
242 
243         m.char_v = mem;
244         set_swapping_ts((magic == TS_MAGIC_NEED_SWAP)); 
245         word_swap(m.char_v);
246         *c = *m.int_v++;
247         s = (int *) ralloc(sizeof(int) * (*c));
248         d = (void **) ralloc(sizeof(void *) * (*c));
249 
250         for (count = 0; count < *c; count++)    
251         {
252                 word_swap(m.char_v);
253                 s[count] = *m.int_v++;
254                 d[count] = s[count] > 0 ? (int *)ralloc(s[count]) : NULL;
255         }
256         for (count = *c - 1; count >= 0; count--)       
257                 if (!d[count])
258                         d[count] = d[count - s[count]];
259         for (count = 0; count < *c; count++)    
260         {
261                 l = ref_addtostart(l, d[count], s[count]);
262                 if (s[count] > 0)
263                         memcpy(l->to, m.char_v, l->type), m.char_v += l->type;
264         }
265         rfree(s);
266         rfree(d);
267         return l;
268 }
269 

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