e83f83db7ae7e20116980c4efe9633587d538f9c
[scilab.git] / scilab / modules / string / src / c / strsplit.c
1 /*
2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2009 - DIGITEO - Allan CORNET
4 *
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution.  The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
10 *
11 */
12 /*----------------------------------------------------------------------------*/
13 #include <string.h>
14 #include "strsplit.h"
15 #include "sci_malloc.h"
16 #include "freeArrayOfString.h"
17 /*----------------------------------------------------------------------------*/
18 char** strsplit(const char* stringToSplit, double *indices, int sizeIndices, strsplit_error *ierr)
19 {
20     char** splitted = NULL;
21     *ierr = STRSPLIT_NO_ERROR;
22
23     if (stringToSplit)
24     {
25         int lengthToCopy = 0;
26         int lenString = (int)strlen(stringToSplit);
27         int i = 0, j = 0;
28         char* strDest = NULL;
29         const char* strSrc = NULL;
30
31         for (i = 0; i < sizeIndices; i++)
32         {
33             /* Check 2nd input matrix position */
34             if ( ((int)indices[i] <= 0) || ((int)indices[i] >= lenString) )
35             {
36                 *ierr = STRSPLIT_INCORRECT_VALUE_ERROR;
37                 return NULL;
38             }
39
40             /* check 2nd input order */
41             if (sizeIndices > 1)
42             {
43                 if ( i < (sizeIndices - 1) )
44                 {
45                     if ((int)indices[i] > (int)indices[i + 1])
46                     {
47                         *ierr = STRSPLIT_INCORRECT_ORDER_ERROR;
48                         return NULL;
49                     }
50                 }
51             }
52         }
53
54         splitted = (char**)MALLOC(sizeof(char*) * (sizeIndices + 1));
55         if (splitted == NULL)
56         {
57             *ierr = STRSPLIT_MEMORY_ALLOCATION_ERROR;
58             return NULL;
59         }
60
61         for (i = 0; i < sizeIndices; i++)
62         {
63
64             if (i == 0)
65             {
66                 lengthToCopy = (int)indices[i];
67             }
68             else
69             {
70                 lengthToCopy = (int)indices[i] - (int)indices[i - 1];
71             }
72
73             splitted[i] = (char*)MALLOC(sizeof(char) * (lengthToCopy + 1));
74             strDest = splitted[i];
75
76             if (splitted[i] == NULL)
77             {
78                 freeArrayOfString(splitted, sizeIndices);
79                 *ierr = STRSPLIT_MEMORY_ALLOCATION_ERROR;
80                 return NULL;
81             }
82             
83             strSrc = &stringToSplit[j];
84             memcpy(strDest, strSrc, lengthToCopy * sizeof(char));
85             strDest[lengthToCopy] = 0;
86
87             j = (int)indices[i];
88         }
89
90         lengthToCopy = lenString - (int)indices[sizeIndices - 1];
91         splitted[sizeIndices] = (char*)MALLOC(sizeof(char) * (lengthToCopy + 1));
92         strDest = splitted[sizeIndices];
93
94         if (splitted[sizeIndices] == NULL)
95         {
96             freeArrayOfString(splitted, sizeIndices + 1);
97             *ierr = STRSPLIT_MEMORY_ALLOCATION_ERROR;
98             return NULL;
99         }
100
101         strSrc = &stringToSplit[j];
102         memcpy(strDest, strSrc, lengthToCopy * sizeof(char));
103         strDest[lengthToCopy] = 0;
104     }
105     return splitted;
106 }
107 /*----------------------------------------------------------------------------*/