File: Raw.c

package info (click to toggle)
pymol 1.7.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 42,668 kB
  • ctags: 25,775
  • sloc: ansic: 494,779; python: 75,446; cpp: 20,088; makefile: 351; sh: 172; csh: 21
file content (399 lines) | stat: -rw-r--r-- 10,264 bytes parent folder | download
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


/* 
A* -------------------------------------------------------------------
B* This file contains source code for the PyMOL computer program
C* Copyright (c) Schrodinger, LLC. 
D* -------------------------------------------------------------------
E* It is unlawful to modify or remove this copyright notice.
F* -------------------------------------------------------------------
G* Please see the accompanying LICENSE file for further information. 
H* -------------------------------------------------------------------
I* Additional authors of this source file include:
-* 
-* 
-*
Z* -------------------------------------------------------------------
*/

#include"os_predef.h"
#include"os_std.h"

#include"Raw.h"
#include"Version.h"
#include"Base.h"
#include"Feedback.h"
#include"MemoryDebug.h"
#include"OOMac.h"

#define cRaw_file_stream 0

#define cRaw_header_size 16

#define swap_bytes(a) { \
char u; \
u=*(((char*)(a))); \
*(((char*)(a)))=*(((char*)(a))+3); \
*(((char*)(a))+3)=u; \
u=*(((char*)(a))+1); \
*(((char*)(a))+1)=*(((char*)(a))+2); \
*(((char*)(a))+2)=u; }

CRaw *RawOpenRead(PyMOLGlobals * G, char *fname)
{
  int target = 0x04030201;
  int reverse = 0x01020304;
  int actual;
  int ok = true;

  OOAlloc(G, CRaw);
  I->bufVLA = NULL;
  I->G = G;
  I->f = fopen(fname, "rb");
  if(!I->f) {
    ok = false;
  } else {
    if(feof(I->f))
      ok = false;
    else if(fread(&actual, 4, 1, I->f) != 1)
      ok = false;
    else if(actual == target)
      I->swap = false;
    else if(actual == reverse)
      I->swap = true;
    else {
      PRINTFB(G, FB_Raw, FB_Errors)
        "Error-RawOpenRead: Unrecognized byte ordering. This may not a PyMOL file.\n"
        ENDFB(G);
      ok = false;
    }
  }
  if(!ok) {
    if(I->f)
      fclose(I->f);
    OOFreeP(I);
    PRINTFB(G, FB_Raw, FB_Errors)
      "Error-RawOpenRead: Unable to open '%s'.\n", fname ENDFB(G);

  } else {
    I->mode = cRaw_file_stream;
  }
  return (I);
}

CRaw *RawOpenWrite(PyMOLGlobals * G, char *fname)
{
  int target = 0x04030201;
  int ok = true;
  OOAlloc(G, CRaw);
  I->bufVLA = NULL;
  I->G = G;
  I->f = fopen(fname, "wb");
  if(!I->f) {
    ok = false;
  } else {
    fwrite(&target, 4, 1, I->f);
  }
  if(!ok) {
    if(I->f)
      fclose(I->f);
    OOFreeP(I);
  } else {
    I->mode = cRaw_file_stream;
  }
  return (I);
}

CRaw *RawOpenAppend(PyMOLGlobals * G, char *fname)
{
  int target = 0x04030201;
  int ok = true;
  OOAlloc(G, CRaw);
  I->bufVLA = NULL;
  I->G = G;
  I->f = fopen(fname, "wba");
  if(!I->f) {
    ok = false;
  } else {
    if(!ftell(I->f))            /* write magic if this is a new file */
      fwrite(&target, 4, 1, I->f);
  }
  if(!ok) {
    if(I->f)
      fclose(I->f);
    OOFreeP(I);
    PRINTFB(G, FB_Raw, FB_Errors)
      "Error-RawOpenAppend: Unable to open '%s'.\n", fname ENDFB(G);
  } else {
    I->mode = cRaw_file_stream;
  }
  return (I);
}

void RawFree(CRaw * I)
{
  switch (I->mode) {
  case cRaw_file_stream:
    if(I->f) {
      fclose(I->f);
      I->f = NULL;
    }
    break;
  }
  VLAFreeP(I->bufVLA);
  OOFreeP(I);
}

