1 /*
2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2015 - Scilab Enterprises - Antoine ELIAS
4 *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13 *
14 */
15 #include <ctype.h>
16 #include "api_scilab.h"
17 #include "Scierror.h"
18 #include "localization.h"
19 #include "sciprint.h"
20 #include "sci_malloc.h"
21 #include "os_string.h"
22 
23 const char fname[] = "string_test";
24 
sci_string_test(scilabEnv env,int nin,scilabVar * in,int nopt,scilabOpt opt,int nout,scilabVar * out)25 int sci_string_test(scilabEnv env, int nin, scilabVar* in, int nopt, scilabOpt opt, int nout, scilabVar* out)
26 {
27     int i = 0;
28     int inr1 = 0;
29     int inc1 = 0;
30     int size1 = 0;
31     wchar_t** in1 = NULL;
32 
33     wchar_t* in2 = 0;
34 
35     int dim1 = 3;
36     int dims1[] = {0, 0, 2};
37     wchar_t** out1 = NULL;
38 
39     wchar_t* out2;
40     int len2 = 0;
41 
42     if (nin != 2)
43     {
44         Scierror(999, _("%s: Wrong number of input arguments: %d expected.\n"), fname, 2);
45         return STATUS_ERROR;
46     }
47 
48     if (nout != 2)
49     {
50         Scierror(999, _("%s: Wrong number of output arguments: %d expected.\n"), fname, 3);
51         return STATUS_ERROR;
52     }
53 
54     //in[0] : matrix 2d of string
55     if (scilab_isString(env, in[0]) == 0 || scilab_isMatrix2d(env, in[0]) == 0)
56     {
57         Scierror(999, _("%s: Wrong type for input argument #%d: A string matrix expected.\n"), fname, 1);
58         return STATUS_ERROR;
59     }
60 
61     size1 = scilab_getDim2d(env, in[0], &inr1, &inc1);
62     scilab_getStringArray(env, in[0], &in1);
63 
64     //in[1] : string
65     if (scilab_isString(env, in[1]) == 0 || scilab_isScalar(env, in[1]) == 0)
66     {
67         Scierror(999, _("%s: Wrong type for input argument #%d: A double expected.\n"), fname, 2);
68         return STATUS_ERROR;
69     }
70 
71     scilab_getString(env, in[1], &in2);
72 
73     //out1 : matrix 2d of string with same size of in[0]
74     dims1[0] = inr1;
75     dims1[1] = inc1;
76     out[0] = scilab_createStringMatrix(env, dim1, dims1);
77     scilab_getStringArray(env, out[0], &out1);
78 
79     for (i = 0; i < size1; ++i)
80     {
81         wchar_t temp[128];
82         wcscpy(temp, in1[i]);
83         wcscat(temp, L".one");
84         out1[i] = os_wcsdup(temp);
85 
86         wcscpy(temp, in1[i]);
87         wcscat(temp, L".two");
88         out1[i + size1] = os_wcsdup(temp);
89     }
90 
91     //out2 : string
92     out2 = os_wcsdup(in2);
93     len2 = wcslen(out2);
94     for (i = 0; i < len2; ++i)
95     {
96         if (out2[i] >= L'a' && out2[i] <= L'z')
97         {
98             out2[i] = ((out2[i] - 'a' + 26 - 1) % 26) + 'a';
99         }
100         else if (out2[i] >= L'A' && out2[i] <= L'Z')
101         {
102             out2[i] = ((out2[i] - 'A' + 26 - 1) % 26) + 'A';
103         }
104         else
105         {
106             //no change
107         }
108     }
109 
110     out[1] = scilab_createString(env, out2);
111     FREE(out2);
112     return STATUS_OK;
113 }
114