GraphicsMagick: Provide a better emulation of C'99 snprintf() fo...

GraphicsMagick Commits <[email protected]>
Newsgroups gmane.comp.video.graphicsmagick.cvs
Message-ID <mailman.16730.1668303466.1350.graphicsmagick-commit@lists.sourceforge.net>
changeset 99cd22c35be0 in /hg/GraphicsMagick
details: http://hg.GraphicsMagick.org/hg/GraphicsMagick?cmd=changeset;node=99cd22c35be0
summary: Provide a better emulation of C'99 snprintf() for MSVC < 2015.

diffstat:

 ChangeLog          |   3 ++
 magick/nt_base.c   |  58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 magick/nt_base.h   |   8 +-----
 www/Changelog.html |   2 +
 4 files changed, 65 insertions(+), 6 deletions(-)

diffs (111 lines):

diff -r 064641820cb8 -r 99cd22c35be0 ChangeLog
--- a/ChangeLog	Sat Nov 12 18:10:28 2022 -0600
+++ b/ChangeLog	Sat Nov 12 19:37:35 2022 -0600
@@ -1,5 +1,8 @@
 2022-11-12  Bob Friesenhahn  <[email protected]>
 
+	* magick/nt_base.c (NTsnprintf): New function intended to emulate
+	C'99 snprintf for MSVC older than 2015.
+
 	* magick/nt_base.h (snprintf): For MSVC older than 2015, fall back
 	to using sprintf for the moment.
 
diff -r 064641820cb8 -r 99cd22c35be0 magick/nt_base.c
--- a/magick/nt_base.c	Sat Nov 12 18:10:28 2022 -0600
+++ b/magick/nt_base.c	Sat Nov 12 19:37:35 2022 -0600
@@ -2794,4 +2794,62 @@
   assert(entry != (DIR *) NULL);
   return(0);
 }
+
+/*
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%   N T s n p r i n t f                                                         %
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+%   Method NTsnprintf emulates C'99 snprintf for MSVC older than Visual
+%   studio 2015
+%
+%  The format of the NTsnprintf method is:
+%
+%      int NTsnprintf(char* str, size_t size, const char* format, ...)
+%
+%  A description of each parameter follows:
+%
+%    o str: buffer to write into
+%
+%    o size: str buffer size
+%
+%    o format: printf style format string
+%
+*/
+#if defined(_VISUALC_) && (_MSC_VER < 1900)
+static int _NTvsnprintf(char* str, size_t size, const char* format, va_list ap)
+{
+  int count = -1;
+
+  if (size != 0)
+#if _MSC_VER <= 1310
+    count = _vsnprintf(str, size, format, ap);
+#else
+    count = _vsnprintf_s(str, size, _TRUNCATE, format, ap);
 #endif
+  if (count == -1)
+    count = _vscprintf(format, ap);
+
+  return count;
+}
+#define vsnprintf _NTvsnprintf
+MagickExport int NTsnprintf(char* str, size_t size, const char* format, ...)
+{
+  int count;
+  va_list ap;
+
+  va_start(ap, format);
+  count = vsnprintf(str, size, format, ap);
+  va_end(ap);
+
+  return count;
+}
+#endif
+
+#endif
diff -r 064641820cb8 -r 99cd22c35be0 magick/nt_base.h
--- a/magick/nt_base.h	Sat Nov 12 18:10:28 2022 -0600
+++ b/magick/nt_base.h	Sat Nov 12 19:37:35 2022 -0600
@@ -215,15 +215,11 @@
 
 /*
   MSVC does not have snprintf (a C'99 feature) until Visual Studio 2015.
-
-  It is possible to cobble together an actual working equivalent using
-  _vsnprintf(), _vsnprintf_s(), and _vscprintf() but this quick hack just uses
-  sprintf since current snprintf usages were direct replacements for sprintf.
-  A proper implementation can come later.
  */
 #if defined(_VISUALC_) && (_MSC_VER < 1900)
 #undef snprintf
-#define snprintf(str,size,format,...) sprintf(str,format,__VA_ARGS__)
+extern MagickExport int NTsnprintf(char* str, size_t size, const char* format, ...);
+#define snprintf(str,size,format,...) NTsnprintf(str,sizeformat,__VA_ARGS__)
 #endif
 
 /*
diff -r 064641820cb8 -r 99cd22c35be0 www/Changelog.html
--- a/www/Changelog.html	Sat Nov 12 18:10:28 2022 -0600
+++ b/www/Changelog.html	Sat Nov 12 19:37:35 2022 -0600
@@ -40,6 +40,8 @@
 <p>2022-11-12  Bob Friesenhahn  &lt;<a class="reference external" href="mailto:bfriesen&#37;&#52;&#48;simple&#46;dallas&#46;tx&#46;us">bfriesen<span>&#64;</span>simple<span>&#46;</span>dallas<span>&#46;</span>tx<span>&#46;</span>us</a>&gt;</p>
 <blockquote>
 <ul class="simple">
+<li><p>magick/nt_base.c (NTsnprintf): New function intended to emulate
+C'99 snprintf for MSVC older than 2015.</p></li>
 <li><p>magick/nt_base.h (snprintf): For MSVC older than 2015, fall back
 to using sprintf for the moment.</p></li>
 <li><p>coders/msl.c (ProcessMSLScript): Handle parser creation failure.</p></li>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.