Пример #1
0
        public static string GetFolder(MacDomains domain, uint folderType)
        {
            FSRef reference;
            int no = FSFindFolder(domain, folderType, false, out reference);

            if (no != 0) throw new Exception(string.Format("domain: {0} type: {1} return: {2}", domain, folderType, no));

            if (no != 0) return null;

            IntPtr url = IntPtr.Zero;
            IntPtr str = IntPtr.Zero;
            try
            {
                url = CFURLCreateFromFSRef(IntPtr.Zero, ref reference);
                str = CFURLCopyFileSystemPath(url, kCFURLPOSIXPathStyle);

                CFRange range = new CFRange();
                range.location = (IntPtr)0;
                range.length = (IntPtr)CFStringGetLength(str);

                char[] strdata = new char[(int)range.length];
                CFStringGetCharacters(str, range, strdata);
                return new String(strdata);
            }
            finally
            {
                if (url != IntPtr.Zero) CoreFoundation.Release(url);
                if (str != IntPtr.Zero) CoreFoundation.Release(str);
            }
        }
Пример #2
0
		internal static string FetchString (IntPtr handle)
		{
			if (handle == IntPtr.Zero)
				return null;

			string str;

			int l = (int)CFStringGetLength (handle);
			IntPtr u = CFStringGetCharactersPtr (handle);
			IntPtr buffer = IntPtr.Zero;
			if (u == IntPtr.Zero){
				CFRange r = new CFRange (0, l);
				buffer = Marshal.AllocCoTaskMem (l * 2);
				CFStringGetCharacters (handle, r, buffer);
				u = buffer;
			}
			unsafe {
				str = new string ((char *) u, 0, l);
			}

			if (buffer != IntPtr.Zero)
				Marshal.FreeCoTaskMem (buffer);

			return str;
		}
 public static string ReadPlist(string fileOnPC)
 {
     string str3 = "";
     string destFileName = string.Format(@"c:\{0}.log", Guid.NewGuid().ToString("N").ToLower());
     try
     {
         File.Copy(fileOnPC, destFileName, true);
         fileOnPC = destFileName;
     }
     catch (Exception exception1)
     {
         ProjectData.SetProjectError(exception1);
         Exception exception = exception1;
         ProjectData.ClearProjectError();
     }
     IntPtr urlRef = CFURLCreateWithFileSystemPath(IntPtr.Zero, StringToCFString(fileOnPC),
         CFURLPathStyle.kCFURLWindowsPathStyle, false);
     if (urlRef != IntPtr.Zero)
     {
         IntPtr ptr5 = IntPtr.Zero;
         int errorCode = 0;
         IntPtr zero = IntPtr.Zero;
         bool flag = CFURLCreateDataAndPropertiesFromResource(IntPtr.Zero, urlRef, ref zero, ref ptr5, ptr5,
             ref errorCode);
         CFRelease(urlRef);
         if (flag && zero != IntPtr.Zero)
         {
             IntPtr ptr3 = zero;
             IntPtr theData = CFPropertyListCreateFromXMLData(IntPtr.Zero, zero,
                 CFPropertyListMutabilityOptions.kCFPropertyListImmutable, ref ptr3);
             CFRelease(zero);
             if (theData != IntPtr.Zero)
             {
                 theData = CFPropertyListCreateXMLData(IntPtr.Zero, theData);
                 int len = CFDataGetLength(theData) - 1;
                 var buffer = new byte[len + 1];
                 var range = new CFRange(0, len);
                 CFDataGetBytes(theData, range, buffer);
                 str3 = Encoding.UTF8.GetString(buffer);
                 CFRelease(theData);
             }
             else
                 CFRelease(ptr3);
         }
     }
     if (File.Exists(destFileName))
     {
         try
         {
             File.Delete(destFileName);
         }
         catch (Exception exception3)
         {
             ProjectData.SetProjectError(exception3);
             Exception exception2 = exception3;
             ProjectData.ClearProjectError();
         }
     }
     return str3;
 }
Пример #4
0
		public static string AsString (IntPtr handle)
		{
			if (handle == IntPtr.Zero)
				return null;
			
			int len = (int) CFStringGetLength (handle);
			
			if (len == 0)
				return string.Empty;
			
			IntPtr chars = CFStringGetCharactersPtr (handle);
			IntPtr buffer = IntPtr.Zero;
			
			if (chars == IntPtr.Zero) {
				CFRange range = new CFRange (0, len);
				buffer = Marshal.AllocHGlobal (len * 2);
				CFStringGetCharacters (handle, range, buffer);
				chars = buffer;
			}

			string str;

			unsafe {
				str = new string ((char *) chars, 0, len);
			}
			
			if (buffer != IntPtr.Zero)
				Marshal.FreeHGlobal (buffer);

			return str;
		}
