File: PapyMemorySystemMac3.c

package info (click to toggle)
papyrus 3.7.1%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,256 kB
  • sloc: ansic: 49,489; cpp: 21,301; asm: 284; makefile: 222; sh: 152
file content (400 lines) | stat: -rwxr-xr-x 14,038 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/********************************************************************************/
/*                                                                              */
/* Papyrus 3 library.                                                           */
/* This library constitutes a DICOM file system which helps reading and writing */
/* DICOM files and DICOMDIR files.                                              */
/*                                                                              */
/* Copyright (C) 2004 - Service of  Medical Informatics -                       */
/* University Hospitals of Geneva (HUG), Geneva, Switzerland                    */
/*                                                                              */
/* This library is a free software; you can redistribute it and/or modify it    */
/* under the terms of the GNU Lesser General Public License as published by the */
/*  Free Software Foundation; either version 2.1 of the License, or             */
/* (at your option) any later version.                                          */
/*                                                                              */
/* This library is distributed in the hope that it will be useful,              */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of               */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                         */
/* See the GNU Lesser General Public License for more details.                  */
/*                                                                              */
/* You should have received a copy of the GNU Lesser General Public License     */
/* along with this library; if not, write to                                    */
/* the Free Software Foundation, Inc.,                                          */
/* 59 Temple Place, Suite 330,                                                  */
/* Boston, MA 02111-1307 USA                                                    */
/*                                                                              */
/* You can contact us for more information at osiris@sim.hcuge.ch               */
/* or by writing to Papyrus,                                                    */
/* Unite d'Imagerie Numerique / Service d'Informatique Medicale / HUG,          */
/* 24, Micheli-du-Crest street, 1211 Geneva 14, Switzerland.                    */
/*                                                                              */
/* The University Hopitals of Geneva, hereby disclaims all copyright interest   */
/* in the library `Papyrus' (a library for reading and writing DICOM files).    */
/*                                                                              */
/* Geneva, april 2004                                                           */
/* Antoine Geissbuhler, head of the Service of Medical Informatics,             */
/* University Hospitals of Geneva, Switzerland                                  */
/*                                                                              */
/********************************************************************************/

/********************************************************************************/
/*                                                                              */
/*	Project  : P A P Y R U S  Toolkit                                           */
/*	File     : PapyMemorySystemMac3.c                                           */
/*	Function : contains machine specific calls to the different file systems    */
/*	Authors  : Antoine ROSSET                                                   */
/*                                                                              */
/*	History  : 02.1998	version 3.1                                             */
/*             04.2001	version 3.7                                             */
/*             09.2001      version 3.7  on CVS                                 */
/*             10.2001      version 3.71 MAJ Dicom par CHG                      */
/*                                                                              */
/********************************************************************************/


/* ------------------------- includes ------------------------------------------*/

#include <Files.h>
#include <string.h>
#include <Script.h>

#ifndef PapyFileSystem3H
#include "PapyFileSystem3.h"
#endif

#ifndef PapyMemorySystemMac3H
#include "PapyMemorySystemMac3.h"
#endif

typedef struct SPapyMemStruct
{
  Ptr 		mFileP;
  long		mFilePos, mFileSize;
  PAPY_FILE	mvRefNum;
  HIOParam  	mPtrParm;
} SPapyMemStruct;

static SPapyMemStruct	thePapMemStruct [200];	/* Maximum of 200 files open */
static short		theListSize = 0;



/********************************************************************************/
/*										*/
/*	Papy3LoadFileMem 							*/
/*	return : standard error message						*/
/*										*/
/********************************************************************************/

PapyShort Papy3LoadFileMem (PAPY_FILE inVRefNum)
{
  OSErr		theErr;
  
  
  theErr = (PapyShort) GetEOF (inVRefNum, &thePapMemStruct [theListSize].mFileSize);
  
  /* thePapMemStruct [theListSize].mFileP = NewPtr (thePapMemStruct [theListSize].mFileSize);*/
  thePapMemStruct [theListSize].mFileP = (char *) emalloc3 ((PapyULong) thePapMemStruct [theListSize].mFileSize);
  if (thePapMemStruct [theListSize].mFileP == 0L) return -1;
  
  thePapMemStruct [theListSize].mPtrParm.ioRefNum   = (short) inVRefNum;
  thePapMemStruct [theListSize].mPtrParm.ioBuffer   = (Ptr)   thePapMemStruct [theListSize].mFileP;
  thePapMemStruct [theListSize].mPtrParm.ioReqCount = (long)  thePapMemStruct [theListSize].mFileSize;
  thePapMemStruct [theListSize].mPtrParm.ioPosMode  = fsFromStart;
  
  theErr = (PapyShort) PBRead ((ParmBlkPtr) &thePapMemStruct [theListSize].mPtrParm, TRUE);	/* Read in ASYNC ! */
  
  thePapMemStruct [theListSize].mFilePos = 0L;
  
  thePapMemStruct [theListSize].mvRefNum = inVRefNum;
  
  theListSize++;

 return noErr;
 
} /* endof Papy3LoadFileMem */


