"Fossies" - the Fresh Open Source Software Archive

Member "tidy-html5-5.8.0/src/sprtf.c" (16 Jul 2021, 9311 Bytes) of package /linux/www/tidy-html5-5.8.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "sprtf.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 5.4.0_vs_5.6.0.

    1 /* sprtf.c
    2  * SPRTF - Log output utility - part of the HTML Tidy project
    3  *
    4  * Copyright (c) 1998-2017 Geoff R. McLane and HTACG
    5  *
    6  * See tidy.h for the copyright notice.
    7  */
    8 
    9 #ifdef _MSC_VER
   10 #  pragma warning( disable : 4995 )
   11 #endif
   12 
   13 #include <stdio.h>  /* fopen()... */
   14 #include <string.h> /* strcpy */
   15 #include <stdarg.h> /* va_start, va_end, ... */
   16 
   17 #ifdef _MSC_VER
   18 #  include <WinSock2.h>
   19 #  include <sys/timeb.h>
   20 #  if (defined(UNICODE) || defined(_UNICODE))
   21 #    include <Strsafe.h>
   22 #  endif
   23 #else /* !_MSC_VER */
   24 #  include <sys/time.h> /* gettimeoday(), struct timeval,... */
   25 #endif /* _MSC_VER y/n */
   26 
   27 #include <time.h>
   28 #include <stdlib.h> /* for exit() in unix */
   29 #include "sprtf.h"
   30 
   31 #ifdef ENABLE_DEBUG_LOG
   32 
   33 #ifdef _MSC_VER
   34 #  ifndef _CRT_SECURE_NO_DEPRECATE
   35 #    define _CRT_SECURE_NO_DEPRECATE
   36 #  endif /* #ifndef _CRT_SECURE_NO_DEPRECATE */
   37 #  pragma warning( disable:4996 )
   38 #else
   39 #  define strcmpi strcasecmp
   40 #endif 
   41 
   42 #ifndef MX_ONE_BUF
   43 #  define MX_ONE_BUF 1024
   44 #endif
   45 #ifndef MX_BUFFERS
   46 #  define MX_BUFFERS 1024
   47 #endif
   48 
   49 static char _s_strbufs[MX_ONE_BUF * MX_BUFFERS];
   50 static int iNextBuf = 0;
   51 
   52 char *GetNxtBuf()
   53 {
   54    iNextBuf++;
   55    if(iNextBuf >= MX_BUFFERS)
   56       iNextBuf = 0;
   57    return &_s_strbufs[MX_ONE_BUF * iNextBuf];
   58 }
   59 
   60 #define  MXIO     512
   61 
   62 static char def_log[] = "temptidy.txt"; /* use local log */
   63 static char logfile[264] = "\0";
   64 static FILE * outfile = NULL;
   65 static int addsystime = 0;
   66 static int addsysdate = 0;
   67 static int addstdout = 1;
   68 static int addflush = 1;
   69 static int add2screen = 0;
   70 static int add2listview = 0;
   71 static int append_to_log = 0;
   72 
   73 #ifndef VFP
   74 #  define VFP(a) ( a && ( a != (FILE *)-1 ) )
   75 #endif
   76 
   77 int   add_list_out( int val )
   78 {
   79    int i = add2listview;
   80    add2listview = val;
   81    return i;
   82 }
   83 
   84 int   add_std_out( int val )
   85 {
   86    int i = addstdout;
   87    addstdout = val;
   88    return i;
   89 }
   90 
   91 int   add_screen_out( int val )
   92 {
   93    int i = add2screen;
   94    add2screen = val;
   95    return i;
   96 }
   97 
   98 
   99 int   add_sys_time( int val )
  100 {
  101    int   i = addsystime;
  102    addsystime = val;
  103    return i;
  104 }
  105 
  106 int   add_sys_date( int val )
  107 {
  108    int   i = addsysdate;
  109    addsysdate = val;
  110    return i;
  111 }
  112 
  113 
  114 int   add_append_log( int val )
  115 {
  116    int   i = append_to_log;
  117    append_to_log = val;
  118    return i;
  119 }
  120 
  121 
  122 #ifdef _MSC_VER
  123 static const char *mode = "wb"; /* in window sprtf looks after the line endings */
  124 #else
  125 static const char *mode = "w";
  126 #endif
  127 
  128 int   open_log_file( void )
  129 {
  130    if (logfile[0] == 0)
  131       strcpy(logfile,def_log);
  132    if (append_to_log) {
  133 #ifdef _MSC_VER
  134         mode = "ab"; /* in window sprtf looks after the line endings */
  135 #else
  136         mode = "a";
  137 #endif
  138    }
  139    outfile = fopen(logfile, mode);
  140    if( outfile == 0 ) {
  141       outfile = (FILE *)-1;
  142       sprtf("ERROR: Failed to open log file [%s] ...\n", logfile);
  143       /* exit(1); failed */
  144       return 0;   /* failed */
  145    }
  146    return 1; /* success */
  147 }
  148 
  149 void close_log_file( void )
  150 {
  151    if( VFP(outfile) ) {
  152       fclose(outfile);
  153    }
  154    outfile = NULL;
  155 }
  156 
  157 char * get_log_file( void )
  158 {
  159    if (logfile[0] == 0)
  160       strcpy(logfile,def_log);
  161    if (outfile == (FILE *)-1) /* disable the log file */
  162        return (char *)"none";
  163    return logfile;
  164 }
  165 
  166 void   set_log_file( char * nf, int open )
  167 {
  168    if (logfile[0] == 0)
  169       strcpy(logfile,def_log);
  170    if ( nf && *nf && strcmpi(nf,logfile) ) {
  171       close_log_file(); /* remove any previous */
  172       strcpy(logfile,nf); /* set new name */
  173       if (strcmp(logfile,"none") == 0) { /* if equal 'none' */
  174           outfile = (FILE *)-1; /* disable the log file */
  175       } else if (open) {
  176           open_log_file();  /* and open it ... anything previous written is 'lost' */
  177       } else
  178           outfile = 0; /* else set 0 to open on first write */
  179    }
  180 }
  181 
  182 #ifdef _MSC_VER
  183 int gettimeofday(struct timeval *tp, void *tzp)
  184 {
  185 #ifdef WIN32
  186     struct _timeb timebuffer;
  187     _ftime(&timebuffer);
  188     tp->tv_sec = (long)timebuffer.time;
  189     tp->tv_usec = timebuffer.millitm * 1000;
  190 #else
  191     tp->tv_sec = time(NULL);
  192     tp->tv_usec = 0;
  193 #endif
  194     return 0;
  195 }
  196 
  197 #endif /* _MSC_VER */
  198 
  199 void add_date_stg( char *ps, struct timeval *ptv )
  200 {
  201     time_t curtime;
  202     struct tm * ptm;
  203     curtime = (ptv->tv_sec & 0xffffffff);
  204     ptm = localtime(&curtime);
  205     if (ptm) {
  206         strftime(EndBuf(ps),128,"%Y/%m/%d",ptm);
  207     }
  208 }
  209 
  210 void add_time_stg( char *ps, struct timeval *ptv )
  211 {
  212     time_t curtime;
  213     struct tm * ptm;
  214     curtime = (ptv->tv_sec & 0xffffffff);
  215     ptm = localtime(&curtime);
  216     if (ptm) {
  217         strftime(EndBuf(ps),128,"%H:%M:%S",ptm);
  218     }
  219 }
  220 
  221 char *get_date_stg()
  222 {
  223     char *ps;
  224     struct timeval tv;
  225     gettimeofday( (struct timeval *)&tv, (struct timezone *)0 );
  226     ps = GetNxtBuf();
  227     *ps = 0;
  228     add_date_stg( ps, &tv );
  229     return ps;
  230 }
  231 
  232 char *get_time_stg()
  233 {
  234     char *ps;
  235     struct timeval tv;
  236     gettimeofday( (struct timeval *)&tv, (struct timezone *)0 );
  237     ps = GetNxtBuf();
  238     *ps = 0;
  239     add_time_stg( ps, &tv );
  240     return ps;
  241 }
  242 
  243 char *get_date_time_stg()
  244 {
  245     char *ps;
  246     struct timeval tv;
  247     gettimeofday( (struct timeval *)&tv, (struct timezone *)0 );
  248     ps = GetNxtBuf();
  249     *ps = 0;
  250     add_date_stg( ps, &tv );
  251     strcat(ps," ");
  252     add_time_stg( ps, &tv );
  253     return ps;
  254 }
  255 
  256 static void oi( char * psin )
  257 {
  258     int len, w;
  259     char * ps = psin;
  260     if (!ps)
  261         return;
  262 
  263    len = (int)strlen(ps);
  264    if (len) {
  265 
  266       if( outfile == 0 ) {
  267          open_log_file();
  268       }
  269       if( VFP(outfile) ) {
  270           char *tb;
  271           if (addsysdate) {
  272               tb = GetNxtBuf();
  273               len = sprintf( tb, "%s - %s", get_date_time_stg(), ps );
  274               ps = tb;
  275           } else if( addsystime ) {
  276               tb = GetNxtBuf();
  277               len = sprintf( tb, "%s - %s", get_time_stg(), ps );
  278               ps = tb;
  279           }
  280 
  281          w = (int)fwrite( ps, 1, len, outfile );
  282          if( w != len ) {
  283             fclose(outfile);
  284             outfile = (FILE *)-1;
  285             sprtf("WARNING: Failed write to log file [%s] ...\n", logfile);
  286             exit(1);
  287          } else if (addflush) {
  288             fflush( outfile );
  289          }
  290       }
  291 
  292       if( addstdout ) {
  293          fwrite( ps, 1, len, stderr );  /* 20170917 - Switch to using 'stderr' in place of 'stdout' */
  294       }
  295 #ifdef ADD_LISTVIEW
  296        if (add2listview) {
  297            LVInsertItem(ps);
  298        } 
  299 #endif /* ADD_LISTVIEW */
  300 #ifdef ADD_SCREENOUT
  301        if (add2screen) {
  302           Add_String(ps);    /* add string to screen list */
  303        }
  304 #endif /* #ifdef ADD_SCREENOUT */
  305    }
  306 }
  307 
  308 #ifdef _MSC_VER
  309 /* service to ensure line endings in windows only */
  310 static void prt( char * ps )
  311 {
  312     static char _s_buf[1024];
  313     char * pb = _s_buf;
  314     size_t i, j, k;
  315     char   c, d;
  316     i = strlen(ps);
  317     k = 0;
  318     d = 0;
  319     if(i) {
  320         k = 0;
  321         d = 0;
  322         for( j = 0; j < i; j++ ) {
  323             c = ps[j];
  324             if( c == 0x0d ) {
  325                 if( (j+1) < i ) {
  326                     if( ps[j+1] != 0x0a ) {
  327                         pb[k++] = c;
  328                         c = 0x0a;
  329                     }
  330             } else {
  331                     pb[k++] = c;
  332                     c = 0x0a;
  333                 }
  334             } else if( c == 0x0a ) {
  335                 if( d != 0x0d ) {
  336                     pb[k++] = 0x0d;
  337                 }
  338             }
  339             pb[k++] = c;
  340             d = c;
  341             if( k >= MXIO ) {
  342                 pb[k] = 0;
  343                 oi(pb);
  344                 k = 0;
  345             }
  346         }   /* for length of string */
  347         if( k ) {
  348             pb[k] = 0;
  349             oi( pb );
  350         }
  351     }
  352 }
  353 #endif /* #ifdef _MSC_VER */
  354 
  355 int direct_out_it( char *cp )
  356 {
  357 #ifdef _MSC_VER
  358     prt(cp);
  359 #else
  360     oi(cp);
  361 #endif
  362     return (int)strlen(cp);
  363 }
  364 
  365 /* STDAPI StringCchVPrintf( OUT LPTSTR  pszDest,
  366  *   IN  size_t  cchDest, IN  LPCTSTR pszFormat, IN  va_list argList ); */
  367 int MCDECL sprtf( const char *pf, ... )
  368 {
  369    static char _s_sprtfbuf[M_MAX_SPRTF+4];
  370    char * pb = _s_sprtfbuf;
  371    int   i;
  372    va_list arglist;
  373    va_start(arglist, pf);
  374    i = vsnprintf( pb, M_MAX_SPRTF, pf, arglist );
  375    va_end(arglist);
  376 #ifdef _MSC_VER
  377    prt(pb); /* ensure CR/LF */
  378 #else
  379    oi(pb);
  380 #endif
  381    return i;
  382 }
  383 
  384 #ifdef UNICODE
  385 /* WIDE VARIETY */
  386 static void wprt( PTSTR ps )
  387 {
  388    static char _s_woibuf[1024];
  389    char * cp = _s_woibuf;
  390    int len = (int)lstrlen(ps);
  391    if(len) {
  392       int ret = WideCharToMultiByte( CP_ACP, /* UINT CodePage, // code page */
  393          0, /* DWORD dwFlags,            // performance and mapping flags */
  394          ps,   /* LPCWSTR lpWideCharStr,    // wide-character string */
  395          len,     /* int cchWideChar,          // number of chars in string. */
  396          cp,      /* LPSTR lpMultiByteStr,     // buffer for new string */
  397          1024,    /* int cbMultiByte,          // size of buffer */
  398          NULL,    /* LPCSTR lpDefaultChar,     // default for unmappable chars */
  399          NULL );  /* LPBOOL lpUsedDefaultChar  // set when default char used */
  400       /* oi(cp); */
  401       prt(cp);
  402    }
  403 }
  404 
  405 int MCDECL wsprtf( PTSTR pf, ... )
  406 {
  407    static WCHAR _s_sprtfwbuf[1024];
  408    PWSTR pb = _s_sprtfwbuf;
  409    int   i = 1;
  410    va_list arglist;
  411    va_start(arglist, pf);
  412    *pb = 0;
  413    StringCchVPrintf(pb,1024,pf,arglist);
  414    va_end(arglist);
  415    wprt(pb);
  416    return i;
  417 }
  418 
  419 #endif /* #ifdef UNICODE */
  420 
  421 #endif /* #ifdef ENABLE_DEBUG_LOG */
  422 /* eof - sprtf.c */