Nsound  0.9.4
Public Member Functions | Public Attributes | Friends | List of all members
Nsound::ID3v1Tag Class Reference

#include <Nsound/Wavefile.h>

Public Member Functions

 ID3v1Tag (const std::string &filename="", boolean show_warnings=true)
 
boolean read (const std::string &filename, boolean show_warnings=true)
 Returns true if it found the tag, false otherwise. More...
 
boolean write (const std::string &filename, boolean show_warnings=true)
 Returns true if it successfully wrote the tag to the end of the file, false otherwise. More...
 

Public Attributes

std::string title
 
std::string artist
 
std::string album
 
std::string year
 
std::string comment
 
char genre
 

Friends

std::ostream & operator<< (std::ostream &out, const ID3v1Tag &rhs)
 Sends the contents of the Buffer to the output stream. More...
 

Detailed Description

Definition at line 254 of file Wavefile.h.

Constructor & Destructor Documentation

ID3v1Tag::ID3v1Tag ( const std::string &  filename = "",
boolean  show_warnings = true 
)

Definition at line 1532 of file Wavefile.cc.

References read().

1533  : title(),
1534  artist(),
1535  album(),
1536  year(),
1537  comment(),
1538  genre(' ')
1539 {
1540  if(filename.size() > 0)
1541  {
1542  this->read(filename,show_warnings);
1543  }
1544 }
std::string title
Definition: Wavefile.h:268
std::string artist
Definition: Wavefile.h:269
boolean read(const std::string &filename, boolean show_warnings=true)
Returns true if it found the tag, false otherwise.
Definition: Wavefile.cc:1663
std::string comment
Definition: Wavefile.h:272
std::string year
Definition: Wavefile.h:271
std::string album
Definition: Wavefile.h:270

Member Function Documentation

boolean ID3v1Tag::read ( const std::string &  filename,
boolean  show_warnings = true 
)

Returns true if it found the tag, false otherwise.

Definition at line 1663 of file Wavefile.cc.

References album, RawTag::album, artist, RawTag::artist, comment, RawTag::comment, findID3v1Tag(), genre, RawTag::genre, RawTag::header, IO_ERROR, M_THROW, NOT_FOUND, title, RawTag::title, year, and RawTag::year.

Referenced by ID3v1Tag().