/********************************************************************************/
/*										*/
/*	Papy3SetMemPtr	 							*/
/*	return : standard error message						*/
/*										*/
/********************************************************************************/

void Papy3SetMemPtr (PAPY_FILE inVRefNum, long inPos, long inSize, Ptr inPtr)
{
  short	i;
	
	
  for (i = 0; i < theListSize; i++)
  {
    if (thePapMemStruct [i].mvRefNum == inVRefNum)
    {
      if (inPtr == NULL)
      {
        thePapMemStruct [i].mFilePos  = 0L;
        thePapMemStruct [i].mFileSize = 0L;
        thePapMemStruct [i].mFileP    = 0L;
        thePapMemStruct [i].mvRefNum  = 0L;
      } /* if */
      else
      {
        thePapMemStruct [i].mFilePos  = inPos;
        thePapMemStruct [i].mFileSize = inSize;
        thePapMemStruct [i].mFileP    = inPtr;
      } /* else */
      
      return;
    } /* if ...inVRefNum */
  } /* for */
  
  DebugStr("\pNot found");
  
  return;
  
} /* endof Papy3SetMemPtr */


/********************************************************************************/
/*										*/
/*	Papy3GetMemPtr	 							*/
/*	return : standard error message						*/
/*										*/
/********************************************************************************/

Ptr Papy3GetMemPtr (PAPY_FILE inVRefNum, long *ioPosP, long *ioSizeP, long *ActCount)
{
  short	i;
  
  for (i = 0; i < theListSize; i++)
  {
    if (thePapMemStruct [i].mvRefNum == inVRefNum)
    {
      if (ioPosP) *ioPosP     = thePapMemStruct [i].mFilePos;
      if (ioSizeP) *ioSizeP   = thePapMemStruct [i].mFileSize;
      if (ActCount) *ActCount = thePapMemStruct [i].mPtrParm.ioActCount;
      return thePapMemStruct [i].mFileP;
    } /* if */
  } /* for */
  
  return 0L;
  
} /* endof Papy3GetMemPtr */


/********************************************************************************/
/*										*/
/*	Papy3FLoadMem 								*/
/*	return : standard error message						*/
/*										*/
/********************************************************************************/

PapyShort Papy3FLoadMem (PAPY_FILE inVRefNum)
{
  PapyShort	theErr;
   
  
  if (Papy3GetMemPtr (inVRefNum, 0L, 0L, 0L)) Debugger();
  
  theErr = Papy3LoadFileMem (inVRefNum);
  if (theErr) DebugStr ("\pPapy3LoadFileMem Err");
  
  return theErr;
  
} /* endof Papy3FLoadMem */


/********************************************************************************/
/*										*/
/*	Papy3FOpenMem : overwrites the standard open file function		*/
/*	return : standard error message						*/
/*										*/
/********************************************************************************/

PapyShort
Papy3FOpenMem (char *inFilenameP, char inPermission, PAPY_FILE inVolumeNb, PAPY_FILE *outFp, 
	       void *inFSSpecP)
/* permission r : read, w : write, a : read/write (all) */
{
  PapyShort		theErr;
  FSSpec		*theFSSpecP;
  
   
  if (inFSSpecP == NULL)
  {
    /* create the FSSpec ptr */
    theFSSpecP = (FSSpec *) emalloc3 ((PapyULong) sizeof (FSSpec));
 
    /* fill in the FSSpec structure */
    theFSSpecP->vRefNum = inVolumeNb;
    theFSSpecP->parID   = LMGetCurDirStore ();
    strcpy ((char *) theFSSpecP->name, inFilenameP);
    c2pstr ((char *) theFSSpecP->name);
  } /* if */
  else theFSSpecP = (FSSpec *) inFSSpecP;
  
  /* open the file */
  switch (inPermission) 
  {
    case 'r' : 
      theErr = (PapyShort) FSpOpenDF (theFSSpecP, fsRdPerm, outFp); 
      break;
    case 'w' : 
      theErr = (PapyShort) FSpOpenDF (theFSSpecP, fsWrPerm, outFp); 
      break;
    case 'a' : 
    default  : 
      theErr = (PapyShort) FSpOpenDF (theFSSpecP, fsRdWrPerm, outFp);
  } /* switch */
  
  
  if (Papy3GetMemPtr (*outFp, 0L, 0L, 0L)) Debugger();
  
  theErr = Papy3LoadFileMem (*outFp);
  if (theErr) DebugStr ("\pPapy3LoadFileMem Err");
  
  if (inFSSpecP == NULL) efree3 (&theFSSpecP);
  
  return theErr;

} /* endof Papy3FOpenMem */


