Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / lang / LangPrimSource / PyrArchiver.cpp
blob4cd80b7b0e41f599b1e46462ae61a06d57a1d313
1 /*
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"
30 #include "GC.h"
31 #include "ReadWriteMacros.h"
33 int prAsArchive(struct VMGlobals *g, int numArgsPushed);
34 int prAsArchive(struct VMGlobals *g, int numArgsPushed)
36 PyrSlot *a = g->sp;
38 PyrArchiver<char*> arch(g);
40 int err = arch.prepareToWriteArchive(a);
41 if (err) return err;
43 int32 size = arch.calcArchiveSize();
45 PyrInt8Array *obj = newPyrInt8Array(g->gc, size, 0, true);
46 obj->size = size;
47 arch.setStream((char*)obj->b);
48 err = arch.writeArchive();
50 if (err == errNone) SetObject(a, obj);
51 else SetNil(a);
53 return err;
56 int prUnarchive(struct VMGlobals *g, int numArgsPushed);
57 int prUnarchive(struct VMGlobals *g, int numArgsPushed)
59 PyrSlot *a = g->sp;
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);
67 return err;
70 int prWriteArchive(struct VMGlobals *g, int numArgsPushed);
71 int prWriteArchive(struct VMGlobals *g, int numArgsPushed)
73 PyrSlot *a = g->sp - 1;
74 PyrSlot *b = g->sp;
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");
84 int err = errNone;
85 if (file) {
86 err = arch.prepareToWriteArchive(a);
87 if (!err) {
88 arch.setStream(file);
89 err = arch.writeArchive();
91 fclose(file);
92 } else {
93 error("file open failed\n");
94 err = errFailed;
96 return err;
99 int prReadArchive(struct VMGlobals *g, int numArgsPushed);
100 int prReadArchive(struct VMGlobals *g, int numArgsPushed)
102 PyrSlot *a = g->sp - 1;
103 PyrSlot *b = g->sp;
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");
114 int err;
115 if (file) {
116 arch.setStream(file);
117 err = arch.readArchive(a);
118 fclose(file);
119 } else {
120 error("file open failed\n");
121 err = errFailed;
123 return err;
126 void initArchiverPrimitives();
127 void initArchiverPrimitives()
129 int base, index;
131 base = nextPrimitiveIndex();
132 index = 0;
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);
141 #if _SC_PLUGINS_
143 #include "SCPlugin.h"
145 // export the function that SC will call to load the plug in.
146 #pragma export on
147 extern "C" { SCPlugIn* loadPlugIn(void); }
148 #pragma export off
151 // define plug in object
152 class APlugIn : public SCPlugIn
154 public:
155 APlugIn();
156 virtual ~APlugIn();
158 virtual void AboutToCompile();
161 APlugIn::APlugIn()
163 // constructor for plug in
166 APlugIn::~APlugIn()
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();
184 #endif