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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
/* Copyright INRIA/ENPC */
#include <string.h>
#include "../machine.h"
#include "men_Sutils.h"
#ifdef __STDC__
#include <stdlib.h>
#else
#include <malloc.h>
#endif
#ifdef WIN32
#include "../os_specific/win_mem_alloc.h" /* MALLOC */
#else
#include "../os_specific/sci_mem_alloc.h" /* MALLOC */
#endif
/* Subroutine */
extern int C2F(cvstr) __PARAMS((integer *n, integer *line, char *str, integer *job,long int str_len));
/********************************************************
* generic functions for strings
********************************************************/
void strwidth(char *string, int *max_width, int *height)
{
int width=0,i;
*height=0;
*max_width=1;
for (i = 0 ; i < (int)strlen(string);i++)
{
width++;
if ( string[i]=='\n' || i == strlen(string) -1)
{
*max_width= (*max_width > width ) ? *max_width : width;
width=0;
*height = *height+1;
}
}
}
/********************************************************
* Converts a Scilab String coded as integer array [ a crazy old feature]
* into a C string [ the C string is allocated with maloc ]
* this routine calls a Fortran Function.
* WARNING : we MUST add a last argument giving the size of p
* when calling the cvstr Fortran routine [ see Fortran compiler
* documentation ]
********************************************************/
void ScilabStr2C(int *n, int *Scistring, char **strh, int *ierr)
{
int job=1;
*strh =(char *) MALLOC( (*n)+1);
if ((*strh) == NULL) {*ierr=1; return;}
C2F(cvstr)(n,Scistring,*strh,&job,(long int)*n);
(*strh)[*n]='\0';
}
/********************************************************
* Converts a Scilab array of
* String coded as integer array [ a crazy old feature]
* into a C array of strings [ NULL terminated ]
* as
* char* items[] = {
* "first list entry",
* "second list entry",
* NULL
* };
*
********************************************************/
void ScilabMStr2CM(int *Scistring, int *nstring, int *ptrstrings, char ***strh, int *ierr)
{
char **strings,*p;
int li,ni,*SciS,i;
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];
ScilabStr2C(&ni,SciS,&p,ierr);
strings[i-1]=p;
if ( *ierr == 1) return;
SciS += ni;
}
strings[*nstring]=NULL;
*strh=strings;
}
/********************************************************
* Converts a Scilab array of
* String coded as integer array [ a crazy old feature]
* into a C string where all the Scilab strings are
* separated by '\n' or '\r\n'
* desc,nd,ptrdesc : scilab string info
* strh : the C coded string
********************************************************/
void ScilabMStr2C(int *desc, int *nd, int *ptrdesc, char **strh, int *ierr)
{
int ln,li=1,di=0,*SciS,job=1,i,ni;
char *description,*p;
#ifdef WIN32
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++)
{
p= &(description[di]);
ni=ptrdesc[i]-li;
C2F(cvstr)(&ni,SciS,p,&job,(long int)0);
SciS += ni;
#ifdef WIN32
p[ni]= '\r';
ni=ni+1;
#endif
di += ni+1;
p[ni]= '\n';
li=ptrdesc[i];
}
description[ln-2]='\0';
*strh=description;
}
/********************************************************
* Converts a C string containing \n or \r\n
* into a Scilab matrix of String
********************************************************/
void ScilabC2MStr2(int *res, int *nr, int *ptrres, char *str, int *ierr, int maxchars, int maxlines)
{
int job=0,li=0,n,i,ni;
*nr=0;
ptrres[0]=1;
n=strlen(str);
if (n <= maxchars)
{
str[n]='\n';
for (i=0;i < n+1;i++)
{
if(str[i]=='\n')
{
int ni1;
ni1=ni=i-li;
#ifdef WIN32
if (i > 0 && str[i-1]=='\r' ) ni1=ni-1;
#endif
ptrres[*nr+1]=ptrres[*nr]+ni1;
C2F(cvstr)(&ni1,res,&str[li],&job,(long int)0);
res+=ni1;
li += ni+1;
ni= -1;
*nr += 1;
if (*nr>maxlines) { *ierr=3; return;}
}
}
}
else *ierr=2;
}
/****************************************
* ScilabCM2MStr
* back convertion from a C
* array of string pointers to a Scilab
* description
* nv is the number of strings
* the result is stored in res and ptrres
* Warning : res and ptrres must have been
* allocated in the scilab interface
* ( see : in xawelm.f the xmdial example )
*
* Squash \r in the given string (DFK)
* Done in place.
********************************************/
#ifdef WIN32
void squash_r(char *s)
{
register char *r = s; /* reading point */
register char *w = s; /* writing point */
for (w = r = s; *r != '\0'; r++) {
if ( *r != '\r' ) *w++ = *r;
}
*w = '\0';
}
#endif /* WIN32 */
void ScilabCM2MStr(char **str, int nv, int *res, int *ptrres, int maxchars, int *ierr)
{
int job=0,nr=0,i,ni,ntot=0;
ptrres[0]=1;
for (i= 0 ; i < nv ;i++)
{
#ifdef WIN32
/** cvstr changes \n to ! : we suppress \r in str **/
squash_r(str[i]);
#endif
ni=strlen(str[i]);
ntot += ni;
if ( ntot > maxchars )
{
*ierr=2;
return;
}
ptrres[nr+1]=ptrres[nr]+ni;
nr++;
C2F(cvstr)(&ni,res,str[i],&job,(long int) 0);
res +=ni;
}
}
|