Skip to content

Commit 1949ebe

Browse files
committed
added more exception handling
1 parent a9d506a commit 1949ebe

File tree

7 files changed

+110
-48
lines changed

7 files changed

+110
-48
lines changed

Ghostscript.NET/GhostscriptLibrary.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,21 +217,83 @@ protected virtual void Dispose(bool disposing)
217217
/// </summary>
218218
private void Initialize()
219219
{
220+
string symbolMappingError = "Delegate of an exported function couldn't be created for symbol '{0}'";
221+
220222
this.gsapi_revision = _library.GetDelegateForFunction<gsapi_revision>("gsapi_revision");
223+
224+
if (this.gsapi_revision == null)
225+
throw new GhostscriptException(string.Format(symbolMappingError, "gsapi_revision"));
226+
221227
this.gsapi_new_instance = _library.GetDelegateForFunction<gsapi_new_instance>("gsapi_new_instance");
228+
229+
if (this.gsapi_new_instance == null)
230+
throw new GhostscriptException(string.Format(symbolMappingError, "gsapi_new_instance"));
231+
222232
this.gsapi_delete_instance = _library.GetDelegateForFunction<gsapi_delete_instance>("gsapi_delete_instance");
233+
234+
if (this.gsapi_delete_instance == null)
235+
throw new GhostscriptException(string.Format(symbolMappingError, "gsapi_delete_instance"));
236+
223237
this.gsapi_set_stdio = _library.GetDelegateForFunction<gsapi_set_stdio>("gsapi_set_stdio");
238+
239+
if (this.gsapi_set_stdio == null)
240+
throw new GhostscriptException(string.Format(symbolMappingError, "gsapi_set_stdio"));
241+
224242
this.gsapi_set_poll = _library.GetDelegateForFunction<gsapi_set_poll>("gsapi_set_poll");
243+
244+
if (this.gsapi_set_poll == null)
245+
throw new GhostscriptException(string.Format(symbolMappingError, "gsapi_set_poll"));
246+
225247
this.gsapi_set_display_callback = _library.GetDelegateForFunction<gsapi_set_display_callback>("gsapi_set_display_callback");
248+
249+
if (this.gsapi_set_display_callback == null)
250+
throw new GhostscriptException(string.Format(symbolMappingError, "gsapi_set_display_callback"));
251+
226252
this.gsapi_set_arg_encoding = _library.GetDelegateForFunction<gsapi_set_arg_encoding>("gsapi_set_arg_encoding");
253+
254+
if (this.gsapi_set_arg_encoding == null)
255+
throw new GhostscriptException(string.Format(symbolMappingError, "gsapi_set_arg_encoding"));
256+
227257
this.gsapi_init_with_args = _library.GetDelegateForFunction<gsapi_init_with_args>("gsapi_init_with_args");
258+
259+
if (this.gsapi_init_with_args == null)
260+
throw new GhostscriptException(string.Format(symbolMappingError, "gsapi_init_with_args"));
261+
228262
this.gsapi_run_string_begin = _library.GetDelegateForFunction<gsapi_run_string_begin>("gsapi_run_string_begin");
263+
264+
if (this.gsapi_run_string_begin == null)
265+
throw new GhostscriptException(string.Format(symbolMappingError, "gsapi_run_string_begin"));
266+
229267
this.gsapi_run_string_continue = _library.GetDelegateForFunction<gsapi_run_string_continue>("gsapi_run_string_continue");
268+
269+
if (this.gsapi_run_string_continue == null)
270+
throw new GhostscriptException(string.Format(symbolMappingError, "gsapi_run_string_continue"));
271+
230272
this.gsapi_run_string_end = _library.GetDelegateForFunction< gsapi_run_string_end>("gsapi_run_string_end");
273+
274+
if (this.gsapi_run_string_end == null)
275+
throw new GhostscriptException(string.Format(symbolMappingError, "gsapi_run_string_end"));
276+
231277
this.gsapi_run_string_with_length = _library.GetDelegateForFunction<gsapi_run_string_with_length>("gsapi_run_string_with_length");
278+
279+
if (this.gsapi_run_string_with_length == null)
280+
throw new GhostscriptException(string.Format(symbolMappingError, "gsapi_run_string_with_length"));
281+
232282
this.gsapi_run_string = _library.GetDelegateForFunction<gsapi_run_string>("gsapi_run_string");
283+
284+
if (this.gsapi_run_string == null)
285+
throw new GhostscriptException(string.Format(symbolMappingError, "gsapi_run_string"));
286+
233287
this.gsapi_run_file = _library.GetDelegateForFunction<gsapi_run_file>("gsapi_run_file");
288+
289+
if (this.gsapi_run_file == null)
290+
throw new GhostscriptException(string.Format(symbolMappingError, "gsapi_run_file"));
291+
234292
this.gsapi_exit = _library.GetDelegateForFunction<gsapi_exit>("gsapi_exit");
293+
294+
if (this.gsapi_exit == null)
295+
throw new GhostscriptException(string.Format(symbolMappingError, "gsapi_exit"));
296+
235297
}
236298

