summaryrefslogtreecommitdiff
path: root/modules/graphics/src/c/StringMatrix.c
blob: 934fa3342c842f0826f8d74bfd95223593baeb34 (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
/*
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) 2006 - INRIA - Jean-Baptiste Silvy
 *
 * 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
 *
 */

/*----------------------------------------------------------------------------------*/
/* COPYRIGHT INRIA 2006                                                                      */
/* Desc.   : Allocation and deletion and modifications of matrices of strings.               */
/*           The matrix is stored by colmuns like in Scilab                                  */
/*----------------------------------------------------------------------------------*/

#include <string.h>
#include <stdio.h>
#include "StringMatrix.h"
#include "MALLOC.h"
#ifdef _MSC_VER
#include "strdup_windows.h"
#endif

/*----------------------------------------------------------------------------------*/
StringMatrix * newFullStringMatrix(char ** textMat, int nbRow, int nbCol)
{
    int i = 0;
    /* create the matrix */
    StringMatrix * newMat = newMatrix(nbRow, nbCol);

    /* copy each element */
    for (i = 0 ; i < nbRow * nbCol ; i++)
    {
        /* +1 for the /0 last character */
        newMat->data[i] = MALLOC((strlen(textMat[i]) + 1) * sizeof(char));
        strcpy(newMat->data[i], textMat[i]);
    }

    return newMat;
}
/*----------------------------------------------------------------------------------*/
StringMatrix * newEmptyStringMatrix(int nbRow, int nbCol)
{
    int i = 0;
    /* create the matrix */
    StringMatrix * newMat = newMatrix(nbRow, nbCol);

    /* copy each element */
    for (i = 0 ; i < nbRow * nbCol ; i++)
    {
        /* +1 for the /0 last character */
        newMat->data[i] = MALLOC((strlen("") + 1) * sizeof(char));
        strcpy(newMat->data[i], "");
    }

    return newMat;
}
/*----------------------------------------------------------------------------------*/
StringMatrix * copyStringMatrix(const StringMatrix * copyMat)
{
    return newFullStringMatrix((char **) copyMat->data, copyMat->nbRow, copyMat->nbCol);
}
/*----------------------------------------------------------------------------------*/
char * getStrMatElement(const StringMatrix * mat, int row, int col)
{
    return (char *) getMatElement(mat, row, col);
}
/*----------------------------------------------------------------------------------*/
char ** getStrMatData(const StringMatrix * mat)
{
    return (char **) getMatData(mat);
}
/*----------------------------------------------------------------------------------*/
void copyStrMatElement(StringMatrix * mat, int row, int col, const char * copyStr)
{
    char * changedString = (char *) mat->data[row + col * mat->nbRow] ; /* for speed */
    if (changedString != NULL)
    {
        FREE(changedString);
    }
    changedString = strdup(copyStr);
    mat->data[row + col * mat->nbRow] = changedString;
}
/*----------------------------------------------------------------------------------*/
void printStrMat(StringMatrix * mat)
{
    int i = 0;
    int nbRow = getMatNbRow(mat);
    int nbCol = getMatNbCol(mat);
    for (i = 0; i < nbRow; i++)
    {
        int j = 0;
        for (j = 0; j < nbCol; j++)
        {
            printf("%s ", getStrMatElement(mat, i, j));
        }
        printf("\n");
    }
}
/*----------------------------------------------------------------------------------*/