home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_08_09 / 8n09110a < prev    next >
Text File  |  1990-07-31  |  5KB  |  150 lines

  1.  
  2.  
  3. ********
  4. Listing 1
  5.  
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <ctype.h>
  9. #include <conio.h>
  10.  
  11. /* repeat_format() - Function that generates from a string with
  12. repeat count information, a new format string for printf().
  13. */
  14.  
  15. char *repeat_format(char *format)
  16.      {
  17.      char newfmt[256], tempstr[80], *fptr, *nptr, *tptr;
  18.      int cnt, pcnt;
  19.  
  20.      for (nptr = newfmt, fptr = format; *fptr; fptr++)  
  21.         {
  22.         if (*fptr == '%') 
  23.            {
  24.            /* if percent specifier found */
  25.            tptr = tempstr;     /* hold text in temporary string */ 
  26.            cnt = 0;            /* keep track of repeat count */
  27.            *tptr++ = *fptr++;
  28.            while (isdigit(*fptr)) 
  29.                 { 
  30.                 /* scan till non-digit found */
  31.                 *tptr++ = *fptr;
  32.                 cnt = cnt * 10 + (*fptr - '0');
  33.                 ++fptr;
  34.                 }
  35.             *tptr = 0;          /* terminate temporary string */
  36.             if ((*fptr == '(') ||
  37.                 (*fptr == '%') ||
  38.                 (*fptr == ' '))
  39.                 {
  40.                 /* data should be repeated*/
  41.                 if (*fptr == '(')
  42.                     {
  43.                     /* if data is enclosed in parenthesis */
  44.                     pcnt = 0;      /* count parenthesis */
  45.                     tptr = tempstr;
  46.                     ++fptr;        /* skip past opening paren */
  47.                     for (;;)
  48.                          {
  49.                          if (*fptr == 0) 
  50.                               {
  51.                               break;
  52.                               }
  53.                          else if (*fptr == ')') 
  54.                                {
  55.                                if (pcnt == 0)
  56.                                    {
  57.                                    break;
  58.                                    }
  59.                               else 
  60.                                    {
  61.                                    --pcnt;
  62.                                    *tptr++ = *fptr++;
  63.                                    }
  64.                               }
  65.                          else if (*fptr == '(') 
  66.                               {
  67.                               ++pcnt;
  68.                               *tptr++ = *fptr++;
  69.                               }
  70.                          else 
  71.                               {
  72.                               *tptr++ = *fptr++;
  73.                               }
  74.                          }
  75.                     *tptr = 0;     /* use recursive call to format */
  76.                     strcpy(tempstr, repeat_format(tempstr));
  77.                     /* string within parenthesis */
  78.                     }
  79.                else 
  80.                    { 
  81.                    /* else data not enclosed in parenthesis */
  82.                     tptr = tempstr;
  83.                     for (;;) 
  84.                         {     
  85.                         /* scan till type specifier found */
  86.                         *tptr++ = *fptr;
  87.                         if (strchr(" duoxXfeEfGcs", *fptr)) 
  88.                                break;
  89.                          ++fptr;
  90.                         }
  91.                     *tptr = 0;
  92.                     }
  93.                for (; cnt; cnt--) 
  94.                    {     
  95.                    /* now copy repeated info */
  96.                     for (tptr = tempstr; *tptr; tptr++) 
  97.                         {
  98.                         *nptr++ = *tptr;
  99.                         }
  100.                    }
  101.               }
  102.           else 
  103.                {         
  104.                /* wasn't repeated info, copy exactly, and scan on */
  105.                for (tptr = tempstr; *tptr; tptr++) 
  106.                    {
  107.                    *nptr++ = *tptr;
  108.                    }
  109.                *nptr++ = *fptr;
  110.               }
  111.          }
  112.      else 
  113.          {
  114.          *nptr++ = *fptr;
  115.          }
  116.      }
  117.      *nptr = 0;
  118.      return(newfmt);
  119.    }
  120.  
  121. char *fmtstr[] =    
  122.     /* some sample format strings to be converted */
  123.     {
  124.     " ABC %5%d",
  125.     "%15 %3%d ",
  126.     "%5(X=%d%6 AAA=%4(%c ) %d )",
  127.     "X%d %2 ( A%2d B%4%c ) %3d %2( %2( C%c D%2d))",
  128.     "%2(%3(%3c %d ))",
  129.     ""
  130.     };
  131.  
  132. void main ()  
  133.     {
  134.      
  135.      int i;
  136.      char newstr [250];
  137.  
  138.      for (i = 0;;i++) 
  139.          {
  140.          if (fmtstr[i] [0] == 0) 
  141.                break;
  142.          strcpy(newstr, repeat_format(fmtstr[i]));   
  143.          /* copy it for use locally */                                
  144.          printf ("\r\n\"%s\" = \"%s\"", fmtstr[i], newstr);
  145.          if (getch() == '\x1b')
  146.                break;
  147.          }
  148.    }     
  149.  
  150.