Better conformity to style guidelines

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@13765 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
DarkWyrm
2005-07-19 15:14:14 +00:00
parent fcccc8d147
commit 1bbb0771e4
2 changed files with 162 additions and 149 deletions
+108 -93
View File
@@ -24,8 +24,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
// #pragma mark - const int kTerminatingSignal = SIGINT; // SIGCONT;
// some glue
template <class InitCheckable> template <class InitCheckable>
void void
@@ -53,15 +52,13 @@ ThrowIfNotSize(ssize_t size)
} }
// #pragma mark -
CDDBQuery::CDDBQuery(const char *server, int32 port, bool log) CDDBQuery::CDDBQuery(const char *server, int32 port, bool log)
: log(log), : fLog(log),
serverName(server), fServerName(server),
port(port), fPort(port),
connected(false), fConnected(false),
discID(-1), fDiscID(-1),
state(kInitial) fState(kInitial)
{ {
} }
@@ -69,9 +66,9 @@ void
CDDBQuery::SetToSite(const char *server, int32 port) CDDBQuery::SetToSite(const char *server, int32 port)
{ {
Disconnect(); Disconnect();
serverName = server; fServerName = server;
port = port; fPort = port;
state = kInitial; fState = kInitial;
} }
void void
@@ -94,9 +91,9 @@ CDDBQuery::SetToCD(const char *path)
return; return;
if (state == kInitial) if (fState == kInitial)
{ {
GetDiscID(&toc, discID, numTracks, discLength, frameOffsetString, discIDStr); GetDiscID(&toc, fDiscID, fTrackCount, fDiscLength, fFrameOffsetString, fDiscIDStr);
} }
else else
{ {
@@ -109,33 +106,29 @@ CDDBQuery::SetToCD(const char *path)
GetDiscID(&toc, tmpDiscID, tmpNumTracks, tmpDiscLength, tmpFrameOffsetString, GetDiscID(&toc, tmpDiscID, tmpNumTracks, tmpDiscLength, tmpFrameOffsetString,
tmpDiscIDStr); tmpDiscIDStr);
if (discID == tmpDiscID && discLength == tmpDiscLength && numTracks == tmpNumTracks if (fDiscID == tmpDiscID && fDiscLength == tmpDiscLength && fTrackCount == tmpNumTracks
&& frameOffsetString == frameOffsetString) && tmpFrameOffsetString == fFrameOffsetString)
return; return;
discID = tmpDiscID; fDiscID = tmpDiscID;
discLength = tmpDiscLength; fDiscLength = tmpDiscLength;
numTracks = tmpNumTracks; fTrackCount = tmpNumTracks;
frameOffsetString = tmpFrameOffsetString; fFrameOffsetString = tmpFrameOffsetString;
discIDStr = tmpDiscIDStr; fDiscIDStr = tmpDiscIDStr;
} }
// ToDo:
// add interrupting here
result = B_OK; result = B_OK;
state = kReading; fState = kReading;
thread = spawn_thread(&CDDBQuery::LookupBinder, "CDDBLookup", B_NORMAL_PRIORITY, this); fThread = spawn_thread(&CDDBQuery::LookupBinder, "CDDBLookup", B_NORMAL_PRIORITY, this);
if (thread >= 0)
resume_thread(thread); if (fThread >= 0)
resume_thread(fThread);
else { else {
state = kError; fState = kError;
result = thread; result = fThread;
} }
} }
const int kTerminatingSignal = SIGINT; // SIGCONT;
static void static void
DoNothing(int) DoNothing(int)
{ {
@@ -150,10 +143,12 @@ CDDBQuery::LookupBinder(void *castToThis)
entry_ref ref; entry_ref ref;
bool newFile = false; bool newFile = false;
try { try
{
signal(kTerminatingSignal, DoNothing); signal(kTerminatingSignal, DoNothing);
if (!query->FindOrCreateContentFileForDisk(&file, &ref, query->discID)) { if (!query->FindOrCreateContentFileForDisk(&file, &ref, query->fDiscID))
{
// new content file, read it in from the server // new content file, read it in from the server
Connector connection(query); Connector connection(query);
query->ReadFromServer(&file); query->ReadFromServer(&file);
@@ -162,14 +157,16 @@ CDDBQuery::LookupBinder(void *castToThis)
} }
query->ParseResult(&file); query->ParseResult(&file);
query->state = kDone; query->fState = kDone;
query->thread = -1; query->fThread = -1;
query->result = B_OK; query->fResult = B_OK;
if (newFile) { if (newFile)
BString newTitle(query->title); {
BString newTitle(query->fTitle);
int32 length = newTitle.Length(); int32 length = newTitle.Length();
for (int32 index = 0; index < length; index++) { for (int32 index = 0; index < length; index++)
{
if (newTitle[index] == '/' || newTitle[index] == ':') if (newTitle[index] == '/' || newTitle[index] == ':')
newTitle[index] = '-'; newTitle[index] = '-';
} }
@@ -179,9 +176,11 @@ CDDBQuery::LookupBinder(void *castToThis)
entry.Rename(newTitle.String(), false); entry.Rename(newTitle.String(), false);
} }
} catch (status_t error) { }
query->state = kError; catch (status_t error)
query->result = error; {
query->fState = kError;
query->fResult = error;
// the cached file is messed up, remove it // the cached file is messed up, remove it
BEntry entry(&ref); BEntry entry(&ref);
@@ -211,23 +210,21 @@ void
CDDBQuery::GetSites(bool (*eachFunc)(const char *site, int port, const char *latitude, CDDBQuery::GetSites(bool (*eachFunc)(const char *site, int port, const char *latitude,
const char *longitude, const char *description, void *state), void *passThru) const char *longitude, const char *description, void *state), void *passThru)
{ {
// ToDo:
// add interrupting here
Connector connection(this); Connector connection(this);
BString tmp; BString tmp;
tmp = "sites\n"; tmp = "sites\n";
if (log) if (fLog)
printf(">%s", tmp.String()); printf(">%s", tmp.String());
ThrowIfNotSize( socket.Send(tmp.String(), tmp.Length()) ); ThrowIfNotSize( fSocket.Send(tmp.String(), tmp.Length()) );
ReadLine(tmp); ReadLine(tmp);
if (tmp.FindFirst("210") == -1) if (tmp.FindFirst("210") == -1)
throw (status_t)B_ERROR; throw (status_t)B_ERROR;
for (;;) { for (;;)
{
BString site; BString site;
int32 sitePort; int32 sitePort;
BString latitude; BString latitude;
@@ -257,19 +254,20 @@ bool
CDDBQuery::GetTitles(BString *resultingTitle, vector<BString> *tracks, bigtime_t timeout) CDDBQuery::GetTitles(BString *resultingTitle, vector<BString> *tracks, bigtime_t timeout)
{ {
bigtime_t deadline = system_time() + timeout; bigtime_t deadline = system_time() + timeout;
while (state == kReading) { while (fState == kReading)
{
snooze(50000); snooze(50000);
if (system_time() > deadline) if (system_time() > deadline)
break; break;
} }
if (state != kDone) if (fState != kDone)
return false; return false;
if (resultingTitle) if (resultingTitle)
*resultingTitle = title; *resultingTitle = fTitle;
if (tracks) if (tracks)
*tracks = trackNames; *tracks = fTrackNames;
return true; return true;
} }
@@ -285,7 +283,8 @@ cddb_sum(int n)
return ret; return ret;
} }
struct ConvertedToc { struct ConvertedToc
{
int32 min; int32 min;
int32 sec; int32 sec;
int32 frame; int32 frame;
@@ -313,7 +312,8 @@ CDDBQuery::GetDiscID(const scsi_toc *toc, int32 &id, int32 &numTracks,
ConvertedToc tocData[100]; ConvertedToc tocData[100];
// figure out the disc ID // figure out the disc ID
for (int index = 0; index < 100; index++) { for (int index = 0; index < 100; index++)
{
tocData[index].min = toc->toc_data[9 + 8*index]; tocData[index].min = toc->toc_data[9 + 8*index];
tocData[index].sec = toc->toc_data[10 + 8*index]; tocData[index].sec = toc->toc_data[10 + 8*index];
tocData[index].frame = toc->toc_data[11 + 8*index]; tocData[index].frame = toc->toc_data[11 + 8*index];
@@ -322,11 +322,13 @@ CDDBQuery::GetDiscID(const scsi_toc *toc, int32 &id, int32 &numTracks,
int32 sum1 = 0; int32 sum1 = 0;
int32 sum2 = 0; int32 sum2 = 0;
for (int index = 0; index < numTracks; index++) { for (int index = 0; index < numTracks; index++)
{
sum1 += cddb_sum((tocData[index].min * 60) + tocData[index].sec); sum1 += cddb_sum((tocData[index].min * 60) + tocData[index].sec);
// the following is probably running over too far // the following is probably running over too far
sum2 += (tocData[index + 1].min * 60 + tocData[index + 1].sec) - sum2 += (tocData[index + 1].min * 60 + tocData[index + 1].sec)
(tocData[index].min * 60 + tocData[index].sec); - (tocData[index].min * 60 + tocData[index].sec);
} }
id = ((sum1 % 0xff) << 24) + (sum2 << 8) + numTracks; id = ((sum1 % 0xff) << 24) + (sum2 << 8) + numTracks;
discIDString = ""; discIDString = "";
@@ -347,20 +349,20 @@ CDDBQuery::ReadFromServer(BDataIO *stream)
{ {
// Format the query // Format the query
BString query; BString query;
query << "cddb query " << discIDStr << ' ' << numTracks << ' ' query << "cddb query " << fDiscIDStr << ' ' << fTrackCount << ' '
<< frameOffsetString << ' ' << discLength << '\n'; << fFrameOffsetString << ' ' << fDiscLength << '\n';
if (log) if (fLog)
printf(">%s", query.String()); printf(">%s", query.String());
// Send it off. // Send it off.
ThrowIfNotSize( socket.Send(query.String(), query.Length()) ); ThrowIfNotSize( fSocket.Send(query.String(), query.Length()) );
BString tmp; BString tmp;
ReadLine(tmp); ReadLine(tmp);
BString category; BString category;
BString queryDiscID(discIDStr); BString queryDiscID(fDiscIDStr);
if(tmp.FindFirst("200") != 0) if(tmp.FindFirst("200") != 0)
{ {
@@ -404,7 +406,7 @@ CDDBQuery::ReadFromServer(BDataIO *stream)
query = ""; query = "";
query << "cddb read " << category << ' ' << queryDiscID << '\n' ; query << "cddb read " << category << ' ' << queryDiscID << '\n' ;
ThrowIfNotSize( socket.Send(query.String(), query.Length()) ); ThrowIfNotSize( fSocket.Send(query.String(), query.Length()) );
for (;;) for (;;)
{ {
@@ -415,15 +417,16 @@ CDDBQuery::ReadFromServer(BDataIO *stream)
if (tmp == "." || tmp == ".\n") if (tmp == "." || tmp == ".\n")
break; break;
} }
state = kDone; fState = kDone;
} }
void void
CDDBQuery::ParseResult(BDataIO *source) CDDBQuery::ParseResult(BDataIO *source)
{ {
title = ""; fTitle = "";
trackNames.clear(); fTrackNames.clear();
for (;;) { for (;;)
{
BString tmp; BString tmp;
ReadLine(source, tmp); ReadLine(source, tmp);
if (tmp == ".") if (tmp == ".")
@@ -433,27 +436,30 @@ CDDBQuery::ParseResult(BDataIO *source)
throw (status_t)B_ERROR; throw (status_t)B_ERROR;
if (tmp.FindFirst("DTITLE=") == 0) if (tmp.FindFirst("DTITLE=") == 0)
title = tmp.String() + sizeof("DTITLE"); fTitle = tmp.String() + sizeof("DTITLE");
else if (tmp.FindFirst("TTITLE") == 0) { else
if (tmp.FindFirst("TTITLE") == 0)
{
int32 afterIndex = tmp.FindFirst('='); int32 afterIndex = tmp.FindFirst('=');
if (afterIndex > 0) { if (afterIndex > 0)
{
BString trackName(tmp.String() + afterIndex + 1); BString trackName(tmp.String() + afterIndex + 1);
trackNames.push_back(trackName); fTrackNames.push_back(trackName);
} }
} }
} }
state = kDone; fState = kDone;
} }
void void
CDDBQuery::Connect() CDDBQuery::Connect()
{ {
BNetAddress address(serverName.String(), port); BNetAddress address(fServerName.String(), fPort);
InitCheck(&address); InitCheck(&address);
ThrowOnError( socket.Connect(address) ); ThrowOnError( fSocket.Connect(address) );
connected = true; fConnected = true;
BString tmp; BString tmp;
ReadLine(tmp); ReadLine(tmp);
@@ -463,14 +469,14 @@ CDDBQuery::Connect()
bool bool
CDDBQuery::IsConnected() const CDDBQuery::IsConnected() const
{ {
return connected; return fConnected;
} }
void void
CDDBQuery::Disconnect() CDDBQuery::Disconnect()
{ {
socket.Close(); fSocket.Close();
connected = false; fConnected = false;
} }
void void
@@ -478,15 +484,16 @@ CDDBQuery::ReadLine(BString &buffer)
{ {
buffer = ""; buffer = "";
char ch; char ch;
for (;;) { for (;;)
if (socket.Receive(&ch, 1) <= 0) {
if (fSocket.Receive(&ch, 1) <= 0)
break; break;
if (ch >= ' ') if (ch >= ' ')
buffer += ch; buffer += ch;
if (ch == '\n') if (ch == '\n')
break; break;
} }
if (log) if (fLog)
printf("<%s\n", buffer.String()); printf("<%s\n", buffer.String());
} }
@@ -499,16 +506,20 @@ CDDBQuery::ReadLine(BDataIO *stream, BString &buffer)
buffer = ""; buffer = "";
char ch; char ch;
for (;;) { for (;;)
{
ssize_t result = stream->Read(&ch, 1); ssize_t result = stream->Read(&ch, 1);
// check for read error
if (result < 0) if (result < 0)
// read error
throw (status_t)result; throw (status_t)result;
if (result == 0) if (result == 0)
break; break;
if (ch >= ' ') if (ch >= ' ')
buffer += ch; buffer += ch;
if (ch == '\n') if (ch == '\n')
break; break;
} }
@@ -529,9 +540,9 @@ CDDBQuery::IdentifySelf()
BString tmp; BString tmp;
tmp << "cddb hello " << username << " " << hostname << " Haiku_CD_Player v1.0\n"; tmp << "cddb hello " << username << " " << hostname << " Haiku_CD_Player v1.0\n";
if (log) if (fLog)
printf(">%s", tmp.String()); printf(">%s", tmp.String());
ThrowIfNotSize( socket.Send(tmp.String(), tmp.Length()) ); ThrowIfNotSize( fSocket.Send(tmp.String(), tmp.Length()) );
ReadLine(tmp); ReadLine(tmp);
} }
@@ -546,9 +557,10 @@ CDDBQuery::FindOrCreateContentFileForDisk(BFile *file, entry_ref *fileRef, int32
BVolumeRoster roster; BVolumeRoster roster;
BVolume volume; BVolume volume;
roster.Rewind(); roster.Rewind();
while (roster.GetNextVolume(&volume) == B_OK) { while (roster.GetNextVolume(&volume) == B_OK)
if (volume.IsReadOnly() || !volume.IsPersistent() {
|| !volume.KnowsAttr() || !volume.KnowsQuery()) if (volume.IsReadOnly() || !volume.IsPersistent() || !volume.KnowsAttr()
|| !volume.KnowsQuery())
continue; continue;
// make sure the volume we are looking at is indexed right // make sure the volume we are looking at is indexed right
@@ -560,7 +572,8 @@ CDDBQuery::FindOrCreateContentFileForDisk(BFile *file, entry_ref *fileRef, int32
if (query.Fetch() != B_OK) if (query.Fetch() != B_OK)
continue; continue;
while (query.GetNextRef(&ref) == B_OK) { while (query.GetNextRef(&ref) == B_OK)
{
// we have one already, return // we have one already, return
file->SetTo(&ref, O_RDONLY); file->SetTo(&ref, O_RDONLY);
*fileRef = ref; *fileRef = ref;
@@ -575,8 +588,10 @@ CDDBQuery::FindOrCreateContentFileForDisk(BFile *file, entry_ref *fileRef, int32
BDirectory dir(path.Path()); BDirectory dir(path.Path());
BString name("CDContent"); BString name("CDContent");
for (int32 index = 0; ;index++) { for (int32 index = 0; ;index++)
if (dir.CreateFile(name.String(), file, true) != B_FILE_EXISTS) { {
if (dir.CreateFile(name.String(), file, true) != B_FILE_EXISTS)
{
BEntry entry(&dir, name.String()); BEntry entry(&dir, name.String());
entry.GetRef(fileRef); entry.GetRef(fileRef);
file->WriteAttr("cddb:discID", B_INT32_TYPE, 0, &discID, sizeof(int32)); file->WriteAttr("cddb:discID", B_INT32_TYPE, 0, &discID, sizeof(int32));
+44 -46
View File
@@ -1,5 +1,5 @@
#ifndef __CDDBSUPPORT__ #ifndef CDDBSUPPORT_H
#define __CDDBSUPPORT__ #define CDDBSUPPORT_H
#include <NetEndpoint.h> #include <NetEndpoint.h>
#include <String.h> #include <String.h>
@@ -15,25 +15,22 @@ public:
CDDBQuery(const char *server, int32 port = 888, bool log = false); CDDBQuery(const char *server, int32 port = 888, bool log = false);
void SetToSite(const char *server, int32 port); void SetToSite(const char *server, int32 port);
void GetSites(bool (*)(const char *site, int port, const char *latitude, void GetSites(bool (*)(const char *site, int port, const char *latitude,
const char *longitude, const char *description, void *state), void *); const char *longitude, const char *description,
void *state), void *);
void SetToCD(const char *devicepath); void SetToCD(const char *devicepath);
bool GetTitles(BString *title, vector<BString> *tracks, bigtime_t timeout); bool GetTitles(BString *title, vector<BString> *tracks,
// title or tracks may be NULL if you are only interrested in one, not the other bigtime_t timeout);
bool Ready() const bool Ready() const { return fState == kDone; }
{ return state == kDone; } int32 CurrentDiscID() const { return fDiscID; }
int32 CurrentDiscID() const
{ return discID; }
static int32 GetDiscID(const scsi_toc *); static int32 GetDiscID(const scsi_toc *);
private: private:
static void GetDiscID(const scsi_toc *, int32 &id, int32 &numTracks,
static void GetDiscID(const scsi_toc *, int32 &id, int32 &numTracks, int32 &length, int32 &length, BString &tmpFrameOffsetString,
BString &tmpFrameOffsetString, BString &discIDString); BString &discIDString);
void Connect(); void Connect();
void Disconnect(); void Disconnect();
bool IsConnected() const; bool IsConnected() const;
@@ -61,32 +58,7 @@ private:
CDDBQuery *client; CDDBQuery *client;
bool wasConnected; bool wasConnected;
}; };
friend class Connector;
bool FindOrCreateContentFileForDisk(BFile *file, entry_ref *ref, int32 discID);
void ReadFromServer(BDataIO *);
void ParseResult(BDataIO *);
void ReadLine(BString &);
static void ReadLine(BDataIO *, BString &);
void IdentifySelf();
const char *GetToken(const char *, BString &);
bool log;
// connection description
BString serverName;
BNetEndpoint socket;
int32 port;
bool connected;
// disc identification
int32 discID;
int32 discLength;
int32 numTracks;
BString frameOffsetString;
BString discIDStr;
// cached retrieved data // cached retrieved data
enum State enum State
@@ -98,13 +70,39 @@ private:
kError kError
}; };
thread_id thread; bool FindOrCreateContentFileForDisk(BFile *file, entry_ref *ref,
State state; int32 discID);
BString title;
vector<BString> trackNames; void ReadFromServer(BDataIO *);
status_t result; void ParseResult(BDataIO *);
void ReadLine(BString &);
static void ReadLine(BDataIO *, BString &);
void IdentifySelf();
const char * GetToken(const char *, BString &);
bool fLog;
// connection description
BString fServerName;
BNetEndpoint fSocket;
int32 fPort;
bool fConnected;
// disc identification
int32 fDiscID;
int32 fDiscLength;
int32 fTrackCount;
BString fFrameOffsetString;
BString fDiscIDStr;
thread_id fThread;
State fState;
BString fTitle;
vector<BString> fTrackNames;
status_t fResult;
friend class Connector;
}; };
#endif #endif