2 SuperCollider real time audio synthesis system
3 Copyright (c) 2002 James McCartney. All rights reserved.
4 http://www.audiosynth.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 An object archiving system for SuperCollider.
26 #include "PyrArchiverT.h"
27 #include "PyrKernel.h"
28 #include "PyrPrimitive.h"
29 #include "VMGlobals.h"
31 #include "ReadWriteMacros.h"
33 int prAsArchive(struct VMGlobals
*g
, int numArgsPushed
);
34 int prAsArchive(struct VMGlobals
*g
, int numArgsPushed
)
38 PyrArchiver
<char*> arch(g
);
40 int err
= arch
.prepareToWriteArchive(a
);
43 int32 size
= arch
.calcArchiveSize();
45 PyrInt8Array
*obj
= newPyrInt8Array(g
->gc
, size
, 0, true);
47 arch
.setStream((char*)obj
->b
);
48 err
= arch
.writeArchive();
50 if (err
== errNone
) SetObject(a
, obj
);
56 int prUnarchive(struct VMGlobals
*g
, int numArgsPushed
);
57 int prUnarchive(struct VMGlobals
*g
, int numArgsPushed
)
61 if (!isKindOfSlot(a
, class_int8array
)) return errWrongType
;
63 PyrArchiver
<char*> arch(g
);
65 arch
.setStream((char*)slotRawObject(a
)->slots
);
66 int err
= arch
.readArchive(a
);
70 int prWriteArchive(struct VMGlobals
*g
, int numArgsPushed
);
71 int prWriteArchive(struct VMGlobals
*g
, int numArgsPushed
)
73 PyrSlot
*a
= g
->sp
- 1;
76 if (!isKindOfSlot(b
, class_string
)) return errWrongType
;
78 char pathname
[PATH_MAX
];
79 memcpy(pathname
, slotRawString(b
)->s
, slotRawObject(b
)->size
);
80 pathname
[slotRawString(b
)->size
] = 0;
82 PyrArchiver
<FILE*> arch(g
);
83 FILE *file
= fopen(pathname
, "wb");
86 err
= arch
.prepareToWriteArchive(a
);
89 err
= arch
.writeArchive();
93 error("file open failed\n");
99 int prReadArchive(struct VMGlobals
*g
, int numArgsPushed
);
100 int prReadArchive(struct VMGlobals
*g
, int numArgsPushed
)
102 PyrSlot
*a
= g
->sp
- 1;
105 if (!isKindOfSlot(b
, class_string
)) return errWrongType
;
107 char pathname
[PATH_MAX
];
108 memcpy(pathname
, slotRawString(b
)->s
, slotRawObject(b
)->size
);
109 pathname
[slotRawString(b
)->size
] = 0;
111 PyrArchiver
<FILE*> arch(g
);
112 FILE *file
= fopen(pathname
, "rb");
116 arch
.setStream(file
);
117 err
= arch
.readArchive(a
);
120 error("file open failed\n");
126 void initArchiverPrimitives();
127 void initArchiverPrimitives()
131 base
= nextPrimitiveIndex();
134 definePrimitive(base
, index
++, "_AsArchive", prAsArchive
, 1, 0);
135 definePrimitive(base
, index
++, "_Unarchive", prUnarchive
, 1, 0);
136 definePrimitive(base
, index
++, "_WriteArchive", prWriteArchive
, 2, 0);
137 definePrimitive(base
, index
++, "_ReadArchive", prReadArchive
, 2, 0);
143 #include "SCPlugin.h"
145 // export the function that SC will call to load the plug in.
147 extern "C" { SCPlugIn
* loadPlugIn(void); }
151 // define plug in object
152 class APlugIn
: public SCPlugIn
158 virtual void AboutToCompile();
163 // constructor for plug in
168 // destructor for plug in
171 void APlugIn::AboutToCompile()
173 // this is called each time the class library is compiled.
174 initArchiverPrimitives();
177 // This function is called when the plug in is loaded into SC.
178 // It returns an instance of APlugIn.
179 SCPlugIn
* loadPlugIn()
181 return new APlugIn();