taglib, C++, tags modifications - c++

I am a classical music fan. My music collection (mp3) has been carefully classified using "composer" (ex. "Surname name (D.O.B-D.O.D"). I frequently get the "artist" from importing the music, ripping or some online data base. Because my mobile music player (Xbox) orders only by "artist", I would like to "swap":
album_artist = artist
artist = composer
and composer would simply remain the same (and same as artist). (Visual Studio 2013, W7, taglib1.9.1):
TagLib::PropertyMap tags = f.file()->properties();
unsigned int longest = 0;
for (TagLib::PropertyMap::ConstIterator i = tags.begin(); i != tags.end(); ++i) {
if (i->first.size() > longest) {
longest = i->first.size();
}
}
cout << "-- TAG (properties) --" << endl;
for (TagLib::PropertyMap::Iterator i = tags.begin(); i != tags.end(); ++i) {
if (i->first == "COMPOSER") {
composer = i->second;
composer_key = i->first;
}
if (i->first == "ARTIST") {
artist.append(i->second);
artist_key = i->first;
}
if (i->first == "ALBUMARTIST") {
album_artist.append(i->second);
album_artist_key = i->first;
}
cout << left << std::setw(longest) << i->first << " - " << '"' << i->second << '"' << endl;
}
if (!tags.replace(album_artist_key, artist))
cout << "album_artist_key is wrong";
else
cout << "replacing " << album_artist_key << " with " << artist << endl;
if (!tags.replace(artist_key, composer))
cout << "artist is wrong";
else
cout << "replacing " << artist_key << " with " << composer << endl;
tag->setArtist(composer.toString());
f.save();
NOTE: this code was modified starting from the tagreader.cpp code found in examples of the library.
This compiles, but after execution, all ID3 tags info disappear (corruption?), as seen by windows explorer. So, I did an experiment and commented out everything that makes any change to the tag. Basically, just open the file (FileRef) and do f.save(). This alone causes the tags to disappear.
Two questions (I think I got this completely wrong ...)
Any reason why f.save would cause a metadata corruption ?
Is the idea I am following (tags.replace and the f.save) correct?

That's not corruption; Windows Explorer just still can't read ID3v2.4 tags, a standard which came out 15 years ago, and is the default in TagLib. TagLib can, however, also write ID3v2.3 tags.

Related

Saving and sorting game information into an external file

I have this Blackjack card game (simple text-only console application) that I have been working on and currently I am trying to improve the way it stores the statistics in an external .txt file. I have been learning and messing around with fstream stuff and I got it to write the statistics into the external file: How the statistics are saved in a .txt file.
Now I would like to improve the system so that it sorts the players' entries according to the win ratio. I basically need to read a value from a specific spot in each existing entry (win ratio), compare them, and insert the new entry to the correct spot according to the value. Each entry has the same format as shown in the picture. Then after the .txt file has been sorted, I just need to basically read it and display it in the console.
I am pretty inexperienced with using fstream stuff and manipulating files in general so I would really appreciate some direction with what is the most efficient way to do this. I have thought of several ways of reading and comparing the win ratios but I can't think of a good way to separate each player's entry and to insert the new entry into the corresponding spot.
Here is how the current code of the saving system works, in case it is useful:
void saveGame() {
string playerName = {};
getline(cin, playerName);
fstream savefile;
savefile.open("Blackjack_Statistics.txt", fstream::app);
if (savefile.is_open()) {
savefile << "\n ---~~=[ " << playerName;
if (playerName[playerName.size() - 1] == 's') {
savefile << "' ";
} else {
savefile << "'s ";
}
savefile << "Statistics ]=~~---\n\n\t[Hands won]: " << countWins << "\n\t[Hands lost]: " << countLosses << "\n\t[Hands pushed]: " << countPushes << "\n\t[Hand win ratio]: ";
if (countLosses > 0) {
savefile << (countWins / countLosses);
} else {
savefile << (countWins / 1);
}
savefile << "\n\n\t[Blackjacks]: " << countBlackjacks << "\n\t[Dealer blackjacks]: " << countDealerBlackjacks << "\n\t[Double downs won]: " << countDouble << "\n\t[Longest win streak]: " << countStreakLongest << "\n\n";
} else {
cout << "Saving failed!";
}
savefile.close();
return;
}
Thanks! :)

Runtime error, unable to determine the cause

