Exemple #1
0
int altFprintf(ALT_FILE *fp, char *format, ...) {
   va_list params;
   va_start(params, format);
   char *string, iVal[10];
   unsigned int ndx, value;
   
   while (*format) {
      if (*format == '%') {
         switch (*++format) {
         case 'c':
            altPutc(va_arg(params, int), fp);
            break;
         case 'i':
            value = va_arg(params, int);
            ndx = 9;
            do {
               iVal[ndx--] = '0' + value % 10;
               value /= 10;
            } while (value > 0);
            
            while (ndx < 9)
               altPutc(iVal[++ndx], fp);
            break;
         case 's':
            string = va_arg(params, char *);
            while (*string)
               altPutc(*string++, fp);
            break;
         default:
            altPutc('%', fp);
            altPutc(*format, fp);
         }
      }
      else
         altPutc(*format, fp);
      format++;
   }
int altFprintf(ALT_FILE *fp, char *format, ...) {
   va_list params;
   va_start(params, format);
   char *string, iVal[NUM_FILES], *temp;
   unsigned int ndx, value;
   int fieldSize, found = 0, star, size, index;
   
   while (*format) {
      if (*format == '%') {        
         size = 0;
         star = 0;        
          if (*++format == '*') {
            format++;
            fieldSize = getVAInt(params);
            star = 1;
         }
         
         switch (*format) {        
         case 'c':
            if (fieldSize > 1 && star) {
               ndx = fieldSize - 1;
               while (ndx--)
                  altPutc(' ', fp);               
            }
               
            altPutc(getVAInt(params), fp);
            break;
         case 'i':
            value = getVAInt(params);              
            
            ndx = NUM_FILES - 1;
            
            do {
               iVal[ndx--] = '0' + value % NUM_FILES;
               value /= NUM_FILES;
               size++;
            } while (value > 0);
            
            if (fieldSize > size && star) { 
               index = fieldSize - size; 
               while (index--)
                  altPutc(' ', fp);               
            }           
            
            while (ndx < NUM_FILES - 1)
               altPutc(iVal[++ndx], fp);
            break;
         case 's':           
            string = getVAStr(params);
            
            temp = string;
            for (; *temp++; size++)
               ;
                              
            if (fieldSize > size && star) { 
               ndx = fieldSize - size;
               while(ndx--)
                  altPutc(' ', fp);               
            }            
            
            while (*string)
               altPutc(*string++, fp);
            break;
         default:
            altPutc('%', fp);
            altPutc(*format, fp);
         }         
      }
      else
         altPutc(*format, fp);
      format++;
   }
   return 1;  // No one checks this anyway.
}