home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1998 February / PCOnline_02_1998.iso / filesbbs / dos / listz21s.exe / LSZ_MACS.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-07  |  4.4 KB  |  153 lines

  1. /*
  2.  * This file is part of Listerz v2.0 (VE)
  3.  *
  4.  * Copyright (C) 1995-1997 by Branislav L. Slantchev
  5.  * A Product of Silicon Creations, Inc.
  6.  *
  7.  * This file is distributed under the terms and conditions of the GNU
  8.  * General Public License. For more information, refer to the file
  9.  * Copying.Doc which is included in the archive.
  10. */
  11. #include "lsz_appl.h"
  12. #include "str.h"
  13. #include "pblsdk.h"
  14.  
  15. #ifndef PB_SDK
  16.     #include <string.h>
  17.     #include <time.h>
  18. #else
  19.     #include "pblibc.h"
  20. #endif
  21.  
  22. static void
  23. MakeTimeStr(char *dest, Boolean date)
  24. {
  25.     time_t     timer  = time(0);
  26.     struct tm *tm     = localtime(&timer);
  27.     char      *format = "%H:%M:%S";
  28.  
  29.     if( date )
  30.     {
  31.         switch( CurUser->dateFormat )
  32.         {
  33.             case DATE_DDMMYY: format = "%d-%m-%y"; break;
  34.             case DATE_YYMMDD: format = "%y-%m-%d"; break;
  35.             case DATE_MMDDYY: // fall-through to default
  36.             default         : format = "%m-%d-%y"; break;
  37.         }
  38.     }
  39.     strftime(dest, 9, format, tm);
  40. }
  41.  
  42. // macro formatting: @MACRO<<<|>>>|%%%@
  43.  
  44. // this uses an internal buffer to store the parsed result. a pointer
  45. // to this buffer is then returned to the caller, with all recognized
  46. // macros expanded already. this buffer is overwritten with each call
  47. char*
  48. Application::ParseLineWithMacros(char *line)
  49. {
  50.     char   buf[1024], *pline, *pbuf, *pmacro, *p;
  51.     size_t len  = 0, replen;
  52.     char   just = 0;
  53.  
  54.     buf[0] = EOS;
  55.     if( line )
  56.     {
  57.         for( pbuf = buf, pline = line; EOS != *pline; )
  58.         {
  59.             if( '@' == *pline )
  60.             {    // can we find the end of the token
  61.                 if( 0 != (pmacro = strtok(pline + 1, "@")) )
  62.                 {    // do the actual marco parsing now
  63.                     // pmacro points to the macro text
  64.                     len = strlen(pmacro) + 2; // get requested length
  65.                     if( 0 != (p = strpbrk(pmacro, "<>%")) )
  66.                     {    // we have justification characters
  67.                         just = *p; // save justification character
  68.                         *p = EOS; // cut off the formatting part
  69.                     }
  70.                     // now we have the macro text only, no formatting
  71.                     // len = 0 if it is to be ignored, or has length
  72.                     replen = MakeMacro(pbuf, pmacro, len, just);
  73.                     pbuf += replen;
  74.                     pline += len;
  75.                 }
  76.                 else
  77.                 {
  78.                     pline++;  // skip the current '@' char when no macro
  79.                 }
  80.             }
  81.             else *pbuf++ = *pline++; // not a '@', copy the character
  82.         }
  83.     }
  84.  
  85.     *pbuf = EOS;
  86.     return buf;
  87. }
  88.  
  89. // writes the header/footer info using the fileTemplate file
  90. void
  91. Application::WriteHeaderOrFooter(char *fileTemplate)
  92. {
  93.     FILE *fp = fopen(fileTemplate, "rt");
  94.     char  buf[512];
  95.  
  96.     if( fp )
  97.     {
  98.         fgets(buf, sizeof(buf), fp);
  99.         while( !feof(fp) )
  100.         {
  101.             fputs(ParseLineWithMacros(buf), m_fp);
  102.             fgets(buf, sizeof(buf), fp);
  103.         }
  104.         fclose(fp);
  105.     }
  106. }
  107.  
  108. // returns the length of the replacement text
  109. int
  110. Application::MakeMacro(char *dest, char *src, int maxlen, char justChar)
  111. {
  112.     char buf[255], numbuf[50];
  113.     int  retval = 0;
  114.  
  115.     strupr(src);
  116.     buf[0]    = EOS;
  117.     numbuf[0] = EOS;
  118.  
  119.     if( STREQU("AREANAME", src) ) strcpy(buf, m_CurArea.name);
  120.     else if( STREQU("PROGID", src) ) sprintf(buf, "%s %s", s_AppName, s_Version);
  121.     else if( STREQU("AREANUM", src) ) sprintf(numbuf, "%d", m_CurAreaNo);
  122.     else if( STREQU("AREAFILES", src) ) sprintf(numbuf, "%lu", m_AreaFiles);
  123.     else if( STREQU("AREASIZE", src) ) sprintf(numbuf, "%lu", m_AreaBytes);
  124.     else if( STREQU("AREASIZEK", src) ) sprintf(numbuf, "%lu", m_AreaBytes/1024);
  125.     else if( STREQU("AREAFDLS", src) ) sprintf(numbuf, "%lu", m_AreaFdls);
  126.     else if( STREQU("TOTALFILES", src) ) sprintf(numbuf, "%lu", m_TotalFiles);
  127.     else if( STREQU("TOTALSIZE", src) ) sprintf(numbuf, "%lu", m_TotalKbytes * 1024L + m_DeltaBytes);
  128.     else if( STREQU("TOTALSIZEK", src) ) sprintf(numbuf, "%lu", m_TotalKbytes);
  129.     else if( STREQU("TOTALFDLS", src) ) sprintf(numbuf, "%lu", m_TotalFdls);
  130.     else if( STREQU("TOTALAREAS", src) ) sprintf(numbuf, "%d", m_TotalAreas);
  131.     else if( STREQU("DATE", src) ) MakeTimeStr(buf, True);
  132.     else if( STREQU("TIME", src) ) MakeTimeStr(buf, False);
  133.     // if we have a number formatted, put the commas
  134.     if( EOS != numbuf[0] ) strcma(buf, numbuf);
  135.  
  136.     if( maxlen && justChar )
  137.     {    // we must do justification
  138.         buf[maxlen] = EOS; // cut off excess in macro replacement
  139.         if( '<' == justChar ) sprintf(dest, "%-*s", maxlen, buf);
  140.         else if( '%' == justChar ) strcent(dest, buf, maxlen);
  141.         else sprintf(dest, "%*s", maxlen, buf);
  142.         dest[maxlen] = EOS; // just in case
  143.         retval = maxlen;
  144.     }
  145.     else
  146.     {    // straight copying
  147.         retval = strlen(buf);
  148.         strcpy(dest, buf);
  149.     }
  150.  
  151.     return retval;
  152. }
  153.