comparison src/os_mswin.c @ 2900:4e21fd3cfc30 v7.3.223

updated for version 7.3.223 Problem: MingW cross compilation doesn't work with tiny features. Solution: Move acp_to_enc(), enc_to_utf16() and utf16_to_enc() outside of "#ifdef CLIPBOARD". Fix typo in makefile.
author Bram Moolenaar <bram@vim.org>
date Sun, 19 Jun 2011 01:30:07 +0200
parents fd4224d9ee09
children e5b17a5f6516
comparison
equal deleted inserted replaced
2899:03af3c403014 2900:4e21fd3cfc30
1103 } 1103 }
1104 1104
1105 return ret; 1105 return ret;
1106 } 1106 }
1107 1107
1108 #if defined(FEAT_MBYTE) || defined(PROTO)
1109 /*
1110 * Note: the following two functions are only guaranteed to work when using
1111 * valid MS-Windows codepages or when iconv() is available.
1112 */
1113
1114 /*
1115 * Convert "str" from 'encoding' to UTF-16.
1116 * Input in "str" with length "*lenp". When "lenp" is NULL, use strlen().
1117 * Output is returned as an allocated string. "*lenp" is set to the length of
1118 * the result. A trailing NUL is always added.
1119 * Returns NULL when out of memory.
1120 */
1121 short_u *
1122 enc_to_utf16(char_u *str, int *lenp)
1123 {
1124 vimconv_T conv;
1125 WCHAR *ret;
1126 char_u *allocbuf = NULL;
1127 int len_loc;
1128 int length;
1129
1130 if (lenp == NULL)
1131 {
1132 len_loc = (int)STRLEN(str) + 1;
1133 lenp = &len_loc;
1134 }
1135
1136 if (enc_codepage > 0)
1137 {
1138 /* We can do any CP### -> UTF-16 in one pass, and we can do it
1139 * without iconv() (convert_* may need iconv). */
1140 MultiByteToWideChar_alloc(enc_codepage, 0, str, *lenp, &ret, &length);
1141 }
1142 else
1143 {
1144 /* Use "latin1" by default, we might be called before we have p_enc
1145 * set up. Convert to utf-8 first, works better with iconv(). Does
1146 * nothing if 'encoding' is "utf-8". */
1147 conv.vc_type = CONV_NONE;
1148 if (convert_setup(&conv, p_enc ? p_enc : (char_u *)"latin1",
1149 (char_u *)"utf-8") == FAIL)
1150 return NULL;
1151 if (conv.vc_type != CONV_NONE)
1152 {
1153 str = allocbuf = string_convert(&conv, str, lenp);
1154 if (str == NULL)
1155 return NULL;
1156 }
1157 convert_setup(&conv, NULL, NULL);
1158
1159 length = utf8_to_utf16(str, *lenp, NULL, NULL);
1160 ret = (WCHAR *)alloc((unsigned)((length + 1) * sizeof(WCHAR)));
1161 if (ret != NULL)
1162 {
1163 utf8_to_utf16(str, *lenp, (short_u *)ret, NULL);
1164 ret[length] = 0;
1165 }
1166
1167 vim_free(allocbuf);
1168 }
1169
1170 *lenp = length;
1171 return (short_u *)ret;
1172 }
1173
1174 /*
1175 * Convert an UTF-16 string to 'encoding'.
1176 * Input in "str" with length (counted in wide characters) "*lenp". When
1177 * "lenp" is NULL, use wcslen().
1178 * Output is returned as an allocated string. If "*lenp" is not NULL it is
1179 * set to the length of the result.
1180 * Returns NULL when out of memory.
1181 */
1182 char_u *
1183 utf16_to_enc(short_u *str, int *lenp)
1184 {
1185 vimconv_T conv;
1186 char_u *utf8_str = NULL, *enc_str = NULL;
1187 int len_loc;
1188
1189 if (lenp == NULL)
1190 {
1191 len_loc = (int)wcslen(str) + 1;
1192 lenp = &len_loc;
1193 }
1194
1195 if (enc_codepage > 0)
1196 {
1197 /* We can do any UTF-16 -> CP### in one pass. */
1198 int length;
1199
1200 WideCharToMultiByte_alloc(enc_codepage, 0, str, *lenp,
1201 (LPSTR *)&enc_str, &length, 0, 0);
1202 *lenp = length;
1203 return enc_str;
1204 }
1205
1206 /* Avoid allocating zero bytes, it generates an error message. */
1207 utf8_str = alloc(utf16_to_utf8(str, *lenp == 0 ? 1 : *lenp, NULL));
1208 if (utf8_str != NULL)
1209 {
1210 *lenp = utf16_to_utf8(str, *lenp, utf8_str);
1211
1212 /* We might be called before we have p_enc set up. */
1213 conv.vc_type = CONV_NONE;
1214 convert_setup(&conv, (char_u *)"utf-8",
1215 p_enc? p_enc: (char_u *)"latin1");
1216 if (conv.vc_type == CONV_NONE)
1217 {
1218 /* p_enc is utf-8, so we're done. */
1219 enc_str = utf8_str;
1220 }
1221 else
1222 {
1223 enc_str = string_convert(&conv, utf8_str, lenp);
1224 vim_free(utf8_str);
1225 }
1226
1227 convert_setup(&conv, NULL, NULL);
1228 }
1229
1230 return enc_str;
1231 }
1232 #endif /* FEAT_MBYTE */
1233
1234 /* 1108 /*
1235 * Wait for another process to Close the Clipboard. 1109 * Wait for another process to Close the Clipboard.
1236 * Returns TRUE for success. 1110 * Returns TRUE for success.
1237 */ 1111 */
1238 static int 1112 static int
1434 #if defined(FEAT_MBYTE) && defined(WIN3264) 1308 #if defined(FEAT_MBYTE) && defined(WIN3264)
1435 vim_free(to_free); 1309 vim_free(to_free);
1436 #endif 1310 #endif
1437 } 1311 }
1438 1312
1439 #if (defined(FEAT_MBYTE) && defined(WIN3264)) || defined(PROTO)
1440 /*
1441 * Convert from the active codepage to 'encoding'.
1442 * Input is "str[str_size]".
1443 * The result is in allocated memory: "out[outlen]". With terminating NUL.
1444 */
1445 void
1446 acp_to_enc(str, str_size, out, outlen)
1447 char_u *str;
1448 int str_size;
1449 char_u **out;
1450 int *outlen;
1451
1452 {
1453 LPWSTR widestr;
1454
1455 MultiByteToWideChar_alloc(GetACP(), 0, str, str_size, &widestr, outlen);
1456 if (widestr != NULL)
1457 {
1458 ++*outlen; /* Include the 0 after the string */
1459 *out = utf16_to_enc((short_u *)widestr, outlen);
1460 vim_free(widestr);
1461 }
1462 }
1463 #endif
1464
1465 /* 1313 /*
1466 * Send the current selection to the clipboard. 1314 * Send the current selection to the clipboard.
1467 */ 1315 */
1468 void 1316 void
1469 clip_mch_set_selection(VimClipboard *cbd) 1317 clip_mch_set_selection(VimClipboard *cbd)
1623 if (hMemVim) 1471 if (hMemVim)
1624 GlobalFree(hMemVim); 1472 GlobalFree(hMemVim);
1625 } 1473 }
1626 1474
1627 #endif /* FEAT_CLIPBOARD */ 1475 #endif /* FEAT_CLIPBOARD */
1476
1477 #if defined(FEAT_MBYTE) || defined(PROTO)
1478 /*
1479 * Note: the following two functions are only guaranteed to work when using
1480 * valid MS-Windows codepages or when iconv() is available.
1481 */
1482
1483 /*
1484 * Convert "str" from 'encoding' to UTF-16.
1485 * Input in "str" with length "*lenp". When "lenp" is NULL, use strlen().
1486 * Output is returned as an allocated string. "*lenp" is set to the length of
1487 * the result. A trailing NUL is always added.
1488 * Returns NULL when out of memory.
1489 */
1490 short_u *
1491 enc_to_utf16(char_u *str, int *lenp)
1492 {
1493 vimconv_T conv;
1494 WCHAR *ret;
1495 char_u *allocbuf = NULL;
1496 int len_loc;
1497 int length;
1498
1499 if (lenp == NULL)
1500 {
1501 len_loc = (int)STRLEN(str) + 1;
1502 lenp = &len_loc;
1503 }
1504
1505 if (enc_codepage > 0)
1506 {
1507 /* We can do any CP### -> UTF-16 in one pass, and we can do it
1508 * without iconv() (convert_* may need iconv). */
1509 MultiByteToWideChar_alloc(enc_codepage, 0, str, *lenp, &ret, &length);
1510 }
1511 else
1512 {
1513 /* Use "latin1" by default, we might be called before we have p_enc
1514 * set up. Convert to utf-8 first, works better with iconv(). Does
1515 * nothing if 'encoding' is "utf-8". */
1516 conv.vc_type = CONV_NONE;
1517 if (convert_setup(&conv, p_enc ? p_enc : (char_u *)"latin1",
1518 (char_u *)"utf-8") == FAIL)
1519 return NULL;
1520 if (conv.vc_type != CONV_NONE)
1521 {
1522 str = allocbuf = string_convert(&conv, str, lenp);
1523 if (str == NULL)
1524 return NULL;
1525 }
1526 convert_setup(&conv, NULL, NULL);
1527
1528 length = utf8_to_utf16(str, *lenp, NULL, NULL);
1529 ret = (WCHAR *)alloc((unsigned)((length + 1) * sizeof(WCHAR)));
1530 if (ret != NULL)
1531 {
1532 utf8_to_utf16(str, *lenp, (short_u *)ret, NULL);
1533 ret[length] = 0;
1534 }
1535
1536 vim_free(allocbuf);
1537 }
1538
1539 *lenp = length;
1540 return (short_u *)ret;
1541 }
1542
1543 /*
1544 * Convert an UTF-16 string to 'encoding'.
1545 * Input in "str" with length (counted in wide characters) "*lenp". When
1546 * "lenp" is NULL, use wcslen().
1547 * Output is returned as an allocated string. If "*lenp" is not NULL it is
1548 * set to the length of the result.
1549 * Returns NULL when out of memory.
1550 */
1551 char_u *
1552 utf16_to_enc(short_u *str, int *lenp)
1553 {
1554 vimconv_T conv;
1555 char_u *utf8_str = NULL, *enc_str = NULL;
1556 int len_loc;
1557
1558 if (lenp == NULL)
1559 {
1560 len_loc = (int)wcslen(str) + 1;
1561 lenp = &len_loc;
1562 }
1563
1564 if (enc_codepage > 0)
1565 {
1566 /* We can do any UTF-16 -> CP### in one pass. */
1567 int length;
1568
1569 WideCharToMultiByte_alloc(enc_codepage, 0, str, *lenp,
1570 (LPSTR *)&enc_str, &length, 0, 0);
1571 *lenp = length;
1572 return enc_str;
1573 }
1574
1575 /* Avoid allocating zero bytes, it generates an error message. */
1576 utf8_str = alloc(utf16_to_utf8(str, *lenp == 0 ? 1 : *lenp, NULL));
1577 if (utf8_str != NULL)
1578 {
1579 *lenp = utf16_to_utf8(str, *lenp, utf8_str);
1580
1581 /* We might be called before we have p_enc set up. */
1582 conv.vc_type = CONV_NONE;
1583 convert_setup(&conv, (char_u *)"utf-8",
1584 p_enc? p_enc: (char_u *)"latin1");
1585 if (conv.vc_type == CONV_NONE)
1586 {
1587 /* p_enc is utf-8, so we're done. */
1588 enc_str = utf8_str;
1589 }
1590 else
1591 {
1592 enc_str = string_convert(&conv, utf8_str, lenp);
1593 vim_free(utf8_str);
1594 }
1595
1596 convert_setup(&conv, NULL, NULL);
1597 }
1598
1599 return enc_str;
1600 }
1601 #endif /* FEAT_MBYTE */
1602
1603 #if (defined(FEAT_MBYTE) && defined(WIN3264)) || defined(PROTO)
1604 /*
1605 * Convert from the active codepage to 'encoding'.
1606 * Input is "str[str_size]".
1607 * The result is in allocated memory: "out[outlen]". With terminating NUL.
1608 */
1609 void
1610 acp_to_enc(str, str_size, out, outlen)
1611 char_u *str;
1612 int str_size;
1613 char_u **out;
1614 int *outlen;
1615
1616 {
1617 LPWSTR widestr;
1618
1619 MultiByteToWideChar_alloc(GetACP(), 0, str, str_size, &widestr, outlen);
1620 if (widestr != NULL)
1621 {
1622 ++*outlen; /* Include the 0 after the string */
1623 *out = utf16_to_enc((short_u *)widestr, outlen);
1624 vim_free(widestr);
1625 }
1626 }
1627 #endif
1628 1628
1629 1629
1630 /* 1630 /*
1631 * Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules 1631 * Debugging helper: expose the MCH_WRITE_DUMP stuff to other modules
1632 */ 1632 */