![]() |
Sylloge
A C# helper library
|
00001 using System; 00002 using System.Collections.Generic; 00003 using System.Linq; 00004 using System.Text; 00005 00006 namespace Sylloge.Audio.Metadata.ID3.Frames 00007 { 00008 public class UserDefinedURL : TextFrame 00009 { 00010 public UserDefinedURL() 00011 : base(ID3.Frame.Keys.UserDefinedURL) 00012 { 00013 this.Initialize(0x00, string.Empty, string.Empty); 00014 this.GenerateTag(); 00015 } 00016 00017 public UserDefinedURL(string url) 00018 : base(ID3.Frame.Keys.UserDefinedString) 00019 { 00020 this.Initialize(0x00, string.Empty, url); 00021 this.GenerateTag(); 00022 } 00023 00024 public UserDefinedURL(string url, string desc) 00025 : base(ID3.Frame.Keys.UserDefinedString) 00026 { 00027 this.Initialize(0x00, desc, url); 00028 this.GenerateTag(); 00029 } 00030 00031 private UserDefinedURL(byte enc, string desc, string url) 00032 : base(ID3.Frame.Keys.UserDefinedURL) 00033 { 00034 this.Initialize(enc, string.Empty, url); 00035 this.GenerateTag(); 00036 } 00037 00038 public UserDefinedURL(Frame other) 00039 : base(other) 00040 { 00041 if (other.ID != ID3.Frame.Keys.UserDefinedURL) { throw new System.InvalidCastException("Frame ID must be equal to copy the data within it."); } 00042 UserDefinedURL c = UserDefinedURL.Parse(other); 00043 this.Description = c.Description; 00044 this.Encoding = c.Encoding; 00045 this.Value = c.Value; 00046 } 00047 00048 public UserDefinedURL(TextFrame other) 00049 : base(other) 00050 { 00051 if (other.ID != ID3.Frame.Keys.UserDefinedURL) { throw new System.InvalidCastException("Frame ID must be equal to copy the data within it."); } 00052 } 00053 00054 public UserDefinedURL(UserDefinedURL other) 00055 : base(other) 00056 { 00057 } 00058 00064 public static ID3.Frames.UserDefinedURL Parse(Frame frame) 00065 { 00066 if (frame.ID != ID3.Frame.Keys.UserDefinedURL) { throw new System.InvalidCastException("Frame ID must be equal to copy the data within it."); } 00067 UserDefinedURL ret = new UserDefinedURL(); // (UserDefinedURL)frame; 00068 ret.Data = frame.Data; 00069 ret.Format = frame.Format; 00070 ret.Status = frame.Status; 00071 /* <Header for 'User defined URL link frame', ID: "WXXX"> 00072 Text encoding $xx 00073 Description <text string according to encoding> $00 (00) 00074 URL <text string>*/ 00075 List<byte> bdata = new List<byte>(ret.Data); 00076 ret.Encoding = bdata[0]; bdata.RemoveAt(0); 00077 System.Text.Encoding enc = ID3.Helper.GetEncoding(ret.Encoding); 00078 if (bdata[0] != Frame.EOL) { 00079 ret.Description = enc.GetString(bdata.ToArray()); 00080 int idx = 0; 00081 for (idx = 0; idx < ret.Description.Length; idx++) { 00082 if (ret.Description[idx] == '\0') { break; } 00083 } 00084 ret.Description = ret.Description.Substring(0, idx); 00085 int len = enc.GetByteCount(ret.Description); 00086 if ((len + 1) > bdata.Count) { 00087 bdata.Clear(); 00088 } else { 00089 bdata.RemoveRange(0, len + 1); 00090 } 00091 if (enc.GetByteCount("A") > 1) { 00092 // for multi-byte encodings 00093 bdata.RemoveAt(0); 00094 } 00095 } else { 00096 bdata.RemoveAt(0); 00097 } 00098 ret.Value = (bdata.Count > 0 ? ID3.Helper.ISO.GetString(bdata.ToArray()) : string.Empty); 00099 return ret; 00100 } 00101 00107 public static ID3.Frames.UserDefinedURL Parse(byte[] data) 00108 { 00109 /* <Header for 'User defined URL link frame', ID: "WXXX"> 00110 Text encoding $xx 00111 Description <text string according to encoding> $00 (00) 00112 URL <text string>*/ 00113 List<byte> bdata = new List<byte>(data); 00114 byte benc = bdata[0]; bdata.RemoveAt(0); 00115 System.Text.Encoding enc = ID3.Helper.GetEncoding(benc); 00116 string desc = string.Empty; 00117 if (bdata[0] != Frame.EOL) { 00118 desc = enc.GetString(bdata.ToArray()); 00119 int idx = 0; 00120 for (idx = 0; idx < desc.Length; idx++) { 00121 if (desc[idx] == '\0') { break; } 00122 } 00123 desc = desc.Substring(0, idx); 00124 int len = enc.GetByteCount(desc); 00125 if ((len + 1) > bdata.Count) { 00126 bdata.Clear(); 00127 } else { 00128 bdata.RemoveRange(0, len + 1); 00129 } 00130 if (enc.GetByteCount("A") > 1) { 00131 // for multi-byte encodings 00132 bdata.RemoveAt(0); 00133 } 00134 } else { 00135 bdata.RemoveAt(0); 00136 } 00137 string url = (bdata.Count > 0 ? ID3.Helper.ISO.GetString(bdata.ToArray()) : string.Empty); 00138 return (new ID3.Frames.UserDefinedURL(benc, desc, url)); 00139 } 00140 } 00141 }