1 /*
2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2009 - DIGITEO - Allan CORNET
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 /*----------------------------------------------------------------------------*/
16 #include <string.h>
17 #include "strsplit.h"
18 #include "sci_malloc.h"
19 #include "freeArrayOfString.h"
20 /*----------------------------------------------------------------------------*/
strsplit(wchar_t * wcstringToSplit,double * indices,int sizeIndices,strsplit_error * ierr)21 wchar_t **strsplit(wchar_t * wcstringToSplit, double *indices, int sizeIndices, strsplit_error *ierr)
22 {
23 wchar_t **splitted = NULL;
24 *ierr = STRSPLIT_NO_ERROR;
25
26 if (wcstringToSplit)
27 {
28 int lengthToCopy = 0;
29 int lenString = (int)wcslen(wcstringToSplit);
30 int i = 0, j = 0;
31 wchar_t* wcStrDest = NULL;
32 wchar_t* wcStrSrc = NULL;
33
34 for (i = 0; i < sizeIndices; i++)
35 {
36 /* Check 2nd input matrix position */
37 if ( ((int)indices[i] <= 0) || ((int)indices[i] >= lenString) )
38 {
39 *ierr = STRSPLIT_INCORRECT_VALUE_ERROR;
40 return NULL;
41 }
42
43 /* check 2nd input order */
44 if (sizeIndices > 1)
45 {
46 if ( i < (sizeIndices - 1) )
47 {
48 if ((int)indices[i] > (int)indices[i + 1])
49 {
50 *ierr = STRSPLIT_INCORRECT_ORDER_ERROR;
51 return NULL;
52 }
53 }
54 }
55 }
56
57 splitted = (wchar_t**)MALLOC(sizeof(wchar_t*) * (sizeIndices + 1));
58 if (splitted == NULL)
59 {
60 *ierr = STRSPLIT_MEMORY_ALLOCATION_ERROR;
61 return NULL;
62 }
63
64 for (i = 0; i < sizeIndices; i++)
65 {
66
67 if (i == 0)
68 {
69 lengthToCopy = (int)indices[i];
70 }
71 else
72 {
73 lengthToCopy = (int)indices[i] - (int)indices[i - 1];
74 }
75
76 splitted[i] = (wchar_t*)MALLOC(sizeof(wchar_t) * (lengthToCopy + 1));
77 wcStrDest = splitted[i];
78
79 if (splitted[i] == NULL)
80 {
81 freeArrayOfWideString(splitted, sizeIndices);
82 *ierr = STRSPLIT_MEMORY_ALLOCATION_ERROR;
83 return NULL;
84 }
85 wcStrSrc = &wcstringToSplit[j];
86 memcpy(wcStrDest, wcStrSrc, lengthToCopy * sizeof(wchar_t));
87 wcStrDest[lengthToCopy] = 0;
88
89 j = (int)indices[i];
90 }
91
92 lengthToCopy = lenString - (int)indices[sizeIndices - 1];
93 splitted[sizeIndices] = (wchar_t*)MALLOC(sizeof(wchar_t) * (lengthToCopy + 1));
94 wcStrDest = splitted[sizeIndices];
95
96 if (splitted[sizeIndices] == NULL)
97 {
98 freeArrayOfWideString(splitted, sizeIndices + 1);
99 *ierr = STRSPLIT_MEMORY_ALLOCATION_ERROR;
100 return NULL;
101 }
102
103 wcStrSrc = &wcstringToSplit[j];
104 memcpy(wcStrDest, wcStrSrc, lengthToCopy * sizeof(wchar_t));
105 wcStrDest[lengthToCopy] = 0;
106 }
107 return splitted;
108 }
109 /*----------------------------------------------------------------------------*/
110