Author: bnickel
Date: 2007-03-23 16:56:30 -0500 (Fri, 23 Mar 2007)
New Revision: 74925

Modified:
   trunk/taglib-sharp/ChangeLog
   trunk/taglib-sharp/src/TagLib/Ape/Tag.cs
   trunk/taglib-sharp/src/TagLib/Asf/ContentDescriptor.cs
   trunk/taglib-sharp/src/TagLib/Asf/Tag.cs
   trunk/taglib-sharp/src/TagLib/File.cs
   trunk/taglib-sharp/src/TagLib/Id3v1/Tag.cs
   trunk/taglib-sharp/src/TagLib/Id3v2/Frames/TextIdentificationFrame.cs
   trunk/taglib-sharp/src/TagLib/Id3v2/Tag.cs
   trunk/taglib-sharp/src/TagLib/Mpeg4/AppleTag.cs
   trunk/taglib-sharp/src/TagLib/Ogg/Vorbis/File.cs
   trunk/taglib-sharp/src/TagLib/Ogg/XiphComment.cs
Log:
2007-03-23  Brian Nickel <[EMAIL PROTECTED]>

        * src/TagLib/File.cs: Don't catch exceptions on mode change.
        * src/TagLib/Id3v1/Tag.cs: Use TryParse rather than try{Parse}.
        * src/TagLib/Id3v2/Tag.cs: Remove "0" values rather than storing "0". 
Use
        TryParse rather than try{Parse}.
        * src/TagLib/Id3v2/Frames/TextIdentificationFrame.cs: Use TryParse 
rather 
        than try{Parse}.
        * src/TagLib/Ape/Tag.cs: Remove "0" values rather than storing "0". Use
        TryParse rather than try{Parse}.
        * src/TagLib/Asf/Tag.cs: Remove "0" values rather than storing "0". Use
        TryParse rather than try{Parse}.
        * src/TagLib/Asf/ContentDescriptor.cs: Use TryParse rather than 
try{Parse}.
        * src/TagLib/Mpeg4/AppleTag.cs: Use TryParse rather than try{Parse}.
        * src/TagLib/Ogg/XiphComment.cs: Remove "0" values rather than storing 
"0". 
        Use TryParse rather than try{Parse}.
        * src/TagLib/Ogg/Vorbis/File.cs: Don't catch exceptions on mode change.


Modified: trunk/taglib-sharp/ChangeLog
===================================================================
--- trunk/taglib-sharp/ChangeLog        2007-03-23 21:32:35 UTC (rev 74924)
+++ trunk/taglib-sharp/ChangeLog        2007-03-23 21:56:30 UTC (rev 74925)
@@ -1,3 +1,21 @@
+2007-03-23  Brian Nickel <[EMAIL PROTECTED]>
+
+       * src/TagLib/File.cs: Don't catch exceptions on mode change.
+       * src/TagLib/Id3v1/Tag.cs: Use TryParse rather than try{Parse}.
+       * src/TagLib/Id3v2/Tag.cs: Remove "0" values rather than storing "0". 
Use
+       TryParse rather than try{Parse}.
+       * src/TagLib/Id3v2/Frames/TextIdentificationFrame.cs: Use TryParse 
rather 
+       than try{Parse}.
+       * src/TagLib/Ape/Tag.cs: Remove "0" values rather than storing "0". Use
+       TryParse rather than try{Parse}.
+       * src/TagLib/Asf/Tag.cs: Remove "0" values rather than storing "0". Use
+       TryParse rather than try{Parse}.
+       * src/TagLib/Asf/ContentDescriptor.cs: Use TryParse rather than 
try{Parse}.
+       * src/TagLib/Mpeg4/AppleTag.cs: Use TryParse rather than try{Parse}.
+       * src/TagLib/Ogg/XiphComment.cs: Remove "0" values rather than storing 
"0". 
+       Use TryParse rather than try{Parse}.
+       * src/TagLib/Ogg/Vorbis/File.cs: Don't catch exceptions on mode change.
+
 2007-03-22  Brian Nickel <[EMAIL PROTECTED]>
 
        * src/TagLib/Id3v2/Frames/TextIdentificationFrame.cs: Remove null 
