00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifdef HAVE_CONFIG_H
00034 # include "config.h"
00035 #endif
00036
00037 #if STDC_HEADERS
00038 # include <stdlib.h>
00039 # include <string.h>
00040 #elif HAVE_STRINGS_H
00041 # include <strings.h>
00042 #endif
00043
00044 #if HAVE_UNISTD_H
00045 # include <unistd.h>
00046 # include <sys/types.h>
00047 #endif
00048
00049 #include <errno.h>
00050 #include <stdio.h>
00051 #include <fcntl.h>
00052
00053 #include "mplib.h"
00054 #include "xmalloc.h"
00055
00056
00057
00058
00059
00060
00061
00062 id3_text_content*
00063 mp_parse_artist(const id3_content* content)
00064 {
00065 return mp_parse_text(content);
00066 }
00067
00068 id3_text_content*
00069 mp_parse_title(const id3_content* content)
00070 {
00071 return mp_parse_text(content);
00072 }
00073
00074 id3_text_content*
00075 mp_parse_album(const id3_content* content)
00076 {
00077 return mp_parse_text(content);
00078 }
00079
00080 id3_text_content*
00081 mp_parse_year(const id3_content* content)
00082 {
00083 return mp_parse_text(content);
00084 }
00085
00086 id3_text_content*
00087 mp_parse_genre(const id3_content* content)
00088 {
00089 return mp_parse_text(content);
00090 }
00091
00092 id3_text_content*
00093 mp_parse_track(const id3_content* content)
00094 {
00095 return mp_parse_text(content);
00096 }
00097
00098 id3_comment_content*
00099 mp_parse_comment(const id3_content* content)
00100 {
00101 id3_comment_content* cc;
00102 int i, e;
00103
00104 if(!content || !content->data)
00105 {
00106 errno = MP_EERROR;
00107 return NULL;
00108 }
00109
00110 if(content->encrypted)
00111 {
00112 errno = MP_EFENCR;
00113 return NULL;
00114 }
00115 if(content->compressed)
00116 {
00117 errno = MP_EFCOMPR;
00118 return NULL;
00119 }
00120
00121 cc = XMALLOCD0(id3_comment_content, "mp_parse_comment:cc");
00122
00123 e = content->data[0];
00124 if(e >= ISO_8859_1 && e <= UTF8) cc->encoding = e;
00125 else cc->encoding = 0;
00126
00127 cc->language = xmallocd(4, "mp_parse_comment:cc->language");
00128 cc->language[0] = content->data[1];
00129 cc->language[1] = content->data[2];
00130 cc->language[2] = content->data[3];
00131 cc->language[3] = 0;
00132
00133 if(content->data[4])
00134 {
00135 i = strlen(content->data + 4) + 1;
00136 cc->short_descr = xmallocd(i, "mp_parse_comment:cc->short_descr");
00137 strncpy(cc->short_descr, content->data + 4, i);
00138 }
00139 else
00140 {
00141 cc->short_descr = NULL;
00142 i = 1;
00143 }
00144
00145 if((int)(content->length - 4 - i) > 0)
00146 {
00147 cc->text = xmallocd(content->length - 4 - i + 1, "mp_parse_comment:cc->text");
00148 memcpy(cc->text, content->data + 4 + i, content->length - 4 - i);
00149 cc->text[content->length - 4 - i] = 0;
00150 }
00151 else
00152 {
00153 cc->text = NULL;
00154 mp_free_comment_content(cc);
00155 cc = NULL;
00156 }
00157
00158 return cc;
00159 }
00160
00161 id3_text_content*
00162 mp_parse_text(const id3_content* content)
00163 {
00164 id3_text_content* tc;
00165 int e;
00166
00167 if(!content || !content->data)
00168 {
00169 errno = MP_EERROR;
00170 return NULL;
00171 }
00172
00173 if(content->encrypted)
00174 {
00175 errno = MP_EFENCR;
00176 return NULL;
00177 }
00178 if(content->compressed)
00179 {
00180 errno = MP_EFCOMPR;
00181 return NULL;
00182 }
00183
00184 tc = XMALLOCD0(id3_text_content, "mp_parse_text:tc");
00185 tc->text = xmallocd(content->length, "mp_parse_text:tc->text");
00186 e = content->data[0];
00187 if(e >= ISO_8859_1 && e <= UTF8) tc->encoding = e;
00188 else tc->encoding = 0;
00189
00190 memcpy(tc->text, content->data + 1, content->length - 1);
00191 tc->text[content->length - 1] = 0;
00192
00193 return tc;
00194 }
00195
00196
00197
00198
00199
00200 id3_content*
00201 mp_assemble_artist_content(const char* text, id3_encoding enc)
00202 {
00203 return mp_assemble_text_content(text, enc);
00204 }
00205
00206 id3_content*
00207 mp_assemble_title_content(const char* text, id3_encoding enc)
00208 {
00209 return mp_assemble_text_content(text, enc);
00210 }
00211
00212 id3_content*
00213 mp_assemble_album_content(const char* text, id3_encoding enc)
00214 {
00215 return mp_assemble_text_content(text, enc);
00216 }
00217
00218 id3_content*
00219 mp_assemble_year_content(const char* text, id3_encoding enc)
00220 {
00221 return mp_assemble_text_content(text, enc);
00222 }
00223
00224 id3_content*
00225 mp_assemble_genre_content(const char* text, id3_encoding enc)
00226 {
00227 return mp_assemble_text_content(text, enc);
00228 }
00229
00230 id3_content*
00231 mp_assemble_text_content(const char* text, id3_encoding enc)
00232 {
00233 id3_content *ret;
00234
00235 if(!text) return NULL;
00236
00237 ret = XMALLOCD0(id3_content, "mp_assemble_text_content:ret");
00238 ret->length = strlen(text) + 1;
00239 ret->data = xmallocd(ret->length, "mp_asseble_text_content:ret->data");
00240 ret->data[0] = enc;
00241 strncpy(ret->data + 1, text, strlen(text));
00242
00243 return ret;
00244 }
00245
00246 id3_content*
00247 mp_assemble_comment_content(const char* text, const char* short_descr, id3_encoding enc, const char* lang)
00248 {
00249 id3_content *ret;
00250
00251 if(!text) return NULL;
00252
00253 ret = XMALLOCD0(id3_content, "mp_assemble_comment_content:ret");
00254 ret->length = strlen(text) + 5;
00255 if(short_descr) ret->length += strlen(short_descr);
00256
00257 ret->data = xmallocd(ret->length, "mp_assemble_comment_content:ret->data");
00258 ret->data[0] = enc;
00259 if(lang && strlen(lang) == 3)
00260 {
00261 ret->data[1] = lang[0];
00262 ret->data[2] = lang[1];
00263 ret->data[3] = lang[2];
00264 }
00265 else
00266 {
00267 ret->data[1] = 'X';
00268 ret->data[2] = 'X';
00269 ret->data[3] = 'X';
00270 }
00271 if(short_descr) strcpy(ret->data + 4, short_descr);
00272 else ret->data[4] = 0;
00273
00274 if(short_descr) strncpy(ret->data + 5 + strlen(short_descr), text, strlen(text));
00275 else strncpy(ret->data + 5, text, strlen(text));
00276
00277 return ret;
00278
00279 }