comparison libinterp/dldfcn/__osmesa_print__.cc @ 19743:5cfb3ccbf24a

style fixes for resource management * __osmesa_print__.cc (close_fcn): New function. (F__osmesa_print__): Manage FILE pointer with unwind_protect. Allocate local buffer with OCTAVE_LOCAL_BUFFER. * gl2ps-renderer.cc (gl2ps_print): Check for valid FILE pointer from octave_popen. * oct-parse.in.yy (parse_fcn_file): Check for valid FILE pointer from fopen.
author John W. Eaton <jwe@octave.org>
date Fri, 13 Feb 2015 16:46:21 -0500
parents 59ad278cfb98
children ce0a1bd0cd47
comparison
equal deleted inserted replaced
19742:1170c849952b 19743:5cfb3ccbf24a
25 25
26 #ifdef HAVE_CONFIG_H 26 #ifdef HAVE_CONFIG_H
27 #include <config.h> 27 #include <config.h>
28 #endif 28 #endif
29 29
30 #include "oct-locbuf.h"
31 #include "unwind-prot.h"
32
30 #include "defun-dld.h" 33 #include "defun-dld.h"
31 #include "gl-render.h" 34 #include "gl-render.h"
32 #include "gl2ps-renderer.h" 35 #include "gl2ps-renderer.h"
33 #include "graphics.h" 36 #include "graphics.h"
34
35 #include "gripes.h" 37 #include "gripes.h"
36 38
37 #ifdef HAVE_OSMESA 39 #ifdef HAVE_OSMESA
38 #include "GL/osmesa.h" 40 #include "GL/osmesa.h"
41
42 static void
43 close_fcn (FILE *f)
44 {
45 gnulib::fclose (f);
46 }
39 #endif 47 #endif
40 48
41 DEFUN_DLD(__osmesa_print__, args, , 49 DEFUN_DLD(__osmesa_print__, args, ,
42 "-*- texinfo -*-\n\ 50 "-*- texinfo -*-\n\
43 @deftypefn {Loadable Function} __osmesa_print__ (@var{h}, @var{file}, @var{term})\n\ 51 @deftypefn {Loadable Function} __osmesa_print__ (@var{h}, @var{file}, @var{term})\n\
118 error ("__osmesa_print__: OSMesaCreateContext failed!\n"); 126 error ("__osmesa_print__: OSMesaCreateContext failed!\n");
119 return retval; 127 return retval;
120 } 128 }
121 129
122 // Allocate the image buffer 130 // Allocate the image buffer
123 buffer = malloc (Width * Height * 4 * sizeof (GLubyte)); 131 OCTAVE_LOCAL_BUFFER (GLubyte, 4 * Width * Height, buffer);
124 if (! buffer)
125 {
126 error ("__osmesa_print__: Alloc image buffer failed!\n");
127 return retval;
128 }
129 132
130 // Bind the buffer to the context and make it current 133 // Bind the buffer to the context and make it current
131 if (! OSMesaMakeCurrent (ctx, buffer, GL_UNSIGNED_BYTE, Width, Height)) 134 if (! OSMesaMakeCurrent (ctx, buffer, GL_UNSIGNED_BYTE, Width, Height))
132 { 135 {
133 error ("__osmesa_print__: OSMesaMakeCurrent failed!\n"); 136 error ("__osmesa_print__: OSMesaMakeCurrent failed!\n");
166 gl2ps_print (fobj, cmd, term); 169 gl2ps_print (fobj, cmd, term);
167 } 170 }
168 else 171 else
169 { 172 {
170 // write gl2ps output directly to file 173 // write gl2ps output directly to file
171 FILE *filep; 174 FILE *filep = fopen (file.c_str (), "w");
172 filep = fopen (file.c_str (), "w"); 175
173 if (filep) 176 if (filep)
174 { 177 {
178 unwind_protect frame;
179
180 frame.add_fcn (close_fcn, filep);
181
175 glps_renderer rend (filep, term); 182 glps_renderer rend (filep, term);
176 rend.draw (fobj, ""); 183 rend.draw (fobj, "");
177 184
178 // Make sure buffered commands are finished!!! 185 // Make sure buffered commands are finished!!!
179 glFinish (); 186 glFinish ();
180 fclose (filep);
181 } 187 }
182 else 188 else
183 error ("__osmesa_print__: Couldn't create file \"%s\"", file.c_str ()); 189 error ("__osmesa_print__: Couldn't create file \"%s\"", file.c_str ());
184 } 190 }
185 } 191 }
220 226
221 // restore visibility if necessary 227 // restore visibility if necessary
222 if (v) 228 if (v)
223 fp.set_visible ("on"); 229 fp.set_visible ("on");
224 230
225 free (buffer);
226 OSMesaDestroyContext (ctx); 231 OSMesaDestroyContext (ctx);
227 232
228 #endif 233 #endif
229 return retval; 234 return retval;
230 } 235 }