summaryrefslogtreecommitdiff
path: root/modules/compatibility_functions/macros/char.sci
blob: a28821d5a01d264fed3f2a849778a144426a8432 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) 2004-2006 - INRIA - Farid BELAHCENE
//
// 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.1-en.txt

function y=char(varargin)
    // Case : One input argument
    // Input : a cell of characters arrays
    // Output : vector (column) of strings,
    // This function returns a vector in  which the rows are the components of the cell characters arrays
    //
    // Input : an array of ascii codes
    // Output :a characters array corresponding into ascii codes
    // If dims input =[n1,n2,n3,n4,....],then returned a value has the dims=[n1,n3,n4,...],
    // returned value have same size as input value instead of second dims !
    //
    // Case : more than one input argument : y=char(st1,st2,st3,....)
    // Inputs must be string : st1,st2,st3,...
    // Outputs : vector (column) of strings
    // This function returns a vector in  which the rows are st1, st2,st3,...,
    // the length of all strings sti is completed by blanks,in order to have the same length for each sti.

    rhs=argn(2)
    y=[]
    // One input argument
    if rhs==1 then
        if typeof(varargin(1))=="ce" then // input argument is a cell of characters arrays
            c=varargin(1)
            lst=c.entries
            if size(c)==1 then // cell contains one element
                if typeof(lst)=="hypermat" then // cell elements are characters arrays
                    if type(lst.entries)==10 then
                        t=emptystr(size(lst,1),1)
                        lst=matrix(lst,[size(lst,1),size(lst,"*")/size(lst,1)])
                        for j=1:size(lst,2)
                            t=t+lst(:,j)
                        end
                        y=[y;t]
                    else
                        error(msprintf(gettext("%s: Wrong type for input argument: Cell elements must be character arrays.\n"),"char"));
                    end
                elseif type(lst)==10 then // cell elements are a string matrix
                    t=emptystr(size(lst,1),1)
                    for k=1:size(lst,2)
                        t=t+lst(:,k)
                    end
                    y=[y,t]
                else
                    error(msprintf(gettext("%s: Wrong type for input argument: Cell elements must be character arrays.\n"),"char"));
                end
            else // cell contains more than one element
                for i=1:size(lst)
                    if typeof(lst(i))=="hypermat" then
                        if type(lst(i).entries)==10 then
                            ctemp=lst(i)
                            t=emptystr(size(ctemp,1),1)
                            ctemp=matrix(ctemp,[size(ctemp,1),size(ctemp,"*")/size(ctemp,1)])
                            for j=1:size(ctemp,2)
                                t=t+ctemp(:,j)
                            end
                            y=[y;t]
                        else
                            error(msprintf(gettext("%s: Wrong type for input argument: Cell elements must be character arrays.\n"),"char"));
                        end
                    elseif type(lst(i))==10 then
                        t=emptystr(size(lst(i),1),1)
                        for k=1:size(lst(i),2)
                            t=t+lst(i)(:,k)
                        end
                        y=[y;t]
                    else
                        error(msprintf(gettext("%s: Wrong type for input argument: Cell elements must be character arrays.\n"),"char"));
                    end
                end
            end
            // Add blank at the length of strings
            bl=" "
            maxstr=max(length(y))
            for i=1:size(y,"*")
                nb_bl=maxstr-length(y(i))
                if nb_bl>0 then
                    y(i)=y(i)+part(bl,ones(1,nb_bl))
                end
            end
        elseif type(varargin(1))==1|type(varargin(1))==8 then // Input is a matrix of integers (or reals)
            y=asciimat(varargin(1))
        elseif type(varargin(1))==10 then // Input is a matrix of strings
            for i=1:size(varargin(1),1)
                y(i)=strcat(varargin(1)(i,:))
            end
        elseif typeof(varargin(1))=="hypermat" then // Input is a hypermatrix of strings
            if type(varargin(1).entries)==10 then
                y=varargin(1)
            elseif type(varargin(1).entries)==1|type(varargin(1).entries)==8 then // Input is a hypermatrix of integers (or reals)
                y=asciimat(varargin(1))
            else
                error(msprintf(gettext("%s: This feature has not been implemented.\n"),"char"));
            end
        else
            error(msprintf(gettext("%s: Wrong type for input argument: Cell expected.\n"),"char"));
        end
        // More than one input argument
    elseif rhs > 1 then
        for i=1:size(varargin)
            if typeof(varargin(i))=="hypermat" then // Input sti is a hypermatrix of strings
                lst=varargin(i)
                if type(lst.entries)==10 then
                    t=emptystr(size(lst,1),1)
                    lst=matrix(lst,[size(lst,1),size(lst,"*")/size(lst,1)])
                    for j=1:size(lst,2)
                        t=t+lst(:,j)
                    end
                    y=[y;t]
                else
                    error(msprintf(gettext("%s: Wrong type for input arguments: String expected.\n"),"char"));
                end
            elseif type(varargin(i))==10 then // Input sti is a matrix of strings
                lst=varargin(i)
                t=emptystr(size(lst,1),1)
                for k=1:size(lst,2)
                    t=t+lst(:,k)
                end
                y=[y;t]
            else
                error(msprintf(gettext("%s: Wrong type for input arguments: String expected.\n"),"char"));
            end
        end
        // Add blanks at the length of strings
        bl=" "
        maxstr=max(length(y))
        for i=1:size(y,"*")
            nb_bl=maxstr-length(y(i))
            if nb_bl>0 then
                y(i)=y(i)+part(bl,ones(1,nb_bl))
            end
        end
    end

endfunction