UNPKG

6.07 kBtext/x-cView Raw
1/*
2 Source File : OutputFileDriver.h
3
4
5 Copyright 2013 Gal Kahana HummusJS
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18
19 */
20#include "OutputFileDriver.h"
21#include "OutputFile.h"
22#include "ByteWriterWithPositionDriver.h"
23#include "ConstructorsHolder.h"
24
25
26using namespace v8;
27
28
29
30OutputFileDriver::OutputFileDriver()
31{
32 mOutputFileInstance = new OutputFile();
33 mOwnsInstance = true;
34}
35
36OutputFileDriver::~OutputFileDriver()
37{
38 if(mOwnsInstance)
39 delete mOutputFileInstance;
40}
41
42PDFHummus::EStatusCode OutputFileDriver::OpenFile(const std::string& inFilePath,bool inAppend)
43{
44 if(!mOutputFileInstance)
45 mOutputFileInstance = new OutputFile();
46 mOwnsInstance = true;
47 return mOutputFileInstance->OpenFile(inFilePath,inAppend);
48}
49
50void OutputFileDriver::SetFromOwnedFile(OutputFile* inFile)
51{
52 if(mOutputFileInstance && mOwnsInstance)
53 delete mOutputFileInstance;
54 mOwnsInstance = false;
55 mOutputFileInstance = inFile;
56}
57
58DEF_SUBORDINATE_INIT(OutputFileDriver::Init)
59{
60 CREATE_ISOLATE_CONTEXT;
61
62 Local<FunctionTemplate> t = NEW_FUNCTION_TEMPLATE_EXTERNAL(New);
63
64 t->SetClassName(NEW_STRING("OutputFile"));
65 t->InstanceTemplate()->SetInternalFieldCount(1);
66
67 SET_PROTOTYPE_METHOD(t, "openFile", OpenFile);
68 SET_PROTOTYPE_METHOD(t, "closeFile", CloseFile);
69 SET_PROTOTYPE_METHOD(t, "getFilePath", GetFilePath);
70 SET_PROTOTYPE_METHOD(t, "getOutputStream", GetOutputStream);
71
72 SET_CONSTRUCTOR_EXPORT("OutputFile",t);
73
74 // save in factory
75 EXPOSE_EXTERNAL_FOR_INIT(ConstructorsHolder, holder)
76 SET_CONSTRUCTOR(holder->OutputFile_constructor, t);
77 SET_CONSTRUCTOR_TEMPLATE(holder->OutputFile_constructor_template, t);
78}
79
80
81METHOD_RETURN_TYPE OutputFileDriver::New(const ARGS_TYPE& args)
82{
83 CREATE_ISOLATE_CONTEXT;
84 CREATE_ESCAPABLE_SCOPE;
85 EXPOSE_EXTERNAL_ARGS(ConstructorsHolder, externalHolder)
86
87 OutputFileDriver* outputFile = new OutputFileDriver();
88 bool append = false;
89 if((args.Length() == 1 || args.Length() == 2) &&
90 args[0]->IsString())
91
92 {
93 if(args.Length() == 2 && args[1]->IsBoolean())
94 append = args[1]->TO_BOOLEAN()->Value();
95 outputFile->OpenFile(*UTF_8_VALUE(args[0]->TO_STRING()),append);
96 }
97
98 outputFile->holder = externalHolder;
99
100 outputFile->Wrap(args.This());
101 SET_FUNCTION_RETURN_VALUE(args.This())
102}
103
104METHOD_RETURN_TYPE OutputFileDriver::OpenFile(const ARGS_TYPE& args)
105{
106 CREATE_ISOLATE_CONTEXT;
107 CREATE_ESCAPABLE_SCOPE;
108
109 if((args.Length() != 1 && args.Length() != 2) ||
110 !args[0]->IsString() ||
111 (args.Length() == 2 && !args[1]->IsBoolean()))
112 {
113 THROW_EXCEPTION("wrong arguments. please provide a string for the file path and optional boolean flag to determine whether this file is opened for appending");
114 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
115 }
116
117 OutputFileDriver* driver = ObjectWrap::Unwrap<OutputFileDriver>(args.This());
118
119
120 if(!driver)
121 {
122 THROW_EXCEPTION("no driver created...please create one through Hummus");
123 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
124
125 }
126
127 if(driver->OpenFile(*UTF_8_VALUE(args[0]->TO_STRING()),args.Length() == 2 ? args[1]->TO_BOOLEAN()->Value() : false) != PDFHummus::eSuccess)
128 {
129 THROW_EXCEPTION("can't open file. make sure path is not busy");
130 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
131 }
132
133 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
134}
135
136METHOD_RETURN_TYPE OutputFileDriver::CloseFile(const ARGS_TYPE& args)
137{
138 CREATE_ISOLATE_CONTEXT;
139 CREATE_ESCAPABLE_SCOPE;
140
141 OutputFileDriver* driver = ObjectWrap::Unwrap<OutputFileDriver>(args.This());
142
143 if(!driver)
144 {
145 THROW_EXCEPTION("no driver created...please create one through Hummus");
146 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
147
148 }
149
150 if(driver->mOutputFileInstance)
151 driver->mOutputFileInstance->CloseFile();
152
153 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
154}
155
156METHOD_RETURN_TYPE OutputFileDriver::GetFilePath(const ARGS_TYPE& args)
157{
158 CREATE_ISOLATE_CONTEXT;
159 CREATE_ESCAPABLE_SCOPE;
160
161 OutputFileDriver* driver = ObjectWrap::Unwrap<OutputFileDriver>(args.This());
162
163 if(!driver)
164 {
165 THROW_EXCEPTION("no driver created...please create one through Hummus");
166 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
167
168 }
169
170 if(driver->mOutputFileInstance && driver->mOutputFileInstance->GetOutputStream())
171 SET_FUNCTION_RETURN_VALUE(NEW_STRING(driver->mOutputFileInstance->GetFilePath().c_str()))
172 else
173 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
174}
175
176METHOD_RETURN_TYPE OutputFileDriver::GetOutputStream(const ARGS_TYPE& args)
177{
178 CREATE_ISOLATE_CONTEXT;
179 CREATE_ESCAPABLE_SCOPE;
180
181 OutputFileDriver* driver = ObjectWrap::Unwrap<OutputFileDriver>(args.This());
182
183 if(!driver)
184 {
185 THROW_EXCEPTION("no driver created...please create one through Hummus");
186 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
187
188 }
189
190 if(driver->mOutputFileInstance && driver->mOutputFileInstance->GetOutputStream())
191 {
192 Local<Value> result = driver->holder->GetNewByteWriterWithPosition(args);
193
194 ObjectWrap::Unwrap<ByteWriterWithPositionDriver>(result->TO_OBJECT())->SetStream(driver->mOutputFileInstance->GetOutputStream(),false);
195
196 SET_FUNCTION_RETURN_VALUE(result)
197 }
198 else
199 SET_FUNCTION_RETURN_VALUE(UNDEFINED)
200}
201