I'm implementing the abstract data type Map for my algorithms class, and we're asked to implement it using 2 arrays.
I have done the implementation last night and everything worked fine(when we send the assignment online it is tested using autotests), and it passed every test.
Then I needed to implement it using binary search, and I did that.
This morning when I ran the code it gave me a runtime error and I think my copy constructor is the cause, because when I commented it out, the code worked fine, yet my copy constructor is exactly as my tutor implemented it.
Here is the code it crashes on:
ArrayMap<string,string> m;
string s1("Sarajevo"), s2("Zagreb");
m[s1] = "BiH";
m[s2] = "Hrvatska";
{
ArrayMap<string,string> m2(m);
ArrayMap<string,string> m3;
m3=m;
m.empty(s1);
cout << m2.numOfElements() << " ";
cout << m2[s1] << " ";
cout << m3.numOfElements() << " ";
cout << m3[s1] << " ";
}
cout << m.numOfElements();
cout << " '" << m[s1] << "' ";
m.obrisi();
cout << m.numOfElements();
cout << " '" << m[s2] << "'";
And here is the constructor:
ArrayMap(const NizMapa<TypeOfKey, TypeOfValue>& rhs){
_capacity = rhs._capacity;
_numOfElements = rhs._numOfElements;
_arrayK = new TypeOfKey[_capacity];
_arrayV = new TypeOfValue[_capacity];
for(int i = 0; i<_numOfElements; i++){
_arrayK[i] = rhs._arrayK[i];
_arrayV[i] = rhs._arrayV[i];
}
}
EDIT:
SSCE
ArrayMap<string,string> m;
string s1("Sarajevo"), s2("Zagreb");
m[s1] = "BiH";
m[s2] = "Hrvatska";
ArrayMap<string,string> m2(m);
cout << m2[s1] << " " << m2.numOfElements();
/* When I do just this it works fine, but when combined with others it crashes the program*/
EDIT: http://pastebin.com/vXdBTs4n
This is the class implementation:
NizMapa is ArrayMap
_kapacitet is _capacity
_brojE is _numOfElements
TipKljuca is TypeOfKey
TipVrijednosti is TypeOfValue
methods:
obrisi is empty/delete(2 versions)
Note:this is NOT the binary search version, it's the sequential search one. If any additional info is needed please ask, this is important.
Thanks in advance!
Here you allocate using _capacity
_capacity = rhs._capacity;
_numOfElements = rhs._numOfElements;
_arrayK = new TypeOfKey[_capacity];
_arrayV = new TypeOfValue[_capacity];
burt here you use _numOfElements
for(int i = 0; i<_numOfElements; i++){
as the loop boundary. So these two probably don't match and your are writing beyond the array boundary if _numOfElements > _capacity.

Parsing text file into list gives segmentation fault

I'm getting a segmentation fault while trying to parse a big text file. The file contains 91 529 mRNA transcripts and details about these transcripts. I've created a RefSeqTranscript object that will take these details. When I parse the file, I create a list of these objects and start putting the details into these lists. It works fine for the first 1829 transcripts and then crashes with a segmentation fault. The method I'm running is:
void TranscriptGBFFParser::ParseFile(list<RefSeqTranscript> &transcripts, const char* filepath)
{
cout << "Parsing " << filepath << "..." << endl;
ifstream infile;
infile.open(filepath);
int num = 0;
RefSeqTranscript *transcript = new RefSeqTranscript();
for(string line; getline(infile, line); )
{
in.clear();
in.str(line);
if (boost::starts_with(line, "LOCUS"))
{
if((*transcript).transcriptRefSeqAcc.size() > 0)
{
cout << (*transcript).transcriptRefSeqAcc << ":" << (*transcript).gi << ":" << (*transcript).gene.geneName << ":" << ++num << endl;
transcripts.push_back(*transcript);
delete transcript;
RefSeqTranscript *transcript = new RefSeqTranscript();
}
}
else if (boost::starts_with(line, " var"))
{
TranscriptVariation variant;
(*transcript).variations.push_back(variant);
}
//Store the definition of the transcript in the description attribute
else if (boost::starts_with(line, "DEFINITION"))
{
(*transcript).description = line.substr(12);
for(line; getline(infile, line); )
{
if(boost::starts_with(line, "ACCESSION "))
break;
(*transcript).description += line.substr(12);
}
}
//The accession number and GI number are obtained from the VERSION line
else if (boost::starts_with(line, "VERSION"))
{
string versions = line.substr(12);
vector<string> strs;
boost::split(strs, versions, boost::is_any_of( " GI:" ), boost::token_compress_on);
boost::trim_left(strs[0]);
(*transcript).transcriptRefSeqAcc = strs[0];
(*transcript).gi = atoi(strs[1].c_str());
}
//Gene information is obtained from the "gene" sections of each transcript
else if (boost::starts_with(line, " gene"))
{
for(line; getline(infile, line); )
{
if(boost::starts_with(line.substr(21), "/gene="))
{
Gene *gene = new Gene();
string name = line.substr(27);
Utilities::trim(name, '\"');
(*gene).geneName = name;
(*transcript).gene = *gene;
delete gene;
break;
}
}
(*transcript).gene.geneID = 0;
}
else if (boost::starts_with(line, " CDS"))
{
(*transcript).proteinRefSeqAcc = "";
}
else if (boost::starts_with(line, "ORIGIN"))
{
(*transcript).sequence = "";
}
}
cout << (*transcript).transcriptRefSeqAcc << ":" << (*transcript).gi << ":" << (*transcript).gene.geneName << endl;
transcripts.push_back(*transcript);
delete transcript;
cout << "No. transcripts: " << transcripts.size() << endl;
cout << flush;
infile.close();
cout << "Finished parsing " << filepath << "." << endl;
}
I'm new to C++ and don't have a great understanding of how to work with pointers etc so I'm guessing I might have done something wrong there. I don't understand why it would work for almost 2000 objects before cutting out though.
The file I'm parsing is 2.1 GB and consists of about 44 000 000 lines so any tips on how to improve the efficiency would also be much appreciated.
This is probably not the only answer, but you have a leak...
if (boost::starts_with(line, "LOCUS"))
{
if((*transcript).transcriptRefSeqAcc.size() > 0)
{
cout << (*transcript).transcriptRefSeqAcc << ":" << (*transcript).gi << ":" << (*transcript).gene.geneName << ":" << ++num << endl;
transcripts.push_back(*transcript);
delete transcript;
// LEAK!
RefSeqTranscript *transcript = new RefSeqTranscript();
}
}
You probably mean:
transcript = new RefSeqTranscript();
It's hard to say anything specific unless you provide some more details:
What line does it crashed in?
Do you really need all of those transcripts at the same time?
But I would suggest you a couple improvements:
Don't use pointer (or at least use smart pointer) for the RefSeqTranscript *transcript;
Don't use pointer for the Gene *gene;
Generally, don't use pointers unless you realy need them;
And you have a bug here:
delete transcript;
RefSeqTranscript *transcript = new RefSeqTranscript();
Since you've laready declared transcript outside the loop's body, here you hide it with new variable with the same name. This causes memory leak, and moreover, you delete an outer transcript and do not replace it with anything. So, you probably get a crash on the next iteration.

How to access a field of exif data from a Photoshop filter plug-in (Photoshop SDK)

I'm looking for a very specific piece of information. I could make this a rather detailed question I guess but I'd rather try keeping it short and to the point.
I need to acces a piece of meta data (exif information) from within a Photoshop filter plug-in. I have never dealt with exif data from within a Photoshop plug-in or without and the PS SDK documentation is of a form that leaves a lot of questions. I would eventually get there but was wondering if anyone here has done this before and could help me out with an example. I'd be very grateful for that...
What we need should be documented here in the SDK:
documentation/html/group___resource_suite.html
documentation/html/imageresourcessection.html
The latter document says that the resource ID I need to retrieve the exif data is 1059 (decimal) and that accessing Exif data is supported since PS 7.0 which is good. But the SDK has no information (that I found) as to what you get, pointer? pointer to what? They just tell you to look at the exif spec. So do I get a pointer to the RAW binary exif data and if so how to I extract a field from that.
The specifications for the Exif data are here:
http://exif.org/specifications.html
As an example I'd like to get at this exif field:
Tag Name Field Name Dec Hex Type Count
Image title ImageDescription 270 10E ASCII Any
EDIT: After a deeper research I've found the following (an excerpt from the documentation):
Document: Photoshop API Guide.
Argument: Callbacks.
The callback routines are organized into “suites” collections of
related routines which implement a particular functionality.
The suites are described by a pointer to a record containing:
a 2 byte version number for the suite,
a 2 byte count of the number of routines in the suite,
a series of function pointers for the callback routines.
You are interested in the Property suite.
Current version: 1; Adobe Photoshop: 5.0; Routines: 2.
Properties are identified by a signature and key, which form a pair to
identify the property of interest.
Adobe Photoshop’s signature is always '8BIM' (0x3842494D).
The EXIF property is controlled by The Japan Electronic Industry Development Association (JEIDA) and Electronic Industries Association of Japan (EIAJ) which merged in Novemeber of 2000. The EXIF specification can be downloaded from their web site at the following location.
http://it.jeita.or.jp/jhistory/document/standard/exif_eng/jeida49eng.htm
GetPropertyProc( )
MACPASCAL OSErr (*GetPropertyProc) (OSType signature, OSType key, int32 index, int32 * simpleProperty, Handle * complexProperty);
This routine allows you to get information about the document currently being processed.
property name: propEXIFData
id:EXIF
type:complex (modifiable)
description:Camera and device data.
To recap I'll write some juicy code:
GetPropertyProc getProperty = formatParamBlock->propertyProcs->getPropertyProc;
rc = getProperty(0x3842494D, propEXIFData, 0, &simpProp, &compProp);
if ( rc )
return;
GetPIHandleSizeProc getSize = formatParamBlock->handleProcs->getSizeProc;
int32 size = getSize(compProp);
if ( !size )
return;
LockPIHandleProc lock = formatParamBlock->handleProcs->lockProc;
uint8* exif = (uint8 *)lock(compProp, false);
if ( !exif )
return;
Here's is a code sample using the Exiv2 Library: http://www.exiv2.org/doc/exifprint_8cpp-example.html
// ***************************************************************** -*- C++ -*-
// exifprint.cpp, $Rev: 2286 $
// Sample program to print the Exif metadata of an image
#include <exiv2/exiv2.hpp>
#include <iostream>
#include <iomanip>
#include <cassert>
int main(int argc, char* const argv[])
try {
if (argc != 2) {
std::cout << "Usage: " << argv[0] << " file\n";
return 1;
}
Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(argv[1]);
assert(image.get() != 0);
image->readMetadata();
Exiv2::ExifData &exifData = image->exifData();
if (exifData.empty()) {
std::string error(argv[1]);
error += ": No Exif data found in the file";
throw Exiv2::Error(1, error);
}
Exiv2::ExifData::const_iterator end = exifData.end();
for (Exiv2::ExifData::const_iterator i = exifData.begin(); i != end; ++i) {
const char* tn = i->typeName();
std::cout << std::setw(44) << std::setfill(' ') << std::left
<< i->key() << " "
<< "0x" << std::setw(4) << std::setfill('0') << std::right
<< std::hex << i->tag() << " "
<< std::setw(9) << std::setfill(' ') << std::left
<< (tn ? tn : "Unknown") << " "
<< std::dec << std::setw(3)
<< std::setfill(' ') << std::right
<< i->count() << " "
<< std::dec << i->value()
<< "\n";
}
return 0;
}
//catch (std::exception& e) {
//catch (Exiv2::AnyError& e) {
catch (Exiv2::Error& e) {
std::cout << "Caught Exiv2 exception '" << e.what() << "'\n";
return -1;
}
I combined response vulkanino and ThdK. My method uses the function PIGetEXIFData declared in file PropertyUtils.h that returns a binary exif. Next, this exif decoded Exiv2 library
#include <PropertyUtils.h>
#include <PIProperties.h>
#include <exif.hpp>
void printExif() {
Handle handle;
checkSPErr(PIGetEXIFData(handle));
std::string ss;
checkSPErr(HandleToString(handle, ss));
Exiv2::ExifData exifData;
Exiv2::ExifParser::decode(exifData, reinterpret_cast<const Exiv2::byte*>(ss.data()), ss.size());
Exiv2::ExifData::const_iterator end = exifData.end();
for (Exiv2::ExifData::const_iterator i = exifData.begin(); i != end; ++i) {
const char* tn = i->typeName();
std::cout << std::setw(44) << std::setfill(' ') << std::left
<< i->key() << " "
<< "0x" << std::setw(4) << std::setfill('0') << std::right
<< std::hex << i->tag() << " "
<< std::setw(9) << std::setfill(' ') << std::left
<< (tn ? tn : "Unknown") << " "
<< std::dec << std::setw(3)
<< std::setfill(' ') << std::right
<< i->count() << " "
<< std::dec << i->value()
<< "\n";
}
}

Super Noob C++ variable help

Ok, I must preface this by stating that I know so so little about c++ and am hoping someone can just help me out...
I have the below code:
string GoogleMapControl::CreatePolyLine(RideItem *ride)
{
std::vector<RideFilePoint> intervalPoints;
ostringstream oss;
int cp;
int intervalTime = 30; // 30 seconds
int zone =ride->zoneRange();
if(zone >= 0)
{
cp = 300; // default cp to 300 watts
}
else
{
cp = ride->zones->getCP(zone);
}
foreach(RideFilePoint* rfp, ride->ride()->dataPoints())
{
intervalPoints.push_back(*rfp);
if((intervalPoints.back().secs - intervalPoints.front().secs) > intervalTime)
{
// find the avg power and color code it and create a polyline...
AvgPower avgPower = for_each(intervalPoints.begin(),
intervalPoints.end(),
AvgPower());
// find the color
QColor color = GetColor(cp,avgPower);
// create the polyline
CreateSubPolyLine(intervalPoints,oss,color);
intervalPoints.clear();
intervalPoints.push_back(*rfp);
}
}
return oss.str();
}
void GoogleMapControl::CreateSubPolyLine(const std::vector<RideFilePoint> &points,
std::ostringstream &oss,
QColor color)
{
oss.precision(6);
QString colorstr = color.name();
oss.setf(ios::fixed,ios::floatfield);
oss << "var polyline = new GPolyline([";
BOOST_FOREACH(RideFilePoint rfp, points)
{
if (ceil(rfp.lat) != 180 && ceil(rfp.lon) != 180)
{
oss << "new GLatLng(" << rfp.lat << "," << rfp.lon << ")," << endl;
}
}
oss << "],\"" << colorstr.toStdString() << "\",4);";
oss << "GEvent.addListener(polyline, 'mouseover', function() {" << endl
<< "var tooltip_text = 'Avg watts:" << avgPower <<" <br> Avg Speed: <br> Color: "<< colorstr.toStdString() <<"';" << endl
<< "var ss={'weight':8};" << endl
<< "this.setStrokeStyle(ss);" << endl
<< "this.overlay = new MapTooltip(this,tooltip_text);" << endl
<< "map.addOverlay(this.overlay);" << endl
<< "});" << endl
<< "GEvent.addListener(polyline, 'mouseout', function() {" << endl
<< "map.removeOverlay(this.overlay);" << endl
<< "var ss={'weight':5};" << endl
<< "this.setStrokeStyle(ss);" << endl
<< "});" << endl;
oss << "map.addOverlay (polyline);" << endl;
}
And I'm trying to get the avgPower from this part:
AvgPower avgPower = for_each(intervalPoints.begin(),
intervalPoints.end(),
AvgPower());
the first part to cary over to the second part:
<< "var tooltip_text = 'Avg watts:" << avgPower <<" <br> Avg Speed: <br> Color: "<< colorstr.toStdString() <<"';" << endl
But of course I haven't the slightest clue how to do it... anyone feeling generous today?
Thanks in advance
Well you didn't state the problem with the code other than it doesn't work. I'm suspicious of your use of AvgPower() in the for_each. Also, you have AvgPower as a class. why not a double or something? For this code I would have expected to see something like this:
PowerType addIntervals(const RideFilePoint &p1, const RideFilePoint &p2) {
//Add and return.
}
...
...
PowerType total = accumulate(i.begin(), i.end(), PowerType(0.0), &addIntervals);
avg = total/i.length();
read these docs carefully:
http://www.sgi.com/tech/stl/accumulate.html
It seems like you are asking how to access the local variable avgPower within a different function (sorry if I am misunderstanding). This is less about c++ specifically and more about functional or object oriented design. There are many different ways to do this, but here are the sane ones I can think of, in the order of my preference.
Create avgPower (and color), within CreateSubPolyLine. There doesn't really seem to be any reason they are in CreatePolyLine anyway. Implement a separate call if there are other consumers. The function signature would change to
void GoogleMapControl::CreateSubPolyLine(const std::vector &points,
std::ostringstream &oss)
Include it in the function's paramemters, e.g., change the signature to:
void GoogleMapControl::CreateSubPolyLine(const std::vector &points,
std::ostringstream &oss,
const QColor& color,
const AvgPower& avgPower)
Put it in a member variable of GoogleMapControl. It doesn't seem like this would be a bad design choice in my opinion.