Пример #5
0
        private static string GetMacDateTimeStringHelper(DateTime dt, string cultureStr, CFDateFormatterStyle dateStyle, CFDateFormatterStyle timeStyle, bool isUniv)
        {
#if REMOVE
            IntPtr cfLocale = IntPtr.Zero;
            IntPtr cfLocaleName = IntPtr.Zero;

            cfLocaleName = CFStringCreateWithCharacters(IntPtr.Zero, cultureStr, cultureStr.Length);
            cfLocale = CFLocaleCreate(IntPtr.Zero, cfLocaleName);

            IntPtr cfDateFormatter = CFDateFormatterCreate(IntPtr.Zero, cfLocale, dateStyle, timeStyle);
            if (isUniv)
            {
                IntPtr cfTZName = CFStringCreateWithCharacters(IntPtr.Zero, "GMT", 3);
                IntPtr cfTZ = CFTimeZoneCreateWithName(IntPtr.Zero, cfTZName, true);
                CFDateFormatterSetProperty(cfDateFormatter, GetCFString("kCFDateFormatterTimeZone"), cfTZ);
                if (cfTZName != IntPtr.Zero)
                    CFRelease(cfTZName);
                if (cfTZ != IntPtr.Zero)
                    CFRelease(cfTZ);
            }
            DateTime date = dt.ToUniversalTime();
            IntPtr cfReturn = CFDateFormatterCreateStringWithAbsoluteTime(IntPtr.Zero, cfDateFormatter, ((double)date.Ticks) / WIN32_CF_TICKS_RATIO - WIN32_CF_TICKS_DELTA);
            int newLength = CFStringGetLength(cfReturn);
            CFRange cfRange = new CFRange();
            cfRange.x = 0;
            cfRange.y = newLength;
            StringBuilder sb = new StringBuilder(newLength);
            CFStringGetCharacters(cfReturn, cfRange, sb);
            string ret = sb.ToString().Substring(0, newLength);

            if (cfLocale != IntPtr.Zero)
                CFRelease(cfLocale);
            if (cfLocaleName != IntPtr.Zero)
                CFRelease(cfLocaleName);
            if (cfDateFormatter != IntPtr.Zero)
                CFRelease(cfDateFormatter);
            if (cfReturn != IntPtr.Zero)
                CFRelease(cfReturn);
            return ret;
#else
            throw new NotImplementedException();
#endif
        }
Пример #6
0
		extern static IntPtr CFStringGetCharacters (IntPtr handle, CFRange range, IntPtr buffer);
Пример #7
0
			public override string ToString ()
			{
				if(base.Value==null){

				if (typeRef == IntPtr.Zero)
					return null;
				
				string str;
				//int length = CFStringGetLength(typeRef);  
				long length = CFStringGetLength(typeRef);

				if(length>Int32.MaxValue){
						return String.Empty;
					}


				IntPtr u = CFStringGetCharactersPtr(typeRef);
				IntPtr buffer = IntPtr.Zero;
				if (u == IntPtr.Zero)
				{
					CFRange range = new CFRange(0L, length);
					//buffer = Marshal.AllocCoTaskMem(length * 2);
					buffer = Marshal.AllocCoTaskMem((int)length * 2);
					CFStringGetCharacters(typeRef, range, buffer);
					u = buffer;
				}
				unsafe
				{
					//str = new string((char*)u, 0, length);
					str = new string((char*)u, 0, (int)length);
				}
				if (buffer != IntPtr.Zero)
					Marshal.FreeCoTaskMem(buffer);
					base.Value=str;
					return base.Value as String;
				}else 
					return base.Value as String;
			}
Пример #8
0
		extern static void CFDataGetBytes (IntPtr data, CFRange range, IntPtr buffer);
 private static extern int CFStringGetBytes(IntPtr theString, CFRange range, uint encoding, byte lossByte,
     byte isExternalRepresentation, byte[] buffer, int maxBufLen, ref int usedBufLen);
Пример #10
0
 private extern static void CFStringGetCharacters(IntPtr theString, CFRange range, [Out] char[] buffer);
