File: sci_stripblanks.c

package info (click to toggle)
scilab 5.2.2-9
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 334,832 kB
  • ctags: 52,586
  • sloc: xml: 526,945; ansic: 223,590; fortran: 163,080; java: 56,934; cpp: 33,840; tcl: 27,936; sh: 20,397; makefile: 9,908; ml: 9,451; perl: 1,323; cs: 614; lisp: 30
file content (130 lines) | stat: -rw-r--r-- 3,696 bytes parent folder | download
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
130

/*
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) INRIA - Cong WU
 *
 * 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-en.txt
 *
 */

/* desc : strips leading and trailing blanks (and tabs) of strings
txt=stripblanks(txt[,tabs])
Parameters
txt : string or matrix of strings
tabs : if TRUE then tabs are also stripped (default value is FALSE)*/
/*------------------------------------------------------------------------*/
#include <string.h>
#include <stdio.h>
#include "gw_string.h"
#include "stack-c.h"
#include "MALLOC.h"
#include "stripblanks.h"
#include "Scierror.h"
#include "freeArrayOfString.h"
#include "localization.h"
/*----------------------------------------------------------------------------*/
int sci_stripblanks(char *fname,unsigned long fname_len)
{
	char **Input_String_Matrix_One = NULL;
	char **Output_String_Matrix = NULL;
	int numRow = 0;        /*@ The row number of the output string matrix*/
	int numCol = 0;        /*@ The col number of the output string matrix*/
	int m1 = 0,n1 = 0,mn = 0,i = 0;
	BOOL bREMOVE_TAB = FALSE; /* DEFAULT no TAB */
	int Type_One = VarType(1);

	CheckRhs(1,2);
	CheckLhs(1,1);

	if (Rhs == 2)
	{
		int Type_Two = VarType(2);

		if (Type_Two == sci_boolean)
		{
			int m2 = 0, n2 = 0, l2 = 0;
			GetRhsVar(2,MATRIX_OF_BOOLEAN_DATATYPE,&m2,&n2,&l2);
			bREMOVE_TAB = (BOOL)*istk(l2);
		}
		else
		{
			Scierror(999,_("%s: Wrong type for input argument #%d: A boolean expected.\n"),fname,2);
			return 0;
		}
	}

	switch (Type_One)
	{
		case sci_matrix :
			{
				/* case stripblanks([]) */
				GetRhsVar(1,MATRIX_OF_DOUBLE_DATATYPE,&m1,&n1,&Input_String_Matrix_One);
				if ( (m1 == 0) && (n1 == 0) )
				{
					int l = 0;
					CreateVar(Rhs+1,MATRIX_OF_DOUBLE_DATATYPE,&m1,&n1,&l);
					LhsVar(1) = Rhs+1 ;
					C2F(putlhsvar)();
					return 0;
				}
				else
				{
					Scierror(999,_("%s: Wrong type for input argument #%d: Matrix of strings or empty matrix expected.\n"),fname,1);
					return 0;
				}
			}
			break;
		case sci_strings :
			{
				GetRhsVar(1,MATRIX_OF_STRING_DATATYPE,&m1,&n1,&Input_String_Matrix_One);
				mn = m1*n1;
			}
			break;
		default :
			Scierror(999,_("%s: Wrong type for input argument #%d: Matrix of strings or empty matrix expected.\n"),fname,1);
			return 0;
	}

	if (mn > 0) Output_String_Matrix = (char**)MALLOC(sizeof(char*)*(mn));
	else Output_String_Matrix = NULL;

	if (Output_String_Matrix == NULL)
	{
		freeArrayOfString(Input_String_Matrix_One,mn);
		Scierror(999,_("%s : No more memory.\n"),fname);
		return 0;
	}

	for (i = 0; i < mn ; i++)  /*@ To malloc a space the same as input string*/
	{
		Output_String_Matrix[i] = (char*)MALLOC(sizeof(char)*(strlen(Input_String_Matrix_One[i])+1));
		if (Output_String_Matrix[i] == NULL)
		{
			freeArrayOfString(Input_String_Matrix_One,mn);
			freeArrayOfString(Output_String_Matrix,i);
			Scierror(999,_("%s : No more memory.\n"),fname);
			return 0;
		}
	}

	/*@ The stripblank function*/
	stripblanks(Input_String_Matrix_One,Output_String_Matrix,mn,bREMOVE_TAB); 

	freeArrayOfString(Input_String_Matrix_One,mn);

	/* put result on scilab stack */
	numRow   = m1;
	numCol   = n1;
	CreateVarFromPtr( Rhs+1,MATRIX_OF_STRING_DATATYPE, &numRow, &numCol, Output_String_Matrix); /*@ Output*/
	LhsVar(1) = Rhs+1 ;
	C2F(putlhsvar)();

	/* free pointers */
	freeArrayOfString(Output_String_Matrix,mn);
	return 0;
}
/*--------------------------------------------------------------------------*/