237299
#endregion

Ghostscript.NET/Helpers/StreamHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static string WriteToTemporaryFile(Stream stream)
3737
{
3838
if (stream.Length < 4)
3939
{
40-
throw new InvalidDataException();
40+
throw new InvalidDataException("Less than 4 bytes found in stream.");
4141
}
4242

4343
stream.Position = 0;

Ghostscript.NET/Interpreter/GhostscriptInterpreter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public GhostscriptInterpreter()
6565

6666
#endregion
6767

68-
#region Constructor - gsDll
68+
#region Constructor - library
6969

7070
/// <summary>
7171
/// Initializes a new instance of the Ghostscript.NET.GhostscriptInterpreter class.

Ghostscript.NET/OutputDevices/GhostscriptDevice.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ public void Process(GhostscriptVersionInfo ghostscriptVersion, bool fromMemory,
262262
{
263263
if (ghostscriptVersion == null)
264264
{
265-
throw new ArgumentNullException("ghostscriptVersion", "Cannot be null.");
265+
throw new ArgumentNullException("ghostscriptVersion");
266266
}
267267

268268
using (GhostscriptProcessor processor = new GhostscriptProcessor(ghostscriptVersion, fromMemory))

Ghostscript.NET/Processor/GhostscriptProcessor.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,16 @@ public GhostscriptProcessor()
119119

120120
#endregion
121121

122-
#region Constructor - gsDll
122+
#region Constructor - library
123123

124-
public GhostscriptProcessor(byte[] gsDll)
124+
public GhostscriptProcessor(byte[] library)
125125
{
126-
if (gsDll == null)
126+
if (library == null)
127127
{
128-
throw new ArgumentNullException("gsDll", "Cannot be null.");
128+
throw new ArgumentNullException("library");
129129
}
130130

131-
_gs = new GhostscriptLibrary(gsDll);
131+
_gs = new GhostscriptLibrary(library);
132132
}
133133

134134
#endregion
@@ -146,7 +146,7 @@ public GhostscriptProcessor(GhostscriptVersionInfo version, bool fromMemory)
146146
{
147147
if (version == null)
148148
{
149-
throw new ArgumentNullException("version", "Cannot be null.");
149+
throw new ArgumentNullException("version");
150150
}
151151

152152
_gs = new GhostscriptLibrary(version, fromMemory);
@@ -200,7 +200,7 @@ public void StartProcessing(GhostscriptDevice device, GhostscriptStdIO stdIO_cal
200200
{
201201
if (device == null)
202202
{
203-
throw new ArgumentNullException("device", "Cannot be null.");
203+
throw new ArgumentNullException("device");
204204
}
205205

206206
this.StartProcessing(device.GetSwitches(), stdIO_callback);
@@ -219,12 +219,12 @@ public void StartProcessing(string[] args, GhostscriptStdIO stdIO_callback)
219219
{
220220
if (args == null)
221221
{
222-
throw new ArgumentNullException("args", "Cannot be null.");
222+
throw new ArgumentNullException("args");
223223
}
224224

225225
if (args.Length < 3)
226226
{
227-
throw new ArgumentOutOfRangeException("args", "Switches count seems to low.");
227+
throw new ArgumentOutOfRangeException("args");
228228
}
229229

230230
for (int i = 0; i < args.Length; i++)

Ghostscript.NET/Rasterizer/GhostscriptRasterizer.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public GhostscriptRasterizer(GhostscriptViewer viewerInstance)
6161
{
6262
if (viewerInstance == null)
6363
{
64-
throw new ArgumentNullException("viewerInstance", "Cannot be null.");
64+
throw new ArgumentNullException("viewerInstance");
6565
}
6666

6767
_viewer = viewerInstance;
@@ -129,7 +129,7 @@ public void Open(MemoryStream stream)
129129
{
130130
if (stream == null)
131131
{
132-
throw new ArgumentNullException("stream", "Cannot be null.");
132+
throw new ArgumentNullException("stream");
133133
}
134134

135135
this.Open(stream, GhostscriptVersionInfo.GetLastInstalledVersion(GhostscriptLicense.GPL | GhostscriptLicense.AFPL, GhostscriptLicense.GPL), false);
@@ -157,12 +157,12 @@ public void Open(MemoryStream stream, GhostscriptVersionInfo versionInfo, bool d
157157
{
158158
if (stream == null)
159159
{
160-
throw new ArgumentNullException("stream", "Cannot be null.");
160+
throw new ArgumentNullException("stream");
161161
}
162162

163163
if (versionInfo == null)
164164
{
165-
throw new ArgumentNullException("versionInfo", "Cannot be null.");
165+
throw new ArgumentNullException("versionInfo");
166166
}
167167

168168
if (_gsViewState == null)
@@ -184,7 +184,7 @@ public void Open(string path, GhostscriptVersionInfo versionInfo, bool dllFromMe
184184

185185
if (versionInfo == null)
186186
{
187-
throw new ArgumentNullException("versionInfo", "Cannot be null.");
187+
throw new ArgumentNullException("versionInfo");
188188
}
189189

190190
if (_gsViewState == null)
@@ -195,45 +195,45 @@ public void Open(string path, GhostscriptVersionInfo versionInfo, bool dllFromMe
195195

196196
#endregion
197197

198-
#region Open - stream, gsDll
198+
#region Open - stream, library
199199

200-
public void Open(MemoryStream stream, byte[] gsDll)
200+
public void Open(MemoryStream stream, byte[] library)
201201
{
202202
if (stream == null)
203203
{
204-
throw new ArgumentNullException("stream", "Cannot be null.");
204+
throw new ArgumentNullException("stream");
205205
}
206206

207-
if (gsDll == null)
207+
if (library == null)
208208
{
209-
throw new ArgumentNullException("gsDll", "Cannot be null.");
209+
throw new ArgumentNullException("library");
210210
}
211211

212212
if (_gsViewState == null)
213213
{
214-
_viewer.Open(stream, gsDll);
214+
_viewer.Open(stream, library);
215215
}
216216
}
217217

218218
#endregion
219219

220-
#region Open - path, gsDll
220+
#region Open - path, library
221221

222-
public void Open(string path, byte[] gsDll)
222+
public void Open(string path, byte[] library)
223223
{
224224
if (!File.Exists(path))
225225
{
226-
throw new FileNotFoundException("Could not find input file.", path);
226+
throw new FileNotFoundException("Couldn't find input file.", path);
227227
}
228228

229-
if (gsDll == null)
229+
if (library == null)
230230
{
231-
throw new ArgumentNullException("gsDll", "Cannot be null.");
231+
throw new ArgumentNullException("library");
232232
}
233233

234234
if (_gsViewState == null)
235235
{
236-
_viewer.Open(path, gsDll);
236+
_viewer.Open(path, library);
237237
}
238238
}
239239

Ghostscript.NET/Viewer/GhostscriptViewer.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public void Open(Stream stream)
165165
{
166166
if (stream == null)
167167
{
168-
throw new ArgumentNullException("stream", "Cannot be null.");
168+
throw new ArgumentNullException("stream");
169169
}
170170

171171
string path = StreamHelper.WriteToTemporaryFile(stream);
@@ -197,12 +197,12 @@ public void Open(Stream stream, GhostscriptVersionInfo versionInfo, bool dllFrom
197197
{
198198
if (stream == null)
199199
{
200-
throw new ArgumentNullException("stream", "Cannot be null.");
200+
throw new ArgumentNullException("stream");
201201
}
202202

203203
if (versionInfo == null)
204204
{
205-
throw new ArgumentNullException("versionInfo", "Cannot be null.");
205+
throw new ArgumentNullException("versionInfo");
206206
}
207207

208208
string path = StreamHelper.WriteToTemporaryFile(stream);
@@ -225,7 +225,7 @@ public void Open(string path, GhostscriptVersionInfo versionInfo, bool dllFromMe
225225

226226
if (versionInfo == null)
227227
{
228-
throw new ArgumentNullException("versionInfo", "Cannot be null.");
228+
throw new ArgumentNullException("versionInfo");
229229
}
230230

231231
this.Close();
@@ -239,43 +239,43 @@ public void Open(string path, GhostscriptVersionInfo versionInfo, bool dllFromMe
239239

240240
#endregion
241241

242-
#region Open - stream, gsDll
242+
#region Open - stream, library
243243

244-
public void Open(Stream stream, byte[] gsDll)
244+
public void Open(Stream stream, byte[] library)
245245
{
246246
if (stream == null)
247247
{
248-
throw new ArgumentNullException("stream", "Cannot be null.");
248+
throw new ArgumentNullException("stream");
249249
}
250250

251251
string path = StreamHelper.WriteToTemporaryFile(stream);
252252

253253
_fileCleanupHelper.Add(path);
254254

255-
this.Open(path, gsDll);
255+
this.Open(path, library);
256256
}
257257

258258
#endregion
259259

260-
#region Open - path, gsDll
260+
#region Open - path, library
261261

262-
public void Open(string path, byte[] gsDll)
262+
public void Open(string path, byte[] library)
263263
{
264264
if (!File.Exists(path))
265265
{
266266
throw new FileNotFoundException("Could not find input file.", path);
267267
}
268268

269-
if (gsDll == null)
269+
if (library == null)
270270
{
271-
throw new ArgumentNullException("gsDll", "Cannot be null.");
271+
throw new ArgumentNullException("library");
272272
}
273273

274274
this.Close();
275275

276276
_filePath = path;
277277

278-
_interpreter = new GhostscriptInterpreter(gsDll);
278+
_interpreter = new GhostscriptInterpreter(library);
279279

280280
this.Open();
281281
}
@@ -288,7 +288,7 @@ public void Open(GhostscriptVersionInfo versionInfo, bool dllFromMemory)
288288
{
289289
if (versionInfo == null)
290290
{
291-
throw new ArgumentNullException("versionInfo", "Cannot be null.");
291+
throw new ArgumentNullException("versionInfo");
292292
}
293293

294294
this.Close();
@@ -302,20 +302,20 @@ public void Open(GhostscriptVersionInfo versionInfo, bool dllFromMemory)
302302

303303
#endregion
304304

305-
#region Open - gsDll
305+
#region Open - library
306306

307-
public void Open(byte[] gsDll)
307+
public void Open(byte[] library)
308308
{
309-
if (gsDll == null)
309+
if (library == null)
310310
{
311-
throw new ArgumentNullException("gsDll", "Cannot be null.");
311+
throw new ArgumentNullException("library");
312312
}
313313

314314
this.Close();
315315

316316
_filePath = string.Empty;
317317

318-
_interpreter = new GhostscriptInterpreter(gsDll);
318+
_interpreter = new GhostscriptInterpreter(library);
319319

320320
this.Open();
321321
}

0 commit comments

Comments
 (0)