File: iffparse.c

package info (click to toggle)
amiwm 0.22pl2-5
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 1,048 kB
  • sloc: ansic: 10,195; perl: 443; makefile: 258; yacc: 245; lex: 215; sh: 211
file content (163 lines) | stat: -rw-r--r-- 4,558 bytes parent folder | download | duplicates (4)
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
#include "libami.h"
#include "alloc.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#ifndef AMIGAOS

static LONG callIFFHook(struct IFFHandle *iff, LONG cmd, APTR buf, LONG nby)
{
  struct IFFStreamCmd c;
  c.sc_Command = cmd;
  c.sc_Buf = buf;
  c.sc_NBytes = nby;
  if(iff->iff_Hook == NULL)
    return IFFERR_NOHOOK;
  else
    return iff->iff_Hook->h_SubEntry(iff->iff_Hook, iff, &c);
}

LONG ReadChunkBytes( struct IFFHandle *iff, APTR buf, long numBytes );
LONG WriteChunkBytes( struct IFFHandle *iff, APTR buf, long numBytes );
LONG ReadChunkRecords( struct IFFHandle *iff, APTR buf, long bytesPerRecord,
	long numRecords );
LONG WriteChunkRecords( struct IFFHandle *iff, APTR buf, long bytesPerRecord,
	long numRecords );

LONG PushChunk( struct IFFHandle *iff, long type, long id, long size );
LONG PopChunk( struct IFFHandle *iff );

LONG EntryHandler( struct IFFHandle *iff, long type, long id, long position,
	struct Hook *handler, APTR object );
LONG ExitHandler( struct IFFHandle *iff, long type, long id, long position,
	struct Hook *handler, APTR object );

LONG PropChunk( struct IFFHandle *iff, long type, long id );
LONG PropChunks( struct IFFHandle *iff, LONG *propArray, long numPairs );
LONG StopChunk( struct IFFHandle *iff, long type, long id );
LONG StopChunks( struct IFFHandle *iff, LONG *propArray, long numPairs );
LONG CollectionChunk( struct IFFHandle *iff, long type, long id );
LONG CollectionChunks( struct IFFHandle *iff, LONG *propArray,
	long numPairs );
LONG StopOnExit( struct IFFHandle *iff, long type, long id );

struct StoredProperty *FindProp( struct IFFHandle *iff, long type, long id );
struct CollectionItem *FindCollection( struct IFFHandle *iff, long type,
	long id );
struct ContextNode *FindPropContext( struct IFFHandle *iff );
struct ContextNode *CurrentChunk( struct IFFHandle *iff );
struct ContextNode *ParentChunk( struct ContextNode *contextNode );

struct LocalContextItem *AllocLocalItem( long type, long id, long ident,
	long dataSize );
APTR LocalItemData( struct LocalContextItem *localItem );
void SetLocalItemPurge( struct LocalContextItem *localItem,
	struct Hook *purgeHook );
void FreeLocalItem( struct LocalContextItem *localItem );
struct LocalContextItem *FindLocalItem( struct IFFHandle *iff, long type,
	long id, long ident );
LONG StoreLocalItem( struct IFFHandle *iff, struct LocalContextItem *localItem,
	long position );
void StoreItemInContext( struct IFFHandle *iff,
	struct LocalContextItem *localItem,
	struct ContextNode *contextNode );

LONG GoodID( long id );
LONG GoodType( long type );
STRPTR IDtoStr( long id, STRPTR buf );


void InitIFF( struct IFFHandle *iff, long flags, struct Hook *streamHook )
{
  iff->iff_Flags = flags;
  iff->iff_Hook = streamHook;
}

struct IFFHandle *AllocIFF( void )
{
  return (struct IFFHandle *)calloc(1, sizeof(struct IFFHandle));
}

LONG OpenIFF( struct IFFHandle *iff, long rwMode )
{
  return callIFFHook(iff, IFFCMD_INIT, NULL, 0);
}

LONG ParseIFF( struct IFFHandle *iff, long control )
{
  return IFFERR_NOHOOK;
}

void CloseIFF( struct IFFHandle *iff )
{
  callIFFHook(iff, IFFCMD_CLEANUP, NULL, 0);
}

void FreeIFF( struct IFFHandle *iff )
{
  free(iff);
}



#else

static ULONG __saveds __asm hllDispatch(register __a0 struct Hook *hook,
					register __a2 APTR object,
					register __a1 APTR message)
{
  return ((ULONG (*)(struct Hook *, APTR, APTR))hook->h_SubEntry)
    (hook, object, message);
}

#endif

static ULONG iffFileHook(struct Hook *hook, APTR object, APTR message)
{
  struct IFFStreamCmd *cmd = (struct IFFStreamCmd *)message;
  switch(cmd->sc_Command) {
  case IFFCMD_INIT:
    return 0;
  case IFFCMD_CLEANUP:
    close(((struct IFFHandle *)object)->iff_Stream);
    return 0;
  case IFFCMD_READ:
    return IFFERR_READ;
  case IFFCMD_WRITE:
    return IFFERR_WRITE;
  case IFFCMD_SEEK:
    return IFFERR_SEEK;
  }
  return 0;
}

LONG OpenIFFasFile( struct IFFHandle *iff, char *fn, char *mode )
{
  static struct Hook hook =
#ifdef AMIGAOS
  { { 0, 0 }, hllDispatch, iffFileHook, 0 };
#else
  { { 0, 0 }, 0, iffFileHook, 0 };
#endif
  int m, fd;
  LONG r;

  if((fd = open(fn, (strchr(mode, 'w')==NULL? O_RDONLY :
		     (O_WRONLY|O_TRUNC|O_CREAT)), 0666))<0)
    return IFFERR_READ;
  iff->iff_Stream = fd;
  InitIFF( iff, (strchr(mode, 'w')==NULL? (m=IFFF_READ|IFFF_RSEEK) :
		 (m=IFFF_WRITE|IFFF_RSEEK)), &hook );
  if((r=OpenIFF( iff, m ))!=0) {
    close(fd);
    return r;
  }
  return 0;
}