* Bug 16269 fixed: scalar struct was poorly displayed
[scilab.git] / scilab / modules / overloading / macros / %st_p.sci
1 8// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) INRIA
3 // Copyright (C) 2010 - DIGITEO - Vincent COUVERT <vincent.couvert@scilab.org>
4 // Copyright (C) 2012 - 2016 - Scilab Enterprises
5 // Copyright (C) 2018 - St├ęphane MOTTELET
6 // Copyright (C) 2019-2020 - Samuel GOUGEON
7 //
8 // This file is hereby licensed under the terms of the GNU GPL v2.0,
9 // pursuant to article 5.3.4 of the CeCILL v.2.1.
10 // This file was originally licensed under the terms of the CeCILL v2.1,
11 // and continues to be available under such terms.
12 // For more information, see the COPYING file which you should have received
13 // along with this program.
14
15 function %st_p(s)
16     recursive = 0
17     tmp = xmlGetValues("//general/body/environment","container_disp_max_depth")
18     maxDisplayDepth = evstr(tmp)
19     consoleWidth = lines()(1)
20     //st_p_compacity = "";         // "" vspacing of blocks
21     st_p_compacity = [];      // for compact display
22     t = %st_p_inc(s, "st")
23     mprintf("  %s\n", t);
24 endfunction
25
26 // -------------------------------------------------------------------
27
28 function t = %st_p_inc(s, parentType)
29     // Can be called with s = struct | Tlist | list
30
31     ll    = lines()
32     t     = []
33     indentFields = "    "
34     if parentType=="list" then
35         eq = "= "
36     else
37         eq = " = "
38     end
39
40     if  typeof(s)=="st"
41         // No fields
42         if isempty(fieldnames(s)) then
43             t = msprintf(_("%s struct with no field"), ..
44                          strcat(msprintf("%d\n",size(s)'), "x"));
45             return
46         end
47
48         multi = size(s,"*")
49
50         // 0x0 struct with fields
51         if multi == 0 then
52             t = _("0x0 struct with fields:")
53             for field = fieldnames(s)'
54                 t = [t ; "   "+field]
55             end
56             return
57         end
58
59         // axb struct where a<>0 & b<>0
60         if multi > 1 | recursive > maxDisplayDepth then
61             if ~recursive
62                 t = msprintf(_("%s struct with fields:"), ..
63                      strcat(msprintf("%d\n", size(s)'), "x"));
64             end
65             tmp = sci2exp(fieldnames(s)', consoleWidth-10)
66             tmp = strsubst(strsubst(tmp, """""", """"), ",", ", ")
67             t = [t ; tmp]
68             return
69         end
70     end
71
72     recursive0 = recursive
73     if type(s)==15 then
74         Fields = 1:length(s)
75     else
76         Fields = fieldnames(s)'
77     end
78
79     // MAIN LOOP
80     // ---------
81     for field = Fields
82         sep = ": "      //   field_name<sep> ...
83         if type(s)==15 then
84             fieldn = msprintf("(%d) ", field)
85         else
86             fieldn = field
87         end
88         clear value
89         value = s(field)
90         if isdef("value","l") then
91             tp = typeof(value)
92         else
93             tp = "void"
94         end
95
96         if tp=="void" then
97             str = _("(void)")
98             sep = eq
99
100         elseif tp == "st" then
101             recursive = recursive + 1
102             str = %st_p_inc(value, "st")
103
104         elseif tp == "implicitlist"
105             str = sci2exp(value)
106             sep = eq
107
108         elseif tp == "function"
109             [out,inp,?] = string(value)
110             if inp==[], inp = "", end
111             if out==[], out = "", end
112             p = macr2tree(value)
113             str = p.name+"("+strcat(inp,",")+") => ["+strcat(out,",")+"] ";
114             str = str + msprintf(_("(%d lines)"),p.nblines)
115             txt = fieldn + ": " + str
116
117         elseif tp == "rational"
118             str = strcat(msprintf("%d\n", size(value)'), "x")
119             tmp = _("rational")
120             if ~isreal(value,0)
121                 tmp = _("complex rational")
122             end
123             str = msprintf(_("[%s %s] of %s"), str, tmp, varn(value));
124
125         elseif tp == "ce"
126             if length(value)==0 then
127                 str = "{}"
128                 txt = fieldn + eq + str
129             else
130                 str = strcat(msprintf("%d\n", size(value)'), "x")
131                 str = msprintf("[%s %s]", str, _("cell"));
132                 txt = fieldn + ": " + str
133             end
134
135         elseif type(value)==15
136             if length(value)==0 then
137                 str = _(" empty list()")
138             else
139                 if recursive < maxDisplayDepth
140                     recursive = recursive + 1
141                     tmp = %st_p_inc(value, "list")
142                     str = ["list:" ; tmp]
143                 else
144                     str = msprintf("list with %d elements.\n", length(value))
145                 end
146             end
147
148         elseif or(type(value)==[16 17])
149             // Tlists or Mlists
150             Tfields = fieldnames(value);
151             sz = []
152             try
153                 sz = size(value)
154             end
155             signature = tp
156             if length(sz) > 1
157                 signature = strcat(msprintf("%d\n", sz'), "x") + " " + tp
158             end
159             listType = "tlist"
160             if type(value)==17
161                 listType = "mlist"
162             end
163             if Tfields==[]
164                 str = msprintf(_("[%s] %s without field."), signature, listType);
165             else
166                 str = msprintf(_("[%s] %s with fields:"), signature, listType);
167                 if recursive < maxDisplayDepth
168                     recursive = recursive + 1
169                     tmp = %st_p_inc(value, "mtlist")
170                     str = [str ; tmp]
171                 else
172                     tmp = sci2exp(Tfields', consoleWidth-10)
173                     tmp = strsubst(strsubst(tmp, """", ""), ",", ", ")
174                     str = [str ; tmp]
175                 end
176             end
177
178         elseif type(value)==14  // Library
179             tmp = string(value)
180             p = tmp(1)
181             libname = xmlGetValues("/scilablib","name",p + "lib")
182             str = msprintf("%s library with %d functions @ %s", ..
183                            libname, size(tmp,1)-1, p)
184
185         elseif type(value)> 10 then
186             str = tp
187
188         else
189             sz = size(value)
190             // If number of elements in value is greater than ll(1) (current
191             // page width) then we do not call sci2exp because the returned
192             // value will be ignored at line 68: size(str,"*")==1
193             // Note that ll(1)/2 elements could be the max because of colon,
194             //  semi-colon and brackets added between elements by sci2exp
195             if sz(1) <= 1 & type(value) <> 9 & prod(sz) < ll(1) then
196                 // This line can avoid some memory issues when
197                 //  field contains a big matrix
198                 str = sci2exp(value, ll(1))
199                 sep = eq
200             else
201                 str = "[" + strcat(msprintf("%d\n",sz'), "x") + " " + tp + "]"
202             end
203         end
204         // ---------------------------
205         if size(str,"*") == 1 & ..
206                and(tp <> ["st" "function" "rational" "ce"])
207             txt = fieldn + sep + str
208
209         elseif and(tp <> ["function" "ce"])
210             if recursive
211                 txt0 = indentFields    // indentation for fields list
212                 //txt0 = field + "."  // to display the chain of parent fields
213             else
214                 txt0 = fieldn
215             end
216             if tp == "st"
217                 txt = fieldn + ": "
218                 if size(value,"*")==0
219                     txt = txt + _("[0x0 struct] with no field")
220                 else
221                     sv = size(value)'
222                     if prod(sv)==1 then
223                         txt = txt + "struct with fields:"
224                     else
225                         txt = txt + msprintf(_("[%s struct] with fields:"), ..
226                                   strcat(msprintf("%d\n", sv), "x"));
227                     end
228                     txt = [txt ; txt0 + str]
229                     if stripblanks(t($)) <> ""
230                         txt = [st_p_compacity ; txt]
231                     end
232                 end
233
234             elseif or(type(value)==[15 16 17])
235                 txt = fieldn + ": " + str(1)
236                 if size(str,1)>1
237                     txt = [txt ; indentFields + str(2:$)]
238                 end
239                 if stripblanks(t($)) <> ""
240                     txt = [st_p_compacity ; txt]
241                 end
242
243             else
244                 txt = txt0 + ..
245                   ": " + "[" + strcat(msprintf("%d\n",size(value)'), "x") ..
246                        + " " + tp + "]"
247             end
248         end
249         t = [t ; txt]
250         recursive = recursive0
251     end
252     // Display a blank line after each field that is a non-empty
253     // struct or tlist:
254     if recursive & stripblanks(t($)) <> ""
255         t = [t ; st_p_compacity]
256     end
257 endfunction