mplib_paas.c

Go to the documentation of this file.
00001 /*
00002  * mplib - a library that enables you to edit ID3 tags
00003  *
00004  * Copyright (c) 2001,2002,2003,2004,2005,2006 Stefan Podkowinski
00005  *               2006                          Michal Kowalczuk
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without 
00009  * modification, are permitted provided that the following conditions are met:
00010  *
00011  * * Redistributions of source code must retain the above copyright notice, this
00012  *   list of conditions and the following disclaimer.
00013  * * Redistributions in binary form must reproduce the above copyright notice,
00014  *   this list of conditions and the following disclaimer in the documentation
00015  *   and/or other materials provided with the distribution.
00016  * * Neither the name of the author nor the names of its contributors
00017  *   may be used to endorse or promote products derived from this software
00018  *   without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00021  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00022  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00023  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00024  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00025  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00026  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00027  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00028  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00029  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00030  * POSSIBILITY OF SUCH DAMAGE.
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 /*STDC_HEADERS*/
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  *                                    Parse functions
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]) /* short descr. */
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          /* XXX multiple entries */
00193          return tc;
00194  }
00195  
00196  /*******************************************************************************************
00197  *                                    Assemble functions
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  }