/********************************************************************************/
/*										*/
/*	Papy3FCloseMem : overwrites the standard close file function		*/
/*	return : standard error message						*/
/*										*/
/********************************************************************************/

int
Papy3FCloseMem (PAPY_FILE *ioFp)
{
  int		theErr;
  Ptr 		theFilePtr;
  long		theFilePos, theFileSize;
  
  
  theFilePtr = Papy3GetMemPtr (*ioFp, &theFilePos, &theFileSize, 0L);
  
  /*DisposePtr (theFilePtr);*/	
  efree3 ((void **) &theFilePtr);	
  theFilePtr = 0L;
  
  theErr = FSClose (*ioFp);

  Papy3SetMemPtr (*ioFp, theFilePos, theFileSize, theFilePtr);

  return theErr;

} /* endof Papy3FCloseMem */


/********************************************************************************/
/*										*/
/*	Papy3FReadMem : overwrites the standard read from file function		*/
/*	return : error (0 if OK, negative value otherwise)			*/
/*										*/
/********************************************************************************/

PapyShort Papy3FReadMem (PAPY_FILE inFp, PapyULong *inBytesToReadP, PapyULong inNb, 
    			 void **inBufferP, Boolean inNoMemTransfer)
{
  Ptr 		theFilePtr;
  long		theFilePos, theFileSize, theActCount;
  
  
  /* dummy instruction... */
  inNb = inNb;
  
  theFilePtr = Papy3GetMemPtr (inFp, &theFilePos, &theFileSize, &theActCount);
  
  /* Wait until the buffer is full to access this data */
  while (theFilePos + *inBytesToReadP > theActCount) Papy3GetMemPtr (inFp, 0L, 0L, &theActCount);
  
  if (inNoMemTransfer) 
    *inBufferP = theFilePtr + theFilePos;
  else 
    BlockMoveData (theFilePtr + theFilePos, *inBufferP, *inBytesToReadP);
  
  theFilePos += *inBytesToReadP;
  
  if (theFilePos > theFileSize) 
  { 
    Debugger();
    return EOF;
  } /* if */
  
  Papy3SetMemPtr (inFp, theFilePos, theFileSize, theFilePtr);
  
  return noErr;

} /* endof Papy3FReadMem */


/********************************************************************************/
/*									 	*/
/*	Papy3FSeekMem : Papyrus own build file pointer positioning function.	*/
/*	return : standard error message						*/
/*										*/
/********************************************************************************/

int
Papy3FSeekMem (PAPY_FILE inFp, int inPosMode, PapyLong inOffset)
{
  int		theErr = noErr;
  short		theMacPosMode;
  Ptr 		theFilePtr;
  long		theFilePos, theFileSize;
  
  
  theFilePtr = Papy3GetMemPtr (inFp, &theFilePos, &theFileSize, 0L);
  
  switch (inPosMode)
  {
    case SEEK_SET :
      theMacPosMode = fsFromStart;
      theFilePos    = inOffset;
      break;
    case SEEK_CUR :
      theMacPosMode = fsFromMark;
      theFilePos   += inOffset;
      break;
    case SEEK_END :
      theMacPosMode = fsFromLEOF;
      theFilePos    = theFileSize + inOffset;
      Debugger();
      break;
  } /* switch */
  
  Papy3SetMemPtr (inFp, theFilePos, theFileSize, theFilePtr);
  
  return theErr;

} /* endof Papy3FSeekMem */


/********************************************************************************/
/*									 	*/
/*	Papy3FTellMem : Papyrus function to get the current position of the file*/
/*	pointer.								*/
/*	return : standard error message						*/
/*										*/
/********************************************************************************/

int
Papy3FTellMem (PAPY_FILE inFp, PapyLong *outFilePosP)
{
  int 		theErr = noErr;
  Ptr 		theFilePtr;
  long		theFilePos, theFileSize;
  
  
  theFilePtr = Papy3GetMemPtr (inFp, &theFilePos, &theFileSize, 0L);
  
  *outFilePosP = theFilePos;
  
  return theErr;

} /* endof Papy3FTellMem */