00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef FREEDB_HXX
00012 #define FREEDB_HXX
00013
00014 #include <assert.h>
00015
00016 #include <sys/types.h>
00017 #include <sys/socket.h>
00018 #include <netinet/in.h>
00019 #include <arpa/inet.h>
00020 #include <netdb.h>
00021 #include <unistd.h>
00022 #include <errno.h>
00023 #include <linux/cdrom.h>
00024
00025 #include <string.h>
00026
00027 #include <iostream>
00028 #include <sstream>
00029 #include <string>
00030 #include <vector>
00031
00041 inline int stoi(const string s)
00042 {
00043 istringstream in(s);
00044 int i;
00045
00046 in >> i;
00047
00048 return(i);
00049 }
00050
00056 inline string itos(int i)
00057 {
00058 ostringstream out;
00059
00060 out << i;
00061
00062 return(out.str());
00063 }
00064
00073 const unsigned short CDDBP_PORT = 8880;
00074
00075
00076 typedef struct
00077 {
00078 string leading;
00079 string version_number;
00080 string release_type;
00081 string level;
00082 } ClientVersion;
00083
00084 typedef struct
00085 {
00086 string name;
00087 ClientVersion version;
00088 string comments;
00089 } ClientInfo;
00090
00093 typedef struct
00094 {
00095 long discid;
00096 string dartist;
00097 string dtitle;
00098 string dyear;
00099 string dgenre;
00100 string dextinfo;
00101 int revision;
00102 vector<int> trackoffsets;
00103 vector<string> tracktitles;
00104 vector<int> playorder;
00105 } CDInfo;
00106
00109 typedef struct
00110 {
00111 long discid;
00112 string category;
00113 string dartist;
00114 string dtitle;
00115 } SearchResult;
00116
00119 typedef struct
00120 {
00121 string compassdirection;
00122 short degrees;
00123 short minutes;
00124 } GeoCoords;
00125
00128 typedef struct
00129 {
00130 string site;
00131 unsigned short port;
00132 GeoCoords latitude;
00133 GeoCoords longitude;
00134 string description;
00135 } ServerInfo;
00136
00137 typedef struct
00138 {
00139 int min;
00140 int sec;
00141 int frame;
00142 } TrackPosition;
00143
00149 typedef struct
00150 {
00151 int errcode;
00152 string errmsg;
00153 int errcode_sys;
00154 string errmsg_sys;
00155 string extinfo;
00156 } Error;
00157
00164 class Freedb
00165 {
00166 public:
00167
00168 virtual vector<SearchResult> search() const = 0;
00169
00170 static string createEntry(CDInfo info);
00171 static CDInfo parseEntry(string entry);
00172
00173 static vector<TrackPosition> getTrackOffsets(string device = "/dev/cdrom");
00174
00175 static unsigned long generateDiscID(const vector<TrackPosition> offsets, const int num_tracks);
00176 static unsigned long getDiscID(string device = "/dev/cdrom");
00177
00178 static void createDefaultDirectories();
00179
00180 private:
00181 static int generateDiscSum(int n);
00182 };
00183
00190 class FreedbRemote : public Freedb
00191 {
00192 public:
00193 FreedbRemote(string servername = "freedb.freedb.org");
00194 ~FreedbRemote();
00195
00196 vector<SearchResult> search() const;
00197
00198 void doHandshake(string username, string clientname, string version) const;
00199
00200 vector<ServerInfo> getServerList() const;
00201
00202 vector<string> getCategories() const;
00203
00204 void submitDiscInfo(CDInfo info) const;
00205
00206 inline string getGreeting() const
00207 {
00208 return(_greeting);
00209 }
00210
00211 protected:
00212 void connect(string servername = "freedb.freedb.org");
00213 void disconnect();
00214
00215 void sendCommand(string command) const;
00216 string getResponseLine(bool stripcr = true) const;
00217 static ServerInfo parseServerInfoLine(string line);
00218
00219 int _sockfd;
00220 string _greeting;
00221 };
00222
00229 class FreedbLocal : public Freedb
00230 {
00231 public:
00232 FreedbLocal(string dbfilename = "~/.freedb/db");
00233 ~FreedbLocal();
00234
00235 vector<SearchResult> search() const;
00236
00237 protected:
00238 int _dbfd;
00239 };
00240
00241 #endif
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366