Changeset 121 for trunk/src/helpers/dosh.c
- Timestamp:
- Dec 5, 2001, 9:37:33 PM (24 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/helpers/dosh.c
r116 r121 1677 1677 + +-------------------------+------+-----------+-----------+ 1678 1678 * 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 * 1679 1687 * *ppFile receives a new XFILE structure describing 1680 1688 * the open file, if NO_ERROR is returned. … … 1684 1692 1685 1693 APIRET doshOpen(PCSZ pcszFilename, // in: filename to open 1686 ULONG ulOpenMode, // in: XOPEN_* mode1694 ULONG flOpenMode, // in: XOPEN_* mode 1687 1695 PULONG pcbFile, // in: new file size (if new file is created) 1688 1696 // out: file size … … 1696 1704 | OPEN_FLAGS_NOINHERIT; 1697 1705 1698 switch ( ulOpenMode)1706 switch (flOpenMode & XOPEN_ACCESS_MASK) 1699 1707 { 1700 1708 case XOPEN_READ_EXISTING: … … 1703 1711 fsOpenMode |= OPEN_SHARE_DENYWRITE 1704 1712 | OPEN_ACCESS_READONLY; 1713 // _Pmpf((__FUNCTION__ ": opening XOPEN_READ_EXISTING")); 1705 1714 break; 1706 1715 … … 1710 1719 fsOpenMode |= OPEN_SHARE_DENYREADWRITE 1711 1720 | OPEN_ACCESS_READWRITE; 1721 // _Pmpf((__FUNCTION__ ": opening XOPEN_READWRITE_APPEND")); 1712 1722 break; 1713 1723 … … 1717 1727 fsOpenMode |= OPEN_SHARE_DENYREADWRITE 1718 1728 | OPEN_ACCESS_READWRITE; 1729 // _Pmpf((__FUNCTION__ ": opening XOPEN_READWRITE_NEW")); 1719 1730 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) 1723 1737 { 1724 1738 PXFILE pFile; … … 1728 1742 1729 1743 ZERO(pFile); 1744 1745 // copy open flags 1746 pFile->flOpenMode = flOpenMode; 1730 1747 1731 1748 if (!(arc = DosOpen((PSZ)pcszFilename, … … 1741 1758 1742 1759 if ( (ulAction == FILE_EXISTED) 1743 && ( ulOpenMode== XOPEN_READWRITE_APPEND)1760 && ((flOpenMode & XOPEN_ACCESS_MASK) == XOPEN_READWRITE_APPEND) 1744 1761 ) 1745 1762 // get its size and set ptr to end for append … … 1814 1831 * to find out the length. 1815 1832 * 1833 * If the file is not in binary mode, all 1834 * \n chars are converted to \r\n before 1835 * writing. 1836 * 1816 1837 *@@added V0.9.16 (2001-10-19) [umoeller] 1838 *@@changed V0.9.16 (2001-12-02) [umoeller]: added XOPEN_BINARY \r\n support 1817 1839 */ 1818 1840 … … 1821 1843 ULONG cb) 1822 1844 { 1823 APIRET arc ;1845 APIRET arc = NO_ERROR; 1824 1846 if (!pcsz) 1825 1847 arc = ERROR_INVALID_PARAMETER; … … 1832 1854 arc = ERROR_INVALID_PARAMETER; 1833 1855 else 1834 if (!(arc = doshLockFile(pFile))) // this checks for pFile 1856 { 1857 PSZ pszNew = NULL; 1858 1859 if (!(pFile->flOpenMode & XOPEN_BINARY)) 1835 1860 { 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 } 1844 1900 } 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 } 1845 1920 } 1846 1921 … … 1854 1929 * 1855 1930 * 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. 1858 1932 */ 1859 1933 … … 1883 1957 va_end(arg_ptr); 1884 1958 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'; 1886 1962 szTemp[ulLength++] = '\n'; 1887 1963
Note:
See TracChangeset
for help on using the changeset viewer.