int RawGetNext(CRaw * I, int *size, int *version)
{
  PyMOLGlobals *G = I->G;
  int result = cRaw_EOF;
  switch (I->mode) {
  case cRaw_file_stream:
    if(I->f) {
      if(!feof(I->f)) {
        if(fread((char *) I->header, cRaw_header_size, 1, I->f) != 1) {
          PRINTFD(G, FB_Raw)
            " RawGetNextType-Debug: Couldn't read header.\n" ENDFD;
        } else {
          if(I->swap) {
            swap_bytes(I->header);
            swap_bytes(I->header + 1);
            swap_bytes(I->header + 2);
            swap_bytes(I->header + 3);
          }
          fseek(I->f, -cRaw_header_size, SEEK_CUR);
          *size = I->header[0];
          result = I->header[1];
          *version = I->header[2];
        }
      }
    }
  }
  return (result);
}

int RawReadSkip(CRaw * I)
{
  PyMOLGlobals *G = I->G;
  int result = false;
  switch (I->mode) {
  case cRaw_file_stream:
    if(I->f) {
      if(!feof(I->f)) {
        if(fread((char *) I->header, cRaw_header_size, 1, I->f) != 1) {
          PRINTFB(G, FB_Raw, FB_Errors)
            "Error-Raw: Error reading header.\n" ENDFB(G);
        } else {
          if(I->swap) {
            swap_bytes(I->header);
            swap_bytes(I->header + 1);
            swap_bytes(I->header + 2);
            swap_bytes(I->header + 3);
          }
          fseek(I->f, I->header[0], SEEK_CUR);
          result = true;
        }
      }
    }
    break;
  }
  return (result);
}

char *RawRead(CRaw * I, int *type, unsigned int *size, int *serial)
{
  PyMOLGlobals *G = I->G;
  char *result = NULL;
  switch (I->mode) {
  case cRaw_file_stream:
    if(I->f) {
      if(!feof(I->f)) {
        if(fread((char *) I->header, cRaw_header_size, 1, I->f) != 1) {
          PRINTFB(G, FB_Raw, FB_Errors)
            "Error-Raw: Error reading header.\n" ENDFB(G);
        } else {
          if(I->swap) {
            swap_bytes(I->header);
            swap_bytes(I->header + 1);
            swap_bytes(I->header + 2);
            swap_bytes(I->header + 3);
          }
          VLACheck(I->bufVLA, char, I->header[0]);
          if(fread(I->bufVLA, I->header[0], 1, I->f) == 1) {
            result = I->bufVLA;
            *size = I->header[0];
            *type = I->header[1];       /* record type */
            *serial = I->header[3];
          } else {
            PRINTFB(G, FB_Raw, FB_Errors)
              "Error-RawRead: Data read error.\n" ENDFB(G);

          }
        }
      } else {
        *type = cRaw_EOF;
      }
    }
    break;
  }
  return (result);
}

char *RawReadPtr(CRaw * I, int type, int *size)
{
  PyMOLGlobals *G = I->G;
  char *result = NULL;
  switch (I->mode) {
  case cRaw_file_stream:
    if(I->f) {
      if(!feof(I->f)) {
        if(fread((char *) I->header, cRaw_header_size, 1, I->f) != 1) {
          PRINTFB(G, FB_Raw, FB_Errors)
            "Error-Raw: Error reading header.\n" ENDFB(G);
        } else {
          if(I->swap) {
            swap_bytes(I->header);
            swap_bytes(I->header + 1);
            swap_bytes(I->header + 2);
            swap_bytes(I->header + 3);
          }
          if((I->header[1]) != type) {
            fseek(I->f, -cRaw_header_size, SEEK_CUR);
            PRINTFD(G, FB_Raw)
              " RawReadPtr-Debug: Type mismatch.\n" ENDFD;
          } else {
            result = (char*) mmalloc(I->header[0]);
            if(fread(result, I->header[0], 1, I->f) != 1) {
              FreeP(result);
              PRINTFB(G, FB_Raw, FB_Errors)
                "Error-RawReadVLA: Data read error.\n" ENDFB(G);

            } else {
              *size = I->header[0];
            }
          }
        }
      }
    }
    break;
  }
  return (result);
}