termination
@@ -7,7 +25,6 @@
        * src/TagLib/Mpeg/File.cs: Include sane default tags.
        * docs/en/*: Synched docs.
 
-
 2007-03-14  Brian Nickel  <[EMAIL PROTECTED]>
 
        * docs/en/*: Synced docs.

Modified: trunk/taglib-sharp/src/TagLib/Ape/Tag.cs
===================================================================
--- trunk/taglib-sharp/src/TagLib/Ape/Tag.cs    2007-03-23 21:32:35 UTC (rev 
74924)
+++ trunk/taglib-sharp/src/TagLib/Ape/Tag.cs    2007-03-23 21:56:30 UTC (rev 
74925)
@@ -87,6 +87,16 @@
       {
          return items.ContainsKey (key.ToUpper ()) ? items [key.ToUpper ()] : 
null;
       }
+      
+      public void AddNumberValue (string key, uint number, uint count, bool 
replace)
+      {
+         if (number == 0 && count == 0)
+            AddValue (key, null, replace);
+         else if (count != 0)
+            AddValue (key, number.ToString () + "/" + count.ToString (), 
replace);
+         else
+            AddValue (key, number.ToString (), replace);
+      }
 
       public void AddValue (string key, string value, bool replace)
       {
@@ -232,18 +242,21 @@
       {
          get
          {
-            try
-            {
-               if (GetItem ("YEAR") != null)
-                  return UInt32.Parse (GetItem ("YEAR").ToString ().Substring 
(0, 4));
-            }
-            catch {}
+            Item item = GetItem ("YEAR");
+            if (item == null)
+               return 0;
             
+            string text = item.ToString ();
+            uint value;
+            
+            if (uint.TryParse (text.Length > 4 ? text.Substring (0, 4) : text, 
out value))
+               return value;
+            
             return 0;
          }
          set
          {
-            AddValue ("YEAR", value.ToString (), true);
+            AddNumberValue ("YEAR", value, 0, true);
          }
       }
       
@@ -251,22 +264,18 @@
       {
          get
          {
-            try
-            {
-               if (GetItem ("TRACK") != null)
-                  return UInt32.Parse (GetItem ("TRACK").ToString ().Split 
(new char [] {'/'}) [0]);
-            }
-            catch {}
+            Item item = GetItem ("TRACK");
+            string [] values;
+            uint value;
             
+            if (item != null && (values = item.ToString ().Split ('/')).Length 
> 0 && uint.TryParse (values [0], out value))
+               return value;
+            
             return 0;
          }
          set
          {
-            uint count = TrackCount;
-            if (count != 0)
-               AddValue ("TRACK", value + "/" + count, true);
-            else
-               AddValue ("TRACK", value.ToString (), true);
+            AddNumberValue ("TRACK", value, TrackCount, true);
          }
       }
       
@@ -274,18 +283,18 @@
       {
          get
          {
-            try
-            {
-               if (GetItem ("TRACK") != null)
-                  return UInt32.Parse (GetItem ("TRACK").ToString ().Split 
(new char [] {'/'}) [1]);
-            }
-            catch {}
+            Item item = GetItem ("TRACK");
+            string [] values;
+            uint value;
             
+            if (item != null && (values = item.ToString ().Split ('/')).Length 
> 1 && uint.TryParse (values [1], out value))
+               return value;
+            
             return 0;
          }
          set
          {
-            AddValue ("TRACK", Track + "/" + value, true);
+            AddNumberValue ("TRACK", Track, value, true);
          }
       }
       
@@ -293,22 +302,18 @@
       {
          get
          {
-            try
-            {
-               if (GetItem ("DISC") != null)
-                  return UInt32.Parse (GetItem ("DISC").ToString ().Split (new 
char [] {'/'}) [0]);
-            }
-            catch {}
+            Item item = GetItem ("DISC");
+            string [] values;
+            uint value;
             
+            if (item != null && (values = item.ToString ().Split ('/')).Length 
> 0 && uint.TryParse (values [0], out value))
+               return value;
+            
             return 0;
          }
          set
          {
-            uint count = DiscCount;
-            if (count != 0)
-               AddValue ("DISC", value + "/" + count, true);
-            else
-               AddValue ("DISC", value.ToString (), true);
+            AddNumberValue ("DISC", value, DiscCount, true);
          }
       }
       
@@ -316,18 +321,18 @@
       {
          get
          {
-            try
-            {
-               if (GetItem ("DISC") != null)
-                  return UInt32.Parse (GetItem ("DISC").ToString ().Split (new 
char [] {'/'}) [1]);
-            }
-            catch {}
+            Item item = GetItem ("DISC");
+            string [] values;
+            uint value;
             
+            if (item != null && (values = item.ToString ().Split ('/')).Length 
> 1 && uint.TryParse (values [1], out value))
+               return value;
+            
             return 0;
          }
          set
          {
-            AddValue ("DISC", Disc + "/" + value, true);
+            AddNumberValue ("DISC", Disc, value, true);
          }
       }
       

Modified: trunk/taglib-sharp/src/TagLib/Asf/ContentDescriptor.cs
===================================================================
--- trunk/taglib-sharp/src/TagLib/Asf/ContentDescriptor.cs      2007-03-23 
21:32:35 UTC (rev 74924)
+++ trunk/taglib-sharp/src/TagLib/Asf/ContentDescriptor.cs      2007-03-23 
21:56:30 UTC (rev 74925)
@@ -112,36 +112,27 @@
 
       public uint ToDWord ()
       {
-         if (type == DataType.Unicode && sValue != null)
-            try
-            {
-               return System.UInt32.Parse (sValue);
-            }
-            catch {}
+         uint value;
+         if (type == DataType.Unicode && sValue != null && uint.TryParse 
(sValue, out value))
+            return value;
          
          return (uint) lValue;
       }
 
       public long ToQWord ()
       {
-         if (type == DataType.Unicode && sValue != null)
-            try
-            {
-               return System.Int64.Parse (sValue);
-            }
-            catch {}
+         long value;
+         if (type == DataType.Unicode && sValue != null && long.TryParse 
(sValue, out value))
+            return value;
          
          return (long) lValue;
       }
 
       public short ToWord ()
       {
-         if (type == DataType.Unicode && sValue != null)
-            try
-            {
-               return System.Int16.Parse (sValue);
-            }
-            catch {}
+         short value;
+         if (type == DataType.Unicode && sValue != null && short.TryParse 
(sValue, out value))
+            return value;
          
          return (short) lValue;
       }

Modified: trunk/taglib-sharp/src/TagLib/Asf/Tag.cs
===================================================================
--- trunk/taglib-sharp/src/TagLib/Asf/Tag.cs    2007-03-23 21:32:35 UTC (rev 
74924)
+++ trunk/taglib-sharp/src/TagLib/Asf/Tag.cs    2007-03-23 21:56:30 UTC (rev 
74925)
@@ -136,18 +136,18 @@
       {
          get
          {
-            string value = GetDescriptorString ("WM/Year");
-            if (value != null)
-               try
-               {
-                  return System.UInt32.Parse (value.Substring (0, 4));
-               }
-               catch {}
+            string text = GetDescriptorString ("WM/Year");
+            uint value;
             
+            if (text != null && uint.TryParse (text.Length > 4 ? 
text.Substring (0, 4) : text, out value))
+               return value;
+            
             return 0;
          }
          set
          {
+            if (value == 0)
+               RemoveDescriptors ("WM/Year");
             SetDescriptorString (value.ToString (), "WM/Year");
          }
       }
@@ -164,6 +164,8 @@
          }
          set
          {
+            if (value == 0)
+               RemoveDescriptors ("WM/TrackNumber");
             SetDescriptors ("WM/TrackNumber", new ContentDescriptor 
("WM/TrackNumber", value));
          }
       }
@@ -181,6 +183,8 @@
          }
          set
          {
+            if (value == 0)
+               RemoveDescriptors ("TrackTotal");
             SetDescriptors ("TrackTotal", new ContentDescriptor ("TrackTotal", 
value));
          }
       }
@@ -189,20 +193,18 @@
       {
          get
          {
-            string value = GetDescriptorString ("WM/PartOfSet");
-            if (value != null)
-               try
-               {
-                  return UInt32.Parse (value.Split (new char [] {'/'}) [0]);
-               }
-               catch {}
+            string text = GetDescriptorString ("WM/PartOfSet");
+            string[] texts;
+            uint value;
             
-            return 0;
+            return (text != null && (texts = text.Split ('/')).Length > 0 && 
uint.TryParse (texts [0], out value)) ? value : 0;
          }
          set
          {
             uint count = DiscCount;
-            if (count != 0)
+            if (value == 0 && count == 0)
+               RemoveDescriptors ("WM/PartOfSet");
+            else if (count != 0)
                SetDescriptorString (value.ToString () + "/" + count.ToString 
(), "WM/PartOfSet");
             else
                SetDescriptorString (value.ToString (), "WM/PartOfSet");
@@ -213,19 +215,21 @@
       {
          get
          {
-            string value = GetDescriptorString ("WM/PartOfSet");
-            if (value != null && value.IndexOf ('/') != -1)
-                   try
-                {
-                return UInt32.Parse (value.Split (new char [] {'/'}) [1]);
-                  }
-               catch {}
+            string text = GetDescriptorString ("WM/PartOfSet");
+            string[] texts;
+            uint value;
             
-            return 0;
+            return (text != null && (texts = text.Split ('/')).Length > 1 && 
uint.TryParse (texts [1], out value)) ? value : 0;
          }
          set
          {
-               SetDescriptorString (Disc.ToString () + "/" + value.ToString 
(), "WM/PartOfSet");
+            uint disc = Disc;
+            if (value == 0 && disc == 0)
+               RemoveDescriptors ("WM/PartOfSet");
+            else if (value != 0)
+               SetDescriptorString (disc.ToString () + "/" + value.ToString 
(), "WM/PartOfSet");
+            else
+               SetDescriptorString (disc.ToString (), "WM/PartOfSet");
          }
       }
       

Modified: trunk/taglib-sharp/src/TagLib/File.cs
===================================================================
--- trunk/taglib-sharp/src/TagLib/File.cs       2007-03-23 21:32:35 UTC (rev 
74924)
+++ trunk/taglib-sharp/src/TagLib/File.cs       2007-03-23 21:56:30 UTC (rev 
74925)
@@ -105,8 +105,7 @@
 
       public long Find (ByteVector pattern, long from_offset, ByteVector 
before)
       {
-         try {Mode = AccessMode.Read;}
-         catch {return -1;}
+         Mode = AccessMode.Read;
          
          if (pattern.Count > buffer_size)
             return -1;
@@ -218,8 +217,7 @@
       
       long RFind (ByteVector pattern, long from_offset, ByteVector before)
       {
-         try {Mode = AccessMode.Read;}
-         catch {return -1;}
+         Mode = AccessMode.Read;
          
          if (pattern.Count > buffer_size)
             return -1;
@@ -561,9 +559,7 @@
       
       protected void Truncate (long length)
       {
-         try {Mode = AccessMode.Write;}
-         catch {return;}
-         
+         Mode = AccessMode.Write;
          file_stream.SetLength (length);
       }
       

Modified: trunk/taglib-sharp/src/TagLib/Id3v1/Tag.cs
===================================================================
--- trunk/taglib-sharp/src/TagLib/Id3v1/Tag.cs  2007-03-23 21:32:35 UTC (rev 
74924)
+++ trunk/taglib-sharp/src/TagLib/Id3v1/Tag.cs  2007-03-23 21:56:30 UTC (rev 
74925)
@@ -138,8 +138,8 @@
       {
          get
          {
-            try {return System.UInt32.Parse (year);}
-            catch {return 0;}
+            uint value;
+            return uint.TryParse (year, out value) ? value : 0;
          }
          set {year = value > 0 ? value.ToString () : String.Empty;}
       }

Modified: trunk/taglib-sharp/src/TagLib/Id3v2/Frames/TextIdentificationFrame.cs
===================================================================
--- trunk/taglib-sharp/src/TagLib/Id3v2/Frames/TextIdentificationFrame.cs       
2007-03-23 21:32:35 UTC (rev 74924)
+++ trunk/taglib-sharp/src/TagLib/Id3v2/Frames/TextIdentificationFrame.cs       
2007-03-23 21:56:30 UTC (rev 74925)
@@ -147,16 +147,10 @@
          {
             if (this.Header.FrameId == "TCON")
             {
+               byte id;
                string data = "";
                foreach (string s in field_list)
-                  try
-                  {
-                     data += "(" + int.Parse (s) + ")";
-                  }
-                  catch
-                  {
-                     data += s;
-                  }
+                  data += byte.TryParse (s, out id) ? ("(" + id + ")") : s;
                v.Add (ByteVector.FromString (data, encoding));
             }
             else

Modified: trunk/taglib-sharp/src/TagLib/Id3v2/Tag.cs
===================================================================
--- trunk/taglib-sharp/src/TagLib/Id3v2/Tag.cs  2007-03-23 21:32:35 UTC (rev 
74924)
+++ trunk/taglib-sharp/src/TagLib/Id3v2/Tag.cs  2007-03-23 21:56:30 UTC (rev 
74925)
@@ -322,43 +322,28 @@
 
                foreach (string s in fields)
                {
+                  byte value;
+                  
                   if (s == null)
                      continue;
                   
-                  bool is_number = true;
-                  foreach (char c in s)
-                     if (c < '0' || c > '9')
-                        is_number = false;
-
-                  if(is_number)
+                  // The string may just be a genre number.
+                  if (byte.TryParse (s, out value))
                   {
-                     try
-                     {
-                        l.Add (Id3v1.GenreList.Genre (Byte.Parse (s)));
-                        continue;
-                     }
-                     catch {}
+                     l.Add (Id3v1.GenreList.Genre (value));
+                     continue;
                   }
                   
+                  // Check for ID3v2.3 style (#)
                   int closing = s.IndexOf (')');
-                  if (closing > 0 && s.Substring (0, 1) == "(")
+                  if (closing > 0 && s[0] == '(' && byte.TryParse (s.Substring 
(1, closing - 1), out value))
                   {
-                     if(closing == s.Length - 1)
-                     {
-                        try
-                        {
-                           l.Add (Id3v1.GenreList.Genre (Byte.Parse 
(s.Substring (1, closing - 1))));
-                        }
-                        catch
-                        {
-                           l.Add (s);
-                        }
-                     }
-                     else
-                        l.Add (s.Substring (closing + 1));
+                     l.Add (Id3v1.GenreList.Genre (value));
+                     continue;
                   }
-                  else
-                     l.Add (s);
+                  
+                  // We don't have a number, add the string.
+                  l.Add (s);
                }
             }
             
@@ -380,19 +365,19 @@
       {
          get
          {
+            uint value;
             foreach (TextIdentificationFrame f in GetFrames ("TDRC"))
             {
-               try
-               {
-                  return UInt32.Parse (f.ToString ().Substring (0, 4));
-               }
-               catch {}
+               string text = f.ToString ();
+               if (text.Length > 3 && uint.TryParse (text.Substring (0, 4), 
out value))
+                  return value;
             }
+            
             return 0;
          }
          set
          {
-            SetTextFrame ("TDRC", value.ToString ());
+            SetNumberFrame ("TDRC", value, 0);
          }
       }
       
@@ -400,19 +385,17 @@
       {
          get
          {
-            try
-            {
-               return UInt32.Parse (GetFrames ("TRCK") [0].ToString ().Split 
(new char [] {'/'}) [0]);
-            }
-            catch {return 0;}
+            string [] values;
+            uint value;
+            foreach (TextIdentificationFrame f in GetFrames ("TRCK"))
+               if ((values = f.ToString ().Split ('/')).Length > 0 && 
uint.TryParse (values [0], out value))
+                  return value;
+            
+            return 0;
          }
          set
          {
-            uint count = TrackCount;
-            if (count != 0)
-               SetTextFrame ("TRCK", value + "/" + count);
-            else
-               SetTextFrame ("TRCK", value.ToString ());
+            SetNumberFrame ("TRCK", value, TrackCount);
          }
       }
       
@@ -420,15 +403,17 @@
       {
          get
          {
-            try
-            {
-               return UInt32.Parse (GetFrames ("TRCK") [0].ToString ().Split 
(new char [] {'/'}) [1]);
-            }
-            catch {return 0;}
+            string [] values;
+            uint value;
+            foreach (TextIdentificationFrame f in GetFrames ("TRCK"))
+               if ((values = f.ToString ().Split ('/')).Length > 1 && 
uint.TryParse (values [1], out value))
+                  return value;
+            
+            return 0;
          }
          set
          {
-            SetTextFrame ("TRCK", Track + "/" + value);
+            SetNumberFrame ("TRCK", Track, value);
          }
       }
       
@@ -436,19 +421,17 @@
       {
          get
          {
-            try
-            {
-               return UInt32.Parse (GetFrames ("TPOS") [0].ToString ().Split 
(new char [] {'/'}) [0]);
-            }
-            catch {return 0;}
+            string [] values;
+            uint value;
+            foreach (TextIdentificationFrame f in GetFrames ("TPOS"))
+               if ((values = f.ToString ().Split ('/')).Length > 0 && 
uint.TryParse (values [0], out value))
+                  return value;
+            
+            return 0;
          }
          set
          {
-            uint count = DiscCount;
-            if (count != 0)
-               SetTextFrame ("TPOS", value + "/" + count);
-            else
-               SetTextFrame ("TPOS", value.ToString ());
+            SetNumberFrame ("TPOS", value, DiscCount);
          }
       }
       
@@ -456,15 +439,17 @@
       {
          get
          {
-            try
-            {
-               return UInt32.Parse (GetFrames ("TPOS") [0].ToString ().Split 
(new char [] {'/'}) [1]);
-            }
-            catch {return 0;}
+            string [] values;
+            uint value;
+            foreach (TextIdentificationFrame f in GetFrames ("TPOS"))
+               if ((values = f.ToString ().Split ('/')).Length > 1 && 
uint.TryParse (values [1], out value))
+                  return value;
+            
+            return 0;
          }
          set
          {
-            SetTextFrame ("TPOS", Disc + "/" + value);
+            SetNumberFrame ("TPOS", Disc, value);
          }
       }
       
@@ -604,10 +589,9 @@
       {
          if (file == null)
             return;
-            
-         try {file.Mode = File.AccessMode.Read;}
-         catch {return;}
          
+         file.Mode = File.AccessMode.Read;
+         
          file.Seek (tag_offset);
          header.SetData (file.ReadBlock ((int) Header.Size));
 
@@ -725,5 +709,15 @@
             f.SetText (value);
          }
       }
+      
+      public void SetNumberFrame (ByteVector id, uint number, uint count)
+      {
+         if (number == 0 && count == 0)
+            this.RemoveFrames (id);
+         else if (count != 0)
+            SetTextFrame (id, number.ToString () + "/" + count.ToString ());
+         else
+            SetTextFrame (id, number.ToString ());
+      }
    }
 }

Modified: trunk/taglib-sharp/src/TagLib/Mpeg4/AppleTag.cs
===================================================================
--- trunk/taglib-sharp/src/TagLib/Mpeg4/AppleTag.cs     2007-03-23 21:32:35 UTC 
(rev 74924)
+++ trunk/taglib-sharp/src/TagLib/Mpeg4/AppleTag.cs     2007-03-23 21:56:30 UTC 
(rev 74925)
@@ -369,12 +369,12 @@
       {
          get
          {
-            AppleDataBox [] boxes = DataBoxes (BoxTypes.Day);
-            try
-            {
-               return boxes.Length == 0 || boxes [0].Text == null ? 0 : 
System.UInt32.Parse (boxes [0].Text.Substring (0, boxes [0].Text.Length < 4 ? 
boxes [0].Text.Length : 4));
-            }
-            catch {return 0;}
+            uint value;
+            foreach (AppleDataBox box in DataBoxes (BoxTypes.Day))
+               if (box.Text != null && uint.TryParse (box.Text.Length > 4 ? 
box.Text.Substring (0, 4) : box.Text, out value))
+                  return value;
+            
+            return 0;
          }
          set
          {

Modified: trunk/taglib-sharp/src/TagLib/Ogg/Vorbis/File.cs
===================================================================
--- trunk/taglib-sharp/src/TagLib/Ogg/Vorbis/File.cs    2007-03-23 21:32:35 UTC 
(rev 74924)
+++ trunk/taglib-sharp/src/TagLib/Ogg/Vorbis/File.cs    2007-03-23 21:56:30 UTC 
(rev 74925)
@@ -52,11 +52,8 @@
          comment = null;
          properties = null;
          
-         try {Mode = AccessMode.Read;}
-         catch {return;}
-         
+         Mode = AccessMode.Read;
          Read (properties_style);
-         
          Mode = AccessMode.Closed;
       }
       

Modified: trunk/taglib-sharp/src/TagLib/Ogg/XiphComment.cs
===================================================================
--- trunk/taglib-sharp/src/TagLib/Ogg/XiphComment.cs    2007-03-23 21:32:35 UTC 
(rev 74924)
+++ trunk/taglib-sharp/src/TagLib/Ogg/XiphComment.cs    2007-03-23 21:56:30 UTC 
(rev 74925)
@@ -55,6 +55,14 @@
          return (field_list.ContainsKey (key.ToUpper ())) ? field_list 
[key.ToUpper ()] : null;
       }
       
+      public void AddNumberField (string key, uint number, bool replace)
+      {
+         if (number == 0)
+            AddField (key, null, replace);
+         else
+            AddField (key, number.ToString (), replace);
+      }
+
       public void AddField (string key, string value, bool replace)
       {
          if (replace)
@@ -305,16 +313,13 @@
       {
          get
          {
-            try
-            {
-               StringList l = GetField ("DATE");
-               return (l != null && l.Count != 0) ? UInt32.Parse (l 
[0].Substring (0, 4)) : 0;
-            }
-            catch {return 0;}
+            StringList l = GetField ("DATE");
+            uint value;
+            return (l != null && l.Count != 0 && uint.TryParse (l[0].Length > 
4 ? l[0].Substring (0, 4) : l[0], out value)) ? value : 0;
          }
          set
          {
-            AddField ("DATE", value.ToString ());
+            AddNumberField ("DATE", value, true);
          }
       }
       
@@ -322,17 +327,19 @@
       {
          get
          {
-            try
-            {
-               StringList l = GetField ("TRACKNUMBER");
-               return (l != null && l.Count != 0) ? UInt32.Parse (l [0].Split 
(new char [] {'/'}) [0]) : 0;
-            }
-            catch {return 0;}
+            StringList l = GetField ("TRACKNUMBER");
+            string [] values;
+            uint value;
+            
+            if (l != null && l.Count != 0 && (values = l[0].Split 
('/')).Length > 0 && uint.TryParse (values [0], out value))
+               return value;
+            
+            return 0;
          }
          set
          {
-            TrackCount = TrackCount;
-            AddField ("TRACKNUMBER", value.ToString ());
+            AddNumberField ("TRACKTOTAL", value == 0 ? 0 : TrackCount, true);
+            AddNumberField ("TRACKNUMBER", value, true);
          }
       }
       
@@ -340,20 +347,21 @@
       {
          get
          {
-            try
-            {
-               StringList l = GetField ("TRACKTOTAL");
-               if (l != null && l.Count != 0)
-                  return UInt32.Parse (l [0]);
-               
-               l = GetField ("TRACKNUMBER");
-               return (l != null && l.Count != 0) ? UInt32.Parse (l [0].Split 
(new char [] {'/'}) [1]) : 0;
-            }
-            catch {return 0;}
+            StringList l;
+            string [] values;
+            uint value;
+            
+            if ((l = GetField ("TRACKTOTAL")) != null && l.Count != 0 && 
uint.TryParse (l[0], out value))
+               return value;
+            
+            if ((l = GetField ("TRACKNUMBER")) != null && l.Count != 0 && 
(values = l[0].Split ('/')).Length > 1 && uint.TryParse (values [1], out value))
+               return value;
+            
+            return 0;
          }
          set
          {
-            AddField ("TRACKTOTAL", value.ToString ());
+            AddNumberField ("TRACKTOTAL", Track == 0 ? 0 : value, true);
          }
       }
       
@@ -361,17 +369,19 @@
       {
          get
          {
-            try
-            {
-               StringList l = GetField ("DISCNUMBER");
-               return (l != null && l.Count != 0) ? UInt32.Parse (l [0].Split 
(new char [] {'/'}) [0]) : 0;
-            }
-            catch {return 0;}
+            StringList l = GetField ("DISCNUMBER");
+            string [] values;
+            uint value;
+            
+            if (l != null && l.Count != 0 && (values = l[0].Split 
('/')).Length > 0 && uint.TryParse (values [0], out value))
+               return value;
+            
+            return 0;
          }
          set
          {
-            DiscCount = DiscCount;
-            AddField ("DISCNUMBER", value.ToString ());
+            AddNumberField ("DISCTOTAL", value == 0 ? 0 : DiscCount, true);
+            AddNumberField ("DISCNUMBER", value, true);
          }
       }
       
@@ -379,20 +389,21 @@
       {
          get
          {
-            try
-            {
-               StringList l = GetField ("DISCTOTAL");
-               if (l != null && l.Count != 0)
-                  return UInt32.Parse (l [0]);
-               
-               l = GetField ("DISCNUMBER");
-               return (l != null && l.Count != 0) ? UInt32.Parse (l [0].Split 
(new char [] {'/'}) [1]) : 0;
-            }
-            catch {return 0;}
+            StringList l;
+            string [] values;
+            uint value;
+            
+            if ((l = GetField ("DISCTOTAL")) != null && l.Count != 0 && 
uint.TryParse (l[0], out value))
+               return value;
+            
+            if ((l = GetField ("DISCNUMBER")) != null && l.Count != 0 && 
(values = l[0].Split ('/')).Length > 1 && uint.TryParse (values [1], out value))
+               return value;
+            
+            return 0;
          }
          set
          {
-            AddField ("DISCTOTAL", value.ToString ());
+            AddNumberField ("DISCTOTAL", Disc == 0 ? 0 : value, true);
          }
       }
       

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to