2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) INRIA - Allan CORNET , Cong WU
4 * Copyright (C) DIGITEO - 2010 - Allan CORNET
5 * Copyright (C) 2010 - DIGITEO - Antoine ELIAS
7 * This file must be used under the terms of the CeCILL.
8 * This source file is licensed as described in the file COPYING, which
9 * you should have received as part of this distribution. The terms
10 * are also available at
11 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
15 /* desc : concatenate character strings */
16 /* Examples: strcat(string(1:10),',') */
18 /*------------------------------------------------------------------------*/
20 #include "string_gw.hxx"
21 #include "function.hxx"
29 #include "sci_malloc.h"
31 #include "localization.h"
33 /*-------------------------------------------------------------------------------------*/
39 /*-------------------------------------------------------------------------------------*/
40 static int sci_strcat_three_rhs(char *fname);
41 static int sci_strcat_two_rhs(char *fname);
42 static int sci_strcat_one_rhs(char *fname);
43 static int sci_strcat_rhs_one_is_a_matrix(char *fname);
44 static int sumlengthstring(int rhspos);
45 static int *lengthEachString(int rhspos, int *sizeArrayReturned);
46 /*-------------------------------------------------------------------------------------*/
47 types::Function::ReturnValue sci_strcat(types::typed_list &in, int _iRetCount, types::typed_list &out)
50 char* pstToInsert = NULL;
52 //check input paramters
53 if (in.size() < 1 || in.size() > 3)
55 Scierror(999, _("%s: Wrong number of input arguments: %d or %d expected.\n"), "strcat", 1, 3);
56 return types::Function::Error;
59 for (int i = 1 ; i < in.size() ; i++)
61 if (in[i]->isString() == false)
63 Scierror(999, _("%s: Wrong type for input argument #%d: Matrix of strings expected.\n"), "strcat", i + 1);
64 return types::Function::Error;
68 if (in[0]->isDouble() && in[0]->getAs<types::Double>()->getSize() == 0)
70 types::String *pOut = new types::String(1, 1);
73 return types::Function::OK;
75 else if (in[0]->isString() == false)
77 Scierror(999, _("%s: Wrong type for input argument #%d: String expected.\n"), "strcat", 1);
78 return types::Function::Error;
83 wchar_t wcMode = in[2]->getAs<types::String>()->get(0)[0];
93 Scierror(999, _("%s: Wrong type for input argument #%d: ''%s'' or ''%s'' expected.\n"), "strcat", 3, "c", "r");
94 return types::Function::Error;
100 if (in[1]->getAs<types::String>()->getSize() != 1)
102 Scierror(999, _("%s: Wrong type for input argument #%d: String expected.\n"), "strcat", 2);
103 return types::Function::Error;
106 pstToInsert = in[1]->getAs<types::String>()->get(0);
109 types::String* pS = in[0]->getAs<types::String>();
111 types::String* pOut = NULL;
116 pOut = new types::String(1, 1);
117 /*compute final size*/
118 int iLen = 1; //L'\0'
119 for (int i = 0 ; i < pS->getSize() ; i++)
121 iLen += (int)strlen(pS->get(i));
124 if (pstToInsert != NULL)
126 iLen += (int)strlen(pstToInsert) * (pS->getSize() - 1);
129 char* pstOut = (char*)MALLOC(sizeof(char) * iLen);
131 for (int i = 0 ; i < pS->getSize() ; i++)
133 size_t iOffset = strlen(pstOut);
134 if (iOffset != 0 && pstToInsert != NULL)
136 strcat(pstOut + iOffset, pstToInsert);
138 strcat(pstOut + iOffset, pS->get(i));
141 pOut->set(0, pstOut);
147 pOut = new types::String(1, pS->getCols());
148 /*compute final size*/
149 for (int i = 0 ; i < pS->getCols() ; i++)
151 int iLen = 1; //L'\0'
152 for (int j = 0 ; j < pS->getRows() ; j++)
154 iLen += (int)strlen(pS->get(j, i));
157 if (pstToInsert != NULL)
159 iLen += (int)strlen(pstToInsert) * (pS->getRows() - 1);
162 char* pstOut = (char*)MALLOC(sizeof(char) * iLen);
165 for (int j = 0 ; j < pS->getRows() ; j++)
167 size_t iOffset = strlen(pstOut);
168 if (iOffset != 0 && pstToInsert != NULL)
170 strcat(pstOut + iOffset, pstToInsert);
172 strcat(pstOut + iOffset, pS->get(j, i));
174 pOut->set(0, i, pstOut);
181 pOut = new types::String(pS->getRows(), 1);
182 /*compute final size*/
183 for (int i = 0 ; i < pS->getRows() ; i++)
185 int iLen = 1; //L'\0'
186 for (int j = 0 ; j < pS->getCols() ; j++)
188 iLen += (int)strlen(pS->get(i, j));
191 if (pstToInsert != NULL)
193 iLen += (int)strlen(pstToInsert) * (pS->getCols() - 1);
196 char* pstOut = (char*)MALLOC(sizeof(char) * iLen);
199 for (int j = 0 ; j < pS->getCols() ; j++)
201 size_t iOffset = strlen(pstOut);
202 if (iOffset != 0 && pstToInsert != NULL)
204 strcat(pstOut + iOffset, pstToInsert);
206 strcat(pstOut + iOffset, pS->get(i, j));
208 pOut->set(i, 0, pstOut);
216 return types::Function::OK;
218 /*-------------------------------------------------------------------------------------*/