* Bug #12527 fixed - Scilab user functions were not listed in browsevar.
[scilab.git] / scilab / modules / spreadsheet / src / c / csv_complex.c
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2010-2011 - 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-en.txt
10  *
11  */
12 #include <string.h>
13 #include "csv_complex.h"
14 #include "MALLOC.h"
15 // =============================================================================
16 csv_complexArray *createCsvComplexArrayEmpty(int nbSize)
17 {
18     csv_complexArray *pCsvComplexArray = NULL;
19     pCsvComplexArray = (csv_complexArray *) MALLOC(sizeof(csv_complexArray));
20     if (pCsvComplexArray)
21     {
22         pCsvComplexArray->realPart = (double *)MALLOC(sizeof(double) * nbSize);
23         if (pCsvComplexArray->realPart == NULL)
24         {
25             FREE(pCsvComplexArray);
26             return NULL;
27         }
28
29         pCsvComplexArray->imagPart = (double *)MALLOC(sizeof(double) * nbSize);
30         if (pCsvComplexArray->imagPart == NULL)
31         {
32             FREE(pCsvComplexArray->realPart);
33             pCsvComplexArray->realPart = NULL;
34             FREE(pCsvComplexArray);
35             return NULL;
36         }
37
38         memset(pCsvComplexArray->realPart, 0, sizeof(double) * nbSize);
39         memset(pCsvComplexArray->imagPart, 0, sizeof(double) * nbSize);
40         pCsvComplexArray->isComplex = 1;
41         pCsvComplexArray->nbElements = nbSize;
42     }
43     return pCsvComplexArray;
44 }
45 // =============================================================================
46 csv_complexArray *createCsvComplexArray(const double *realpart, const double *imagpart, int nbSize)
47 {
48     csv_complexArray *pCsvComplexArray = NULL;
49
50     if (realpart == NULL)
51     {
52         return pCsvComplexArray;
53     }
54
55     if (nbSize <= 0)
56     {
57         return pCsvComplexArray;
58     }
59
60     pCsvComplexArray = (csv_complexArray *) MALLOC(sizeof(csv_complexArray));
61     if (pCsvComplexArray)
62     {
63         if (imagpart == NULL)
64         {
65             pCsvComplexArray->isComplex = 0;
66         }
67         else
68         {
69             int i = 0;
70             pCsvComplexArray->isComplex = 0;
71             for (i = 0; i < nbSize; i++)
72             {
73                 if (imagpart[i] != 0)
74                 {
75                     pCsvComplexArray->isComplex = 1;
76                     break;
77                 }
78             }
79         }
80
81         pCsvComplexArray->realPart = (double *)MALLOC(sizeof(double) * nbSize);
82         if (pCsvComplexArray->realPart)
83         {
84             memcpy(pCsvComplexArray->realPart, realpart, nbSize);
85             pCsvComplexArray->nbElements = nbSize;
86         }
87         if (pCsvComplexArray->isComplex)
88         {
89             pCsvComplexArray->imagPart = (double *)MALLOC(sizeof(double) * nbSize);
90             if (pCsvComplexArray->imagPart)
91             {
92                 memcpy(pCsvComplexArray->imagPart, imagpart, nbSize);
93             }
94         }
95         else
96         {
97             pCsvComplexArray->imagPart = NULL;
98         }
99     }
100     return pCsvComplexArray;
101 }
102 // =============================================================================
103 void freeCsvComplexArray(csv_complexArray *pCsvComplexArray)
104 {
105     if (pCsvComplexArray)
106     {
107         if (pCsvComplexArray->realPart)
108         {
109             FREE(pCsvComplexArray->realPart);
110             pCsvComplexArray->realPart = NULL;
111         }
112
113         if (pCsvComplexArray->imagPart)
114         {
115             FREE(pCsvComplexArray->imagPart);
116             pCsvComplexArray->imagPart = NULL;
117         }
118         pCsvComplexArray->isComplex = 0;
119         pCsvComplexArray->nbElements = 0;
120
121         FREE(pCsvComplexArray);
122         pCsvComplexArray = NULL;
123     }
124 }
125 // =============================================================================
126 int cleanImagPartCsvComplexArray(csv_complexArray *pCsvComplexArray)
127 {
128     if (pCsvComplexArray)
129     {
130         if (pCsvComplexArray->imagPart)
131         {
132             int i = 0;
133             int isComplex = 0;
134             for (i = 0; i < pCsvComplexArray->nbElements; i++)
135             {
136                 if (pCsvComplexArray->imagPart[i] != 0)
137                 {
138                     isComplex = 1;
139                     break;
140                 }
141             }
142
143             if (isComplex)
144             {
145                 pCsvComplexArray->isComplex = 1;
146             }
147             else
148             {
149                 pCsvComplexArray->isComplex = 0;
150             }
151         }
152         else
153         {
154             return 1;
155         }
156     }
157     return 0;
158 }
159 // =============================================================================