comparison src/fileio.c @ 835:8bebcabccc2c v7.0e01

updated for version 7.0e01
author vimboss
date Mon, 17 Apr 2006 22:14:47 +0000
parents 5117153003bd
children 5a7843c57316
comparison
equal deleted inserted replaced
834:5117153003bd 835:8bebcabccc2c
1141 * become up to 4 bytes, size must be multiple of 2 1141 * become up to 4 bytes, size must be multiple of 2
1142 * ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be 1142 * ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be
1143 * multiple of 2 1143 * multiple of 2
1144 * ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be 1144 * ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be
1145 * multiple of 4 */ 1145 * multiple of 4 */
1146 real_size = size; 1146 real_size = (int)size;
1147 # ifdef USE_ICONV 1147 # ifdef USE_ICONV
1148 if (iconv_fd != (iconv_t)-1) 1148 if (iconv_fd != (iconv_t)-1)
1149 size = size / ICONV_MULT; 1149 size = size / ICONV_MULT;
1150 else 1150 else
1151 # endif 1151 # endif
1198 if ((int)tlen + n + 1 > size) 1198 if ((int)tlen + n + 1 > size)
1199 { 1199 {
1200 /* Filled up to "size", append partial line. 1200 /* Filled up to "size", append partial line.
1201 * Change NL to NUL to reverse the effect done 1201 * Change NL to NUL to reverse the effect done
1202 * below. */ 1202 * below. */
1203 n = size - tlen; 1203 n = (int)(size - tlen);
1204 for (ni = 0; ni < n; ++ni) 1204 for (ni = 0; ni < n; ++ni)
1205 { 1205 {
1206 if (p[ni] == NL) 1206 if (p[ni] == NL)
1207 ptr[tlen++] = NUL; 1207 ptr[tlen++] = NUL;
1208 else 1208 else
1486 if (codepage == CP_UTF8) 1486 if (codepage == CP_UTF8)
1487 { 1487 {
1488 /* Handle CP_UTF8 input ourselves to be able to handle 1488 /* Handle CP_UTF8 input ourselves to be able to handle
1489 * trailing bytes properly. 1489 * trailing bytes properly.
1490 * Get one UTF-8 character from src. */ 1490 * Get one UTF-8 character from src. */
1491 bytelen = utf_ptr2len_len(src, size); 1491 bytelen = (int)utf_ptr2len_len(src, size);
1492 if (bytelen > size) 1492 if (bytelen > size)
1493 { 1493 {
1494 /* Only got some bytes of a character. Normally 1494 /* Only got some bytes of a character. Normally
1495 * it's put in "conv_rest", but if it's too long 1495 * it's put in "conv_rest", but if it's too long
1496 * deal with it as if they were illegal bytes. */ 1496 * deal with it as if they were illegal bytes. */
1557 /* From UCS-2 to "enc_codepage". If the 1557 /* From UCS-2 to "enc_codepage". If the
1558 * conversion uses the default character "?", 1558 * conversion uses the default character "?",
1559 * the data doesn't fit in this encoding. */ 1559 * the data doesn't fit in this encoding. */
1560 dstlen = WideCharToMultiByte(enc_codepage, 0, 1560 dstlen = WideCharToMultiByte(enc_codepage, 0,
1561 (LPCWSTR)ucs2buf, ucs2len, 1561 (LPCWSTR)ucs2buf, ucs2len,
1562 (LPSTR)dst, (src - dst), 1562 (LPSTR)dst, (int)(src - dst),
1563 replstr, &bad); 1563 replstr, &bad);
1564 if (bad) 1564 if (bad)
1565 found_bad = TRUE; 1565 found_bad = TRUE;
1566 else 1566 else
1567 dst += dstlen; 1567 dst += dstlen;
1597 mch_memmove(conv_rest, src, size); 1597 mch_memmove(conv_rest, src, size);
1598 conv_restlen = size; 1598 conv_restlen = size;
1599 } 1599 }
1600 1600
1601 /* The new size is equal to how much "dst" was advanced. */ 1601 /* The new size is equal to how much "dst" was advanced. */
1602 size = dst - ptr; 1602 size = (long)(dst - ptr);
1603 } 1603 }
1604 else 1604 else
1605 # endif 1605 # endif
1606 # ifdef MACOS_CONVERT 1606 # ifdef MACOS_CONVERT
1607 if (fio_flags & FIO_MACROMAN) 1607 if (fio_flags & FIO_MACROMAN)
1838 /* Reading UTF-8: Check if the bytes are valid UTF-8. 1838 /* Reading UTF-8: Check if the bytes are valid UTF-8.
1839 * Need to start before "ptr" when part of the character was 1839 * Need to start before "ptr" when part of the character was
1840 * read in the previous read() call. */ 1840 * read in the previous read() call. */
1841 for (p = ptr - utf_head_off(buffer, ptr); ; ++p) 1841 for (p = ptr - utf_head_off(buffer, ptr); ; ++p)
1842 { 1842 {
1843 int todo = (ptr + size) - p; 1843 int todo = (int)((ptr + size) - p);
1844 int l; 1844 int l;
1845 1845
1846 if (todo <= 0) 1846 if (todo <= 0)
1847 break; 1847 break;
1848 if (*p >= 0x80) 1848 if (*p >= 0x80)
4536 mch_free_acl(acl); 4536 mch_free_acl(acl);
4537 #endif 4537 #endif
4538 4538
4539 if (errmsg != NULL) 4539 if (errmsg != NULL)
4540 { 4540 {
4541 int numlen = errnum != NULL ? STRLEN(errnum) : 0; 4541 int numlen = errnum != NULL ? (int)STRLEN(errnum) : 0;
4542 4542
4543 attr = hl_attr(HLF_E); /* set highlight for error messages */ 4543 attr = hl_attr(HLF_E); /* set highlight for error messages */
4544 msg_add_fname(buf, 4544 msg_add_fname(buf,
4545 #ifndef UNIX 4545 #ifndef UNIX
4546 sfname 4546 sfname
4956 { 4956 {
4957 /* Convert from UTF-8 to UCS-2, to the start of the buffer. 4957 /* Convert from UTF-8 to UCS-2, to the start of the buffer.
4958 * The buffer has been allocated to be big enough. */ 4958 * The buffer has been allocated to be big enough. */
4959 while (fromlen > 0) 4959 while (fromlen > 0)
4960 { 4960 {
4961 n = utf_ptr2len_len(from, fromlen); 4961 n = (int)utf_ptr2len_len(from, (int)fromlen);
4962 if (n > (int)fromlen) /* incomplete byte sequence */ 4962 if (n > (int)fromlen) /* incomplete byte sequence */
4963 break; 4963 break;
4964 u8c = utf_ptr2char(from); 4964 u8c = utf_ptr2char(from);
4965 *to++ = (u8c & 0xff); 4965 *to++ = (u8c & 0xff);
4966 *to++ = (u8c >> 8); 4966 *to++ = (u8c >> 8);
4975 /* weird overlong sequence */ 4975 /* weird overlong sequence */
4976 ip->bw_conv_error = TRUE; 4976 ip->bw_conv_error = TRUE;
4977 return FAIL; 4977 return FAIL;
4978 } 4978 }
4979 mch_memmove(ip->bw_rest, from, fromlen); 4979 mch_memmove(ip->bw_rest, from, fromlen);
4980 ip->bw_restlen = fromlen; 4980 ip->bw_restlen = (int)fromlen;
4981 } 4981 }
4982 else 4982 else
4983 { 4983 {
4984 /* Convert from enc_codepage to UCS-2, to the start of the 4984 /* Convert from enc_codepage to UCS-2, to the start of the
4985 * buffer. The buffer has been allocated to be big enough. */ 4985 * buffer. The buffer has been allocated to be big enough. */
4986 ip->bw_restlen = 0; 4986 ip->bw_restlen = 0;
4987 needed = MultiByteToWideChar(enc_codepage, 4987 needed = MultiByteToWideChar(enc_codepage,
4988 MB_ERR_INVALID_CHARS, (LPCSTR)from, fromlen, 4988 MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen,
4989 NULL, 0); 4989 NULL, 0);
4990 if (needed == 0) 4990 if (needed == 0)
4991 { 4991 {
4992 /* When conversion fails there may be a trailing byte. */ 4992 /* When conversion fails there may be a trailing byte. */
4993 needed = MultiByteToWideChar(enc_codepage, 4993 needed = MultiByteToWideChar(enc_codepage,
4994 MB_ERR_INVALID_CHARS, (LPCSTR)from, fromlen - 1, 4994 MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen - 1,
4995 NULL, 0); 4995 NULL, 0);
4996 if (needed == 0) 4996 if (needed == 0)
4997 { 4997 {
4998 /* Conversion doesn't work. */ 4998 /* Conversion doesn't work. */
4999 ip->bw_conv_error = TRUE; 4999 ip->bw_conv_error = TRUE;
5002 /* Save the trailing byte for the next call. */ 5002 /* Save the trailing byte for the next call. */
5003 ip->bw_rest[0] = from[fromlen - 1]; 5003 ip->bw_rest[0] = from[fromlen - 1];
5004 ip->bw_restlen = 1; 5004 ip->bw_restlen = 1;
5005 } 5005 }
5006 needed = MultiByteToWideChar(enc_codepage, MB_ERR_INVALID_CHARS, 5006 needed = MultiByteToWideChar(enc_codepage, MB_ERR_INVALID_CHARS,
5007 (LPCSTR)from, fromlen - ip->bw_restlen, 5007 (LPCSTR)from, (int)(fromlen - ip->bw_restlen),
5008 (LPWSTR)to, needed); 5008 (LPWSTR)to, needed);
5009 if (needed == 0) 5009 if (needed == 0)
5010 { 5010 {
5011 /* Safety check: Conversion doesn't work. */ 5011 /* Safety check: Conversion doesn't work. */
5012 ip->bw_conv_error = TRUE; 5012 ip->bw_conv_error = TRUE;
5031 { 5031 {
5032 ip->bw_conv_error = TRUE; 5032 ip->bw_conv_error = TRUE;
5033 return FAIL; 5033 return FAIL;
5034 } 5034 }
5035 } 5035 }
5036 len = to - buf; 5036 len = (int)(to - buf);
5037 } 5037 }
5038 else 5038 else
5039 #endif 5039 #endif
5040 { 5040 {
5041 /* Convert from UCS-2 to the codepage, using the remainder of 5041 /* Convert from UCS-2 to the codepage, using the remainder of
5042 * the conversion buffer. If the conversion uses the default 5042 * the conversion buffer. If the conversion uses the default
5043 * character "0", the data doesn't fit in this encoding, so 5043 * character "0", the data doesn't fit in this encoding, so
5044 * fail. */ 5044 * fail. */
5045 len = WideCharToMultiByte(FIO_GET_CP(flags), 0, 5045 len = WideCharToMultiByte(FIO_GET_CP(flags), 0,
5046 (LPCWSTR)ip->bw_conv_buf, (int)fromlen / sizeof(WCHAR), 5046 (LPCWSTR)ip->bw_conv_buf, (int)fromlen / sizeof(WCHAR),
5047 (LPSTR)to, ip->bw_conv_buflen - fromlen, 0, &bad); 5047 (LPSTR)to, (int)(ip->bw_conv_buflen - fromlen), 0, &bad);
5048 if (bad) 5048 if (bad)
5049 { 5049 {
5050 ip->bw_conv_error = TRUE; 5050 ip->bw_conv_error = TRUE;
5051 return FAIL; 5051 return FAIL;
5052 } 5052 }
7833 if (is_buflocal) 7833 if (is_buflocal)
7834 { 7834 {
7835 /* normalize pat into standard "<buffer>#N" form */ 7835 /* normalize pat into standard "<buffer>#N" form */
7836 sprintf((char *)buflocal_pat, "<buffer=%d>", buflocal_nr); 7836 sprintf((char *)buflocal_pat, "<buffer=%d>", buflocal_nr);
7837 pat = buflocal_pat; /* can modify pat and patlen */ 7837 pat = buflocal_pat; /* can modify pat and patlen */
7838 patlen = STRLEN(buflocal_pat); /* but not endpat */ 7838 patlen = (int)STRLEN(buflocal_pat); /* but not endpat */
7839 } 7839 }
7840 7840
7841 /* 7841 /*
7842 * Find AutoPat entries with this pattern. 7842 * Find AutoPat entries with this pattern.
7843 */ 7843 */