5a6d9352a10762c692a780777a0ec432c5ebf188
[scilab.git] / scilab / modules / output_stream / src / c / sciprint_full.c
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) Francois Vogel
4  * Copyright (C) 2008-2008 - INRIA - Bruno JOFRET
5  *
6  * This file must be used under the terms of the CeCILL.
7  * This source file is licensed as described in the file COPYING, which
8  * you should have received as part of this distribution.  The terms
9  * are also available at
10  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
11  *
12  */
13
14 /*--------------------------------------------------------------------------*/
15 /* sciprint_full function */
16 /*--------------------------------------------------------------------------*/
17 /* sciprint geared towards long strings (>MAXPRINTF) */
18 /* the long string is splitted in elements of length equal to the number of columns  */
19 /* from lines()                                                                      */
20 /*--------------------------------------------------------------------------*/
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include "sciprint_full.h"
25 #include "sciprint.h"
26 #include "MALLOC.h"
27 #include "localization.h"
28 #include "scilines.h"
29 /*--------------------------------------------------------------------------*/
30 #ifdef _MSC_VER
31 #define vsnprintf _vsnprintf
32 #endif
33 /*--------------------------------------------------------------------------*/
34 /* MAXCHARSSCIPRINT_FULL is for sciprint_full - more than this gets truncated */
35 #define MAXCHARSSCIPRINT_FULL 5000
36 /*--------------------------------------------------------------------------*/
37 void sciprint_full(char *fmt,...)
38 {
39         int lstr;
40         va_list ap;
41         char *s_buf=NULL;
42         char *split_s_buf=NULL;
43         int count=0;
44         int p_s=0;
45         static int colwidth;
46
47         s_buf=MALLOC(sizeof(char)*(MAXCHARSSCIPRINT_FULL+1));
48         if (s_buf == (char *) 0)
49         {
50                 sciprint(_("%s: No more memory.\n"),"sciprint_full");
51                 return;
52         }
53
54         /* number of columns as set by command lines() */
55         colwidth = getColumnsSize();
56
57         split_s_buf=MALLOC(sizeof(char)*(colwidth+1));
58         if (split_s_buf == (char *) 0)
59         {
60                 sciprint(_("%s: No more memory.\n"),"sciprint_full");
61                 return;
62         }
63
64         va_start(ap,fmt);
65
66 #if defined(linux) || defined(_MSC_VER)
67         count = vsnprintf (s_buf,MAXCHARSSCIPRINT_FULL-1, fmt, ap );
68         if (count == -1)
69         {
70                 s_buf[MAXCHARSSCIPRINT_FULL-1]='\0';
71         }
72 #else
73         (void )vsprintf(s_buf, fmt, ap );
74 #endif
75
76         va_end(ap);
77
78         lstr=(int) strlen(s_buf);
79
80         if (lstr<colwidth)
81         {
82                 sciprint("%s",s_buf);
83         }
84         else
85         {
86                 strncpy(split_s_buf,s_buf+p_s,colwidth-1);
87                 split_s_buf[colwidth]='\0';
88                 p_s=p_s+colwidth-1;
89                 sciprint("%s",split_s_buf);
90                 sciprint("\n");
91                 while (p_s+colwidth-1<(int)lstr)
92                 {
93                         strncpy(split_s_buf,s_buf+p_s,colwidth-1);
94                         split_s_buf[colwidth]='\0';
95                         p_s=p_s+colwidth-1;
96                         sciprint(_("  (cont'd) %s\n"),split_s_buf);
97                 }
98                 strncpy(split_s_buf,s_buf+p_s,lstr-p_s);
99                 split_s_buf[lstr-p_s]='\0';
100                 sciprint(_("     (end) %s\n"),split_s_buf);
101         }
102
103         if (s_buf){FREE(s_buf);s_buf=NULL;}
104         if (split_s_buf){FREE(split_s_buf);split_s_buf=NULL;}
105
106 }
107 /*--------------------------------------------------------------------------*/