Ignore:
Timestamp:
Dec 5, 2001, 9:37:33 PM (24 years ago)
Author:
umoeller
Message:

Misc changes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/helpers/dosh.c

    r116 r121  
    16771677 +      +-------------------------+------+-----------+-----------+
    16781678 *
     1679 *      In addition, you can specify the XOPEN_BINARY flag:
     1680 *
     1681 *      --  If XOPEN_BINARY is set, no conversion is performed
     1682 *          on read and write.
     1683 *
     1684 *      --  If XOPEN_BINARY is _not_ set, all \n chars are
     1685 *          converted to \r\n on write.
     1686 *
    16791687 *      *ppFile receives a new XFILE structure describing
    16801688 *      the open file, if NO_ERROR is returned.
     
    16841692
    16851693APIRET doshOpen(PCSZ pcszFilename,   // in: filename to open
    1686                 ULONG ulOpenMode,       // in: XOPEN_* mode
     1694                ULONG flOpenMode,       // in: XOPEN_* mode
    16871695                PULONG pcbFile,         // in: new file size (if new file is created)
    16881696                                        // out: file size
     
    16961704                          | OPEN_FLAGS_NOINHERIT;
    16971705
    1698     switch (ulOpenMode)
     1706    switch (flOpenMode & XOPEN_ACCESS_MASK)
    16991707    {
    17001708        case XOPEN_READ_EXISTING:
     
    17031711            fsOpenMode |=   OPEN_SHARE_DENYWRITE
    17041712                          | OPEN_ACCESS_READONLY;
     1713            // _Pmpf((__FUNCTION__ ": opening XOPEN_READ_EXISTING"));
    17051714        break;
    17061715
     
    17101719            fsOpenMode |=   OPEN_SHARE_DENYREADWRITE
    17111720                          | OPEN_ACCESS_READWRITE;
     1721            // _Pmpf((__FUNCTION__ ": opening XOPEN_READWRITE_APPEND"));
    17121722        break;
    17131723
     
    17171727            fsOpenMode |=   OPEN_SHARE_DENYREADWRITE
    17181728                          | OPEN_ACCESS_READWRITE;
     1729            // _Pmpf((__FUNCTION__ ": opening XOPEN_READWRITE_NEW"));
    17191730        break;
    1720     }
    1721 
    1722     if (pcszFilename && fsOpenFlags && pcbFile && ppFile)
     1731
     1732        default:
     1733            arc = ERROR_INVALID_PARAMETER;
     1734    }
     1735
     1736    if ((!arc) && pcszFilename && fsOpenFlags && pcbFile && ppFile)
    17231737    {
    17241738        PXFILE pFile;
     
    17281742
    17291743            ZERO(pFile);
     1744
     1745            // copy open flags
     1746            pFile->flOpenMode = flOpenMode;
    17301747
    17311748            if (!(arc = DosOpen((PSZ)pcszFilename,
     
    17411758
    17421759                if (    (ulAction == FILE_EXISTED)
    1743                      && (ulOpenMode == XOPEN_READWRITE_APPEND)
     1760                     && ((flOpenMode & XOPEN_ACCESS_MASK) == XOPEN_READWRITE_APPEND)
    17441761                   )
    17451762                    // get its size and set ptr to end for append
     
    18141831 *      to find out the length.
    18151832 *
     1833 *      If the file is not in binary mode, all
     1834 *      \n chars are converted to \r\n before
     1835 *      writing.
     1836 *
    18161837 *@@added V0.9.16 (2001-10-19) [umoeller]
     1838 *@@changed V0.9.16 (2001-12-02) [umoeller]: added XOPEN_BINARY \r\n support
    18171839 */
    18181840
     
    18211843                 ULONG cb)
    18221844{
    1823     APIRET arc;
     1845    APIRET arc = NO_ERROR;
    18241846    if (!pcsz)
    18251847        arc = ERROR_INVALID_PARAMETER;
     
    18321854            arc = ERROR_INVALID_PARAMETER;
    18331855        else
    1834             if (!(arc = doshLockFile(pFile)))   // this checks for pFile
     1856        {
     1857            PSZ pszNew = NULL;
     1858
     1859            if (!(pFile->flOpenMode & XOPEN_BINARY))
    18351860            {
    1836                 ULONG cbWritten;
    1837                 if (!(arc = DosWrite(pFile->hf,
    1838                                      (PSZ)pcsz,
    1839                                      cb,
    1840                                      &cbWritten)))
    1841                     pFile->cbCurrent += cbWritten;
    1842 
    1843                 doshUnlockFile(pFile);
     1861                // convert all \n to \r\n:
     1862                // V0.9.16 (2001-12-02) [umoeller]
     1863
     1864                // count all \n first
     1865                ULONG cNewLines = 0;
     1866                PCSZ pSource = pcsz;
     1867                ULONG ul;
     1868                for (ul = 0;
     1869                     ul < cb;
     1870                     ul++)
     1871                {
     1872                    if (*pSource++ == '\n')
     1873                        cNewLines++;
     1874                }
     1875
     1876                if (cNewLines)
     1877                {
     1878                    // we have '\n' chars:
     1879                    // then we need just as many \r chars inserted
     1880                    ULONG cbNew = cb + cNewLines;
     1881                    if (!(pszNew = (PSZ)malloc(cbNew)))
     1882                        arc = ERROR_NOT_ENOUGH_MEMORY;
     1883                    else
     1884                    {
     1885                        PSZ pTarget = pszNew;
     1886                        pSource = pcsz;
     1887                        for (ul = 0;
     1888                             ul < cb;
     1889                             ul++)
     1890                        {
     1891                            CHAR c = *pSource++;
     1892                            if (c == '\n')
     1893                                *pTarget++ = '\r';
     1894                            *pTarget++ = c;
     1895                        }
     1896
     1897                        cb = cbNew;
     1898                    }
     1899                }
    18441900            }
     1901
     1902            if (!arc)
     1903                if (!(arc = doshLockFile(pFile)))   // this checks for pFile
     1904                {
     1905                    ULONG cbWritten;
     1906                    if (!(arc = DosWrite(pFile->hf,
     1907                                         (pszNew)
     1908                                                ? pszNew
     1909                                                : (PSZ)pcsz,
     1910                                         cb,
     1911                                         &cbWritten)))
     1912                        pFile->cbCurrent += cbWritten;
     1913
     1914                    doshUnlockFile(pFile);
     1915                }
     1916
     1917            if (pszNew)
     1918                free(pszNew);
     1919        }
    18451920    }
    18461921
     
    18541929 *
    18551930 *      The internal string buffer is limited to 2000
    1856  *      characters. \n is NOT translated to \r\n before
    1857  *      writing.
     1931 *      characters. Length checking is _not_ performed.
    18581932 */
    18591933
     
    18831957            va_end(arg_ptr);
    18841958
    1885             szTemp[ulLength++] = '\r';
     1959            if (pFile->flOpenMode & XOPEN_BINARY)
     1960                // if we're in binary mode, we need to add \r too
     1961                szTemp[ulLength++] = '\r';
    18861962            szTemp[ulLength++] = '\n';
    18871963
Note: See TracChangeset for help on using the changeset viewer.