Ignore:
Timestamp:
May 29, 2000, 9:45:58 PM (25 years ago)
Author:
bird
Message:

Added printf method (works for upto 64KB of data).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/common/kFile.cpp

    r3613 r3629  
    1 /* $Id: kFile.cpp,v 1.2 2000-05-27 02:14:20 bird Exp $
     1/* $Id: kFile.cpp,v 1.3 2000-05-29 19:45:57 bird Exp $
    22 *
    33 * kFile - Simple (for the time being) file class.
     
    2020*******************************************************************************/
    2121#include <os2.h>
     22
     23#include <malloc.h>
    2224#include <string.h>
     25#include <stdarg.h>
     26#include <stdio.h>
    2327
    2428#include <kFile.h>
     
    212216
    213217/**
     218 * printf - formatted write.
     219 *
     220 * Lonely '\n's are prefixed with a '\r' to make output conform with
     221 * PC line ending.
     222 *
     223 * @returns     Number of bytes written.
     224 * @param       pszFormat   Format string.
     225 * @param       ...         Ellipcis.
     226 * @remark      Currently limited to 64KB of result data.
     227 */
     228int             kFile::printf(const char *pszFormat, ...) throw (int)
     229{
     230    long        offStart = getPos();
     231    va_list     arg;
     232
     233    /* !QUICK AND DIRTY! */
     234    char *  psz, * pszEnd;
     235    char *  pszBuffer = (char*)malloc(1024*64); //64KB should normally be enough...
     236
     237    va_start(arg, pszFormat);
     238    pszEnd = vsprintf(pszBuffer, pszFormat, arg) + pszBuffer;
     239    va_end(arg);
     240
     241    psz = pszEnd;
     242    while (psz > pszBuffer)
     243    {
     244        if (*psz-- == '\n' && *psz != '\r')
     245        {
     246            memmove(psz+2, psz+1, pszEnd - psz + 1);
     247            psz[1] = '\r';
     248            pszEnd++;
     249        }
     250    }
     251
     252    write(pszBuffer, pszEnd - pszBuffer);
     253    free(pszBuffer);
     254
     255    return getPos() - offStart;
     256}
     257
     258
     259
     260
     261/**
    214262 * Seek relative to the current position.
    215263 * @returns     Success indicator.
Note: See TracChangeset for help on using the changeset viewer.