示例#1
0
文件: mplib.c 项目: gitpan/MP3-Mplib
int 
mp_convert_to_v1(id3_tag *tag)
{
	id3v1_tag *v1;
	id3_tag* tmp;
	id3_content* content;
	id3_text_content* tc;
	id3_comment_content* cc;
	char* c;
	int j, k = 0;
	
	if(tag->version == 1) return 0;
	else if(tag->version == -1) return MP_EVERSION;
	
	v1 = XMALLOCD0(id3v1_tag, "mp_convert_to_v1:v1");
	
	content = mp_get_content(tag, MP_ARTIST);
	tc = mp_parse_artist(content);
	v1->artist = tc->text;
	xfree(tc);
	mp_free_content(content);
	
	content = mp_get_content(tag, MP_TITLE);
	tc = mp_parse_title(content);
	v1->title = tc->text;
	xfree(tc);
	mp_free_content(content);
	
	content = mp_get_content(tag, MP_ALBUM);
	tc = mp_parse_album(content);
	v1->album = tc->text;
	xfree(tc);
	mp_free_content(content);
	
	content = mp_get_content(tag, MP_YEAR);
	tc = mp_parse_year(content);
	v1->year = tc->text;
	xfree(tc);
	mp_free_content(content);
	
	content = mp_get_content(tag, MP_COMMENT);
	cc = mp_parse_comment(content);
	v1->comment = cc->text;
	xfree(cc->language);
	xfree(cc->short_descr);
	xfree(cc);
	mp_free_content(content);
	
	content = mp_get_content(tag, MP_TRACK);
	tc = mp_parse_track(content);
	c = tc->text;
	if(c)
	{
#ifdef HAVE_STRTOL
		errno = 0;
		j = strtol(c, (char **)NULL, 10);
		if(errno != ERANGE) v1->track = j;
		else v1->track = 0;
#else
		v1->track = atoi(c);
#endif
	}
	else v1->track = 0;
	xfree(c);
	mp_free_text_content(tc);
	mp_free_content(content);
	
	content = mp_get_content(tag, MP_GENRE);
	tc = mp_parse_genre(content);
	c = tc->text;
	for(j = 0; c, j < GLL; j++) {
		if(!strcmp(genre_list[j], c))
		{
			v1->genre = j;
			k = 1;
		}
	}
	if(!c) v1->genre = 0xFF;
	xfree(c);
	mp_free_text_content(tc);
	mp_free_content(content);
	
	id3v1_truncate_tag(v1);
	
	id3v2_free_tag(tag->tag);
	
	tag->version = 1;
	tag->tag = v1;
	
	return 0;
}
示例#2
0
void
do_expr(char *dir, char *file) 
{
  id3_tag_list *list, *fstlist;
  id3_tag *tag;
  mpeg_header *head;

  printf("Checking tag of file %s\n", file);

  res->checked++;
  head = mp_get_mpeg_header_from_file(file);
  if(!head) {
      printf("W A R N I N G: Cannot get mpeg header for file %s\n", file);
  }
  // TODO

  xfree(head);

  /* Get tag list */
  if((fstlist = list = mp_get_tag_list_from_file(file)))
  {
      id3_content* cont;
      id3_text_content* tcont;
      id3_comment_content* ccont;
      do {
	  char* curver = (list->tag->version == 2 ? "v2" : "v1");
	  tag = list->tag;
	  if(!tag) {
	      printf("%s: empty tag as list element\n");
	  }
	  cont = mp_get_content(tag, MP_ARTIST);
	  if(cont) {
	      tcont = mp_parse_artist(cont);
	      if(!tcont)
		  printf("%s: artist text content is null", file);
	      else
		  if(!tcont->text)
		      printf("%s: artist string provided with value null", file);
		  else
		      printf("artist %s: %s\n", curver, tcont->text);
	      
	      mp_free_text_content(tcont);
	      mp_free_content(cont);
	  }

	  /* Beware! C&P code */

	  cont = mp_get_content(tag, MP_TITLE);
	  if(cont) {
	      tcont = mp_parse_title(cont);
	      if(!tcont)
		  printf("E R R O R: title text content is null", file);
	      else
		  if(!tcont->text)
		      printf("E R R O R: artist string provided with value null", file);
		  else
		      printf("title %s: %s\n", curver, tcont->text);
	      
	      mp_free_text_content(tcont);
	      mp_free_content(cont);
	  }

	  cont = mp_get_content(tag, MP_ALBUM);
	  if(cont) {
	      tcont = mp_parse_album(cont);
	      if(!tcont)
		  printf("E R R O R: album text content is null", file);
	      else
		  if(!tcont->text)
		      printf("E R R O R: album string provided with value null", file);
		  else
		      printf("album %s: %s\n", curver, tcont->text);
	      
	      mp_free_text_content(tcont);
	      mp_free_content(cont);
	  }

	  cont = mp_get_content(tag, MP_YEAR);
	  if(cont) {
	      tcont = mp_parse_year(cont);
	      if(!tcont)
		  printf("E R R O R: year text content is null", file);
	      else
		  if(!tcont->text)
		      printf("E R R O R: year string provided with value null", file);
		  else
		      printf("year %s: %s\n", curver, tcont->text);
	      
	      mp_free_text_content(tcont);
	      mp_free_content(cont);
	  }

	  cont = mp_get_content(tag, MP_TRACK);
	  if(cont) {
	      tcont = mp_parse_track(cont);
	      if(!tcont)
		  printf("E R R O R: track text content is null", file);
	      else
		  if(!tcont->text)
		      printf("E R R O R: track string provided with value null", file);
		  else
		      printf("track %s: %s\n", curver, tcont->text);
	      
	      mp_free_text_content(tcont);
	      mp_free_content(cont);
	  }

	  cont = mp_get_content(tag, MP_GENRE);
	  if(cont) {
	      tcont = mp_parse_genre(cont);
	      if(!tcont)
		  printf("E R R O R: genre text content is null", file);
	      else
		  if(!tcont->text)
		      printf("E R R O R: genre string provided with value null", file);
		  else
		      printf("genre %s: %s\n", curver, tcont->text);
	      
	      mp_free_text_content(tcont);
	      mp_free_content(cont);
	  }

	  cont = mp_get_content(tag, MP_COMMENT);
	  if(cont) {
	      ccont = mp_parse_comment(cont);
	      if(!ccont)
		  printf("E R R O R: comment text content is null", file);
	      else
		  if(!ccont->text)
		      printf("E R R O R: comment string provided with value null", file);
		  else
		      printf("comment %s: %s\n", curver, ccont->text);
	      
	      mp_free_comment_content(ccont);
	      mp_free_content(cont);
	  }

	  
      } while((list = list->next));

      mp_free_list(fstlist);
  }
  else
  {
      puts("No tags found");
  }
}
示例#3
0
文件: mplib.c 项目: gitpan/MP3-Mplib
 int
 mp_set_content(id3_tag* tag, const int field, id3_content* new_content)
 {
	 id3v1_tag *v1;
	 id3v2_tag *v2;
	 
	 
	 if(!tag) return MP_EERROR;
	 
	 if(tag->version == 2)
	 {
		 return mp_set_content_at_pos(tag, field, new_content, 0);
	 }
	 else if(tag->version == 1)
	 {
		unsigned char c;
		char *my_val;
		int len, j;
		 
		v1 = tag->tag;
		 
		 switch(field)
		 {

#define FLD(str1, str2, str3, str4) \
			case str1:\
				if(!new_content) v1->str2 = NULL;\
				else\
				{\
					id3_text_content *tc = str4(new_content);\
					if(strlen(tc->text) > str3 || tc->encoding != ISO_8859_1)\
					{\
						mp_convert_to_v2(tag);\
						mp_free_text_content(tc);\
						return mp_set_content(tag, field, new_content);\
					}\
					\
				 	v1->str2 = tc->text;\
					xfree(tc);\
				}\
				break;
			 
			 FLD(MP_ARTIST, artist, 30, mp_parse_artist);
			 FLD(MP_TITLE, title, 30, mp_parse_title);
			 FLD(MP_ALBUM, album, 30, mp_parse_album);
			 FLD(MP_YEAR, year, 4, mp_parse_year);
				 
			 case MP_COMMENT:
				 if(!new_content) v1->comment = NULL;
				 else
				 {
					 id3_comment_content *tc = mp_parse_comment(new_content);
					 if(strlen(tc->text) > 30 || tc->short_descr || tc->encoding != ISO_8859_1)
					 {
						 mp_convert_to_v2(tag);
						 mp_free_comment_content(tc);
						 return mp_set_content(tag, field, new_content);
					 }
					 v1->comment = xmallocd0(strlen(tc->text)+1,
						       "mp_set_content:v1->comment");
					 memcpy(v1->comment, tc->text, strlen(tc->text));
					 mp_free_comment_content(tc);
				 }
				 break;

			 case MP_TRACK:
				 if(!new_content) v1->track = 0;
				 else
				 {
					 id3_text_content *tc = mp_parse_track(new_content);
#ifdef HAVE_STRTOL
					 errno = 0;
					 j = strtol(tc->text, (char **)NULL, 10);
					 if(errno != ERANGE) v1->track = j;
					 else return MP_EERROR;
#else
					 v1->track = atoi(tc->text);
#endif
					 mp_free_text_content(tc);
				 }
				 break;

			 case MP_GENRE:
				 if(!new_content) v1->genre = 0xFF;
				 else
				 {
					 int b = 0, i;
					 id3_text_content *tc = mp_parse_genre(new_content);
					 /* i = strlen(tc->text); */
					 for(c = 0; c < GLL; c++) {
						 if(!strcmp(genre_list[c], tc->text))
						 {
							 v1->genre = c;
							 b = 1;
						 }
					 }
					 mp_free_text_content(tc);
					 if(!b)
					 {
						 mp_convert_to_v2(tag);
						 return mp_set_content(tag, field, new_content);
					 }
					 break;
				 }
			 }
	 }
	 else if(tag->version == -1) return MP_EVERSION;
	 else return MP_EFNF;
	 
	 return 0;
 }