sciprint.c

Go to the documentation of this file.
00001 /*-----------------------------------------------------------------------------------*/
00002 /* INRIA 2006 */
00003 /* Allan CORNET */
00004 /* Francois VOGEL  sciprint_full function */
00005 /* HUANG Xu */
00006 /*-----------------------------------------------------------------------------------*/ 
00007 #if defined (__STDC__) || defined(_MSC_VER)
00008   #include <stdarg.h>
00009 #else
00010   #include <varargs.h>
00011 #endif 
00012 
00013 #include <stdio.h>
00014 
00015 #ifdef _MSC_VER
00016 #include "../../gui/src/c/wsci/wgnuplib.h"
00017 #else
00018 #include "../../gui/src/c/xsci/x_charproc.h"
00019 #endif
00020 
00021 #include "machine.h"
00022 #include "stack-c.h"
00023 #include "sciprint.h"
00024 #include "MALLOC.h"
00025 /*-----------------------------------------------------------------------------------*/ 
00026 #ifdef _MSC_VER
00027   #define vsnprintf _vsnprintf
00028   extern char *QueryStringError(char *Tag);
00029   TW textwin;
00030 #endif
00031 /*-----------------------------------------------------------------------------------*/ 
00032 #ifdef _MSC_VER
00033   extern int PutString(char *Str);
00034 #else
00035 #include "../../gui/src/c/xsci/x_charproc.h"
00036 #endif
00037 extern int getdiary __PARAMS(());
00038 extern int C2F(xscion)();
00039 extern void diary_nnl __PARAMS((char *str,int *n));
00040 /*-----------------------------------------------------------------------------------*/ 
00041 /* any string of length greater than MAXPRINTF gets truncated in sciprint */
00042 #define MAXPRINTF 512
00043 /* MAXCHARSSCIPRINT_FULL is for sciprint_full - more than this gets truncated */
00044 #define MAXCHARSSCIPRINT_FULL 5000  /* */
00045 /*-----------------------------------------------------------------------------------*/ 
00046 #if defined(__STDC__) || defined(_MSC_VER)
00047   void  sciprint(char *fmt,...) 
00048 #else 
00049   void sciprint(va_alist) va_dcl
00050 #endif 
00051 {
00052         int i;
00053         integer lstr;
00054         va_list ap;
00055         char s_buf[MAXPRINTF];
00056         int count=0;
00057 #if defined(__STDC__) || defined(_MSC_VER)
00058         va_start(ap,fmt);
00059 #else
00060         char *fmt;
00061         va_start(ap);
00062         fmt = va_arg(ap, char *);
00063 #endif
00064 
00065 #if defined(linux) || defined(_MSC_VER)
00066 //      count = vsnprintf (s_buf,MAXPRINTF-1, fmt, ap );
00067 {
00068         #ifdef _MSC_VER
00069                 char *LocalizedString=QueryStringError(fmt);
00070                 if (LocalizedString)
00071                 {
00072                         count= vsnprintf(s_buf,MAXPRINTF-1, LocalizedString, ap );
00073                         if (LocalizedString) {FREE(LocalizedString);LocalizedString=NULL;}
00074                 }
00075                 else
00076                 {
00077                         count= vsnprintf(s_buf,MAXPRINTF-1, fmt, ap );
00078                 }
00079         #else
00080                 count= vsnprintf(s_buf,MAXPRINTF-1, fmt, ap );
00081         #endif
00082         if (count == -1)
00083         {
00084                 s_buf[MAXPRINTF-1]='\0';
00085         }
00086 }
00087 #else
00088         (void )vsprintf(s_buf, fmt, ap );
00089 #endif
00090         lstr=strlen(s_buf);
00091 
00092         C2F(xscion)(&i);
00093         if (i == 0) 
00094         {
00095                 printf("%s",s_buf); 
00096         }
00097         else 
00098         {
00099                 #ifdef _MSC_VER
00100                  TextPutS (&textwin,s_buf);
00101                  PutString(s_buf);
00102                 #else
00103                  C2F(xscisrn)(s_buf,&lstr,0L);
00104                 #endif
00105                 
00106         }
00107         if (getdiary()) diary_nnl(s_buf,&lstr);
00108         va_end(ap);
00109 }
00110 /*-----------------------------------------------------------------------------------*/ 
00111 /* as sciprint but with an added first argument which is ignored (used in do_printf) */
00112 #ifdef _MSC_VER
00113   int sciprint2 (int iv, char *fmt,...)
00114   {
00115           int i, count,lstr;
00116           va_list ap;
00117           char s_buf[MAXPRINTF];
00118           va_start (ap, fmt);
00119           /* next three lines added for diary SS */
00120           count = _vsnprintf (s_buf,MAXPRINTF-1, fmt, ap);
00121           if (count == -1)
00122           {
00123                   s_buf[MAXPRINTF-1]='\0';
00124           }
00125 
00126           lstr = strlen (s_buf);
00127 
00128           C2F (xscion) (&i);
00129           if (i == 0)
00130           {
00131                   printf ("%s", s_buf);
00132           }
00133           else
00134           {
00135                   TextPutS (&textwin, s_buf);
00136                   PutString(s_buf);
00137           }
00138           if (getdiary()) diary_nnl(s_buf,&lstr);
00139 
00140           va_end (ap);
00141           return count;
00142   }
00143 #else
00144 #if defined(__STDC__)
00145   int  sciprint2(int iv,char *fmt,...) 
00146 #else 
00147   int sciprint2(va_alist) va_dcl
00148 #endif 
00149   {
00150           int i,retval;
00151           integer lstr;
00152           va_list ap;
00153           char s_buf[MAXPRINTF];
00154 #if defined(__STDC__)
00155           va_start(ap,fmt);
00156 #else
00157           int iv;
00158           char *fmt;
00159           va_start(ap);
00160           iv = va_arg(ap,int);
00161           fmt = va_arg(ap, char *);
00162 #endif
00163 
00164           C2F(xscion)(&i);
00165           if (i == 0) 
00166           {
00167                   retval= vfprintf(stdout, fmt, ap );
00168           }
00169           else 
00170           {
00171 #ifdef LINUX
00172                   retval= vsnprintf (s_buf,MAXPRINTF-1, fmt, ap);
00173                   if (retval == -1)
00174                   {
00175                           s_buf[MAXPRINTF-1]='\0';
00176                   }
00177 #else
00178                   retval= vsprintf(s_buf, fmt, ap );
00179 #endif
00180 
00181                   lstr=strlen(s_buf);
00182                   C2F(xscisncr)(s_buf,&lstr,0L);
00183           }
00184 
00185           if (getdiary()) 
00186           {
00187 #ifdef LINUX
00188                   retval= vsnprintf (s_buf,MAXPRINTF-1, fmt, ap);
00189                   if (retval == -1)
00190                   {
00191                           s_buf[MAXPRINTF-1]='\0';
00192                   }
00193 #else
00194                   retval= vsprintf(s_buf, fmt, ap );
00195 #endif
00196                   lstr=strlen(s_buf);
00197                   diary_nnl(s_buf,&lstr);
00198           }
00199 
00200           va_end(ap);
00201           return retval;
00202 
00203   }
00204 #endif  
00205 /*-----------------------------------------------------------------------------------*/ 
00206 /* sciprint geared towards long strings (>MAXPRINTF) */
00207 /* the long string is splitted in elements of length equal to the number of columns  */
00208 /* from lines()                                                                      */
00209 #if defined(__STDC__) || defined(_MSC_VER)
00210   void sciprint_full(char *fmt,...) 
00211 #else 
00212   void sciprint_full(va_alist) va_dcl
00213 #endif 
00214 {
00215   integer lstr;
00216   va_list ap;
00217   char *s_buf=NULL;
00218   char *split_s_buf=NULL;
00219   int count=0;
00220   int p_s=0;
00221   static integer colwidth;
00222 
00223   s_buf=MALLOC(sizeof(char)*(MAXCHARSSCIPRINT_FULL+1));
00224   if (s_buf == (char *) 0)
00225   {
00226      sciprint("sciprint_full: No more memory\r\n");
00227      return;
00228   }
00229 
00230   /* number of columns as set by command lines() */
00231   colwidth = C2F(iop).lct[4];  /* lct[4] in C is lct(5) from fortran */
00232   /* clamp to a minimum: value is arbitrary */
00233   if (colwidth < 20) {colwidth=20;}
00234   /* clamp to a maximum: value is selected so that each line fits in a single console line */
00235   /* this is needed because computation of the lines() value in ON_WND_TEXT_WM_SIZE is not */
00236   /* consistent with the limit before a carriage return occurs in TextPutStr - this latter */
00237   /* limit uses lptw->ScreenSize, which is set to x=120,y=80 at init and apparently never  */
00238   /* changed on window resizing                                                            */
00239   if (colwidth > 109) {colwidth=109;}
00240 
00241   split_s_buf=MALLOC(sizeof(char)*(colwidth+1));
00242   if (split_s_buf == (char *) 0)
00243   {
00244      sciprint("sciprint_full: No more memory\r\n");
00245      return;
00246   }
00247 
00248 #if defined(__STDC__) || defined(_MSC_VER)
00249         va_start(ap,fmt);
00250 #else
00251         char *fmt;
00252         va_start(ap);
00253         fmt = va_arg(ap, char *);
00254 #endif
00255 
00256 #if defined(linux) || defined(_MSC_VER)
00257         count = vsnprintf (s_buf,MAXCHARSSCIPRINT_FULL-1, fmt, ap );
00258         if (count == -1)
00259         {
00260                 s_buf[MAXCHARSSCIPRINT_FULL-1]='\0';
00261         }
00262 #else
00263         (void )vsprintf(s_buf, fmt, ap );
00264 #endif
00265 
00266   va_end(ap);
00267 
00268   lstr=strlen(s_buf);
00269 
00270   if (lstr<colwidth)
00271   {
00272      sciprint(s_buf);
00273   } 
00274   else 
00275   {
00276      strncpy(split_s_buf,s_buf+p_s,colwidth-1);
00277      split_s_buf[colwidth]='\0';
00278      p_s=p_s+colwidth-1;
00279      sciprint(split_s_buf);
00280      sciprint("\n");
00281      while (p_s+colwidth-1<(int)lstr) 
00282      {
00283         strncpy(split_s_buf,s_buf+p_s,colwidth-1);
00284         split_s_buf[colwidth]='\0';
00285         p_s=p_s+colwidth-1;
00286         sciprint("  (cont'd) %s",split_s_buf);
00287         sciprint("\n");
00288      }
00289      strncpy(split_s_buf,s_buf+p_s,lstr-p_s);
00290      split_s_buf[lstr-p_s]='\0';
00291      sciprint("     (end) %s",split_s_buf);
00292   }
00293 
00294   if (s_buf){FREE(s_buf);s_buf=NULL;}
00295   if (split_s_buf){FREE(split_s_buf);split_s_buf=NULL;}
00296 
00297 }
00298 /*-----------------------------------------------------------------------------------*/ 
00299 /* sciprint_l is used especially in Scierror, for it would not search the hashtable  */
00300 /* in fact, it is the former version of sciprint                                                                         */
00301 /* changed by HUANG Xu                                                                   */
00302 #if defined(__STDC__) || defined(_MSC_VER)
00303   void  sciprint_l(char *fmt,...) 
00304 #else 
00305   void sciprint_l(va_alist) va_dcl
00306 #endif 
00307 {
00308         int i;
00309         integer lstr;
00310         va_list ap;
00311         char s_buf[MAXPRINTF];
00312         int count=0;
00313 #if defined(__STDC__) || defined(_MSC_VER)
00314         va_start(ap,fmt);
00315 #else
00316         char *fmt;
00317         va_start(ap);
00318         fmt = va_arg(ap, char *);
00319 #endif
00320 
00321 #if defined(linux) || defined(_MSC_VER)
00322         count = vsnprintf (s_buf,MAXPRINTF-1, fmt, ap );
00323         if (count == -1)
00324         {
00325                 s_buf[MAXPRINTF-1]='\0';
00326         }
00327 #else
00328         (void )vsprintf(s_buf, fmt, ap );
00329 #endif
00330         lstr=strlen(s_buf);
00331 
00332         C2F(xscion)(&i);
00333         if (i == 0) 
00334         {
00335                 printf("%s",s_buf); 
00336         }
00337         else 
00338         {
00339                 #ifdef _MSC_VER
00340                  TextPutS (&textwin,s_buf);
00341                  PutString(s_buf);
00342                 #else
00343                  C2F(xscisrn)(s_buf,&lstr,0L);
00344                 #endif
00345                 
00346         }
00347         if (getdiary()) diary_nnl(s_buf,&lstr);
00348         va_end(ap);
00349 }
00350 /*-----------------------------------------------------------------------------------*/

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