1664 {
1665  FILE * fin = fopen(filename.c_str(), "rb");
1666 
1667  if(fin == NULL)
1668  {
1669  M_THROW("ID3v1Tag::read(): could not open the file '"
1670  << filename
1671  << "'");
1672  return false;
1673  }
1674 
1675  int64 tag_pos = static_cast<size_t>(findID3v1Tag(fin));
1676 
1677  if(tag_pos == IO_ERROR)
1678  {
1679  fclose(fin);
1680  M_THROW("ID3v1Tag::read(): ID3v1Tag::find() failed!");
1681  return false;
1682  }
1683 
1684  if(tag_pos == NOT_FOUND && show_warnings)
1685  {
1686  fclose(fin);
1687  cerr << "*** Warning --> ID3v1Tag::read(): could not find the ID3v1 'TAG'"
1688  << endl;
1689  return false;
1690  }
1691 
1692  // Seek to the tag.
1693  #ifdef NSOUND_PLATFORM_OS_WINDOWS
1694  fseek(fin, static_cast<long>(tag_pos), SEEK_SET);
1695  #else
1696  fseek(fin, tag_pos, SEEK_SET);
1697  #endif
1698 
1699  RawTag tag;
1700 
1701  memset(reinterpret_cast<char *>(&tag), 0, sizeof(RawTag));
1702 
1703  // Read the tag.
1704  if(sizeof(RawTag) != fread(tag.header, 1, sizeof(RawTag), fin))
1705  {
1706  fclose(fin);
1707  cerr << "*** Error --> ID3v1Tag::read(): failed to read the ID3v1 TAG!"
1708  << endl;
1709  return false;
1710  }
1711 
1712  fclose(fin);
1713 
1714  this->title.clear();
1715  this->artist.clear();
1716  this->album.clear();
1717  this->year.clear();
1718  this->comment.clear();
1719  this->genre = ' ';
1720 
1721  // Title
1722  for(uint32 i = 0; i < 30; ++i)
1723  {
1724  if(tag.title[i] != '\0') this->title += tag.title[i];
1725  else break;
1726  }
1727 
1728  // Artist
1729  for(uint32 i = 0; i < 30; ++i)
1730  {
1731  if(tag.artist[i] != '\0') this->artist += tag.artist[i];
1732  else break;
1733  }
1734 
1735  // Album
1736  for(uint32 i = 0; i < 30; ++i)
1737  {
1738  if(tag.album[i] != '\0') this->album += tag.album[i];
1739  else break;
1740  }
1741 
1742  // Year
1743  for(uint32 i = 0; i < 4; ++i)
1744  {
1745  if(tag.year[i] != '\0') this->year += tag.year[i];
1746  else break;
1747  }
1748 
1749  // Comment
1750  for(uint32 i = 0; i < 30; ++i)
1751  {
1752  if(tag.comment[i] != '\0') this->comment += tag.comment[i];
1753  else break;
1754  }
1755 
1756  // Genre
1757  this->genre = tag.genre;
1758 
1759  return true;
1760 }
#define NOT_FOUND
Definition: Wavefile.cc:1550
unsigned int uint32
Definition: Nsound.h:153
#define IO_ERROR
Definition: Wavefile.cc:1551
char year[4]
Definition: Wavefile.cc:1524
char title[30]
Definition: Wavefile.cc:1521
char genre
Definition: Wavefile.cc:1526
std::string title
Definition: Wavefile.h:268
char comment[30]
Definition: Wavefile.cc:1525
char album[30]
Definition: Wavefile.cc:1523
char header[3]
Definition: Wavefile.cc:1520
#define M_THROW(message)
Definition: Macros.h:108
std::string artist
Definition: Wavefile.h:269
int64 findID3v1Tag(FILE *fin)
Definition: Wavefile.cc:1554
std::string comment
Definition: Wavefile.h:272
signed long long int64
Definition: Nsound.h:143
std::string year
Definition: Wavefile.h:271
std::string album
Definition: Wavefile.h:270
char artist[30]
Definition: Wavefile.cc:1522
boolean ID3v1Tag::write ( const std::string &  filename,
boolean  show_warnings = true 
)

Returns true if it successfully wrote the tag to the end of the file, false otherwise.

Definition at line 1764 of file Wavefile.cc.

References album, RawTag::album, artist, RawTag::artist, comment, RawTag::comment, findID3v1Tag(), genre, RawTag::genre, RawTag::header, IO_ERROR, M_THROW, MIN, NOT_FOUND, title, RawTag::title, year, and RawTag::year.

