Hi, Alan,

On Dec 16, 2010, at 6:54 PM, Alan W. Irwin wrote:

> Here is what I propose instead of this temporary measure.  We go with
> a backward-incompatible API change (and associated soname bump to
> force everybody to recompile) of
> 
> c_plgfnam( char *fnam ); ==> c_plgfnam( char *fnam, PLINT n);
> 
> where n is the size of the buffer that has been allocated. 
> Internally, c_plgfnam will use n for the strncpy call as well as
> figuring out the last byte to zero.
> 
> Note, the other two PLplot functions (plgver and plgdev) that return
> character strings have recommendations in the documentation to
> allocate 80-byte buffers to contain the version string and device
> name.

I notice that all of these functions currently have a null return type.  One 
approach I've seen elsewhere, is to have functions like this return an int 
indicating how many bytes were copied into the buffer if the pointer given is 
non-null *or*, if the given pointer is null, how many more bytes are needed to 
fill the buffer.

This allows the client code to determine the buffer size needed, (re-)allocate 
the buffer dynamically, then fill the buffer...

bufsize = plgver(NULL, 0);
buf = (char *)malloc(bufsize+1);
plgver(buf, bufsize);
buf[bufsize] = '\0';

// Note how this can be used to determine
// whether a buffer is already big enough

extra = plgfnam(NULL, bufsize);
if(extra > 0) {
  buf = (char *)realloc(buf, bufsize+extra+1);
}
bufsize += extra;

// if extra < 0, bufsize will be smaller
// than size of region allocated
plgfnam(buf, bufsize);
buf[bufsize] = '\0';

Just an idea,
Dave


------------------------------------------------------------------------------
Lotusphere 2011
Register now for Lotusphere 2011 and learn how
to connect the dots, take your collaborative environment
to the next level, and enter the era of Social Business.
http://p.sf.net/sfu/lotusphere-d2d
_______________________________________________
Plplot-devel mailing list
Plplot-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/plplot-devel

Reply via email to