Changeset 101980 in vbox
- Timestamp:
- Nov 8, 2023 1:46:21 PM (15 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/libs/xpcom18a4/xpcom/io/nsNativeCharsetUtils.cpp
r101978 r101980 829 829 830 830 //----------------------------------------------------------------------------- 831 // XP_BEOS 832 //----------------------------------------------------------------------------- 833 #elif defined(XP_BEOS) 834 835 #include "nsAString.h" 831 // default : truncate/zeropad 832 //----------------------------------------------------------------------------- 833 #else 834 836 835 #include "nsReadableUtils.h" 837 #include "nsString.h"838 836 839 837 NS_COM nsresult 840 838 NS_CopyNativeToUnicode(const nsACString &input, nsAString &output) 841 839 { 842 Copy UTF8toUTF16(input, output);840 CopyASCIItoUCS2(input, output); 843 841 return NS_OK; 844 842 } … … 847 845 NS_CopyUnicodeToNative(const nsAString &input, nsACString &output) 848 846 { 849 CopyU TF16toUTF8(input, output);847 CopyUCS2toASCII(input, output); 850 848 return NS_OK; 851 849 } … … 861 859 } 862 860 863 //----------------------------------------------------------------------------- 864 // XP_WIN 865 //----------------------------------------------------------------------------- 866 #elif defined(XP_WIN) 867 868 #include <windows.h> 869 #include "nsAString.h" 870 871 NS_COM nsresult 872 NS_CopyNativeToUnicode(const nsACString &input, nsAString &output) 873 { 874 PRUint32 inputLen = input.Length(); 875 876 nsACString::const_iterator iter; 877 input.BeginReading(iter); 878 879 const char *buf = iter.get(); 880 881 // determine length of result 882 PRUint32 resultLen = 0; 883 int n = ::MultiByteToWideChar(CP_ACP, 0, buf, inputLen, NULL, 0); 884 if (n > 0) 885 resultLen += n; 886 887 // allocate sufficient space 888 output.SetLength(resultLen); 889 if (resultLen > 0) { 890 nsAString::iterator out_iter; 891 output.BeginWriting(out_iter); 892 893 PRUnichar *result = out_iter.get(); 894 895 ::MultiByteToWideChar(CP_ACP, 0, buf, inputLen, result, resultLen); 896 } 897 return NS_OK; 898 } 899 900 NS_COM nsresult 901 NS_CopyUnicodeToNative(const nsAString &input, nsACString &output) 902 { 903 PRUint32 inputLen = input.Length(); 904 905 nsAString::const_iterator iter; 906 input.BeginReading(iter); 907 908 const PRUnichar *buf = iter.get(); 909 910 // determine length of result 911 PRUint32 resultLen = 0; 912 913 int n = ::WideCharToMultiByte(CP_ACP, 0, buf, inputLen, NULL, 0, NULL, NULL); 914 if (n > 0) 915 resultLen += n; 916 917 // allocate sufficient space 918 output.SetLength(resultLen); 919 if (resultLen > 0) { 920 nsACString::iterator out_iter; 921 output.BeginWriting(out_iter); 922 923 // default "defaultChar" is '?', which is an illegal character on windows 924 // file system. That will cause file uncreatable. Change it to '_' 925 const char defaultChar = '_'; 926 927 char *result = out_iter.get(); 928 929 ::WideCharToMultiByte(CP_ACP, 0, buf, inputLen, result, resultLen, 930 &defaultChar, NULL); 931 } 932 return NS_OK; 933 } 934 935 void 936 NS_StartupNativeCharsetUtils() 937 { 938 } 939 940 void 941 NS_ShutdownNativeCharsetUtils() 942 { 943 } 944 945 //----------------------------------------------------------------------------- 946 // XP_OS2 947 //----------------------------------------------------------------------------- 948 #elif defined(XP_OS2) 949 950 #define INCL_DOS 951 #include <os2.h> 952 #include <uconv.h> 953 #include "nsAString.h" 954 #include <ulserrno.h> 955 #include "nsNativeCharsetUtils.h" 956 957 static UconvObject UnicodeConverter = NULL; 958 959 NS_COM nsresult 960 NS_CopyNativeToUnicode(const nsACString &input, nsAString &output) 961 { 962 PRUint32 inputLen = input.Length(); 963 964 nsACString::const_iterator iter; 965 input.BeginReading(iter); 966 const char *inputStr = iter.get(); 967 968 // determine length of result 969 PRUint32 resultLen = inputLen; 970 output.SetLength(resultLen); 971 972 nsAString::iterator out_iter; 973 output.BeginWriting(out_iter); 974 UniChar *result = (UniChar*)out_iter.get(); 975 976 size_t cSubs = 0; 977 size_t resultLeft = resultLen; 978 979 if (!UnicodeConverter) 980 NS_StartupNativeCharsetUtils(); 981 982 int unirc = ::UniUconvToUcs(UnicodeConverter, (void**)&inputStr, &inputLen, 983 &result, &resultLeft, &cSubs); 984 985 NS_ASSERTION(unirc != UCONV_E2BIG, "Path too big"); 986 987 if (unirc != ULS_SUCCESS) { 988 output.Truncate(); 989 return NS_ERROR_FAILURE; 990 } 991 992 // Need to update string length to reflect how many bytes were actually 993 // written. 994 output.Truncate(resultLen - resultLeft); 995 return NS_OK; 996 } 997 998 NS_COM nsresult 999 NS_CopyUnicodeToNative(const nsAString &input, nsACString &output) 1000 { 1001 size_t inputLen = input.Length(); 1002 1003 nsAString::const_iterator iter; 1004 input.BeginReading(iter); 1005 UniChar* inputStr = (UniChar*) NS_CONST_CAST(PRUnichar*, iter.get()); 1006 1007 // maximum length of unicode string of length x converted to native 1008 // codepage is x*2 1009 size_t resultLen = inputLen * 2; 1010 output.SetLength(resultLen); 1011 1012 nsACString::iterator out_iter; 1013 output.BeginWriting(out_iter); 1014 char *result = out_iter.get(); 1015 1016 size_t cSubs = 0; 1017 size_t resultLeft = resultLen; 1018 1019 if (!UnicodeConverter) 1020 NS_StartupNativeCharsetUtils(); 1021 1022 int unirc = ::UniUconvFromUcs(UnicodeConverter, &inputStr, &inputLen, 1023 (void**)&result, &resultLeft, &cSubs); 1024 1025 NS_ASSERTION(unirc != UCONV_E2BIG, "Path too big"); 1026 1027 if (unirc != ULS_SUCCESS) { 1028 output.Truncate(); 1029 return NS_ERROR_FAILURE; 1030 } 1031 1032 // Need to update string length to reflect how many bytes were actually 1033 // written. 1034 output.Truncate(resultLen - resultLeft); 1035 return NS_OK; 1036 } 1037 1038 void 1039 NS_StartupNativeCharsetUtils() 1040 { 1041 ULONG ulLength; 1042 ULONG ulCodePage; 1043 DosQueryCp(sizeof(ULONG), &ulCodePage, &ulLength); 1044 1045 UniChar codepage[20]; 1046 int unirc = ::UniMapCpToUcsCp(ulCodePage, codepage, 20); 1047 if (unirc == ULS_SUCCESS) { 1048 unirc = ::UniCreateUconvObject(codepage, &UnicodeConverter); 1049 if (unirc == ULS_SUCCESS) { 1050 uconv_attribute_t attr; 1051 ::UniQueryUconvObject(UnicodeConverter, &attr, sizeof(uconv_attribute_t), 1052 NULL, NULL, NULL); 1053 attr.options = UCONV_OPTION_SUBSTITUTE_BOTH; 1054 attr.subchar_len=1; 1055 attr.subchar[0]='_'; 1056 ::UniSetUconvObject(UnicodeConverter, &attr); 1057 } 1058 } 1059 } 1060 1061 void 1062 NS_ShutdownNativeCharsetUtils() 1063 { 1064 ::UniFreeUconvObject(UnicodeConverter); 1065 } 1066 1067 //----------------------------------------------------------------------------- 1068 // XP_MAC 1069 //----------------------------------------------------------------------------- 1070 #elif defined(XP_MAC) 1071 1072 #include <UnicodeConverter.h> 1073 #include <TextCommon.h> 1074 #include <Script.h> 1075 #include <MacErrors.h> 1076 #include "nsAString.h" 1077 1078 class nsFSStringConversionMac { 1079 public: 1080 static nsresult UCSToFS(const nsAString& aIn, nsACString& aOut); 1081 static nsresult FSToUCS(const nsACString& ain, nsAString& aOut); 1082 1083 static void CleanUp(); 1084 1085 private: 1086 static TextEncoding GetSystemEncoding(); 1087 static nsresult PrepareEncoder(); 1088 static nsresult PrepareDecoder(); 1089 1090 static UnicodeToTextInfo sEncoderInfo; 1091 static TextToUnicodeInfo sDecoderInfo; 1092 }; 1093 1094 UnicodeToTextInfo nsFSStringConversionMac::sEncoderInfo = nsnull; 1095 TextToUnicodeInfo nsFSStringConversionMac::sDecoderInfo = nsnull; 1096 1097 nsresult nsFSStringConversionMac::UCSToFS(const nsAString& aIn, nsACString& aOut) 1098 { 1099 nsresult rv = PrepareEncoder(); 1100 if (NS_FAILED(rv)) return rv; 1101 1102 OSStatus err = noErr; 1103 char stackBuffer[512]; 1104 1105 aOut.Truncate(); 1106 1107 // for each chunk of |aIn|... 1108 nsReadingIterator<PRUnichar> iter; 1109 aIn.BeginReading(iter); 1110 1111 PRUint32 fragmentLength = PRUint32(iter.size_forward()); 1112 UInt32 bytesLeft = fragmentLength * sizeof(UniChar); 1113 1114 do { 1115 UInt32 bytesRead = 0, bytesWritten = 0; 1116 err = ::ConvertFromUnicodeToText(sEncoderInfo, 1117 bytesLeft, 1118 (const UniChar*)iter.get(), 1119 kUnicodeUseFallbacksMask | kUnicodeLooseMappingsMask, 1120 0, nsnull, nsnull, nsnull, 1121 sizeof(stackBuffer), 1122 &bytesRead, 1123 &bytesWritten, 1124 stackBuffer); 1125 if (err == kTECUsedFallbacksStatus) 1126 err = noErr; 1127 else if (err == kTECOutputBufferFullStatus) { 1128 bytesLeft -= bytesRead; 1129 iter.advance(bytesRead / sizeof(UniChar)); 1130 } 1131 aOut.Append(stackBuffer, bytesWritten); 1132 } 1133 while (err == kTECOutputBufferFullStatus); 1134 1135 return (err == noErr) ? NS_OK : NS_ERROR_FAILURE; 1136 } 1137 1138 nsresult nsFSStringConversionMac::FSToUCS(const nsACString& aIn, nsAString& aOut) 1139 { 1140 nsresult rv = PrepareDecoder(); 1141 if (NS_FAILED(rv)) return rv; 1142 1143 OSStatus err = noErr; 1144 UniChar stackBuffer[512]; 1145 1146 aOut.Truncate(0); 1147 1148 // for each chunk of |aIn|... 1149 nsReadingIterator<char> iter; 1150 aIn.BeginReading(iter); 1151 1152 PRUint32 fragmentLength = PRUint32(iter.size_forward()); 1153 UInt32 bytesLeft = fragmentLength; 1154 1155 do { 1156 UInt32 bytesRead = 0, bytesWritten = 0; 1157 err = ::ConvertFromTextToUnicode(sDecoderInfo, 1158 bytesLeft, 1159 iter.get(), 1160 kUnicodeUseFallbacksMask | kUnicodeLooseMappingsMask, 1161 0, nsnull, nsnull, nsnull, 1162 sizeof(stackBuffer), 1163 &bytesRead, 1164 &bytesWritten, 1165 stackBuffer); 1166 if (err == kTECUsedFallbacksStatus) 1167 err = noErr; 1168 else if (err == kTECOutputBufferFullStatus) { 1169 bytesLeft -= bytesRead; 1170 iter.advance(bytesRead); 1171 } 1172 aOut.Append((PRUnichar *)stackBuffer, bytesWritten / sizeof(PRUnichar)); 1173 } 1174 while (err == kTECOutputBufferFullStatus); 1175 1176 return (err == noErr) ? NS_OK : NS_ERROR_FAILURE; 1177 } 1178 1179 void nsFSStringConversionMac::CleanUp() 1180 { 1181 if (sDecoderInfo) { 1182 ::DisposeTextToUnicodeInfo(&sDecoderInfo); 1183 sDecoderInfo = nsnull; 1184 } 1185 if (sEncoderInfo) { 1186 ::DisposeUnicodeToTextInfo(&sEncoderInfo); 1187 sEncoderInfo = nsnull; 1188 } 1189 } 1190 1191 TextEncoding nsFSStringConversionMac::GetSystemEncoding() 1192 { 1193 OSStatus err; 1194 TextEncoding theEncoding; 1195 1196 err = ::UpgradeScriptInfoToTextEncoding(smSystemScript, kTextLanguageDontCare, 1197 kTextRegionDontCare, NULL, &theEncoding); 1198 1199 if (err != noErr) 1200 theEncoding = kTextEncodingMacRoman; 1201 1202 return theEncoding; 1203 } 1204 1205 nsresult nsFSStringConversionMac::PrepareEncoder() 1206 { 1207 nsresult rv = NS_OK; 1208 if (!sEncoderInfo) { 1209 OSStatus err; 1210 err = ::CreateUnicodeToTextInfoByEncoding(GetSystemEncoding(), &sEncoderInfo); 1211 if (err) 1212 rv = NS_ERROR_FAILURE; 1213 } 1214 return rv; 1215 } 1216 1217 nsresult nsFSStringConversionMac::PrepareDecoder() 1218 { 1219 nsresult rv = NS_OK; 1220 if (!sDecoderInfo) { 1221 OSStatus err; 1222 err = ::CreateTextToUnicodeInfoByEncoding(GetSystemEncoding(), &sDecoderInfo); 1223 if (err) 1224 rv = NS_ERROR_FAILURE; 1225 } 1226 return rv; 1227 } 1228 1229 NS_COM nsresult 1230 NS_CopyNativeToUnicode(const nsACString &input, nsAString &output) 1231 { 1232 return nsFSStringConversionMac::FSToUCS(input, output); 1233 } 1234 1235 NS_COM nsresult 1236 NS_CopyUnicodeToNative(const nsAString &input, nsACString &output) 1237 { 1238 return nsFSStringConversionMac::UCSToFS(input, output); 1239 } 1240 1241 void 1242 NS_StartupNativeCharsetUtils() 1243 { 1244 } 1245 1246 void 1247 NS_ShutdownNativeCharsetUtils() 1248 { 1249 nsFSStringConversionMac::CleanUp(); 1250 } 1251 1252 //----------------------------------------------------------------------------- 1253 // default : truncate/zeropad 1254 //----------------------------------------------------------------------------- 1255 #else 1256 1257 #include "nsReadableUtils.h" 1258 1259 NS_COM nsresult 1260 NS_CopyNativeToUnicode(const nsACString &input, nsAString &output) 1261 { 1262 CopyASCIItoUCS2(input, output); 1263 return NS_OK; 1264 } 1265 1266 NS_COM nsresult 1267 NS_CopyUnicodeToNative(const nsAString &input, nsACString &output) 1268 { 1269 CopyUCS2toASCII(input, output); 1270 return NS_OK; 1271 } 1272 1273 void 1274 NS_StartupNativeCharsetUtils() 1275 { 1276 } 1277 1278 void 1279 NS_ShutdownNativeCharsetUtils() 1280 { 1281 } 1282 1283 #endif 1284 861 #endif 862
Note:
See TracChangeset
for help on using the changeset viewer.