strcat-c.c

Go to the documentation of this file.
00001 #include <ctype.h> 
00002 #include <string.h>
00003 #include <stdio.h>
00004 
00005 #include "machine.h"
00006 
00007 #include "MALLOC.h" /* MALLOC */
00008 
00009 #include "stack-c.h"
00010 
00011 
00012 /**************************************************** 
00013  * y= strcat(str1,sep,[op]) 
00014  * op = 'c' or 'r'
00015  ******************************************************/
00016 int C2F(intstrcat) (char* fname);
00017 /****************************************************/
00018 int C2F(intstrcat) (char* fname) 
00019 { 
00020   char typ = '*';
00021   char **Str,**Str1;
00022   static char def_sep[] ="";
00023   char *sep = def_sep;
00024   static int un=1;
00025   int m1,n1,mn,i,j,k,m2,n2,l2,m3,n3,l3,nchars=0;
00026   Rhs = Max(0, Rhs);
00027 
00028   CheckRhs(1,3);
00029   CheckLhs(1,1);
00030 
00031   switch ( VarType(1)) {
00032   case 10 :
00033     GetRhsVar(1,"S",&m1,&n1,&Str);
00034     mn = m1*n1;  
00035     if (Rhs >= 2) {
00036       GetRhsVar(2,"c",&m2,&n2,&l2);
00037       sep = cstk(l2);
00038     }
00039     if (Rhs >= 3) {
00040       GetRhsVar(3,"c",&m3,&n3,&l3);
00041       if ( m3*n3 != 0) 
00042         typ = cstk(l3)[0];
00043       if (typ != 'c' && typ != 'r' ) {
00044         Scierror(999,"%s: third argument should be 'c' or 'r'\r\n",fname);
00045         return 0;
00046       }
00047     }
00048     switch ( typ ) 
00049         {
00050     case '*' : 
00051       /* just return one string */ 
00052       for ( i = 0 ; i < mn ; i++ ) nchars += strlen(Str[i]);
00053       nchars += (mn-1)*strlen(sep);
00054       CreateVar(Rhs+1,"c",&un,&nchars,&l3);
00055       k=0;
00056       for ( i = 0 ; i < mn ; i++ ) 
00057           {
00058                   for ( j =0 ; j < (int)strlen(Str[i]) ; j++ ) *cstk(l3+ k++) = Str[i][j];
00059                   if ( i != mn-1) for ( j =0 ; j < (int)strlen(sep) ; j++ ) *cstk(l3+ k++) = sep[j];
00060           }
00061           if (Str)
00062           {
00063                   for ( i = 0 ; i < mn ; i++ )
00064                   {
00065                         if (Str[i])
00066                         {
00067                                 FREE(Str[i]);
00068                                 Str[i]=NULL;
00069                         }
00070                   }
00071                   FREE(Str);
00072                   Str=NULL;
00073           }
00074       LhsVar(1) = Rhs+1  ;
00075       break;
00076     case 'c': 
00077       /* return a column matrix */ 
00078       if ( (Str1 = MALLOC((m1+1)*sizeof(char *)))==NULL) 
00079         {
00080           Scierror(999,"%s: Out of memory\r\n",fname);
00081           return 0;
00082         }
00083       Str1[m1]=NULL;
00084       for (i= 0 ; i < m1 ; i++) {
00085         /* length of row i */ 
00086         nchars = 0;
00087         for ( j = 0 ; j < n1 ; j++ ) nchars += strlen(Str[i+ m1*j]);
00088         nchars += (n1-1)*strlen(sep);
00089         if ( (Str1[i]=MALLOC((nchars+1)*sizeof(char)))==NULL) 
00090           {
00091             Scierror(999,"%s: Out of memory\r\n",fname);
00092             return 0;
00093           }
00094         /* fill the string */ 
00095         strcpy(Str1[i],Str[i]); 
00096         for ( j = 1 ; j < n1 ; j++ ) 
00097         {
00098           strcat(Str1[i],sep);
00099           strcat(Str1[i],Str[i+ m1*j]);
00100         }
00101       }
00102       CreateVarFromPtr(Rhs+1,"S", &m1, &un, Str1);
00103           if (Str)
00104           {
00105                   for ( i = 0 ; i < mn ; i++ )
00106                   {
00107                           if (Str[i])
00108                           {
00109                                   FREE(Str[i]);
00110                                   Str[i]=NULL;
00111                           }
00112                   }
00113                   FREE(Str);
00114                   Str=NULL;
00115           }
00116       LhsVar(1) = Rhs+1  ;
00117           if (Str1)
00118           {
00119                   int i2=0;
00120                   for (i2=0;i2<m1+1;i2++)
00121                   {
00122                           if (Str1[i2])
00123                           {
00124                                   FREE(Str1[i2]);
00125                                   Str1[i2]=NULL;
00126                           }
00127                   }
00128                   FREE(Str1);
00129                   Str1=NULL;
00130           }
00131       break;
00132     case 'r': 
00133       /* return a row matrix */ 
00134       if ( (Str1 = MALLOC((n1+1)*sizeof(char *)))==NULL) 
00135         {
00136           Scierror(999,"%s: Out of memory\r\n",fname);
00137           return 0;
00138         }
00139       Str1[n1]=NULL;
00140       for (j= 0 ; j < n1 ; j++) {
00141         /* length of col j */ 
00142         nchars = 0;
00143         for ( i = 0 ; i < m1 ; i++ ) 
00144           nchars += strlen(Str[i+ m1*j]);
00145         nchars += (m1-1)*strlen(sep);
00146         if ( (Str1[j]=MALLOC((nchars+1)*sizeof(char)))==NULL) 
00147           {
00148             Scierror(999,"%s: Out of memory\r\n",fname);
00149             return 0;
00150           }
00151         /* fill the string */ 
00152         strcpy(Str1[j],Str[j*m1]); 
00153         for ( i = 1 ; i < m1 ; i++ ) {
00154           strcat(Str1[j],sep);
00155           strcat(Str1[j],Str[i+ m1*j]);
00156         }
00157       }
00158       CreateVarFromPtr(Rhs+1,"S", &un, &n1, Str1);
00159           if (Str)
00160           {
00161                   for ( i = 0 ; i < mn ; i++ )
00162                   {
00163                           if (Str[i])
00164                           {
00165                                   FREE(Str[i]);
00166                                   Str[i]=NULL;
00167                           }
00168                   }
00169                   FREE(Str);
00170                   Str=NULL;
00171           }
00172           if (Str1)
00173           {
00174                   int i2=0;
00175                   for (i2=0;i2<n1+1;i2++)
00176                   {
00177                           if (Str1[i2])
00178                           {
00179                                   FREE(Str1[i2]);
00180                                   Str1[i2]=NULL;
00181                           }
00182                   }
00183                   FREE(Str1);
00184                   Str1=NULL;
00185           }
00186       LhsVar(1) = Rhs+1  ;
00187       break;
00188     }
00189     break; 
00190   default : 
00191     OverLoad(1);
00192     break;
00193   }
00194   
00195   C2F(putlhsvar)();
00196         return 0;
00197 }

Generated on Sun Mar 4 15:04:01 2007 for Scilab [trunk] by  doxygen 1.5.1