char *RawReadVLA(CRaw * I, int type, unsigned int rec_size, int grow_factor,
                 int auto_zero)
{
  PyMOLGlobals *G = I->G;
  char *result = NULL;
  switch (I->mode) {
  case cRaw_file_stream:
    if(I->f) {
      if(!feof(I->f)) {
        if(fread((char *) I->header, cRaw_header_size, 1, I->f) != 1) {
          PRINTFB(G, FB_Raw, FB_Errors)
            "Error-Raw: Error reading header.\n" ENDFB(G);
        } else {
          if(I->swap) {
            swap_bytes(I->header);
            swap_bytes(I->header + 1);
            swap_bytes(I->header + 2);
            swap_bytes(I->header + 3);
          }
          if((I->header[1]) != type) {
            fseek(I->f, -cRaw_header_size, SEEK_CUR);
            PRINTFD(G, FB_Raw)
              " RawReadVLA-Debug: Type mismatch %d != %d.\n", I->header[1], type ENDFD;

          } else {
            result = (char*)
              VLAMalloc((I->header[0] / rec_size), rec_size, grow_factor, auto_zero);
            if(fread(result, I->header[0], 1, I->f) != 1) {
              VLAFreeP(result);
              PRINTFB(G, FB_Raw, FB_Errors)
                "Error-RawReadVLA: Data read error.\n" ENDFB(G);

            } else {
              result = (char *) VLASetSize(result, I->header[0] / rec_size);
            }
          }
        }
      }
    }
    break;
  }
  return (result);
}

int RawReadInto(CRaw * I, int type, unsigned int size, char *buffer)
{
  PyMOLGlobals *G = I->G;
  int ok = false;
  switch (I->mode) {
  case cRaw_file_stream:
    if(I->f) {
      if(!feof(I->f)) {
        if(fread((char *) I->header, cRaw_header_size, 1, I->f) != 1) {
          PRINTFB(G, FB_Raw, FB_Errors)
            "Error-RawReadInfo: Error reading header.\n" ENDFB(G);
        } else {
          if(I->swap) {
            swap_bytes(I->header);
            swap_bytes(I->header + 1);
            swap_bytes(I->header + 2);
            swap_bytes(I->header + 3);
          }
          if((I->header[1]) != type) {
            fseek(I->f, -cRaw_header_size, SEEK_CUR);
            PRINTFD(G, FB_Raw)
              " RawReadPtr-Debug: Type mismatch.\n" ENDFD;
          } else if(I->header[0] != (signed) size) {
            PRINTFB(G, FB_Raw, FB_Errors)
              "Error-RawReadInfo: Size mismatch %d!=%d (disk/RAM).\n", I->header[0], size
              ENDFB(G);
          } else {
            if(fread(buffer, size, 1, I->f) != 1) {
              PRINTFB(G, FB_Raw, FB_Errors)
                "Error-RawReadInfo: Data read error.\n" ENDFB(G);
            } else {
              ok = true;
            }
          }
        }
      }
    }
    break;
  }
  return (ok);
}

int RawWrite(CRaw * I, int type, unsigned int size, int serial, char *bytes)
{
  PyMOLGlobals *G = I->G;
  int header[4];
  int ok = false;
  PRINTFD(G, FB_Raw)
    " RawWrite-Debug: type %d size %d %p\n", type, size, bytes ENDFD;
  switch (I->mode) {
  case cRaw_file_stream:
    if(I->f) {
      header[0] = size;
      header[1] = type;
      header[2] = _PyMOL_VERSION_int;
      header[3] = serial;
      if(fwrite((char *) header, cRaw_header_size, 1, I->f) != 1) {
        PRINTFB(G, FB_Raw, FB_Errors)
          "Error-RawWrite: can't write header.\n" ENDFB(G);
      } else if(fwrite((char *) bytes, size, 1, I->f) != 1) {
        PRINTFB(G, FB_Raw, FB_Errors)
          "Error-RawWrite: can't write data.\n" ENDFB(G);
      } else {
        ok = true;
      }
    }
  }
  PRINTFD(G, FB_Raw)
    " RawWrite-Debug: leaving... %d\n", ok ENDFD;

  return (ok);
}