summaryrefslogtreecommitdiffstats
path: root/scilab/modules/io
diff options
context:
space:
mode:
authorAllan CORNET <allan.cornet@scilab.org>2010-04-16 14:42:27 +0200
committerSylvestre Ledru <sylvestre.ledru@scilab.org>2010-04-21 11:25:20 +0200
commit834b412acd9048119e86c1cb87f63deeb54bb7e4 (patch)
treec60fd357865d08cb3ac0b692b294da3bf2886cca /scilab/modules/io
parent6b6a530c578e4b31a2e25250f4ffb5a1d3f6df47 (diff)
downloadscilab-834b412acd9048119e86c1cb87f63deeb54bb7e4.zip
scilab-834b412acd9048119e86c1cb87f63deeb54bb7e4.tar.gz
bug 4985
Change-Id: I1da0493ad78af512c8273f03b4bbdd71f7dbdfcb
Diffstat (limited to 'scilab/modules/io')
-rw-r--r--scilab/modules/io/Makefile.am1
-rwxr-xr-x[-rw-r--r--]scilab/modules/io/Makefile.in13
-rw-r--r--scilab/modules/io/sci_gateway/c/sci_mgetl.c272
-rw-r--r--scilab/modules/io/src/c/LibScilab_Import.def1
-rw-r--r--scilab/modules/io/src/c/io.vcproj10
-rw-r--r--scilab/modules/io/src/c/mgetl.c152
-rw-r--r--scilab/modules/io/src/c/mgetl.h44
7 files changed, 469 insertions, 24 deletions
diff --git a/scilab/modules/io/Makefile.am b/scilab/modules/io/Makefile.am
index 8a7e00a..5109d66 100644
--- a/scilab/modules/io/Makefile.am
+++ b/scilab/modules/io/Makefile.am
@@ -7,6 +7,7 @@ IO_C_SOURCES = src/c/getpidc.c \
7src/c/setenvc.c \ 7src/c/setenvc.c \
8src/c/systemc.c \ 8src/c/systemc.c \
9src/c/getenvc.c \ 9src/c/getenvc.c \
10src/c/mgetl.c \
10src/c/GetenvB.c 11src/c/GetenvB.c
11 12
12IO_FORTRAN_SOURCES = src/fortran/v2unit.f \ 13IO_FORTRAN_SOURCES = src/fortran/v2unit.f \
diff --git a/scilab/modules/io/Makefile.in b/scilab/modules/io/Makefile.in
index 68a3c7a..5afdefe 100644..100755
--- a/scilab/modules/io/Makefile.in
+++ b/scilab/modules/io/Makefile.in
@@ -1,4 +1,4 @@
1# Makefile.in generated by automake 1.11.1 from Makefile.am. 1# Makefile.in generated by automake 1.11 from Makefile.am.
2# @configure_input@ 2# @configure_input@
3 3
4# Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 4# Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
@@ -125,7 +125,7 @@ libsciio_la_DEPENDENCIES = libdummy-io.la \
125 $(top_builddir)/modules/output_stream/libscioutput_stream.la 125 $(top_builddir)/modules/output_stream/libscioutput_stream.la
126am__objects_1 = libsciio_la-getpidc.lo libsciio_la-setenvc.lo \ 126am__objects_1 = libsciio_la-getpidc.lo libsciio_la-setenvc.lo \
127 libsciio_la-systemc.lo libsciio_la-getenvc.lo \ 127 libsciio_la-systemc.lo libsciio_la-getenvc.lo \
128 libsciio_la-GetenvB.lo 128 libsciio_la-mgetl.lo libsciio_la-GetenvB.lo
129am__objects_2 = v2unit.lo newsave.lo v2cunit.lo writebuf.lo 129am__objects_2 = v2unit.lo newsave.lo v2cunit.lo writebuf.lo
130am__objects_3 = libsciio_la-sci_read4b.lo libsciio_la-sci_getio.lo \ 130am__objects_3 = libsciio_la-sci_read4b.lo libsciio_la-sci_getio.lo \
131 libsciio_la-sci_readb.lo libsciio_la-sci_host.lo \ 131 libsciio_la-sci_readb.lo libsciio_la-sci_host.lo \
@@ -415,6 +415,7 @@ IO_C_SOURCES = src/c/getpidc.c \
415src/c/setenvc.c \ 415src/c/setenvc.c \
416src/c/systemc.c \ 416src/c/systemc.c \
417src/c/getenvc.c \ 417src/c/getenvc.c \
418src/c/mgetl.c \
418src/c/GetenvB.c 419src/c/GetenvB.c
419 420
420IO_FORTRAN_SOURCES = src/fortran/v2unit.f \ 421IO_FORTRAN_SOURCES = src/fortran/v2unit.f \
@@ -642,6 +643,7 @@ distclean-compile:
642@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libsciio_la-getenvc.Plo@am__quote@ 643@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libsciio_la-getenvc.Plo@am__quote@
643@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libsciio_la-getpidc.Plo@am__quote@ 644@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libsciio_la-getpidc.Plo@am__quote@
644@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libsciio_la-gw_io.Plo@am__quote@ 645@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libsciio_la-gw_io.Plo@am__quote@
646@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libsciio_la-mgetl.Plo@am__quote@
645@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libsciio_la-sci_file.Plo@am__quote@ 647@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libsciio_la-sci_file.Plo@am__quote@
646@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libsciio_la-sci_getenv.Plo@am__quote@ 648@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libsciio_la-sci_getenv.Plo@am__quote@
647@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libsciio_la-sci_getio.Plo@am__quote@ 649@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libsciio_la-sci_getio.Plo@am__quote@
@@ -711,6 +713,13 @@ libsciio_la-getenvc.lo: src/c/getenvc.c
711@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ 713@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
712@am__fastdepCC_FALSE@ $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libsciio_la_CPPFLAGS) $(CPPFLAGS) $(libsciio_la_CFLAGS) $(CFLAGS) -c -o libsciio_la-getenvc.lo `test -f 'src/c/getenvc.c' || echo '$(srcdir)/'`src/c/getenvc.c 714@am__fastdepCC_FALSE@ $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libsciio_la_CPPFLAGS) $(CPPFLAGS) $(libsciio_la_CFLAGS) $(CFLAGS) -c -o libsciio_la-getenvc.lo `test -f 'src/c/getenvc.c' || echo '$(srcdir)/'`src/c/getenvc.c
713 715
716libsciio_la-mgetl.lo: src/c/mgetl.c
717@am__fastdepCC_TRUE@ $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libsciio_la_CPPFLAGS) $(CPPFLAGS) $(libsciio_la_CFLAGS) $(CFLAGS) -MT libsciio_la-mgetl.lo -MD -MP -MF $(DEPDIR)/libsciio_la-mgetl.Tpo -c -o libsciio_la-mgetl.lo `test -f 'src/c/mgetl.c' || echo '$(srcdir)/'`src/c/mgetl.c
718@am__fastdepCC_TRUE@ $(am__mv) $(DEPDIR)/libsciio_la-mgetl.Tpo $(DEPDIR)/libsciio_la-mgetl.Plo
719@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='src/c/mgetl.c' object='libsciio_la-mgetl.lo' libtool=yes @AMDEPBACKSLASH@
720@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
721@am__fastdepCC_FALSE@ $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libsciio_la_CPPFLAGS) $(CPPFLAGS) $(libsciio_la_CFLAGS) $(CFLAGS) -c -o libsciio_la-mgetl.lo `test -f 'src/c/mgetl.c' || echo '$(srcdir)/'`src/c/mgetl.c
722
714libsciio_la-GetenvB.lo: src/c/GetenvB.c 723libsciio_la-GetenvB.lo: src/c/GetenvB.c
715@am__fastdepCC_TRUE@ $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libsciio_la_CPPFLAGS) $(CPPFLAGS) $(libsciio_la_CFLAGS) $(CFLAGS) -MT libsciio_la-GetenvB.lo -MD -MP -MF $(DEPDIR)/libsciio_la-GetenvB.Tpo -c -o libsciio_la-GetenvB.lo `test -f 'src/c/GetenvB.c' || echo '$(srcdir)/'`src/c/GetenvB.c 724@am__fastdepCC_TRUE@ $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libsciio_la_CPPFLAGS) $(CPPFLAGS) $(libsciio_la_CFLAGS) $(CFLAGS) -MT libsciio_la-GetenvB.lo -MD -MP -MF $(DEPDIR)/libsciio_la-GetenvB.Tpo -c -o libsciio_la-GetenvB.lo `test -f 'src/c/GetenvB.c' || echo '$(srcdir)/'`src/c/GetenvB.c
716@am__fastdepCC_TRUE@ $(am__mv) $(DEPDIR)/libsciio_la-GetenvB.Tpo $(DEPDIR)/libsciio_la-GetenvB.Plo 725@am__fastdepCC_TRUE@ $(am__mv) $(DEPDIR)/libsciio_la-GetenvB.Tpo $(DEPDIR)/libsciio_la-GetenvB.Plo
diff --git a/scilab/modules/io/sci_gateway/c/sci_mgetl.c b/scilab/modules/io/sci_gateway/c/sci_mgetl.c
index a7dc7bd..1548018 100644
--- a/scilab/modules/io/sci_gateway/c/sci_mgetl.c
+++ b/scilab/modules/io/sci_gateway/c/sci_mgetl.c
@@ -1,6 +1,7 @@
1/* 1/*
2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab 2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2006 - INRIA - Allan CORNET 3 * Copyright (C) 2006 - INRIA - Allan CORNET
4 * Copyright (C) 2010 - DIGITEO - Allan CORNET
4 * 5 *
5 * This file must be used under the terms of the CeCILL. 6 * 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 * This source file is licensed as described in the file COPYING, which
@@ -9,33 +10,262 @@
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt 10 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10 * 11 *
11 */ 12 */
12 13/*--------------------------------------------------------------------------*/
13#include "gw_io.h" 14#include "gw_io.h"
14#include "stack-c.h" 15#include "stack-c.h"
15#include "localization.h" 16#include "localization.h"
16#include "Scierror.h" 17#include "Scierror.h"
17#include "IsAScalar.h" 18#include "api_scilab.h"
19#include "mgetl.h"
20#include "mopen.h"
21#include "mclose.h"
22#include "expandPathVariable.h"
23#include "filesmanagement.h"
24#include "freeArrayOfString.h"
25#include "BOOL.h"
18/*--------------------------------------------------------------------------*/ 26/*--------------------------------------------------------------------------*/
19int C2F(sci_mgetl)(char *fname,unsigned long fname_len) 27int C2F(sci_mgetl)(char *fname,unsigned long fname_len)
20{ 28{
21 if (Rhs == 2) 29 SciErr sciErr;
22 { 30 int *piAddressVarOne = NULL;
23 if (VarType(2) != sci_matrix) 31 int numberOfLinesToRead = -1;
24 { 32
25 Scierror(999,_("%s: Wrong type for input argument #%d: An int scalar expected.\n"),fname,2); 33 Rhs = Max(0,Rhs);
26 return 0; 34
27 } 35 CheckRhs(1,2);
28 else 36 CheckLhs(1,1);
29 { 37
30 if ( !IsAScalar(2) ) 38 if (Rhs == 2)
31 { 39 {
32 Scierror(999,_("%s: Wrong type for input argument #%d: An int scalar expected.\n"),fname,2); 40 int *piAddressVarTwo = NULL;
33 return 0; 41
34 } 42 sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);
35 } 43 if(sciErr.iErr)
36 } 44 {
37 45 printError(&sciErr, 0);
38 C2F(intmgetl)(); 46 return 0;
39 return 0; 47 }
48
49 if ( isDoubleType(pvApiCtx, piAddressVarTwo) )
50 {
51 double dValue = 0.;
52 if (!isScalar(pvApiCtx, piAddressVarTwo))
53 {
54 Scierror(999,_("%s: Wrong size for input argument #%d: Integer expected.\n"), fname, 2);
55 return 0;
56 }
57
58 if ( getScalarDouble(pvApiCtx, piAddressVarTwo, &dValue) == 0)
59 {
60 numberOfLinesToRead = (int)dValue;
61 }
62 else
63 {
64 Scierror(999,_("%s: Memory allocation error.\n"), fname);
65 return 0;
66 }
67 }
68 else
69 {
70 Scierror(999,_("%s: Wrong type for input argument #%d: Integer expected.\n"), fname, 2);
71 return 0;
72 }
73 }
74
75 sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
76 if(sciErr.iErr)
77 {
78 printError(&sciErr, 0);
79 return 0;
80 }
81
82 if ( isStringType(pvApiCtx, piAddressVarOne) || isDoubleType(pvApiCtx, piAddressVarOne) )
83 {
84 char **wcReadedStrings = NULL;
85 int numberOfLinesReaded = 0;
86 int fileDescriptor = -1;
87 int iErrorMgetl = 0;
88 BOOL bCloseFile = FALSE;
89
90 if (!isScalar(pvApiCtx, piAddressVarOne))
91 {
92 Scierror(999,_("%s: Wrong size for input argument #%d: String or logical unit expected.\n"), fname, 1);
93 return 0;
94 }
95
96 if (isStringType(pvApiCtx, piAddressVarOne))
97 {
98 char *fileName = NULL;
99 if (getAllocatedSingleString(pvApiCtx, piAddressVarOne, &fileName) == 0)
100 {
101 char *expandedFileName = expandPathVariable(fileName);
102 freeAllocatedSingleString(fileName);
103 fileName = NULL;
104
105 if (IsAlreadyOpenedInScilab(expandedFileName))
106 {
107 int fd = GetIdFromFilename(expandedFileName);
108 fileDescriptor = fd;
109 if (expandedFileName) {FREE(expandedFileName); expandedFileName = NULL;}
110 bCloseFile = FALSE;
111 }
112 else
113 {
114 #define READ_ONLY_TEXT_MODE "rt"
115 int fd = 0;
116 int f_swap = 0;
117 double res = 0.0;
118 int ierr = 0;
119
120 C2F(mopen)(&fd, expandedFileName, READ_ONLY_TEXT_MODE, &f_swap, &res, &ierr);
121 bCloseFile = TRUE;
122
123 switch (ierr)
124 {
125 case MOPEN_NO_ERROR:
126 fileDescriptor = fd;
127 break;
128 case MOPEN_NO_MORE_LOGICAL_UNIT:
129 {
130 Scierror(66, _("%s: Too many files opened!\n"), fname);
131 if (expandedFileName) {FREE(expandedFileName); expandedFileName = NULL;}
132 return 0;
133 }
134 break;
135 case MOPEN_CAN_NOT_OPEN_FILE:
136 {
137 Scierror(999, _("%s: Cannot open file %s.\n"), fname, expandedFileName);
138 if (expandedFileName) {FREE(expandedFileName); expandedFileName = NULL;}
139 return 0;
140 }
141 break;
142 case MOPEN_NO_MORE_MEMORY:
143 {
144 if (expandedFileName) {FREE(expandedFileName); expandedFileName = NULL;}
145 Scierror(999, _("%s: No more memory.\n"), fname);
146 return 0;
147 }
148 break;
149 case MOPEN_INVALID_FILENAME:
150 {
151 Scierror(999, _("%s: invalid filename %s.\n"), fname, expandedFileName);
152 if (expandedFileName) {FREE(expandedFileName); expandedFileName = NULL;}
153 return 0;
154 }
155 break;
156 case MOPEN_INVALID_STATUS: default:
157 {
158 if (expandedFileName) {FREE(expandedFileName); expandedFileName = NULL;}
159 Scierror(999, _("%s: invalid status.\n"), fname);
160 return 0;
161 }
162 break;
163 }
164 }
165 }
166 else
167 {
168 Scierror(999,_("%s: Memory allocation error.\n"), fname);
169 return 0;
170 }
171 }
172 else /* double */
173 {
174 double dValue = 0.;
175
176 if ( !getScalarDouble(pvApiCtx, piAddressVarOne, &dValue) )
177 {
178 fileDescriptor = (int)dValue;
179 }
180 else
181 {
182 Scierror(999,_("%s: Memory allocation error.\n"), fname);
183 return 0;
184 }
185 }
186
187 wcReadedStrings = mgetl(fileDescriptor, numberOfLinesToRead, &numberOfLinesReaded, &iErrorMgetl);
188
189 if (bCloseFile)
190 {
191 double dErrClose = 0.;
192 C2F(mclose)(&fileDescriptor, &dErrClose);
193 bCloseFile = FALSE;
194 }
195
196 switch(iErrorMgetl)
197 {
198 case MGETL_NO_ERROR:
199 {
200 if (numberOfLinesReaded == 0)
201 {
202 if (createEmptyMatrix(pvApiCtx, Rhs + 1) != 0)
203 {
204 Scierror(999,_("%s: Memory allocation error.\n"), fname);
205 return 0;
206 }
207 }
208 else
209 {
210 int m = numberOfLinesReaded;
211 int n = 1;
212
213 sciErr = createMatrixOfString(pvApiCtx, Rhs + 1, m, n, wcReadedStrings);
214 if(sciErr.iErr)
215 {
216 printError(&sciErr, 0);
217 return 0;
218 }
219 }
220
221 freeArrayOfString(wcReadedStrings, numberOfLinesReaded);
222 wcReadedStrings = NULL;
223 }
224 break;
225
226 case MGETL_EOF:
227 {
228 if (createEmptyMatrix(pvApiCtx, Rhs + 1) != 0)
229 {
230 Scierror(999,_("%s: Memory allocation error.\n"), fname);
231 return 0;
232 }
233 }
234 break;
235
236 case MGETL_MEMORY_ALLOCATION_ERROR:
237 {
238 if (wcReadedStrings)
239 {
240 freeArrayOfString(wcReadedStrings, numberOfLinesReaded);
241 wcReadedStrings = NULL;
242 }
243 Scierror(999,_("%s: Memory allocation error.\n"), fname);
244 return 0;
245 }
246 break;
247
248 case MGETL_ERROR:
249 {
250 if (wcReadedStrings)
251 {
252 freeArrayOfString(wcReadedStrings, numberOfLinesReaded);
253 wcReadedStrings = NULL;
254 }
255 Scierror(999,_("%s: error.\n"), fname);
256 return 0;
257 }
258 break;
259 }
260
261 LhsVar(1) = Rhs + 1;
262 C2F(putlhsvar)();
263 }
264 else
265 {
266 Scierror(999,_("%s: Wrong type for input argument #%d: String or logical unit expected.\n"), fname, 1);
267 }
268
269 return 0;
40} 270}
41/*--------------------------------------------------------------------------*/ 271/*--------------------------------------------------------------------------*/
diff --git a/scilab/modules/io/src/c/LibScilab_Import.def b/scilab/modules/io/src/c/LibScilab_Import.def
index 07b21fc..061a757 100644
--- a/scilab/modules/io/src/c/LibScilab_Import.def
+++ b/scilab/modules/io/src/c/LibScilab_Import.def
@@ -24,4 +24,5 @@ createvarfromptr_
24intersci_ 24intersci_
25putlhsvar_ 25putlhsvar_
26freeArrayOfString 26freeArrayOfString
27freeArrayOfWideString
27dynamic_setenvtcl \ No newline at end of file 28dynamic_setenvtcl \ No newline at end of file
diff --git a/scilab/modules/io/src/c/io.vcproj b/scilab/modules/io/src/c/io.vcproj
index 094b170..3d9893a 100644
--- a/scilab/modules/io/src/c/io.vcproj
+++ b/scilab/modules/io/src/c/io.vcproj
@@ -392,7 +392,7 @@
392 > 392 >
393 </File> 393 </File>
394 <File 394 <File
395 RelativePath=".\io.rc" 395 RelativePath=".\mgetl.c"
396 > 396 >
397 </File> 397 </File>
398 <File 398 <File
@@ -497,6 +497,10 @@
497 > 497 >
498 </File> 498 </File>
499 <File 499 <File
500 RelativePath=".\mgetl.h"
501 >
502 </File>
503 <File
500 RelativePath=".\resource.h" 504 RelativePath=".\resource.h"
501 > 505 >
502 </File> 506 </File>
@@ -536,6 +540,10 @@
536 <Filter 540 <Filter
537 Name="Resource File" 541 Name="Resource File"
538 > 542 >
543 <File
544 RelativePath=".\io.rc"
545 >
546 </File>
539 </Filter> 547 </Filter>
540 <File 548 <File
541 RelativePath="..\..\io.iss" 549 RelativePath="..\..\io.iss"
diff --git a/scilab/modules/io/src/c/mgetl.c b/scilab/modules/io/src/c/mgetl.c
new file mode 100644
index 0000000..ba6e4f1
--- /dev/null
+++ b/scilab/modules/io/src/c/mgetl.c
@@ -0,0 +1,152 @@
1/*
2* Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3* Copyright (C) 2010 - 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/*--------------------------------------------------------------------------*/
13#include "mgetl.h"
14#include "filesmanagement.h"
15#include "mopen.h"
16#include "MALLOC.h"
17#include "BOOL.h"
18/*--------------------------------------------------------------------------*/
19#define LINE_MAX 4096
20#define CR 13
21#define LF 10
22#define EMPTYSTR ""
23/*--------------------------------------------------------------------------*/
24char **mgetl(int fd, int nbLinesIn, int *nbLinesOut, int *ierr)
25{
26 char **wcLines = NULL;
27 FILE *fa = NULL;
28
29 *ierr = MGETL_ERROR;
30 *nbLinesOut = 0;
31
32 fa = GetFileOpenedInScilab(fd);
33
34 if (fa)
35 {
36 char Line[LINE_MAX * 2];
37 int nbLines = 0;
38
39 if (nbLinesIn < 0)
40 {
41 wcLines = (char **)MALLOC(sizeof(char *));
42 if (wcLines == NULL)
43 {
44 *nbLinesOut = 0;
45 *ierr = MGETL_MEMORY_ALLOCATION_ERROR;
46 return NULL;
47 }
48
49 while ( fgets ( Line, sizeof(Line), fa ) != NULL )
50 {
51 int wcLen = (int)strlen(Line);
52 nbLines++;
53 wcLines = (char **)REALLOC(wcLines, nbLines * sizeof(char *));
54 if (wcLines == NULL)
55 {
56 *nbLinesOut = 0;
57 *ierr = MGETL_MEMORY_ALLOCATION_ERROR;
58 return NULL;
59 }
60
61 wcLines[nbLines - 1] = (char*)MALLOC(sizeof(char) * (wcLen + 1));
62 if (wcLines[nbLines - 1] == NULL)
63 {
64 *nbLinesOut = 0;
65 *ierr = MGETL_MEMORY_ALLOCATION_ERROR;
66 return NULL;
67 }
68
69 if (wcLen >= 1)
70 {
71 /* remove EOL */
72 if ( (Line[wcLen - 1] == CR) || (Line[wcLen - 1] == LF) )
73 {
74 Line[wcLen - 1] = 0;
75 }
76 }
77
78 strcpy(wcLines[nbLines - 1], Line);
79 strcpy(Line, EMPTYSTR);
80 }
81 *nbLinesOut = nbLines;
82 *ierr = MGETL_NO_ERROR;
83 }
84 else
85 {
86 if (nbLinesIn == 0)
87 {
88 *ierr = MGETL_EOF;
89 *nbLinesOut = 0;
90 if (wcLines)
91 {
92 FREE(wcLines);
93 }
94 wcLines = NULL;
95 }
96 else
97 {
98 BOOL bContinue = TRUE;
99 wcLines = (char **)MALLOC(sizeof(char *) * nbLinesIn);
100 if (wcLines == NULL)
101 {
102 *nbLinesOut = 0;
103 *ierr = MGETL_MEMORY_ALLOCATION_ERROR;
104 return NULL;
105 }
106
107 do
108 {
109 if (nbLines < nbLinesIn)
110 {
111 if ( fgets ( Line, sizeof(Line), fa ) != NULL)
112 {
113 int wcLen = (int)strlen(Line);
114 if (wcLen >= 1)
115 {
116 /* remove EOL */
117 if ( (Line[wcLen - 1] == CR) || (Line[wcLen - 1] == LF) )
118 {
119 Line[wcLen - 1] = 0;
120 }
121 }
122 nbLines++;
123 wcLines[nbLines - 1] = (char*)MALLOC(sizeof(char) * (wcLen + 1));
124 if (wcLines[nbLines - 1] == NULL)
125 {
126 *nbLinesOut = 0;
127 *ierr = MGETL_MEMORY_ALLOCATION_ERROR;
128 return NULL;
129 }
130 strcpy(wcLines[nbLines - 1], Line);
131
132 strcpy(Line, EMPTYSTR);
133 }
134 else
135 {
136 bContinue = FALSE;
137 }
138 }
139 else
140 {
141 bContinue = FALSE;
142 }
143 } while (bContinue);
144
145 *nbLinesOut = nbLines;
146 *ierr = MGETL_NO_ERROR;
147 }
148 }
149 }
150 return wcLines;
151}
152/*--------------------------------------------------------------------------*/
diff --git a/scilab/modules/io/src/c/mgetl.h b/scilab/modules/io/src/c/mgetl.h
new file mode 100644
index 0000000..e3ee367
--- /dev/null
+++ b/scilab/modules/io/src/c/mgetl.h
@@ -0,0 +1,44 @@
1/*
2* Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3* Copyright (C) 2010 - 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/*--------------------------------------------------------------------------*/
13#ifndef __MGETL_H__
14#define __MGETL_H__
15
16#include "charEncoding.h"
17
18typedef enum {
19 MGETL_NO_ERROR = 0,
20 MGETL_EOF = 1,
21 MGETL_MEMORY_ALLOCATION_ERROR = 2,
22 MGETL_ERROR = 3
23} mgetlError;
24
25/**
26 * @fn wchar_t ** mgetl(int fd, int nbLinesIn, int *nbLinesOut, int *ierr) #endif
27 *
28 * @brief Mgetls.
29 *
30 * @author Allan Cornet
31 * @date 4/16/2010
32 *
33 * @param fd The file descriptor id.
34 * @param nbLinesIn The nb lines in .
35 * @param [in,out] nbLinesOut If non-null, the nb lines out.
36 * @param [in,out] ierr If non-null, the ierr.
37 *
38 * @return null if it fails, else strings readed.
39**/
40char ** mgetl(int fd, int nbLinesIn, int *nbLinesOut, int *ierr);
41
42
43#endif /* __MGETL_H__ */
44/*--------------------------------------------------------------------------*/