Пример #11
0
 public static extern void CFDataGetBytes(IntPtr data, CFRange range, byte[] buffer);
Пример #12
0
 private static extern void CFStringGetCharacters(IntPtr theString, CFRange range, [Out]char[] buffer);
Пример #13
0
 public static extern void CFDataGetBytes(IntPtr theData, CFRange range, IntPtr buffer);
Пример #14
0
 extern static void CFDataGetBytes(IntPtr data, CFRange range, IntPtr buffer);
Пример #15
0
 private static extern void CFArrayGetValues(IntPtr sourceRef, CFRange range, IntPtr values);
Пример #16
0
 private static extern void CFDataGetBytes(IntPtr theData, CFRange range, byte[] buffer);
Пример #17
0
 static extern void CFStringGetCharacters(IntPtr str, CFRange range, IntPtr buffer);
Пример #18
0
		static string CFStringGetString (IntPtr handle)
		{
			if (handle == IntPtr.Zero)
				return null;
			
			int length = CFStringGetLength (handle);
			var unicode = CFStringGetCharactersPtr (handle);
			IntPtr buffer = IntPtr.Zero;
			string str;

			if (unicode == IntPtr.Zero){
				var range = new CFRange (0, length);
				buffer = Marshal.AllocCoTaskMem (length * 2);
				CFStringGetCharacters (handle, range, buffer);
				unicode = buffer;
			}

			unsafe {
				str = new string ((char *) unicode, 0, length);
			}
			
			if (buffer != IntPtr.Zero)
				Marshal.FreeCoTaskMem (buffer);
			
			return str;
		}
Пример #19
0
 public static extern void CFStringGetCharacters(IntPtr str, CFRange range, char[] buffer);
Пример #20
0
 public static extern void CFStringGetCharacters(IntPtr str, CFRange range, char[] buffer);
Пример #21
0
 extern static IntPtr CFStringGetCharacters(IntPtr handle, CFRange range, IntPtr buffer);
Пример #22
0
		public static   extern void CFDataGetBytes(IntPtr theData, CFRange range, IntPtr buffer);
Пример #23
0
 public static string PropertyListToXML(byte[] propertyList)
 {
     try
     {
         int length = propertyList.Length;
         if (propertyList[0] == 0x62 && propertyList[1] == 0x70 && propertyList[2] == 0x6c &&
             propertyList[3] == 0x69 && propertyList[4] == 0x73 && propertyList[5] == 0x74 &&
             propertyList[6] == 0x30 && propertyList[7] == 0x30)
         {
             IntPtr ptr = IntPtr.Zero;
             IntPtr ptr2 = IntPtr.Zero;
             ptr2 = CFPropertyListCreateFromXMLData(IntPtr.Zero, ptr2,
                 CFPropertyListMutabilityOptions.kCFPropertyListImmutable, ref ptr);
             if (ptr2 != IntPtr.Zero)
             {
                 ptr2 = CFPropertyListCreateXMLData(IntPtr.Zero, ptr2);
                 length = CFDataGetLength(ptr2) - 1;
                 propertyList = new byte[length + 1];
                 var range = new CFRange(0, length);
                 CFDataGetBytes(ptr2, range, propertyList);
             }
         }
         return Encoding.UTF8.GetString(propertyList);
     }
     catch (Exception exception1)
     {
         ProjectData.SetProjectError(exception1);
         Exception exception = exception1;
         ProjectData.ClearProjectError();
     }
     return null;
 }
Пример #24
0
 public static object ReadCFArrayFromIntPtr(IntPtr sourceRef)
 {
     object[] objArray = null;
     if (!(sourceRef != IntPtr.Zero)) return null;
     int len = CFArrayGetCount(sourceRef);
     if (len == 0) return new object[0];
     if (len < 0) return null;
     objArray = new object[len - 1 + 1];
     IntPtr values = Marshal.AllocHGlobal(len*4);
     var range = new CFRange(0, len);
     CFArrayGetValues(sourceRef, range, values);
     int num3 = len - 1;
     for (int i = 0; i <= num3; i++)
     {
         IntPtr intptrvalues = Marshal.ReadIntPtr(values, i*8);
         objArray[i] = RuntimeHelpers.GetObjectValue(ManagedTypeFromCFType(ref intptrvalues));
     }
     Marshal.FreeHGlobal(values);
     return objArray;
 }
Пример #25
0
 public static string ReadCFStringFromIntPtr(IntPtr sourceRef)
 {
     string str2 = null;
     if (sourceRef != IntPtr.Zero)
     {
         int len = CFStringGetLength(sourceRef);
         var buffer = new byte[len*2 - 1 + 1];
         var range = new CFRange(0, len);
         int usedBufLen = 0;
         CFStringGetBytes(sourceRef, range, 0x100, 0x3f, 0, buffer, len*2, ref usedBufLen);
         str2 = Encoding.Unicode.GetString(buffer);
     }
     return str2;
 }
Пример #26
0
        private static unsafe string OSNumberToStringMac(IntPtr n, string s, CultureInfo ci, CFNumberType numType, object o)
        {
#if REMOVE
            if (string.IsNullOrEmpty(s)) s = "G";
            IntPtr cfLocale = IntPtr.Zero;
            IntPtr cfLocaleName = IntPtr.Zero;
            if (ci == null)
            {
                cfLocale = CFLocaleCopyCurrent();
                ci = Utilities.CurrentCulture;
            }
            else
            {
                cfLocaleName = CFStringCreateWithCharacters(IntPtr.Zero, ci.Name, ci.Name.Length);
                cfLocale = CFLocaleCreate(IntPtr.Zero, cfLocaleName);
            }
            IntPtr cfNumber = CFNumberCreate(IntPtr.Zero, numType, n);
            IntPtr cfFormat = IntPtr.Zero;

            int formatLen = 0;

            if (s.Length > 1)
            {
                formatLen = int.Parse(s.Substring(1));
            }
            if (s[0] == 'Y')
            {
                int dotLoc = double.Parse(o.ToString()).ToString(CultureInfo.InvariantCulture).Trim('-').IndexOf('.');
                if (dotLoc == -1) formatLen = 0;
                else
                {
                    string subStr = double.Parse(o.ToString()).ToString(CultureInfo.InvariantCulture).Trim('-').Substring(dotLoc + 1);
                    formatLen = Math.Min(formatLen - dotLoc, subStr.Length);
                    if (s.Length == 1) formatLen = subStr.Length;
                }
            }
            if (s[0] == 'Z') formatLen--;
            if (formatLen < 0) formatLen = 0;

            int round = 1;
            IntPtr cfFormatLen = CFNumberCreate(IntPtr.Zero, CFNumberType.kCFNumberSInt32Type, new IntPtr((void*)&formatLen));
            IntPtr cfOne = CFNumberCreate(IntPtr.Zero, CFNumberType.kCFNumberSInt32Type, new IntPtr((void*)&round));

            int two = 2;
            IntPtr cfTwo = CFNumberCreate(IntPtr.Zero, CFNumberType.kCFNumberSInt32Type, new IntPtr((void*)&two));

            int three = 3;
            IntPtr cfThree = CFNumberCreate(IntPtr.Zero, CFNumberType.kCFNumberSInt32Type, new IntPtr((void*)&three));

            int zero = 0;
            IntPtr cfZero = CFNumberCreate(IntPtr.Zero, CFNumberType.kCFNumberSInt32Type, new IntPtr((void*)&zero));

            int six = 6;
            IntPtr cfSix = CFNumberCreate(IntPtr.Zero, CFNumberType.kCFNumberSInt32Type, new IntPtr((void*)&six));

            int numberDecimalDigit = ci.NumberFormat.NumberDecimalDigits;
            IntPtr cfNumberDecimalDigits = CFNumberCreate(IntPtr.Zero, CFNumberType.kCFNumberSInt32Type, new IntPtr((void*)&numberDecimalDigit));

            switch (s[0].ToString().ToUpper())
            //' switch (s[0].ToString().ToUpper(CultureInfo.InvariantCulture))
            {
                case "P":
                    cfFormat = CFNumberFormatterCreate(IntPtr.Zero, cfLocale, CFNumberFormatterStyle.kCFNumberFormatterPercentStyle);
                    if (formatLen > 0)
                    {
                        CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMaxFractionDigits"), cfFormatLen);
                        CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinFractionDigits"), cfFormatLen);
                    }
                    break;
                case "D":
                    cfFormat = CFNumberFormatterCreate(IntPtr.Zero, cfLocale, CFNumberFormatterStyle.kCFNumberFormatterDecimalStyle);
                    CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterUseGroupingSeparator"), IntPtr.Zero);
                    CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinIntegerDigits"), cfFormatLen);
                    break;
                case "e":
                    return OSNumberToStringMac(n, s, ci, numType, o).ToLower();
                    //' return OSNumberToStringMac(n, s, ci, numType, o).ToLower(CultureInfo.InvariantCulture);
                case "E":
                    cfFormat = CFNumberFormatterCreate(IntPtr.Zero, cfLocale, CFNumberFormatterStyle.kCFNumberFormatterScientificStyle);
                    if (formatLen > 0)
                    {
                        CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMaxFractionDigits"), cfFormatLen);
                        CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinFractionDigits"), cfFormatLen);
                        CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinIntegerDigits"), cfOne);
                    }
                    else
                    {
                        CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMaxFractionDigits"), cfSix);
                        CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinFractionDigits"), cfSix);
                        CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinIntegerDigits"), cfOne);
                    }
                    break;
                case "C":
                    cfFormat = CFNumberFormatterCreate(IntPtr.Zero, cfLocale, CFNumberFormatterStyle.kCFNumberFormatterCurrencyStyle);
                    if (formatLen > 0)
                    {
                        CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMaxFractionDigits"), cfFormatLen);
                        CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinFractionDigits"), cfFormatLen);
                        CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinIntegerDigits"), cfOne);
                    }
                    break;
                case "F":
                    cfFormat = CFNumberFormatterCreate(IntPtr.Zero, cfLocale, CFNumberFormatterStyle.kCFNumberFormatterNoStyle);
                    if (formatLen > 0)
                    {
                        CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMaxFractionDigits"), cfFormatLen);
                        CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinFractionDigits"), cfFormatLen);
                        CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinIntegerDigits"), cfOne);
                    }
                    else
                    {
                        CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMaxFractionDigits"), cfNumberDecimalDigits);
                        CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinFractionDigits"), cfNumberDecimalDigits);
                        CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinIntegerDigits"), cfOne);
                    }
                    break;
                case "N":
                    cfFormat = CFNumberFormatterCreate(IntPtr.Zero, cfLocale, CFNumberFormatterStyle.kCFNumberFormatterDecimalStyle);
                    if (formatLen > 0)
                    {
                        CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMaxFractionDigits"), cfFormatLen);
                        CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinFractionDigits"), cfFormatLen);
                        CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinIntegerDigits"), cfOne);
                    }
                    else
                    {
                        CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinFractionDigits"), cfNumberDecimalDigits);
                        CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMaxFractionDigits"), cfNumberDecimalDigits);
                        CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinIntegerDigits"), cfOne);
                    }
                    break;
                case "G":
                    if (IsIntType(numType))
return OSNumberToStringMac(n, s.ToUpper().Replace('G', 'D'), ci, numType, o); ;
                        //' return OSNumberToStringMac(n, s.ToUpper(CultureInfo.InvariantCulture).Replace('G', 'D'), ci, numType, o); ;

                    string zReplace = OSNumberToStringMac(n, s.ToUpper().Replace('G', 'Z'), ci, numType, o);
                    string yReplace = OSNumberToStringMac(n, s.ToUpper().Replace('G', 'Y'), ci, numType, o);
                    //' string zReplace = OSNumberToStringMac(n, s.ToUpper(CultureInfo.InvariantCulture).Replace('G', 'Z'), ci, numType, o);
                    //' string yReplace = OSNumberToStringMac(n, s.ToUpper(CultureInfo.InvariantCulture).Replace('G', 'Y'), ci, numType, o);
                    if ((numType == CFNumberType.kCFNumberDoubleType) && (yReplace.Trim(ci.NumberFormat.NegativeSign.ToCharArray()).Replace(ci.NumberFormat.NumberDecimalSeparator, "").Trim('0').Length > 15)) return zReplace;
                    if ((numType == CFNumberType.kCFNumberFloat32Type) && (yReplace.Trim(ci.NumberFormat.NegativeSign.ToCharArray()).Replace(ci.NumberFormat.NumberDecimalSeparator, "").Trim('0').Length > 7)) return zReplace;

                    if (zReplace.Length < yReplace.Length) return zReplace;
                    return yReplace;
                case "X":
                    // TODO
                    throw new NotSupportedException("GlobLocHelper does not currently handle hex formatting.");
                case "Y":
                    cfFormat = CFNumberFormatterCreate(IntPtr.Zero, cfLocale, CFNumberFormatterStyle.kCFNumberFormatterDecimalStyle);
                    CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterUseGroupingSeparator"), IntPtr.Zero);
                    if (s.Length > 1 || formatLen > 0)
                    {
                        CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinFractionDigits"), cfFormatLen);
                        CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMaxFractionDigits"), cfFormatLen);
                    }
                    else
                    {
                        int largeNum = (numType == CFNumberType.kCFNumberFloat32Type) ? 7 : 15;
                        IntPtr cfLargeNum = CFNumberCreate(IntPtr.Zero, CFNumberType.kCFNumberSInt32Type, new IntPtr((void*)&largeNum));
                        CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMaxFractionDigits"), cfLargeNum);
                        if (cfLargeNum != IntPtr.Zero)
                            CFRelease(cfLargeNum);
                    }
                    break;
                case "Z":
                    cfFormat = CFNumberFormatterCreate(IntPtr.Zero, cfLocale, CFNumberFormatterStyle.kCFNumberFormatterScientificStyle);

                    if (s.Length > 1 || formatLen > 0)
                    {
                        CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinFractionDigits"), cfFormatLen);
                        CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMaxFractionDigits"), cfFormatLen);
                        CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMinIntegerDigits"), cfOne);
                    }
                    else
                    {
                        int largeNum = (numType == CFNumberType.kCFNumberFloat32Type) ? 7 : 15;
                        IntPtr cfLargeNum = CFNumberCreate(IntPtr.Zero, CFNumberType.kCFNumberSInt32Type, new IntPtr((void*)&largeNum));
                        CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMaxFractionDigits"), cfLargeNum);
                        if (cfLargeNum != IntPtr.Zero)
                            CFRelease(cfLargeNum);
                    }
                    break;
                case "R":
                    cfFormat = CFNumberFormatterCreate(IntPtr.Zero, cfLocale, CFNumberFormatterStyle.kCFNumberFormatterDecimalStyle);

                    int precision = (numType == CFNumberType.kCFNumberDoubleType) ? 15 : 7;
                    IntPtr cfPrecision = CFNumberCreate(IntPtr.Zero, CFNumberType.kCFNumberSInt32Type, new IntPtr((void*)&precision));
                    CFNumberFormatterSetProperty(cfFormat, GetCFString("kCFNumberFormatterMaxFractionDigits"), cfPrecision);
                    if (cfPrecision != IntPtr.Zero)
                        CFRelease(cfPrecision);

                    break;
                default:
                    cfFormat = CFNumberFormatterCreate(IntPtr.Zero, cfLocale, CFNumberFormatterStyle.kCFNumberFormatterNoStyle);
                    break;
            }

            IntPtr cfReturn = CFNumberFormatterCreateStringWithNumber(IntPtr.Zero, cfFormat, cfNumber);

            int newLength = CFStringGetLength(cfReturn);
            CFRange cfRange = new CFRange();
            cfRange.x = 0;
            cfRange.y = newLength;

            StringBuilder sb = new StringBuilder(newLength);
            CFStringGetCharacters(cfReturn, cfRange, sb);
            string ret = sb.ToString().Substring(0, newLength);
            if (s[0] == 'e' || s[0] == 'g') ret = ret.Replace('E', 'e');

            if (cfOne != IntPtr.Zero)
                CFRelease(cfOne);
            if (cfThree != IntPtr.Zero)
                CFRelease(cfThree);
            if (cfFormatLen != IntPtr.Zero)
                CFRelease(cfFormatLen);
            if (cfReturn != IntPtr.Zero)
                CFRelease(cfReturn);
            if (cfLocaleName != IntPtr.Zero)
                CFRelease(cfLocaleName);
            if (cfLocale != IntPtr.Zero)
                CFRelease(cfLocale);
            if (cfFormat != IntPtr.Zero)
                CFRelease(cfFormat);
            if (cfNumber != IntPtr.Zero)
                CFRelease(cfNumber);
            if (cfTwo != IntPtr.Zero)
                CFRelease(cfTwo);
            if (cfZero != IntPtr.Zero)
                CFRelease(cfZero);
            if (cfSix != IntPtr.Zero)
                CFRelease(cfSix);
            if (cfNumberDecimalDigits != IntPtr.Zero)
                CFRelease(cfNumberDecimalDigits);
            return ret;
#else
            throw new NotImplementedException();
#endif
        }
Пример #27
0
 private static extern void CFDataGetBytes(IntPtr theData, CFRange range, byte[] buffer);