/ID3.cpp
C++ | 104 lines | 75 code | 16 blank | 13 comment | 8 complexity | 17fc8b977b9293192a7e43d319914b27 MD5 | raw file
Possible License(s): LGPL-2.0
- #include <sys/stat.h> // For stat | TODO: Replace with c++ equivalent
- #include <string>
- #include <cstring>
- #include <fstream>
- #include <stdlib.h>
- #include <iostream>
- #include <bitset>
- #include <vector>
- #include "ID3.hpp"
- ID3::ID3(std::string filename)
- {
- file.open(filename.c_str(), std::ios::in | std::ios::binary);
- struct stat fstat;
- if (stat(filename.c_str(), &fstat) != 0)
- {
- std::cerr << "Error opening file! (stat)" << std::endl;
- exit(1);
- }
-
- flen = fstat.st_size;
- }
- bool ID3::hasTag()
- {
- char magic[3];
- file.seekg(0);
- file.read(magic, 3);
- return strncmp( magic, "ID3", 3 ) == 0;
- }
- ID3_Header ID3::getHeader()
- {
- ID3_Header header;
- if(!hasTag()) // Maybe throw exception
- return header;
-
- file.seekg(0);
- file.read((char*)&header, 10);
- return header;
- }
- void ID3::parse()
- {
- /*
- ID3_Header header;
- if(!hasTag())
- return;
- header = getHeader();
- */
-
- /*
- std::cout.write(header.ident, 3);
- std::cout << std::endl;
-
- std::cout << *((int16_t*)header.version) << std::endl;
- */
- }
- int32_t ID3::syncIntConv(int32_t num)
- {
- // Reverse Byte Order
- unsigned char bytes[4];
- bytes[0] = (num >> 24) & 0xFF;
- bytes[1] = (num >> 16) & 0xFF;
- bytes[2] = (num >> 8) & 0xFF;
- bytes[3] = num & 0xFF;
- num = *((int32_t*)bytes);
- std::bitset<sizeof(int32_t)*8> source(num);
- std::bitset<sizeof(int32_t)*8> target;
- unsigned int targetCounter = 0;
- for(unsigned int i = 0; i < sizeof(int32_t)*8; ++i)
- {
- if((i+1) % 8 != 0)
- {
- target.set(targetCounter, source[i]);
- targetCounter++; // Set bits from back to beginning to convert to msb order
- }
- }
- return target.to_ulong();
- }
- int32_t ID3::size()
- {
- ID3_Header header;
- if(hasTag())
- {
- header = getHeader();
- return syncIntConv(*((int32_t*)header.size)) + sizeof(ID3_Header);
- }
- else
- return 0;
- }
- ID3::~ID3()
- {
- file.close();
- }