1765 {
1766  FILE * fin = fopen(filename.c_str(), "r+b");
1767 
1768  if(fin == NULL)
1769  {
1770  M_THROW("ID3v1Tag::write(): could not open the file '"
1771  << filename
1772  << "'");
1773  return false;
1774  }
1775 
1776  int64 tag_pos = findID3v1Tag(fin);
1777 
1778  if(tag_pos == IO_ERROR)
1779  {
1780  M_THROW("ID3v1Tag::write(): ID3v1Tag::find() failed!"
1781  << filename
1782  << "'");
1783  return false;
1784  }
1785 
1786  // If he tag wasn't found, just go to the end of the file.
1787  else if(tag_pos == NOT_FOUND)
1788  {
1789  fseek(fin, 0, SEEK_END);
1790  }
1791 
1792  // Seek to the tag.
1793  else
1794  {
1795  #ifdef NSOUND_PLATFORM_OS_WINDOWS
1796  fseek(fin, static_cast<long>(tag_pos), SEEK_SET);
1797  #else
1798  fseek(fin, tag_pos, SEEK_SET);
1799  #endif
1800  }
1801 
1802  RawTag tag;
1803 
1804  memset(reinterpret_cast<char *>(&tag), 0, sizeof(RawTag));
1805 
1806  tag.header[0] = 'T';
1807  tag.header[1] = 'A';
1808  tag.header[2] = 'G';
1809 
1810  #define MIN(a,b) ((a < b) ? a : b)
1811 
1812  // Title
1813  for(uint32 i = 0; i < MIN(30, this->title.size()); ++i)
1814  {
1815  tag.title[i] = this->title[i];
1816  }
1817 
1818  // Artist
1819  for(uint32 i = 0; i < MIN(30, this->artist.size()); ++i)
1820  {
1821  tag.artist[i] = this->artist[i];
1822  }
1823 
1824  // Album
1825  for(uint32 i = 0; i < MIN(30, this->album.size()); ++i)
1826  {
1827  tag.album[i] = this->album[i];
1828  }
1829 
1830  // Year
1831  for(uint32 i = 0; i < MIN(4, this->year.size()); ++i)
1832  {
1833  tag.year[i] = this->year[i];
1834  }
1835 
1836  // Comment
1837  for(uint32 i = 0; i < MIN(30, this->comment.size()); ++i)
1838  {
1839  tag.comment[i] = this->comment[i];
1840  }
1841 
1842  // Genre
1843  tag.genre = this->genre;
1844 
1845  // Write the tag.
1846  if(sizeof(RawTag) != fwrite(tag.header, 1, sizeof(RawTag), fin))
1847  {
1848  fclose(fin);
1849  M_THROW("ID3v1Tag::write(): failed to write the ID3v1Tag!");
1850  return false;
1851  }
1852 
1853  fclose(fin);
1854  return true;
1855 }
#define NOT_FOUND
Definition: Wavefile.cc:1550
unsigned int uint32
Definition: Nsound.h:153
#define IO_ERROR
Definition: Wavefile.cc:1551
char year[4]
Definition: Wavefile.cc:1524
char title[30]
Definition: Wavefile.cc:1521
#define MIN(a, b)
char genre
Definition: Wavefile.cc:1526
std::string title
Definition: Wavefile.h:268
char comment[30]
Definition: Wavefile.cc:1525
char album[30]
Definition: Wavefile.cc:1523
char header[3]
Definition: Wavefile.cc:1520
#define M_THROW(message)
Definition: Macros.h:108
std::string artist
Definition: Wavefile.h:269
int64 findID3v1Tag(FILE *fin)
Definition: Wavefile.cc:1554
std::string comment
Definition: Wavefile.h:272
signed long long int64
Definition: Nsound.h:143
std::string year
Definition: Wavefile.h:271
std::string album
Definition: Wavefile.h:270
char artist[30]
Definition: Wavefile.cc:1522

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  out,
const ID3v1Tag rhs 
)
friend

Sends the contents of the Buffer to the output stream.

Member Data Documentation

std::string Nsound::ID3v1Tag::title

Definition at line 268 of file Wavefile.h.

Referenced by Nsound::operator<<(), read(), and write().

std::string Nsound::ID3v1Tag::artist

Definition at line 269 of file Wavefile.h.

Referenced by Nsound::operator<<(), read(), and write().

std::string Nsound::ID3v1Tag::album

Definition at line 270 of file Wavefile.h.

Referenced by Nsound::operator<<(), read(), and write().

std::string Nsound::ID3v1Tag::year

Definition at line 271 of file Wavefile.h.

Referenced by Nsound::operator<<(), read(), and write().

std::string Nsound::ID3v1Tag::comment

Definition at line 272 of file Wavefile.h.

Referenced by Nsound::operator<<(), read(), and write().

char Nsound::ID3v1Tag::genre

Definition at line 273 of file Wavefile.h.

Referenced by Nsound::operator<<(), read(), and write().


The documentation for this class was generated from the following files: