Esempio n. 1
0
        public NSString Decode(NSData data, out uint encoding)
        {
            Contract.Requires(data != null, "data is null");

            NSString result = null;

            if (data.length() > 0)
            {
                int skipBytes = 0;
                encoding = DoGetEncoding(data, ref skipBytes);
                if (encoding != 0)
                {
                    if (skipBytes > 0)
                        data = data.subdataWithRange(new NSRange(skipBytes, (int) data.length() - skipBytes));
                    result = DoDecode(data, encoding);

                    // The first few bytes of most legacy documents will look like utf8 so
                    // if we couldn't decode it using utf8 we need to fall back onto Mac
                    // OS Roman.
                    if (NSObject.IsNullOrNil(result) && encoding == Enums.NSUTF8StringEncoding)
                    {
                        encoding = Enums.NSMacOSRomanStringEncoding;
                        result = DoDecode(data, encoding);
                    }
                }

                if (NSObject.IsNullOrNil(result))
                    throw new InvalidOperationException("Couldn't read the file as Unicode or Mac OS Roman.");	// should only happen if there are embedded control characters in the header
            }
            else
            {
                result = NSString.Empty;
                encoding = Enums.NSUTF8StringEncoding;
            }

            return result;
        }
Esempio n. 2
0
		private NSMutableAttributedString DoReadWrapped(NSData data, NSString type)
		{
			NSMutableAttributedString str;
			
			if (data.length() > 0)
			{
				NSDictionary options = NSDictionary.dictionaryWithObject_forKey(type, Externs.NSDocumentTypeDocumentAttribute);
				NSError error;
				str = NSMutableAttributedString.Alloc().initWithData_options_documentAttributes_error(data, options, IntPtr.Zero, out error).To<NSMutableAttributedString>();
				if (!NSObject.IsNullOrNil(error))
					error.Raise();
				
				str.autorelease();
			}
			else
				str = NSMutableAttributedString.Create();
			
			return str;
		}
Esempio n. 3
0
		public bool readFromData_ofType_error(NSData data, NSString typeName, IntPtr outError)
		{
			bool read = false;
			
			try
			{
				Contract.Assert(NSObject.IsNullOrNil(m_text), "m_text is not null");
				
				if (DoShouldOpen(data.length()))
				{
					DoReadData(data, typeName);
					if (NSObject.IsNullOrNil(m_text))
						throw new InvalidOperationException("Couldn't decode the file.");
					
					string text = m_text.string_().description();
					DoSetEndian(text);
					DoCheckForControlChars(m_text.string_());
					
					if (m_controller != null)			// will be null for initial open, but non-null for revert
					{
						m_controller.RichText = m_text;
						m_text = null;
					}
					else
						m_text.retain();
						
					read = true;
					Marshal.WriteIntPtr(outError, IntPtr.Zero);
				}
				else
				{
					NSObject error = NSError.errorWithDomain_code_userInfo(Externs.Cocoa3Domain, Enums.NSUserCancelledError, null);
					Marshal.WriteIntPtr(outError, error);
				}
			}
			catch (Exception e)
			{
				Log.WriteLine(TraceLevel.Error, "App", "Couldn't open {0:D}", fileURL());
				Log.WriteLine(TraceLevel.Error, "App", "{0}", e);
				
				NSMutableDictionary userInfo = NSMutableDictionary.Create();
				userInfo.setObject_forKey(NSString.Create("Couldn't read the document data."), Externs.NSLocalizedDescriptionKey);
				userInfo.setObject_forKey(NSString.Create(e.Message), Externs.NSLocalizedFailureReasonErrorKey);
				
				NSObject error = NSError.errorWithDomain_code_userInfo(Externs.Cocoa3Domain, 1, userInfo);
				Marshal.WriteIntPtr(outError, error);
			}
			
			return read;
		}