![]() |
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 UserDefinedString : TextFrame 00009 { 00010 public UserDefinedString() 00011 : base(ID3.Frame.Keys.UserDefinedString) 00012 { 00013 this.Initialize(0x00, string.Empty, string.Empty); 00014 this.GenerateTag(); 00015 } 00016 00017 public UserDefinedString(string text) 00018 : base(ID3.Frame.Keys.UserDefinedString) 00019 { 00020 this.Initialize(0x00, string.Empty, text); 00021 this.GenerateTag(); 00022 } 00023 00024 public UserDefinedString(string text, string desc) 00025 : base(ID3.Frame.Keys.UserDefinedString) 00026 { 00027 this.Initialize(0x00, desc, text); 00028 this.GenerateTag(); 00029 } 00030 00031 private UserDefinedString(byte encoding, string desc, string text) 00032 : base(ID3.Frame.Keys.UserDefinedString) 00033 { 00034 this.Initialize(encoding, desc, text); 00035 this.GenerateTag(); 00036 } 00037 00038 public UserDefinedString(Frame other) 00039 : base(other) 00040 { 00041 if (other.ID != ID3.Frame.Keys.UserDefinedString) { throw new System.InvalidCastException("Frame ID must be equal to copy the data within it."); } 00042 UserDefinedString c = UserDefinedString.Parse(other); 00043 this.Description = c.Description; 00044 this.Encoding = c.Encoding; 00045 this.Value = c.Value; 00046 } 00047 00048 public UserDefinedString(TextFrame other) 00049 : base(other) 00050 { 00051 if (other.ID != ID3.Frame.Keys.UserDefinedString) { throw new System.InvalidCastException("Frame ID must be equal to copy the data within it."); } 00052 } 00053 00054 public UserDefinedString(UserDefinedString other) 00055 : base(other) 00056 { 00057 } 00058 00064 public static ID3.Frames.UserDefinedString Parse(Frame frame) 00065 { 00066 if (frame.ID != ID3.Frame.Keys.UserDefinedString) { throw new System.InvalidCastException("Frame ID must be equal to copy the data within it."); } 00067 UserDefinedString ret = new UserDefinedString(); //(UserDefinedText)frame; 00068 ret.Data = frame.Data; 00069 ret.Format = frame.Format; 00070 ret.Status = frame.Status; 00071 /* <Header for 'User defined text information frame', ID: "TXXX"> 00072 Text encoding $xx 00073 Description <text string according to encoding> $00 (00) 00074 Value <text string according to encoding>*/ 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 if (bdata.Count > 0 && bdata[0] != Frame.EOL) { 00099 ret.Value = enc.GetString(bdata.ToArray()); 00100 int idx = 0; 00101 for (; idx < ret.Value.Length; idx++) { 00102 if (ret.Value[idx] == '\0') { break; } 00103 } 00104 ret.Value = ret.Value.Substring(0, idx); 00105 } 00106 return ret; 00107 } 00108 00114 public static ID3.Frames.UserDefinedString Parse(byte[] data) 00115 { 00116 /* <Header for 'User defined text information frame', ID: "TXXX"> 00117 Text encoding $xx 00118 Description <text string according to encoding> $00 (00) 00119 Value <text string according to encoding>*/ 00120 List<byte> bdata = new List<byte>(data); 00121 byte benc = bdata[0]; bdata.RemoveAt(0); 00122 System.Text.Encoding enc = ID3.Helper.GetEncoding(benc); 00123 string desc = string.Empty; 00124 string text = string.Empty; 00125 if (bdata[0] != Frame.EOL) { 00126 desc = enc.GetString(bdata.ToArray()); 00127 int idx = 0; 00128 for (idx = 0; idx < desc.Length; idx++) { 00129 if (desc[idx] == '\0') { break; } 00130 } 00131 desc = desc.Substring(0, idx); 00132 int len = enc.GetByteCount(desc); 00133 if ((len + 1) > bdata.Count) { 00134 bdata.Clear(); 00135 } else { 00136 bdata.RemoveRange(0, len + 1); 00137 } 00138 if (enc.GetByteCount("A") > 1) { 00139 // for multi-byte encodings 00140 bdata.RemoveAt(0); 00141 } 00142 } else { 00143 bdata.RemoveAt(0); 00144 } 00145 if (bdata[0] != Frame.EOL) { 00146 text = enc.GetString(bdata.ToArray()); 00147 int idx = 0; 00148 for (; idx < text.Length; idx++) { 00149 if (text[idx] == '\0') { break; } 00150 } 00151 text = text.Substring(0, idx); 00152 } 00153 return (new ID3.Frames.UserDefinedString(benc, desc, text)); 00154 } 00155 } 00156 }