1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
/* --------------------------------------------------------------------------
MusicBrainz -- The Internet music metadatabase
Copyright (C) 2000 Robert Kaye
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
$Id: cmd.cpp,v 1.19 2001/10/04 08:19:38 pouwelse Exp $
----------------------------------------------------------------------------*/
#include <stdio.h>
#include "musicbrainz.h"
#include "queries.h"
struct Queries
{
char *name, *value;
int numArgs;
char *arg1, *arg2, *arg3, *arg4;
};
const char *SUBMIT_LYRICS_QUERY= "";
Queries queries[] =
{
{ "GetCDInfoFromCDIndexId", MBQ_GetCDInfoFromCDIndexId,
1, "CD Index ID", "", "", "" },
{ "FindArtist", MBQ_FindArtistByName,
1, "Artist Name", "", "", "" },
{ "FindTrack", MBQ_FindTrackByName,
3, "Artist Name", "Album Name", "Track Name", "" },
{ "FindAlbum", MBQ_FindAlbumByName,
1, "Artist Name", "Album name", "", "" },
{ "FindDistinctGUID", MBQ_FindDistinctTRMId,
2, "Artist Name", "Track Name", "", "" },
{ "GetArtist", MBQ_GetArtistById ,
1, "Artist ID", "", "", "" },
{ "GetAlbum", MBQ_GetAlbumById ,
1, "Album ID", "", "", "" },
{ "GetTrack", MBQ_GetTrackById ,
1, "Track ID", "", "", "" },
{ "GetTrackTRMId", MBQ_GetTrackByTRMId ,
1, "Track TRM Id", "", "", "" },
{ "GetSyncText", MBQ_GetSyncTextById ,
1, "Track ID", "", "", "" },
{ "GetCDInfo", MBQ_GetCDInfo ,
0, "", "", "", "" },
{ "GetCDTOC", MBQ_GetCDTOC ,
0, "", "", "", "" },
{ "AssociateCD", MBQ_AssociateCD ,
0, "", "", "", "" },
{ "SubmitTRMId", MBQ_SubmitTrackTRMId,
2, "Track GID", "TRM ID", "", "" },
{ "\0", "\0", 0, "", "", "", "" }
};
#ifdef WIN32
#define strcasecmp stricmp
#endif
int main(void)
{
MusicBrainz o;
string error, data;
bool ret;
Queries *p;
char szLine[256], *szCmd, *ptr, szLast[100];
string xml, qarg, query, server;
vector<string> args;
// Set the server you want to use. Defaults to www.musicbrainz.org:80
server = string("www.musicbrainz.org");
o.SetServer(server, 80);
// Tell the client library to return data in ISO8859-1 and not UTF-8
o.UseUTF8(0);
// If you need to use a proxy, uncomment/edit the following line
// as appropriate
//o.SetProxy("proxy.mydomain.com", 80);
for(;;)
{
query = "";
printf("query> ");
fflush(stdout);
if (fgets(szLine, 255, stdin) < 0)
break;
if (strncmp(szLine, "!!", 2) == 0)
{
printf("%s\n", szLast);
strcpy(szLine, szLast);
}
else
strcpy(szLast, szLine);
szLine[strlen(szLine) - 1] = 0;
szCmd = strtok(szLine, " ");
if (szCmd == NULL)
continue;
args.clear();
for(;;)
{
ptr = strtok(NULL, " ");
if (!ptr)
break;
if (*ptr == '\"' && !(*(ptr + strlen(ptr) - 1) == '\"'))
{
qarg = string(ptr + 1);
for(;;)
{
ptr = strtok(NULL, " ");
if (!ptr)
break;
if (*(ptr + strlen(ptr) - 1) == '\"')
{
*(ptr + strlen(ptr) - 1) = 0;
qarg += string(" ") + string(ptr);
ptr = (char *)qarg.c_str();
break;
}
qarg += string(" ") + string(ptr);
}
if (!ptr)
{
continue;
}
}
if (*ptr == '\"' && *(ptr + strlen(ptr) - 1) == '\"')
{
*(ptr + strlen(ptr) - 1) = 0;
args.push_back(string(ptr + 1));
}
else
{
if (strcmp(ptr, "\"\"") == 0)
*ptr = 0;
args.push_back(string(ptr));
}
}
if (strncmp(szCmd, "bye", 3) == 0 ||
strncmp(szCmd, "quit", 4) == 0 ||
strncmp(szCmd, "exit", 4) == 0)
{
break;
}
if (strncmp(szCmd, "help", 4) == 0)
{
printf("Available commands: Arguments:\n\n");
for(p = queries; *p->name; p++)
{
printf("%-34s ", p->name);
if (strlen(p->arg1))
printf("<%s> ", p->arg1);
if (strlen(p->arg2))
printf("<%s> ", p->arg2);
if (strlen(p->arg3))
printf("<%s> ", p->arg3);
if (strlen(p->arg4))
printf("<%s> ", p->arg4);
printf("\n");
}
printf("exit/quit/bye [quit]\n");
printf("submit [get web submit URL]\n");
printf("info [help on a command]\n");
continue;
}
if (strncmp(szCmd, "lyrics", 6) == 0)
{
query = string(SUBMIT_LYRICS_QUERY);
}
if (strncmp(szCmd, "submit", 6) == 0)
{
string url;
ret = o.GetWebSubmitURL(url);
if (ret)
printf("URL: %s\n", url.c_str());
else
printf("Could not determine web submit url.\n");
continue;
}
if (strncmp(szCmd, "info", 4) == 0)
{
for(p = queries; p->name; p++)
{
if (strcasecmp(szCmd + 5, p->name) == 0)
{
printf("usage: %s ", p->name);
if (strlen(p->arg1))
printf("<%s> ", p->arg1);
if (strlen(p->arg2))
printf("<%s> ", p->arg2);
if (strlen(p->arg3))
printf("<%s> ", p->arg3);
if (strlen(p->arg4))
printf("<%s> ", p->arg4);
printf("\n");
break;
}
}
if (p->name == NULL)
printf("No info available for %s\n", szCmd + 5);
continue;
}
if (query.length() == 0)
{
for(p = queries; p->name; p++)
{
if (strcasecmp(szCmd, p->name) == 0)
break;
}
if (p->name == NULL)
{
printf("Unknown query.\n");
continue;
}
query = string(p->value);
}
ret = o.Query(query, &args);
if (!ret)
{
o.GetQueryError(error);
printf("Query failed: %s\n", error.c_str());
continue;
}
printf("Query ok.\n");
o.GetResultRDF(xml);
printf("Returned RDF object:\n\n%s\n", xml.c_str());
}
return 0;
}
|