summaryrefslogtreecommitdiff
path: root/modules/graphics/src/c/scitokenize.c
blob: 4cfd3ba625ffaace3c6ad95756de2508a80069cb (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
/*
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) 2002-2004 - INRIA - Djalel Abdemouche
 * Copyright (C) 2004-2006 - INRIA - Fabrice Leray
 *
 * 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
 *
 */

/*--------------------------------------------------------------------------*/
#include <string.h>
#include "scitokenize.h"
#include "MALLOC.h" /* MALLOC */
#include "freeArrayOfString.h"
/*--------------------------------------------------------------------------*/
int scitokenize(char * legend, char *** Strptr, int *n)
{
    int arobaseIndex = 0;
    int i = 0;
    int count = 1;
    int prevIndex = 0;
    int stringLength = 0, j = 0;
    char **Str = NULL;
    int lenlegend = (int)strlen(legend);

    /* first get the number of tokens */
    for (i = 0; i < lenlegend; i++)
    {
        if (legend[i] == '@')
        {
            count++;
        }
    }
    *Strptr = (char **)MALLOC(count * sizeof(char *));
    Str = *Strptr;
    if (Str == NULL)
    {
        return 1;    /* not enough memory */
    }
    for (i = 0; i < count; i++)
    {
        Str[i] = NULL;
    }

    for (i = 0; i < count; i++)
    {
        while (legend[arobaseIndex] != '@' && legend[arobaseIndex] != 0)
        {
            arobaseIndex++;
        }
        /* we found a token */
        stringLength =  arobaseIndex - prevIndex;
        Str[i] = (char *)MALLOC((stringLength + 1) * sizeof(char)); /*  +1 for null terminating character*/

        if (Str[i] == NULL)
        {
            freeArrayOfString(Str, i - 1);
            return 1;
        }

        for (j = 0; j < stringLength; j++)
        {
            Str[i][j] = legend[prevIndex + j];
        }
        Str[i][stringLength] = 0;

        /* skip the @*/
        arobaseIndex++;
        prevIndex = arobaseIndex;
    }
    *n = count;
    return 0;
}
/*--------------------------------------------------------------------------*/