You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
193 lines
5.4 KiB
193 lines
5.4 KiB
15 years ago
|
#ifdef HAVE_CONFIG_H
|
||
|
#include <config.h>
|
||
|
#endif
|
||
|
|
||
12 years ago
|
#include <tdeapplication.h>
|
||
14 years ago
|
#include <tqstring.h>
|
||
13 years ago
|
#include <tqtextstream.h>
|
||
14 years ago
|
#include <tqfile.h>
|
||
12 years ago
|
#include <tdelocale.h>
|
||
15 years ago
|
#include <kdebug.h>
|
||
12 years ago
|
#include <tdeaboutdata.h>
|
||
|
#include <tdecmdlineargs.h>
|
||
14 years ago
|
#include <tqregexp.h>
|
||
15 years ago
|
#include <time.h>
|
||
|
#include <sys/fcntl.h>
|
||
|
#include <unistd.h>
|
||
|
|
||
|
#define dev_urandom "/dev/urandom"
|
||
|
|
||
14 years ago
|
TQString createStationID()
|
||
15 years ago
|
{
|
||
|
const int buffersize = 32;
|
||
|
unsigned char buffer[buffersize];
|
||
|
|
||
14 years ago
|
TQString stime, srandom = "";
|
||
15 years ago
|
stime.setNum(time(NULL));
|
||
|
|
||
|
int fd = open (dev_urandom, O_RDONLY);
|
||
|
read(fd, buffer, buffersize);
|
||
|
close(fd);
|
||
|
for (int i = 0; i < buffersize; ++i)
|
||
14 years ago
|
srandom += TQString().sprintf("%02X", (unsigned int)buffer[i]);
|
||
15 years ago
|
|
||
|
// kdDebug() << i18n("generated StationID: ") << stime << srandom << endl;
|
||
|
|
||
|
return stime + srandom;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
14 years ago
|
bool convertFile(const TQString &file)
|
||
15 years ago
|
{
|
||
|
////////////////////////////////////////////////////////////////////////
|
||
|
// read input
|
||
|
////////////////////////////////////////////////////////////////////////
|
||
|
|
||
14 years ago
|
TQFile presetFile (file);
|
||
15 years ago
|
|
||
|
if (! presetFile.open(IO_ReadOnly)) {
|
||
|
kdDebug() << "convertFile: "
|
||
|
<< i18n("error opening preset file")
|
||
|
<< " " << file << " "
|
||
|
<< i18n("for reading") << endl;
|
||
|
return false;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString xmlData;
|
||
15 years ago
|
|
||
|
// make sure that qtextstream is gone when we close presetFile
|
||
|
{
|
||
14 years ago
|
TQTextStream ins(&presetFile);
|
||
|
ins.setEncoding(TQTextStream::Locale);
|
||
15 years ago
|
xmlData = ins.read();
|
||
|
}
|
||
|
|
||
14 years ago
|
if (xmlData.find("<format>", 0, false) >= 0) {
|
||
15 years ago
|
kdDebug() << "file " << file << " already in new format" << endl;
|
||
|
// but add <?xml line at beginning if missing
|
||
|
|
||
|
{
|
||
|
presetFile.reset();
|
||
14 years ago
|
TQTextStream ins(&presetFile);
|
||
|
ins.setEncoding(TQTextStream::UnicodeUTF8);
|
||
15 years ago
|
xmlData = ins.read();
|
||
|
}
|
||
|
|
||
14 years ago
|
if (xmlData.find("<?xml", 0, false) < 0) {
|
||
15 years ago
|
xmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + xmlData;
|
||
|
}
|
||
|
|
||
|
} else {
|
||
|
|
||
|
////////////////////////////////////////////////////////////////////////
|
||
|
// convert file
|
||
|
////////////////////////////////////////////////////////////////////////
|
||
|
|
||
12 years ago
|
TQRegExp qselect("<quickselect>.*</quickselect>");
|
||
14 years ago
|
TQRegExp docking("<dockingmenu>.*</dockingmenu>");
|
||
|
TQRegExp station("<station>(.*)</station>");
|
||
|
TQRegExp stationlist("<stationlist>");
|
||
|
TQRegExp emptyLines("\\n\\s*\\n");
|
||
15 years ago
|
|
||
|
#define stationIDElement "stationID"
|
||
|
|
||
|
qselect.setMinimal(true);
|
||
|
docking.setMinimal(true);
|
||
|
station.setMinimal(true);
|
||
|
|
||
|
xmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + xmlData;
|
||
12 years ago
|
xmlData.replace(stationlist, "<stationlist>\n\t\t<format>tderadio-1.0</format>");
|
||
14 years ago
|
xmlData.replace(qselect, "");
|
||
|
xmlData.replace(docking, "");
|
||
|
xmlData.replace(station, "<FrequencyRadioStation>\n"
|
||
15 years ago
|
"\t\t\t<" stationIDElement "></" stationIDElement ">"
|
||
|
"\\1</FrequencyRadioStation>"
|
||
|
);
|
||
|
|
||
|
int p = 0;
|
||
|
int f = 0;
|
||
14 years ago
|
while ( (f = xmlData.find("<" stationIDElement "></" stationIDElement ">", p) ) >= 0) {
|
||
14 years ago
|
xmlData.insert(f + 2 + TQString(stationIDElement).length(), createStationID());
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
xmlData.replace(emptyLines, "\n");
|
||
15 years ago
|
}
|
||
|
|
||
|
presetFile.close();
|
||
|
|
||
|
|
||
|
////////////////////////////////////////////////////////////////////////
|
||
|
// write output
|
||
|
////////////////////////////////////////////////////////////////////////
|
||
|
|
||
|
if (! presetFile.open(IO_WriteOnly)) {
|
||
|
kdDebug() << "convertFile: "
|
||
|
<< i18n("error opening preset file")
|
||
|
<< " " << file << " "
|
||
|
<< i18n("for writing") << endl;
|
||
|
return false;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQTextStream outs(&presetFile);
|
||
|
outs.setEncoding(TQTextStream::UnicodeUTF8);
|
||
15 years ago
|
|
||
|
outs << xmlData;
|
||
|
|
||
|
if (presetFile.status() != IO_Ok) {
|
||
|
kdDebug() << "StationList::writeXML: "
|
||
|
<< i18n("error writing preset file")
|
||
|
<< " " << file
|
||
|
<< " (" << presetFile.state() << ")"
|
||
|
<< endl;
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
|
||
|
static const char *description = "convert-presets";
|
||
|
|
||
12 years ago
|
static TDECmdLineOptions options[] =
|
||
15 years ago
|
{
|
||
|
{ "q", I18N_NOOP("be quiet"), 0},
|
||
|
{ "+[preset files]", I18N_NOOP("preset file to convert"), 0 },
|
||
12 years ago
|
TDECmdLineLastOption
|
||
15 years ago
|
};
|
||
|
|
||
|
int main(int argc, char *argv[])
|
||
|
{
|
||
12 years ago
|
TDEAboutData aboutData("convert-presets", I18N_NOOP("convert-presets"),
|
||
|
VERSION, description, TDEAboutData::License_GPL,
|
||
15 years ago
|
"(c) 2003-2005 Martin Witte",
|
||
|
0,
|
||
12 years ago
|
"http://sourceforge.net/projects/tderadio",
|
||
15 years ago
|
0);
|
||
|
aboutData.addAuthor("Martin Witte", "", "witte@kawo1.rwth-aachen.de");
|
||
|
|
||
12 years ago
|
TDECmdLineArgs::init( argc, argv, &aboutData );
|
||
|
TDECmdLineArgs::addCmdLineOptions( options ); // Add our own options.
|
||
15 years ago
|
|
||
12 years ago
|
TDEApplication a (false, false);
|
||
15 years ago
|
|
||
12 years ago
|
TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();
|
||
15 years ago
|
|
||
|
for (int i = 0; i < args->count(); ++i) {
|
||
|
const char *x = args->arg(i);
|
||
|
if (! convertFile(x)) {
|
||
|
return -1;
|
||
|
} else {
|
||
|
if (! args->isSet("q"))
|
||
|
kdDebug() << x << ": ok" << endl;
|
||
|
}
|
||
|
}
|
||
|
if (args->count() == 0) {
|
||
|
kdDebug() << "no input" << endl;
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|