Ignore:
Timestamp:
Aug 31, 2000, 5:00:13 AM (25 years ago)
Author:
bird
Message:

Fixes and enhancements.

File:
1 edited

Legend:

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

    r3629 r4129  
    1 /* $Id: kFile.cpp,v 1.3 2000-05-29 19:45:57 bird Exp $
     1/* $Id: kFile.cpp,v 1.4 2000-08-31 03:00:12 bird Exp $
    22 *
    33 * kFile - Simple (for the time being) file class.
     
    3939    if (!fStatusClean)
    4040    {
    41         rc = (int)DosQueryFileInfo(hFile, FIL_QUERYEASIZE, &filestatus, sizeof(filestatus));
     41        rc = DosQueryFileInfo(hFile, FIL_QUERYEASIZE, &filestatus, sizeof(filestatus));
    4242        fStatusClean = (rc == NO_ERROR);
    4343        if (!fStatusClean && fThrowErrors)
    44             throw (rc);
     44            throw ((int)rc);
    4545    }
    4646    else
     
    6464    {
    6565        ULONG   off;
    66         rc = (int)DosSetFilePtr(hFile, offVirtual, FILE_BEGIN, &off);
     66        rc = DosSetFilePtr(hFile, offVirtual, FILE_BEGIN, &off);
    6767        if (rc != NO_ERROR || off != offVirtual)
    6868        {
    6969            if (fThrowErrors)
    70                 throw (rc);
     70                throw ((int)rc);
    7171            return FALSE;
    7272        }
     
    8686 *                              FALSE: Open the file readwrite appending
    8787 *                                     existing files.
    88  * @author    knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
     88 * @author      knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
    8989 */
    9090kFile::kFile(const char *pszFilename, BOOL fReadOnly/*=TRUE*/)
     
    9393    fThrowErrors(FALSE),
    9494    offVirtual(0),
    95     offReal(0)
     95    offReal(0),
     96    pszFilename(NULL)
    9697{
    9798    ULONG   fulOpenFlags;
     
    113114    }
    114115
    115     rc = (int)DosOpen((PCSZ)pszFilename, &hFile, &ulAction, 0, FILE_NORMAL,
    116                       fulOpenFlags, fulOpenMode, NULL);
     116    rc = DosOpen((PCSZ)pszFilename, &hFile, &ulAction, 0, FILE_NORMAL,
     117                 fulOpenFlags, fulOpenMode, NULL);
    117118    if (rc != NO_ERROR)
    118         throw (rc);
     119        throw ((int)rc);
    119120
    120121    if (!refreshFileStatus())
    121         throw (rc);
     122        throw ((int)rc);
     123
     124    char szFullName[CCHMAXPATH];
     125    if (DosQueryPathInfo(pszFilename, FIL_QUERYFULLNAME, szFullName, sizeof(szFullName)))
     126        strcpy(szFullName, pszFilename);
     127    this->pszFilename = strdup(szFullName);
     128    if (this->pszFilename == NULL)
     129        throw (ERROR_NOT_ENOUGH_MEMORY);
    122130}
    123131
     
    143151    {
    144152        ULONG   cbRead;
    145         rc = (int)DosRead(hFile, pvBuffer, cbBuffer, &cbRead);
     153        rc = DosRead(hFile, pvBuffer, cbBuffer, &cbRead);
    146154        if (rc == NO_ERROR)
    147155        {
     
    152160
    153161    if (fThrowErrors)
    154         throw (rc);
     162        throw ((int)rc);
    155163    return FALSE;
    156164}
     
    171179
    172180/**
     181 * Reads the entire file into a single memory block.
     182 * (The memory block has a '\0' at the end just in case you
     183 *  are using it as a long string.)
     184 * @returns     Pointer to file in memory.
     185 */
     186void *          kFile::readFile() throw(int)
     187{
     188    void *pv;
     189
     190    /* allocate memory for the file */
     191    pv = calloc((size_t)this->getSize() + 1, 1);
     192    if (pv == NULL)
     193    {
     194        if (fThrowErrors)
     195            throw(ERROR_NOT_ENOUGH_MEMORY);
     196        return NULL;
     197    }
     198
     199    /* go the start of the file and read it. */
     200    if (start() && read(pv, this->getSize()))
     201        return pv; // successfull exit!
     202
     203    /* we failed, cleanup and return NULL */
     204    free(pv);
     205    return NULL;
     206}
     207
     208
     209/**
    173210 * Writes <cbBuffer> bytes to the file at the current file position.
    174211 * @returns     success indicator. (TRUE/FALSE)
     
    186223            ULONG   cbWrote;
    187224
    188             rc = (int)DosWrite(hFile, pvBuffer, cbBuffer, &cbWrote);
     225            rc = DosWrite(hFile, pvBuffer, cbBuffer, &cbWrote);
    189226            if (rc == NO_ERROR)
    190227            {
     
    197234
    198235    if (fThrowErrors)
    199         throw(rc);
     236        throw ((int)rc);
    200237    return FALSE;
    201238}
     
    253290    free(pszBuffer);
    254291
    255     return getPos() - offStart;
    256 }
    257 
     292    return (int)(getPos() - offStart);
     293}
     294
     295
     296/**
     297 * Sets the filesize.
     298 * @returns     Success indicator.
     299 * @param       cbFile      New filesize.
     300 *                          Defaults to 0xffffffff, which results in
     301 *                          cutting the file at the current position.
     302 */
     303BOOL            kFile::setSize(unsigned long cbFile/*= ~0UL*/)
     304{
     305    if (cbFile == ~0UL)
     306        cbFile = offVirtual;
     307    rc = DosSetFileSize(hFile, cbFile);
     308    if (rc != NO_ERROR && fThrowErrors)
     309        throw ((int)rc);
     310
     311    return rc == NO_ERROR;
     312}
    258313
    259314
     
    280335
    281336    if (fThrowErrors)
    282         throw(rc);
     337        throw ((int)rc);
    283338    return FALSE;
    284339}
     
    306361    }
    307362    if (fThrowErrors)
    308         throw(rc);
     363        throw ((int)rc);
    309364    return FALSE;
    310365}
Note: See TracChangeset for help on using the changeset viewer.