Remove memory leaks
[scilab.git] / scilab / modules / output_stream / src / c / basout.c
1 /*
2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) INRIA - Allan CORNET
4 * Copyright (C) DIGITEO - 2009 - Allan CORNET
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 #include <stdio.h>
15 #include <string.h>
16 #include <stdlib.h>
17 #include "stack-def.h"
18 #include "basout.h"
19 #include "MALLOC.h"
20 #include "diary.h"
21 #include "sciprint.h"
22 #include "charEncoding.h"
23 #include "more.h"
24 #include "scilines.h"
25 /*--------------------------------------------------------------------------*/ 
26 extern int C2F(basouttofile)(); /* fortran subroutine */
27 /*--------------------------------------------------------------------------*/ 
28 int C2F(basout)(int *io, int *lunit, char *string,long int nbcharacters)
29 {
30         /* bug 3831 */
31         if (string)
32         {
33                 int i = 0;
34                 for (i = 0; i < nbcharacters - 1; i++) 
35                 {
36                         if (string[i] == 0) string[i] = ' ';
37                 }
38         }
39
40         if (*lunit == C2F(iop).wte)
41         {
42                 /* Display on the standard output */
43
44                 /* We haven't called this function before ... Then we call it and 
45                 store the result once for all because it won't change 
46                 */
47                 *io = 0;
48                 if (C2F(iop).lct[0] == -1) { return 0; }
49                 if (getLinesSize() > 0) 
50                 {
51                         /* Management of the page numbering (pagination in French) */
52                         if (C2F(iop).lct[0] + 3 > getLinesSize())
53                         {
54                                 int ich = 0;
55
56                                 /* Number of max line reached, management of the 'more' */
57                                 C2F(iop).lct[0] = 0;
58
59                                 ich = linesmore();
60
61                                 if (ich == 1) 
62                                 {
63                                         C2F(iop).lct[0] = -1;
64                                         *io = -1;
65                                         return 0;
66                                 }
67                         } 
68                         else
69                         {
70                                 ++C2F(iop).lct[0];
71                         }
72                 }
73
74                 if (string)
75                 {
76                         if (nbcharacters > 1)
77                         {
78                                 /* on linux , q=[] crashs with previous version 
79                                 in printf.f line 102 
80                                 call basout(io,lunit,'     []')
81                                 if we do basout(io,lunit,'     []',7) it works ...
82                                 temp workaround , we returns to old version with an allocation
83                                 */
84                                 char *buffer = (char *)MALLOC(sizeof(char)*(nbcharacters+1));
85                                 if (buffer)
86                                 {
87                                         strncpy(buffer,string,nbcharacters);
88                                         buffer[nbcharacters]='\0';
89                                         sciprint("%s\n",buffer);
90                                         FREE(buffer); buffer = NULL;
91                                 }
92                                 else
93                                 {
94                                         sciprint("\n");
95                                 }
96                         }
97                         else if (nbcharacters == 1)
98                         {
99                                 sciprint("%c\n", string[0]);
100                         }
101                         else
102                         {
103                                 sciprint("\n");
104                         }
105                 }
106                 else sciprint("\n");
107         } 
108         else
109         {
110                 if (*lunit == -2)
111                 {
112                         wchar_t *wcBuffer = NULL;
113
114                         string[nbcharacters] = '\0';
115
116                         /* remove blanks at end of line */
117                         if (*lunit == -2)
118                         {
119                                 int i = 0;
120                                 int len = (int) strlen(string) - 2;
121                                 for (i = len; i >= 0; i--)
122                                 {
123                                         if (string[i] == ' ') string[i] = '\0';
124                                         else break;
125                                 }
126                         }
127
128                         wcBuffer = to_wide_string(string);
129                         if (wcBuffer)
130                         {
131                                 if (wcscmp(wcBuffer,L"")) diaryWriteln(wcBuffer, TRUE);
132                                 FREE(wcBuffer);
133                                 wcBuffer = NULL;
134                         }
135                 }
136                 else
137                 {
138                         C2F(basouttofile)(lunit, string,nbcharacters);
139                 }
140         }
141         return 0;
142
143 /*--------------------------------------------------------------------------*/ 
144