Remove memory leaks
[scilab.git] / scilab / modules / output_stream / src / c / sciprint_full.c
index 0ceccce..e60899a 100644 (file)
-/*-----------------------------------------------------------------------------------*/\r
-/* Francois VOGEL  sciprint_full function */\r
-/*-----------------------------------------------------------------------------------*/\r
-/* sciprint geared towards long strings (>MAXPRINTF) */\r
-/* the long string is splitted in elements of length equal to the number of columns  */\r
-/* from lines()                                                                      */\r
-/*-----------------------------------------------------------------------------------*/\r
-#include <string.h>\r
-#include "sciprint_full.h"\r
-#include "sciprint.h"\r
-#include "MALLOC.h"\r
-#include "localization.h"\r
-#include "../../shell/includes/scilines.h"\r
-/*-----------------------------------------------------------------------------------*/\r
-/* MAXCHARSSCIPRINT_FULL is for sciprint_full - more than this gets truncated */\r
-#define MAXCHARSSCIPRINT_FULL 5000  \r
-/*-----------------------------------------------------------------------------------*/\r
-void sciprint_full(char *fmt,...) \r
-{\r
-       integer lstr;\r
-       va_list ap;\r
-       char *s_buf=NULL;\r
-       char *split_s_buf=NULL;\r
-       int count=0;\r
-       int p_s=0;\r
-       static integer colwidth;\r
-       char *MSG;\r
-\r
-       s_buf=MALLOC(sizeof(char)*(MAXCHARSSCIPRINT_FULL+1));\r
-       if (s_buf == (char *) 0)\r
-       {\r
-               sciprint(_("sciprint_full : No more memory.\n"));\r
-               return;\r
-       }\r
-\r
-       /* number of columns as set by command lines() */\r
-       colwidth = getLinesSize();\r
-       /* clamp to a minimum: value is arbitrary */\r
-       if (colwidth < 20) {colwidth=20;}\r
-       /* clamp to a maximum: value is selected so that each line fits in a single console line */\r
-       /* this is needed because computation of the lines() value in ON_WND_TEXT_WM_SIZE is not */\r
-       /* consistent with the limit before a carriage return occurs in TextPutStr - this latter */\r
-       /* limit uses lptw->ScreenSize, which is set to x=120,y=80 at init and apparently never  */\r
-       /* changed on window resizing                                                            */\r
-       if (colwidth > 109) {colwidth=109;}\r
-\r
-       split_s_buf=MALLOC(sizeof(char)*(colwidth+1));\r
-       if (split_s_buf == (char *) 0)\r
-       {\r
-               sciprint(_("sciprint_full : No more memory.\n"));\r
-               return;\r
-       }\r
-\r
-       va_start(ap,fmt);\r
-\r
-#if defined(linux) || defined(_MSC_VER)\r
-       count = vsnprintf (s_buf,MAXCHARSSCIPRINT_FULL-1, fmt, ap );\r
-       if (count == -1)\r
-       {\r
-               s_buf[MAXCHARSSCIPRINT_FULL-1]='\0';\r
-       }\r
-#else\r
-       (void )vsprintf(s_buf, fmt, ap );\r
-#endif\r
-\r
-       va_end(ap);\r
-\r
-       lstr=(integer)strlen(s_buf);\r
-\r
-       if (lstr<colwidth)\r
-       {\r
-               sciprint("%s",s_buf);\r
-       } \r
-       else \r
-       {\r
-               strncpy(split_s_buf,s_buf+p_s,colwidth-1);\r
-               split_s_buf[colwidth]='\0';\r
-               p_s=p_s+colwidth-1;\r
-               sciprint("%s",split_s_buf);\r
-               sciprint("\n");\r
-               while (p_s+colwidth-1<(int)lstr) \r
-               {\r
-                       strncpy(split_s_buf,s_buf+p_s,colwidth-1);\r
-                       split_s_buf[colwidth]='\0';\r
-                       p_s=p_s+colwidth-1;\r
-                       MSG=_("  (cont'd) %s");\r
-                       if (MSG)\r
-                       {\r
-                               sciprint(MSG,split_s_buf);\r
-                               sciprint("\n");\r
-                       }\r
-                       if (MSG) {FREE(MSG);MSG=NULL;}\r
-               }\r
-               strncpy(split_s_buf,s_buf+p_s,lstr-p_s);\r
-               split_s_buf[lstr-p_s]='\0';\r
-               MSG=_("     (end) %s");\r
-               if (MSG)\r
-               {\r
-                       sciprint(MSG,split_s_buf);\r
-                       sciprint("\n");\r
-               }\r
-               if (MSG) {FREE(MSG);MSG=NULL;}\r
-       }\r
-\r
-       if (s_buf){FREE(s_buf);s_buf=NULL;}\r
-       if (split_s_buf){FREE(split_s_buf);split_s_buf=NULL;}\r
-\r
-}\r
-/*-----------------------------------------------------------------------------------*/ \r
+/*
+ * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+ * Copyright (C) Francois Vogel
+ * Copyright (C) 2008-2008 - INRIA - Bruno JOFRET
+ *
+ * This file must be used under the terms of the CeCILL.
+ * This source file is licensed as described in the file COPYING, which
+ * you should have received as part of this distribution.  The terms
+ * are also available at
+ * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
+ *
+ */
+
+/*--------------------------------------------------------------------------*/
+/* sciprint_full function */
+/*--------------------------------------------------------------------------*/
+/* sciprint geared towards long strings (>MAXPRINTF) */
+/* the long string is splitted in elements of length equal to the number of columns  */
+/* from lines()                                                                      */
+/*--------------------------------------------------------------------------*/
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "sciprint_full.h"
+#include "sciprint.h"
+#include "MALLOC.h"
+#include "localization.h"
+#include "scilines.h"
+/*--------------------------------------------------------------------------*/
+#ifdef _MSC_VER
+#define vsnprintf _vsnprintf
+#endif
+/*--------------------------------------------------------------------------*/
+/* MAXCHARSSCIPRINT_FULL is for sciprint_full - more than this gets truncated */
+#define MAXCHARSSCIPRINT_FULL 5000
+/*--------------------------------------------------------------------------*/
+void sciprint_full(char *fmt, ...)
+{
+    int lstr;
+    va_list ap;
+    char *s_buf = NULL;
+    char *split_s_buf = NULL;
+    int count = 0;
+    int p_s = 0;
+    static int colwidth;
+
+    s_buf = MALLOC(sizeof(char) * (MAXCHARSSCIPRINT_FULL + 1));
+    if (s_buf == (char *) 0)
+    {
+        sciprint(_("%s: No more memory.\n"), "sciprint_full");
+        return;
+    }
+
+    /* number of columns as set by command lines() */
+    colwidth = getColumnsSize();
+
+    split_s_buf = MALLOC(sizeof(char) * (colwidth + 1));
+    if (split_s_buf == (char *) 0)
+    {
+        sciprint(_("%s: No more memory.\n"), "sciprint_full");
+        FREE(s_buf);
+        return;
+    }
+
+    va_start(ap, fmt);
+
+#if defined(linux) || defined(_MSC_VER)
+    count = vsnprintf (s_buf, MAXCHARSSCIPRINT_FULL - 1, fmt, ap );
+    if (count == -1)
+    {
+        s_buf[MAXCHARSSCIPRINT_FULL - 1] = '\0';
+    }
+#else
+    (void )vsprintf(s_buf, fmt, ap );
+#endif
+
+    va_end(ap);
+
+    lstr = (int) strlen(s_buf);
+
+    if (lstr < colwidth)
+    {
+        sciprint("%s", s_buf);
+    }
+    else
+    {
+        strncpy(split_s_buf, s_buf + p_s, colwidth - 1);
+        split_s_buf[colwidth] = '\0';
+        p_s = p_s + colwidth - 1;
+        sciprint("%s", split_s_buf);
+        sciprint("\n");
+        while (p_s + colwidth - 1 < (int)lstr)
+        {
+            strncpy(split_s_buf, s_buf + p_s, colwidth - 1);
+            split_s_buf[colwidth] = '\0';
+            p_s = p_s + colwidth - 1;
+            sciprint(_("  (cont'd) %s\n"), split_s_buf);
+        }
+        strncpy(split_s_buf, s_buf + p_s, lstr - p_s);
+        split_s_buf[lstr - p_s] = '\0';
+        sciprint(_("     (end) %s\n"), split_s_buf);
+    }
+
+    if (s_buf)
+    {
+        FREE(s_buf);
+        s_buf = NULL;
+    }
+    if (split_s_buf)
+    {
+        FREE(split_s_buf);
+        split_s_buf = NULL;
+    }
+
+}
+/*--------------------------------------------------------------------------*/