summaryrefslogtreecommitdiff
path: root/modules/string/src/c/men_Sutils.c
blob: 79185f80180532b427a5a6d354ae757d656209fb (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129

/*
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) INRIA
 *
 * 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 <stdlib.h>
#include <string.h>
#include "men_Sutils.h"
#include "getfastcode.h"
#include "MALLOC.h"             /* MALLOC */
#include "freeArrayOfString.h"
/*--------------------------------------------------------------------------*/
void ScilabStr2C(int *n, int *Scistring, char **strh, int *ierr)
{
    int j = 0;

    *strh = (char *)MALLOC((*n + 1) * sizeof(char));
    if ((*strh) == NULL)
    {
        *ierr = 1;
        return;
    }

    for (j = 0; j < *n; ++j)
    {
        (*strh)[j] = convertScilabCodeToAsciiCode(Scistring[j]);
    }
    (*strh)[*n] = '\0';
}

/*--------------------------------------------------------------------------*/
void ScilabMStr2CM(int *Scistring, int *nstring, int *ptrstrings, char ***strh, int *ierr)
{
    char **strings = NULL, *p = NULL;
    int li = 0, ni = 0, *SciS = NULL, i = 0;

    strings = (char **)MALLOC(((*nstring) + 1) * sizeof(char *));

    if (strings == NULL)
    {
        *ierr = 1;
        return;
    }

    li = 1;
    SciS = Scistring;
    for (i = 1; i < *nstring + 1; i++)
    {
        ni = ptrstrings[i] - li;
        li = ptrstrings[i];
        /* p is allocated here */
        ScilabStr2C(&ni, SciS, &p, ierr);
        if (*ierr == 1)
        {
            if (p)
            {
                FREE(p);
            }

            freeArrayOfString(strings, i - 1);
            return;
        }
        strings[i - 1] = p;
        SciS += ni;
    }
    strings[*nstring] = NULL;
    *strh = strings;
}

/*--------------------------------------------------------------------------*/
void ScilabMStr2C(int *desc, int *nd, int *ptrdesc, char **strh, int *ierr)
{
    int ln = 0, i = 0;
    int *SciS = NULL;
    char *description = NULL;

#ifdef _MSC_VER
    ln = ptrdesc[*nd] + 2 * (*nd) + 1;
#else
    ln = ptrdesc[*nd] + *nd + 1;
#endif

    description = (char *)MALLOC(ln * sizeof(char));

    if (description == NULL)
    {
        *ierr = 1;
        return;
    }

    SciS = desc;

    for (i = 1; i < *nd + 1; i++)
    {
        int j = 0;
        int ni = 0;
        int li = 1;
        int di = 0;
        char *p = NULL;

        p = &(description[di]);
        ni = ptrdesc[i] - li;

        /* conversion code -> ascii */
        for (j = 0; j < ni; ++j)
        {
            p[j] = convertScilabCodeToAsciiCode(SciS[j]);
        }
        SciS += ni;
#ifdef _MSC_VER
        p[ni] = '\r';
        ni++;
#endif
        p[ni] = '\n';
    }
    description[ln - 2] = '\0';
    *strh = description;
}

/*--------------------------------------------------------------------------*/